C#でExcelグラフを作成・編集する方法
IronXLはC#の開発者がシンプルなAPIコールを使ってExcelのチャートをプログラムで作成、編集、削除できるようにします。 Excel Interopに依存することなく、データから列、折れ線、円、その他のグラフタイプを直接生成できます。
Excelでは、チャートは、情報を視覚的に表示し、分析するために使用されるデータのグラフ表示です。 Excelには、棒グラフ、折れ線グラフ、円グラフなど、さまざまな種類のグラフがあり、それぞれ異なるデータや分析のニーズに適しています。 IronXLの包括的なExcelライブラリを使用すると、レポートやダッシュボードを強化するために、これらの視覚化をプログラムで作成することができます。
クイックスタート:数秒で折れ線グラフを作成してプロットIronXL を使用すると、インストールしてワークブックを読み込み、CreateChart を呼び出してデータシリーズを追加し、タイトルと凡例の位置を設定して Plot - ほんの数行で可能です。 この例では、Interop のオーバーヘッドなしに C# ネイティブのメソッドを使用してチャートを作成する方法を示します。
-
1IronXL を NuGet Package Manager でインストールします
-
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();' Load workbook and create a line chart, then add a data series Dim chart As IChart = workSheet.CreateChart(ChartType.Line, 2, 2, 15, 10) Dim series As IChartSeries = 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()' Load workbook and create a line chart, then add a data series Dim chart As IChart = workSheet.CreateChart(ChartType.Line, 2, 2, 15, 10) Dim series As IChartSeries = 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を使い始めるには、無料トライアルをお試しください。
最小限のワークフロー(5ステップ)
- グラフを作成・編集するための C# ライブラリをダウンロード
- チャートを作成するためのデータを準備する
CreateChartメソッドを使用して、グラフの種類と位置を設定します。AddSeriesメソッドを使用してデータ系列を追加しますPlotメソッドを使用してチャートをプロットする
IronXLを使い始める
Excelでグラフを作成する方法
IronXLは、柱状、散布、折れ線、円、棒、面の各チャートをサポートしています。 チャートを作成するには、以下のコンポーネントを指定します。 この柔軟性により、データのプレゼンテーションのニーズに合わせたリッチなビジュアルを用いたExcelスプレッドシートを作成することができます。
CreateChartを使用して、グラフの種類とワークシートの場所を指定します。AddSeriesでシリーズを追加します。 このメソッドは、いくつかのチャート・タイプについて単一の列を受け入れます。最初のパラメータは横軸の値です。 2つ目は、縦軸の値です。- オプションでシリーズ名、グラフ名、凡例の位置を指定します。
- グラフをレンダリングするために
Plotを呼び出します。 複数のコールで複数のチャートを作成します。
chart.xlsx Excelファイルのデータからチャートを作成してみましょう。データのプレビューが以下に表示されます:

棒グラフを作成するプロセスは何ですか?
列グラフは、カテゴリ間の値を比較するのに理想的です。 スプレッドシートデータをロードするとき、データポイント間の違いを強調するために列グラフを使用して効果的に視覚化することができます。 次の例は、動物の個体数データで多列列グラフを作成する例です:
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");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")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")
折れ線グラフを作成するにはどうすればいいですか?
折れ線グラフは、経時的な傾向を示すのに優れています。折れ線グラフは、列グラフと同じ情報を表示するので、両者を切り替えるには、グラフの種類を変えるだけでよい。 これは特に、時系列データを含むXLSXファイルを読む際にラインチャートが非常に有用です。
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");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")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")
円グラフはいつ使用すべきですか?
円グラフは、全体の割合とパーセンテージを示します。 円グラフの場合、必要なデータは1列だけなので、実装は簡単です。 スプレッドシートのデータを市場占有率、予算配分、カテゴリーの分布の視覚的な表現に変換したいときに効果的です。
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");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")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")
既存のチャートを編集する方法
既存のExcelファイルを扱う場合、すでに作成されたチャートを修正する必要があるかもしれません。 IronXLは既存のチャートを編集する簡単な方法を提供し、タイトルの更新、凡例の位置変更、データの更新を可能にします。 これは、事前に存在するビジュアライゼーションを含むExcelファイルを編集するときに便利です。
既存のチャートの凡例位置とチャートタイトルを編集できます。 グラフを編集するには、まず Charts プロパティにアクセスして対象のグラフを選択します。 その後、チャートのプロパティにアクセスして編集してください:
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");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")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")
ビフォー

