How to Create and Edit Excel Charts in C#
IronXLはC#の開発者がシンプルなAPIコールを使ってExcelのチャートをプログラムで作成、編集、削除できるようにします。 Excel Interopに依存することなく、データから列、折れ線、円、その他のグラフタイプを直接生成できます。
Excelでは、チャートは、情報を視覚的に表示し、分析するために使用されるデータのグラフ表示です。 Excelには、棒グラフ、折れ線グラフ、円グラフなど、さまざまな種類のグラフがあり、それぞれ異なるデータや分析のニーズに適しています。 IronXLの包括的なExcelライブラリを使用すると、レポートやダッシュボードを強化するために、これらの視覚化をプログラムで作成することができます。
クイックスタート:数秒で折れ線グラフを作成してプロットIronXLを使用すれば、インストール、ワークブックの読み込み、CreateChartの呼び出し、データ系列の追加、タイトルと凡例位置の設定、そしてPlotの実行が、わずか数行で可能です。 この例では、Interop のオーバーヘッドなしに C# ネイティブのメソッドを使用してチャートを作成する方法を示します。
-
1Install IronXL with 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();C# -
3実際の環境でテストするためにデプロイする
今日プロジェクトで IronXL を使い始めましょう無料トライアル
最小限のワークフロー(5ステップ)
- C#ライブラリをダウンロードしてチャートを作成および編集します
- チャートを作成するためのデータを準備する
CreateChartメソッドを使用して、グラフの種類と位置を設定します。AddSeriesメソッドを使用してデータ系列を追加しますPlotメソッドを使用してチャートをプロットする
IronXLを使い始める
Excelでグラフを作成するには?
IronXLは、柱状、散布、折れ線、円、棒、面の各チャートをサポートしています。 チャートを作成するには、以下のコンポーネントを指定します。 この柔軟性により、データプレゼンテーションのニーズに合わせた豊富なビジュアライゼーションを持つExcelスプレッドシートを作成することができます。
- グラフタイプとワークシートの位置を指定するには
CreateChartを使用します。 AddSeriesでシリーズを追加します。 このメソッドは、いくつかのチャート・タイプについて単一の列を受け入れます。最初のパラメータは横軸の値です。 2つ目は、縦軸の値です。 3.オプションで、系列名、チャート名、凡例の位置を指定します。- グラフをレンダリングするには
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");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");

円グラフはいつ使うべきですか?
円グラフは、全体の割合とパーセンテージを示します。 円グラフの場合、必要なデータは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");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");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");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データ構造とシームレスに統合し、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をインストールできないサーバー環境やアプリケーションに最適です。
How do I create a column chart using IronXL?
To create a column chart, load your workbook using IronXL, set the chart type with `CreateChart`, add data series using `AddSeries`, and call `Plot` to finalize and display the chart.
Can IronXL handle real-time data for chart creation?
Yes, IronXL integrates with .NET data structures like `DataTables` and `Lists`, making it ideal for creating charts from real-time data sources and generating automated reports.
Why should I consider using pie charts with IronXL?
Pie charts are useful for showing proportions and percentages of a whole, and with IronXL, you can easily implement them to visually represent market share, budget allocation, or category distribution.
Can I create Excel charts without Excel installed using IronXL?
Yes, IronXL operates independently of Excel, allowing you to create and manipulate Excel charts programmatically without needing Excel to be installed on your system.

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