C#でExcelチャートを作成および編集する方法
Excelでは、チャートはデータのグラフィカルな表示です。 それはデータをより理解しやすく、有意義な形で表示および分析するための視覚ツールです。 Excelは、棒グラフ、折れ線グラフ、円グラフなど、さまざまな種類のチャートを提供しており、それぞれが異なる種類のデータおよび分析に適しています。
IronXLは、列、散布図、折れ線、円、棒、およびエリアチャートに対応しており、シリーズ名、凡例の位置、チャートタイトル、チャートの位置を設定することができます。
C#でExcelチャートを作成および編集する方法
- チャートを作成および編集するためのC#ライブラリをダウンロード
- グラフ作成用のデータを準備する
CreateChart
メソッドを使用して、チャートの種類と位置を設定します。- データシリーズを追加するには、
AddSeries
メソッドを使用します。 Plot
メソッドを使用してチャートをプロットする
IronXLで始めましょう
今日から無料トライアルでIronXLをあなたのプロジェクトで使い始めましょう。
グラフを作成する例
IronXLは、列グラフ、散布図、折れ線グラフ、円グラフ、棒グラフ、および面グラフをサポートしています。 チャートを作成するためには、いくつかの項目を別々に指定しなければなりません。
-
最初に、
CreateChart
メソッドを使用して、ワークシート内のチャートの種類と位置を指定します。 -
シリーズを
AddSeries
メソッドで追加します。 このメソッドは、一部のチャートタイプにはそれだけで十分であるため、1列のデータも受け入れます。 最初のパラメータは横軸の値を表します。 2番目のパラメーターは垂直軸の値を表します。 -
シリーズ名、チャート名、および凡例の位置をオプションで指定できます。
-
チャートをプロットするには、
Plot
メソッドを呼び出します。 メソッドは追加されたすべてのデータを使用してチャートをプロットします。 このメソッドを複数回呼び出すと、既存のチャートを修正するのではなく、複数のチャートがプロットされます。Excelファイルchart.xlsxのデータからいくつかのチャートを作成しましょう。データのプレビューは以下に表示されています:
カラムチャート
: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")

折れ線グラフ
折れ線グラフは棒グラフと同じくらいの情報を表すことができるため、両者を切り替えるのは非常に簡単です。 グラフの種類を変更するだけで済みます。
: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プロパティからチャートを取得します。 チャートプロパティからチャートのリストを受け取ります。 対象のチャートオブジェクトを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")