アフター
Excelからグラフを削除する方法
古いグラフや不要なグラフを削除して、Excelファイルを整理する必要がある場合があります。 これは複数のビジュアライゼーションを含むワークシートを管理する場合によくあります。 スプレッドシートから既存のグラフを削除するには、まず Charts プロパティからグラフを取得します。 図表のリストをお渡しします。 対象のグラフオブジェクトを RemoveChart に渡します:
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");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")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")高度なグラフカスタマイズ
IronXLは基本的なチャート作成だけでなく、高度なカスタマイズもサポートしています。 複雑なレポートやダッシュボードを作成する場合、条件付き書式設定のような他のExcel機能とチャートを組み合わせることで、包括的なデータの視覚化を作成することができます。
ビジネスアプリケーションの場合、チャートはデータベースクエリやリアルタイムデータソースから動的に生成する必要があります。 IronXL は .NET データ構造と統合されており、DataTables、Lists、または任意の列挙可能なコレクションからグラフを作成することができます。 そのため、視覚的要素を含む自動レポートの作成に最適です。
まとめ
IronXLはC#アプリケーションでExcelチャートを扱うための完全なソリューションを提供します。 新しいビジュアライゼーションの作成、既存のビジュアライゼーションの修正、古いチャートの削除のいずれにおいても、ライブラリはExcel Interopを必要としない直感的な方法を提供します。チャート機能をIronXLのデータ操作や書式設定などの他の機能と組み合わせることで、.NETアプリケーションでのデータ表示や分析を強化する洗練されたExcel自動化ソリューションを構築することができます。
よくある質問
IronXL を使用して作成できる Excel グラフの種類は何ですか?
IronXL は、Excel Interop を必要とせずに、C# アプリケーション内で直接、列、線、円、棒、散布、および面グラフなどのさまざまな Excel グラフの作成をサポートしています。
IronXL を使用して折れ線グラフを作成するにはどうすればよいですか?
IronXL で折れ線グラフを作成するには、ライブラリをインストールし、ワークブックを読み込み、CreateChart メソッドを使用してグラフタイプを線に設定し、AddSeries でデータ系列を追加し、その後 Plot を呼び出してグラフをレンダリングしてください。
IronXL は既存の Excel グラフの編集に役立ちますか?
はい、IronXL は既存の Excel グラフを編集するためのメソッドを提供しており、グラフのタイトルを更新したり、凡例の位置を変更したり、データを簡単に更新することができます。
IronXL を使用して Excel ワークシートからグラフを削除することは可能ですか?
はい、IronXL を使用すると Charts プロパティにアクセスし、目的のグラフで RemoveChart を呼び出すことにより、Excel ワークシートからグラフを削除できます。
IronXL を使用して Excel グラフをさらにカスタマイズするにはどうすればよいですか?
基本的なグラフ作成に加えて、IronXL はデータベースクエリからの動的生成や .NET データ構造との統合など、高度なカスタマイズをサポートしており、包括的なデータビジュアライゼーションを可能にします。
Excel Interop を使用してグラフを作成する場合と比べて、IronXL を使用する利点は何ですか?
IronXL は C# でグラフの作成と編集を Excel Interop に依存せずに可能にし、Excel ファイルをプログラム的に扱うためのより簡単で効率的な方法を提供します。
IronXL を使用して列グラフを作成するにはどうすればよいですか?
列グラフを作成するには、IronXL を使用してワークブックを読み込み、CreateChart でグラフタイプを設定し、AddSeries を使用してデータ系列を追加し、Plot を呼び出してグラフの作成を完了して表示してください。
IronXL はリアルタイムデータを扱うグラフ作成に対応していますか?
はい、IronXL は DataTables や Lists などの .NET データ構造と連携しており、リアルタイムデータソースからのグラフ作成や自動報告書の生成に理想的です。
IronXL を使用して円グラフを使用する理由は何ですか?
円グラフは全体の中での割合や割合を示すのに便利であり、IronXL を使用すると、市場シェア、予算配分、カテゴリ分布を視覚的に表示するために容易に実装できます。
Excel がインストールされていなくても IronXL を使用して Excel グラフを作成できますか?
はい、IronXL は Excel とは独立して動作し、システムに Excel をインストールすることなく、プログラム的に Excel グラフを作成および操作できます。
Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。