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

This article was translated from English: Does it need improvement?
Translated
View the article in English

によって チャクニット・ビン

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

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


Excel 用 C# NuGet ライブラリ

でインストール NuGet

Install-Package IronXL.Excel
または
Java PDF JAR(ジャバPDF JAR)

ダウンロード DLL (ディーエルエル)

DLLをダウンロード

プロジェクトに手動でインストールする

Excel 用 C# NuGet ライブラリ

でインストール NuGet

Install-Package IronXL.Excel
または
Java PDF JAR(ジャバPDF JAR)

ダウンロード DLL (ディーエルエル)

DLLをダウンロード

プロジェクトに手動でインストールする

今日からプロジェクトでIronPDFを使い始めましょう。無料のトライアルをお試しください。

最初のステップ:
green arrow pointer

チェックアウト IronXL オン Nuget 迅速なインストールと展開のために。8百万以上のダウンロード数により、ExcelをC#で変革しています。

Excel 用 C# NuGet ライブラリ nuget.org/packages/IronXL.Excel/
Install-Package IronXL.Excel

インストールを検討してください IronXL DLL 直接。ダウンロードして、プロジェクトまたはGACの形式で手動でインストールしてください。 IronXL.zip

プロジェクトに手動でインストールする

DLLをダウンロード

グラフを作成する例

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

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

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

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

  4. Plot メソッドを呼び出してグラフをプロットします。 メソッドは追加されたすべてのデータを使用してチャートをプロットします。 このメソッドを複数回呼び出すと、既存のチャートを修正するのではなく、複数のチャートがプロットされます。

    以下のデータからいくつかのチャートを作成しましょう <a href='/static-assets/excel/how-to/create-edit-charts/chart.xlsx

チャート.xlsx' download='chart.xlsx

チャート.xlsx'>chart.xlsx

チャート.xlsx Excel file. A preview of the data is displayed below:

データ

カラムチャート

: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")
VB   C#
カラムチャート

折れ線グラフ

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

: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");
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("Line Chart")

' Set the legend position
chart.SetLegendPosition(LegendPosition.Bottom)

' Plot the chart
chart.Plot()

workBook.SaveAs("lineChart.xlsx")
VB   C#
折れ線グラフ

円グラフ

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

チャート編集の例

既存のチャートで編集できる項目がいくつかあります。 凡例の位置とチャートタイトルを編集できます。 チャートを編集するには、まず 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")
VB   C#
前
以下を日本語に翻訳してください:  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");
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")
VB   C#

チャクニット・ビン

ソフトウェアエンジニア

チャクニットは開発者のシャーロック・ホームズです。彼がソフトウェアエンジニアリングの将来性に気付いたのは、楽しみでコーディングチャレンジをしていたときでした。彼のフォーカスはIronXLとIronBarcodeにありますが、すべての製品でお客様を助けることに誇りを持っています。チャクニットは顧客と直接話すことで得た知識を活用して、製品自体のさらなる改善に貢献しています。彼の逸話的なフィードバックは、単なるJiraチケットを超えて、製品開発、ドキュメントおよびマーケティングをサポートし、顧客の全体的な体験を向上させます。オフィスにいないときは、機械学習やコーディングについて学んだり、ハイキングを楽しんだりしています。