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には、棒グラフ、折れ線グラフ、円グラフなど、さまざまな種類のグラフがあり、それぞれ異なるデータや分析のニーズに適しています。 IronXLの包括的なExcelライブラリを使用すると、レポートやダッシュボードを強化するために、これらの視覚化をプログラムで作成することができます。

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

IronXLを使用すれば、インストール、ワークブックの読み込み、CreateChartの呼び出し、データ系列の追加、タイトルと凡例位置の設定、そしてPlotの実行が、わずか数行で可能です。 この例では、Interop のオーバーヘッドなしに C# ネイティブのメソッドを使用してチャートを作成する方法を示します。

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

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

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

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

    arrow pointer


IronXLを使い始める


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

IronXLは、柱状、散布、折れ線、円、棒、面の各チャートをサポートしています。 チャートを作成するには、以下のコンポーネントを指定します。 この柔軟性により、データプレゼンテーションのニーズに合わせた豊富なビジュアライゼーションを持つExcelスプレッドシートを作成することができます。

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

chart.xlsx Excelファイルのデータからチャートを作成してみましょう。データのプレビューが以下に表示されます:

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

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

列グラフは、カテゴリ間の値を比較するのに理想的です。 スプレッドシートデータをロードするとき、データポイント間の違いを強調するために列グラフを使用して効果的に視覚化することができます。 次の例は、動物の個体数データで多列列グラフを作成する例です:

: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-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.Line, 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

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

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

Dim xAxis As String = "A2:A7"

' Add the series
Dim 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")
$vbLabelText   $csharpLabel
キリン、ゾウ、サイの動物データ表と、それに対応する3つの傾向線を持つ折れ線グラフを示すExcelスプレッドシート。

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

円グラフは、全体の割合とパーセンテージを示します。 円グラフの場合、必要なデータは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")
$vbLabelText   $csharpLabel
野生動物のデータと月ごとのキリンの分布を示す円グラフを含むスプレッドシート。

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

既存のExcelファイルを扱う場合、すでに作成されたチャートを修正する必要があるかもしれません。 IronXLは既存のチャートを編集する簡単な方法を提供し、タイトルの更新、凡例の位置変更、データの更新を可能にします。 これは、既存のビジュアライゼーションを含むExcelファイルを編集する際に役立ちます。

既存のチャートの凡例位置とチャートタイトルを編集できます。 グラフを編集するには、まず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
色分けされたセグメントと下部の凡例を含む、1 月から 6 月までの月次データを示す円グラフ
色分けされたセグメントと凡例を含む、1 月から 6 月までの月次データ分布を示す円グラフ

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

古いグラフや不要なグラフを削除して、Excelファイルを整理する必要がある場合があります。 これは、複数のビジュアライゼーションを含むワークシートを管理する場合によく見られます。 スプレッドシートから既存のグラフを削除するには、まず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は基本的なチャート作成だけでなく、高度なカスタマイズもサポートしています。 複雑なレポートやダッシュボードを作成する場合、条件付き書式設定のような他のExcel機能とチャートを組み合わせることで、包括的なデータの視覚化を作成することができます。

ビジネスアプリケーションの場合、チャートはデータベースクエリやリアルタイムデータソースから動的に生成する必要があります。 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
テクニカルライター

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

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

準備はできましたか?
Nuget ダウンロード 2,134,203 | バージョン: 2026.7 リリースされたばかり
Still Scrolling Icon

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

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