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

Chaknith related to C#でExcelチャートを作成および編集する方法
チャクニット・ビン
2023年10月17日
更新済み 2024年12月10日
共有:
This article was translated from English: Does it need improvement?
Translated
View the article in English

Excelでは、チャートはデータのグラフィカルな表示です。 それはデータをより理解しやすく、有意義な形で表示および分析するための視覚ツールです。 Excelは、棒グラフ、折れ線グラフ、円グラフなど、さまざまな種類のチャートを提供しており、それぞれが異なる種類のデータおよび分析に適しています。

IronXLは、列、散布図、折れ線、円、棒、およびエリアチャートに対応しており、シリーズ名、凡例の位置、チャートタイトル、チャートの位置を設定することができます。


IronXLで始めましょう

今日から無料トライアルでIronXLをあなたのプロジェクトで使い始めましょう。

最初のステップ:
green arrow pointer


グラフを作成する例

IronXLは、列グラフ、散布図、折れ線グラフ、円グラフ、棒グラフ、および面グラフをサポートしています。 チャートを作成するためには、いくつかの項目を別々に指定しなければなりません。

  1. 最初に、CreateChart メソッドを使用して、ワークシート内のチャートの種類と位置を指定します。

  2. シリーズをAddSeriesメソッドで追加します。 このメソッドは、一部のチャートタイプにはそれだけで十分であるため、1列のデータも受け入れます。 最初のパラメータは横軸の値を表します。 2番目のパラメーターは垂直軸の値を表します。

  3. シリーズ名、チャート名、および凡例の位置をオプションで指定できます。

  4. チャートをプロットするには、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");
柱状図

折れ線グラフ

折れ線グラフは棒グラフと同じくらいの情報を表すことができるため、両者を切り替えるのは非常に簡単です。 グラフの種類を変更するだけで済みます。

: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");
折れ線グラフ

円グラフ

円グラフには、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");
円グラフ

チャート編集の例

既存のチャートで編集できる項目がいくつかあります。 凡例の位置とチャートタイトルを編集できます。 グラフを編集するには、まず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");
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");
Chaknith related to チャート削除の例
ソフトウェアエンジニア
チャクニットは開発者のシャーロック・ホームズです。彼がソフトウェアエンジニアリングの将来性に気付いたのは、楽しみでコーディングチャレンジをしていたときでした。彼のフォーカスはIronXLとIronBarcodeにありますが、すべての製品でお客様を助けることに誇りを持っています。チャクニットは顧客と直接話すことで得た知識を活用して、製品自体のさらなる改善に貢献しています。彼の逸話的なフィードバックは、単なるJiraチケットを超えて、製品開発、ドキュメントおよびマーケティングをサポートし、顧客の全体的な体験を向上させます。オフィスにいないときは、機械学習やコーディングについて学んだり、ハイキングを楽しんだりしています。