C#でExcelチャートを作成および編集する方法

How to Create and Edit Excel Charts in C

This article was translated from English: Does it need improvement?
Translated
View the article in English

IronXLはC#の開発者がシンプルなAPIコールを使ってExcelのチャートをプログラムで作成、編集、削除できるようにします。 Excel Interopに依存することなく、データから列、折れ線、円、その他のグラフタイプを直接生成できます。

Excelでは、チャートは、情報を視覚的に表示し、分析するために使用されるデータのグラフ表示です。 Excelには、棒グラフ、折れ線グラフ、円グラフなど、さまざまな種類のグラフがあり、それぞれ異なるデータや分析のニーズに適しています。 When working with IronXL's comprehensive Excel library, you can programmatically create these visualizations to enhance your reports and dashboards.

クイックスタート:数秒で折れ線グラフを作成してプロット

IronXL を使用すれば、ワークブックのインストール、読み込み、Plotまで、すべてわずか数行のコードで実行できます。 この例では、Interop のオーバーヘッドなしに C# ネイティブのメソッドを使用してチャートを作成する方法を示します。

  1. IronXL をNuGetパッケージマネージャでインストール

    PM > Install-Package IronXL.Excel
  2. このコード スニペットをコピーして実行します。

    // Load workbook and create a line chart with data series
    var chart = workSheet.CreateChart(ChartType.Line, 2, 2, 15, 10).AddSeries("A2:A10","B2:B10").Title = workSheet["B1"].StringValue; 
    // Set title and legend position, then plot the chart
    chart.SetTitle("Quick Line Chart").SetLegendPosition(LegendPosition.Bottom).Plot();
  3. 実際の環境でテストするためにデプロイする

    今日プロジェクトで IronXL を使い始めましょう無料トライアル

    arrow pointer


IronXLを使い始める


Excelでグラフを作成するには?

IronXLは、柱状、散布、折れ線、円、棒、面の各チャートをサポートしています。 チャートを作成するには、以下のコンポーネントを指定します。 This flexibility allows you to create Excel spreadsheets with rich visualizations tailored to your data presentation needs.

  1. チャートタイプとワークシートの場所を指定するには、CreateChart を使用してください。
  2. AddSeries を使用してシリーズを追加してください。 このメソッドは、いくつかのチャート・タイプについて単一の列を受け入れます。最初のパラメータは横軸の値です。 2つ目は、縦軸の値です。 3.オプションで、系列名、チャート名、凡例の位置を指定します。
  3. チャートを表示するには、Plot を呼び出してください。 複数のコールで複数のチャートを作成します。

Let's create charts from the data in the chart.xlsx Excel file. A preview of the data is displayed below:

1月~6月のキリン、ゾウ、サイの月間動物数を示すサンプルチャートデータ付きスプレッドシート

列チャートを作成するプロセスとは

列グラフは、カテゴリ間の値を比較するのに理想的です。 When you load spreadsheet data, you can visualize it effectively using column charts to highlight differences between data points. 次の例は、動物の個体数データで多列列グラフを作成する例です:

:path=/static-assets/excel/content-code-examples/how-to/create-edit-charts-column-chart.cs
using IronXL;
using IronXL.Drawing.Charts;

