C#でExcelグラフを作成および編集する方法: xlsxファイル対応 Excel操作 C# チュートリアル
IronXLはC#の開発者がシンプルなAPIコールを使ってxlsxファイルのExcelのチャートをプログラムで作成、編集、削除できるようにします。 Excel Interopに依存することなく、スプレッドシート作成でデータから列、折れ線、円、その他のグラフタイプを直接生成できます。
Excelでは、チャートは、情報を視覚的に表示し、分析するために使用されるデータのグラフ表示です。 Excelには、棒グラフ、折れ線グラフ、円グラフなど、さまざまな種類のグラフがあり、それぞれ異なるデータや分析のニーズに適しています。 When working with IronXL's comprehensive Excel library, you can programmatically create these visualizations to enhance your reports and dashboards.
クイックスタート:数秒で折れ線グラフを作成してプロット
IronXLを使えば、インストール、ワークブックの読み込み、CreateChartの呼び出し、データ系列の追加、タイトルと凡例の位置の設定、Plot-すべて数行で行えます。 この例では、Interop のオーバーヘッドなしに C# ネイティブのメソッドを使用してチャートを作成する方法を示します。
-
IronXL をNuGetパッケージマネージャでインストール
PM > Install-Package IronXL.Excel -
このコード スニペットをコピーして実行します。
// Load workbook and create a line chart with data series var chart = workSheet.CreateChart(ChartType.Line, 2, 2, 15, 10).AddSeries("A2:A10","B2:B10").Title = workSheet["B1"].StringValue; // Set title and legend position, then plot the chart chart.SetTitle("Quick Line Chart").SetLegendPosition(LegendPosition.Bottom).Plot(); -
実際の環境でテストするためにデプロイする
今日プロジェクトで IronXL を使い始めましょう無料トライアル
最小限のワークフロー(5ステップ)
- C#ライブラリをダウンロードしてチャートを作成および編集します
- チャートを作成するためのデータを準備する
CreateChartメソッドを使用して、グラフの種類と位置を設定します。AddSeriesメソッドを使用してデータ系列を追加しますPlotメソッドを使用してチャートをプロットする
IronXLを使い始める
Excelでグラフを作成するには?
IronXLは、柱状、散布、折れ線、円、棒、面の各チャートをサポートしています。 チャートを作成するには、以下のコンポーネントを指定します。 This flexibility allows you to create Excel spreadsheets with rich visualizations tailored to your data presentation needs.
1.CreateChartを使用して、チャートの種類とワークシートの場所を指定します。
2.AddSeriesでシリーズを追加します。 このメソッドは、いくつかのチャート・タイプについて単一の列を受け入れます。最初のパラメータは横軸の値です。 2つ目は、縦軸の値です。
3.オプションで、系列名、チャート名、凡例の位置を指定します。
4.Plotを呼び出してチャートを表示します。 複数のコールで複数のチャートを作成します。
Let's create charts from the data in the chart.xlsx Excel file. A preview of the data is displayed below:
列チャートを作成するプロセスとは
列グラフは、カテゴリ間の値を比較するのに理想的です。 When you load spreadsheet data, you can visualize it effectively using column charts to highlight differences between data points. 次の例は、動物の個体数データで多列列グラフを作成する例です:
: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")
どのように折れ線グラフを作成しますか?
折れ線グラフは、経時的な傾向を示すのに優れています。折れ線グラフは、列グラフと同じ情報を表示するので、両者を切り替えるには、グラフの種類を変えるだけでよい。 これは、時系列データを含むXLSXファイルを読むときに、折れ線グラフが特に役立ちます:
: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")
円グラフはいつ使うべきですか?
円グラフは、全体の割合とパーセンテージを示します。 円グラフの場合、必要なデータは1列だけなので、実装は簡単です。 They're effective when you want to convert spreadsheet data into visual representations of market share, budget allocation, or category distribution:
: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")
既存のチャートを編集するにはどうすればよいですか?
既存のExcelファイルを扱う場合、すでに作成されたチャートを修正する必要があるかもしれません。 IronXLは既存のチャートを編集する簡単な方法を提供し、タイトルの更新、凡例の位置変更、データの更新を可能にします。 This is useful when editing Excel files that contain pre-existing visualizations.
既存のチャートの凡例位置とチャートタイトルを編集できます。 チャートを編集するには、まず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")
ビフォー
アフター
Excelからグラフを削除するには?
古いグラフや不要なグラフを削除して、Excelファイルを整理する必要がある場合があります。 This is common when managing worksheets containing multiple visualizations. スプレッドシートから既存のチャートを削除するには、まず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")
高度なグラフのカスタマイズ
IronXLは基本的なチャート作成だけでなく、高度なカスタマイズもサポートしています。 When creating complex reports or dashboards, you can combine charts with other Excel features like conditional formatting to create comprehensive data visualizations.
ビジネスアプリケーションの場合、チャートはデータベースクエリやリアルタイムデータソースから動的に生成する必要があります。 IronXLは.NETのデータ構造とシームレスに統合され、DataTables、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をインストールできないサーバー環境やアプリケーションに最適です。

