How to Create and Edit Excel Charts in C#
Excelでは、チャートはデータのグラフィカルな表現です。 データをより理解しやすく、有意義な方法で表示および分析するためのビジュアルツールです。 Excelは、棒グラフ、折れ線グラフ、円グラフなど、さまざまな種類のチャートを提供しており、それぞれが異なる種類のデータと分析に適しています。
クイックスタート:数秒で折れ線グラフを作成してプロット
IronXLを使用すると、インストールしてワークブックをロードし、CreateChartを呼び出し、データシリーズを追加し、タイトルと凡例の位置を設定し、プロットできます—すべてが数行で完了します。 この例は、ネイティブC#メソッドを使用してチャートがない状態から洗練されたビジュアルをいかに迅速に作成できるかを示しています—Interopのオーバーヘッドはありません。
Get started making PDFs with NuGet now:
Install IronXL with NuGet Package Manager
Copy and run this code snippet.
var chart = workSheet.CreateChart(ChartType.Line, 2, 2, 15, 10).AddSeries("A2:A10","B2:B10").Title = workSheet["B1"].StringValue; chart.SetTitle("Quick Line Chart").SetLegendPosition(LegendPosition.Bottom).Plot();Deploy to test on your live environment
最小限のワークフロー(5ステップ)
- C#ライブラリをダウンロードしてチャートを作成および編集します
- チャートを作成するためのデータを準備します
CreateChartメソッドを使用してチャートの種類と位置を設定しますAddSeriesメソッドを使用してデータシリーズを追加しますPlotメソッドを使用してチャートをプロットします
IronXL の開始
今日あなたのプロジェクトでIronXLを無料トライアルで使用開始。
チャート作成の例
IronXLは、柱状、散布、折れ線、円、棒、面の各チャートをサポートしています。 チャートを作成するには、いくつかのことを個別に指定する必要があります。
CreateChartメソッドを使用して、ワークシート上のチャートの種類と位置を指定することから始めます。AddSeriesメソッドを使用してシリーズを追加します。 このメソッドは、ある種類のチャートには十分なため、1つの列のデータを受け入れることもできます。最初のパラメーターは水平方向の軸の値を表します。 2番目のパラメーターは垂直方向の軸の値を表します。- 必要に応じて、シリーズ名、チャート名、および凡例の位置を指定します。
Plotメソッドを呼び出してチャートをプロットします。 このメソッドは、追加されたすべてのデータを使用してチャートをプロットします。 このメソッドを複数回呼び出すと、既存のチャートを修正するのではなく、複数のチャートをプロットします。
Let's create some charts from the data in the chart.xlsx Excel file. A preview of the data is displayed below:
柱状グラフ
: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")
折れ線グラフ
折れ線グラフは柱状グラフと同じくらい多くの情報を表現できるため、2つの間を切り替えるのは非常に簡単です。 チャートの種類を変更するだけです。
:path=/static-assets/excel/content-code-examples/how-to/create-edit-charts-line-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("Line Chart");
// Set the legend position
chart.SetLegendPosition(LegendPosition.Bottom);
// Plot the chart
chart.Plot();
workBook.SaveAs("lineChart.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("Line Chart")
' Set the legend position
chart.SetLegendPosition(LegendPosition.Bottom)
' Plot the chart
chart.Plot()
workBook.SaveAs("lineChart.xlsx")
円グラフ
円グラフには1つのデータ列だけが必要です。
: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")
チャート編集の例
既存のチャートで編集できるのはいくつかのものです。 凡例の位置やチャートのタイトルを編集できます。 チャートを編集するには、まず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")
Before
After
チャート削除の例
スプレッドシートから既存のチャートを削除するには、まずワークシートオブジェクトのChartsプロパティからチャートを取得します。 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")
よくある質問
Interopを使用せずにC#でExcelチャートを作成するにはどうすればよいですか?
IronXLライブラリを使用することで、Interopを使用せずにC#でExcelチャートを作成できます。まずNuGetからライブラリをダウンロードし、たとえばCreateChartなどのメソッドを使用してチャートタイプとワークシート内の位置を指定します。
C#を使用してExcelチャートをカスタマイズするために必要な手順は何ですか?
C#を使用してExcelチャートをカスタマイズするには、まずデータを準備し、それからAddSeriesメソッドを使用してデータシリーズを追加します。ワークシートのChartsプロパティを通じてチャートにアクセスし、タイトルや凡例の位置などのチャートプロパティをカスタマイズします。
C#で異なるチャートタイプを切り替える方法は?
C#で異なるチャートタイプを切り替えるのは簡単です。CreateChartメソッドのチャートタイプパラメータを変更するだけで、柱状、散布、線、円グラフ、バー、または面チャートを切り替えます。
C#でExcelチャートを編集した後、変更を保存する方法は?
Excelチャートを編集した後は、SaveAsメソッドを使用してブックに変更を保存し、すべての修正が保持されることを確認します。
C#を使用してExcelワークシートから既存のチャートを削除することはできますか?
はい、ワークシートのChartsプロパティを通じてチャートを取得し、それからRemoveChartメソッドを使用して削除することで、既存のチャートを削除できます。
.NETアプリケーションでのチャート作成にIronXLを使用する利点は何ですか?
IronXLを使用すると、Excel Interopに依存することなく効率的に.NETアプリケーションでチャートを作成および編集できます。これはパフォーマンスを向上させ、複雑さを軽減します。さまざまなチャートタイプをサポートし、簡単にカスタマイズできるメソッドを提供します。
データシリーズを追加した後、C#でチャートをプロットする方法は?
AddSeriesメソッドを使用してデータシリーズを追加した後、Plotメソッドを呼び出すことで、提供されたデータを使用してチャートを生成します。
C#で作成された後にチャートのプロパティを編集することは可能ですか?
はい、ワークシートのChartsプロパティを介してチャートにアクセスし、チャートタイトル、凡例の位置などの属性を変更することで、C#でチャートのプロパティを編集できます。