WorkBook workBook = WorkBook.Load("chart.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Set the chart type and position
IChart chart = workSheet.CreateChart(ChartType.Column, 5, 5, 20, 10);

string xAxis = "A2:A7";

// Add the series
IChartSeries series = chart.AddSeries(xAxis, "B2:B7");
series.Title = workSheet["B1"].StringValue;

// Add the series
series = chart.AddSeries(xAxis, "C2:C7");
series.Title = workSheet["C1"].StringValue;

// Add the series
series = chart.AddSeries(xAxis, "D2:D7");
series.Title = workSheet["D1"].StringValue;

// Set the chart title
chart.SetTitle("Column Chart");

// Set the legend position
chart.SetLegendPosition(LegendPosition.Bottom);

// Plot the chart
chart.Plot();

workBook.SaveAs("columnChart.xlsx");
Imports IronXL
Imports IronXL.Drawing.Charts

Private workBook As WorkBook = WorkBook.Load("chart.xlsx")
Private workSheet As WorkSheet = workBook.DefaultWorkSheet

' Set the chart type and position
Private chart As IChart = workSheet.CreateChart(ChartType.Column, 5, 5, 20, 10)

Private xAxis As String = "A2:A7"

' Add the series
Private series As IChartSeries = chart.AddSeries(xAxis, "B2:B7")
series.Title = workSheet("B1").StringValue

' Add the series
series = chart.AddSeries(xAxis, "C2:C7")
series.Title = workSheet("C1").StringValue

' Add the series
series = chart.AddSeries(xAxis, "D2:D7")
series.Title = workSheet("D1").StringValue

' Set the chart title
chart.SetTitle("Column Chart")

' Set the legend position
chart.SetLegendPosition(LegendPosition.Bottom)

' Plot the chart
chart.Plot()

workBook.SaveAs("columnChart.xlsx")
$vbLabelText   $csharpLabel
動物のデータ表と、それに対応するキリン、ゾウ、サイの月別数のグループ化された列グラフを示すExcelスプレッドシート

どのように折れ線グラフを作成しますか?

折れ線グラフは、経時的な傾向を示すのに優れています。折れ線グラフは、列グラフと同じ情報を表示するので、両者を切り替えるには、グラフの種類を変えるだけでよい。 これは、時系列データを含むXLSXファイルを読むときに、折れ線グラフが特に役立ちます:

:path=/static-assets/excel/content-code-examples/how-to/create-edit-charts-pie-chart.cs
using IronXL;
using IronXL.Drawing.Charts;

WorkBook workBook = WorkBook.Load("chart.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Set the chart type and position
IChart chart = workSheet.CreateChart(ChartType.Pie, 5, 5, 20, 10);

string xAxis = "A2:A7";

// Add the series
IChartSeries series = chart.AddSeries(xAxis, "B2:B7");
series.Title = workSheet["B1"].StringValue;

// Set the chart title
chart.SetTitle("Pie Chart");

// Set the legend position
chart.SetLegendPosition(LegendPosition.Bottom);

// Plot the chart
chart.Plot();

workBook.SaveAs("pieChart.xlsx");
Imports IronXL
Imports IronXL.Drawing.Charts

Private workBook As WorkBook = WorkBook.Load("chart.xlsx")
Private workSheet As WorkSheet = workBook.DefaultWorkSheet

' Set the chart type and position
Private chart As IChart = workSheet.CreateChart(ChartType.Pie, 5, 5, 20, 10)

Private xAxis As String = "A2:A7"

' Add the series
Private series As IChartSeries = chart.AddSeries(xAxis, "B2:B7")
series.Title = workSheet("B1").StringValue

' Set the chart title
chart.SetTitle("Pie Chart")

' Set the legend position
chart.SetLegendPosition(LegendPosition.Bottom)

' Plot the chart
chart.Plot()

workBook.SaveAs("pieChart.xlsx")
$vbLabelText   $csharpLabel
キリン、ゾウ、サイの動物データ表と、それに対応する3つの傾向線を持つ折れ線グラフを示すExcelスプレッドシート。

円グラフはいつ使うべきですか?

円グラフは、全体の割合とパーセンテージを示します。 円グラフの場合、必要なデータは1列だけなので、実装は簡単です。 They're effective when you want to convert spreadsheet data into visual representations of market share, budget allocation, or category distribution:

:path=/static-assets/excel/content-code-examples/how-to/create-edit-charts-pie-chart.cs
using IronXL;
using IronXL.Drawing.Charts;

WorkBook workBook = WorkBook.Load("chart.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Set the chart type and position
IChart chart = workSheet.CreateChart(ChartType.Pie, 5, 5, 20, 10);

string xAxis = "A2:A7";

// Add the series
IChartSeries series = chart.AddSeries(xAxis, "B2:B7");
series.Title = workSheet["B1"].StringValue;

// Set the chart title
chart.SetTitle("Pie Chart");

// Set the legend position
chart.SetLegendPosition(LegendPosition.Bottom);

// Plot the chart
chart.Plot();

workBook.SaveAs("pieChart.xlsx");
Imports IronXL
Imports IronXL.Drawing.Charts

Private workBook As WorkBook = WorkBook.Load("chart.xlsx")
Private workSheet As WorkSheet = workBook.DefaultWorkSheet

' Set the chart type and position
Private chart As IChart = workSheet.CreateChart(ChartType.Pie, 5, 5, 20, 10)

Private xAxis As String = "A2:A7"

' Add the series
Private series As IChartSeries = chart.AddSeries(xAxis, "B2:B7")
series.Title = workSheet("B1").StringValue

' Set the chart title
chart.SetTitle("Pie Chart")

' Set the legend position
chart.SetLegendPosition(LegendPosition.Bottom)

' Plot the chart
chart.Plot()

workBook.SaveAs("pieChart.xlsx")
$vbLabelText   $csharpLabel
野生動物のデータと月ごとのキリンの分布を示す円グラフを含むスプレッドシート。

既存のチャートを編集するにはどうすればよいですか?

既存のExcelファイルを扱う場合、すでに作成されたチャートを修正する必要があるかもしれません。 IronXLは既存のチャートを編集する簡単な方法を提供し、タイトルの更新、凡例の位置変更、データの更新を可能にします。 This is useful when editing Excel files that contain pre-existing visualizations.

既存のチャートの凡例位置とチャートタイトルを編集できます。 チャートを編集するには、まず Charts プロパティにアクセスして対象のチャートを選択し、チャートを取得します。 その後、チャートのプロパティにアクセスして編集してください:

:path=/static-assets/excel/content-code-examples/how-to/create-edit-charts-edit-chart.cs
using IronXL;
using IronXL.Drawing.Charts;

WorkBook workBook = WorkBook.Load("pieChart.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Retrieve the chart
IChart chart = workSheet.Charts[0];

// Edit the legend position
chart.SetLegendPosition(LegendPosition.Top);

// Edit the chart title
chart.SetTitle("Edited Chart");

workBook.SaveAs("editedChart.xlsx");
Imports IronXL
Imports IronXL.Drawing.Charts

Private workBook As WorkBook = WorkBook.Load("pieChart.xlsx")
Private workSheet As WorkSheet = workBook.DefaultWorkSheet

' Retrieve the chart
Private chart As IChart = workSheet.Charts(0)

' Edit the legend position
chart.SetLegendPosition(LegendPosition.Top)

' Edit the chart title
chart.SetTitle("Edited Chart")

workBook.SaveAs("editedChart.xlsx")
$vbLabelText   $csharpLabel
Pie chart showing monthly data from Jan-Jun with color-coded segments and legend below
Pie chart showing monthly data distribution from January to June with color-coded segments and legend

Excelからグラフを削除するには?

古いグラフや不要なグラフを削除して、Excelファイルを整理する必要がある場合があります。 This is common when managing worksheets containing multiple visualizations. スプレッドシートから既存のグラフを削除するには、まず Charts プロパティからそのグラフを取得します。 図表のリストをお渡しします。 対象のチャートオブジェクトを RemoveChart に渡してください:

:path=/static-assets/excel/content-code-examples/how-to/create-edit-charts-remove-chart.cs
using IronXL;
using IronXL.Drawing.Charts;
using System.Collections.Generic;

WorkBook workBook = WorkBook.Load("pieChart.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Retrieve the chart
List<IChart> chart = workSheet.Charts;

// Remove the chart
workSheet.RemoveChart(chart[0]);

workBook.SaveAs("removedChart.xlsx");
Imports IronXL
Imports IronXL.Drawing.Charts
Imports System.Collections.Generic

Private workBook As WorkBook = WorkBook.Load("pieChart.xlsx")
Private workSheet As WorkSheet = workBook.DefaultWorkSheet

' Retrieve the chart
Private chart As List(Of IChart) = workSheet.Charts

' Remove the chart
workSheet.RemoveChart(chart(0))

workBook.SaveAs("removedChart.xlsx")
$vbLabelText   $csharpLabel

高度なグラフのカスタマイズ

IronXLは基本的なチャート作成だけでなく、高度なカスタマイズもサポートしています。 When creating complex reports or dashboards, you can combine charts with other Excel features like conditional formatting to create comprehensive data visualizations.

ビジネスアプリケーションの場合、チャートはデータベースクエリやリアルタイムデータソースから動的に生成する必要があります。 IronXLは.NETのデータ構造とシームレスに統合され、Lists、または任意の列挙可能なコレクションからチャートを作成することができます。 そのため、視覚的要素を含む自動レポートの作成に最適です。

まとめ

IronXLはC#アプリケーションでExcelチャートを扱うための完全なソリューションを提供します。 新しいビジュアライゼーションの作成、既存のビジュアライゼーションの修正、古いチャートの削除のいずれにおいても、ライブラリはExcel Interopを必要としない直感的な方法を提供します。チャート機能をIronXLのデータ操作や書式設定などの他の機能と組み合わせることで、.NETアプリケーションでのデータ表示や分析を強化する洗練されたExcel自動化ソリューションを構築することができます。

よくある質問

Interop を使用せずに、C# で Excel のチャートをプログラムで作成するにはどうすればよいですか?

IronXLはInteropに依存せずにC#でExcelチャートを作成するシンプルなAPIを提供します。CreateChartメソッドでチャートの種類と位置を指定し、AddSeriesメソッドでデータを追加し、Plotメソッドでチャートを描画します。

C#を使用して、Excelスプレッドシートでどのような種類のグラフを作成できますか?

IronXLは列、散布図、折れ線グラフ、円グラフ、棒グラフ、面グラフなどさまざまな種類のグラフの作成をサポートしています。CreateChartメソッドを呼び出す際にチャートの種類を指定し、各チャートをタイトル、凡例、データ系列でカスタマイズできます。

Excelのチャートにプログラムでデータ系列を追加するには?

チャートにデータを追加するにはIronXLのAddSeriesメソッドを使用します。このメソッドはセル範囲をパラメータとして受け取ります - 最初のパラメータは横軸の値、2番目のパラメータは縦軸の値です。複数の系列を追加して複数系列のチャートを作成できます。

C#を使用してExcelで折れ線グラフを作成する最も簡単な方法は何ですか?

CreateChart(ChartType.Line)でチャートを初期化し、AddSeries()でデータ範囲を追加し、SetTitle()でチャートのタイトルを設定し、Plot()でワークシート上にレンダリングします。

タイトルや凡例の位置など、チャートのプロパティをカスタマイズできますか?

はい、IronXLではExcelのチャートを完全にカスタマイズできます。SetTitle()を使ってグラフのタイトルを追加したり、SetLegendPosition()を使って凡例を配置したり(上、下、左、右)、オプションで系列名を指定してデータを識別しやすくすることができます。

プログラムでチャートを作成するには、Microsoft Excelがインストールされている必要がありますか?

IronXLはMicrosoft Excelをインストールすることなく単独で動作します。すべてのExcelファイル操作とチャート作成を内部で処理するため、Excelをインストールできないサーバー環境やアプリケーションに最適です。

カーティス・チャウ
テクニカルライター

Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。

開発以外にも、CurtisはIoT(Internet of Things)への強い関心を持ち、ハードウェアとソフトウェアの統合方法を模索しています。余暇には、ゲームをしたりDiscordボットを作成したりして、技術に対する愛情と創造性を組み合わせています。

準備はできましたか?
Nuget ダウンロード 2,052,917 | バージョン: 2026.6 just released
Still Scrolling Icon

まだスクロールしていますか?

すぐに証拠が欲しいですか? PM > Install-Package IronXL.Excel
サンプルを実行する あなたのデータがスプレッドシートになるのを見る。