在 Excel 中,圖表是資料的圖形化呈現,用於以視覺化方式顯示和分析資訊。 Excel 提供多種圖表型別,例如條形圖、線圖和圓餅圖,每種圖表皆適用於不同的資料與分析需求。 When working with IronXL's comprehensive Excel library, you can programmatically create these visualizations to enhance your reports and dashboards.
// Load workbook and create a line chart with data seriesvar 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 chartchart.SetTitle("Quick Line Chart").SetLegendPosition(LegendPosition.Bottom).Plot();
// 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 支援柱狀圖、散點圖、線圖、圓餅圖、條形圖及面積圖。 要建立圖表,請指定以下元件。 This flexibility allows you to create Excel spreadsheets with rich visualizations tailored to your data presentation needs.
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. 以下範例展示如何使用動物族群資料建立多系列柱狀圖:
using IronXL;using IronXL.Drawing.Charts;WorkBook workBook = WorkBook.Load("chart.xlsx");WorkSheet workSheet = workBook.DefaultWorkSheet;// Set the chart type and positionIChart chart = workSheet.CreateChart(ChartType.Column, 5, 5, 20, 10);string xAxis = "A2:A7";// Add the seriesIChartSeries series = chart.AddSeries(xAxis, "B2:B7");series.Title = workSheet["B1"].StringValue;// Add the seriesseries = chart.AddSeries(xAxis, "C2:C7");series.Title = workSheet["C1"].StringValue;// Add the seriesseries = chart.AddSeries(xAxis, "D2:D7");series.Title = workSheet["D1"].StringValue;// Set the chart titlechart.SetTitle("Column Chart");// Set the legend positionchart.SetLegendPosition(LegendPosition.Bottom);// Plot the chartchart.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");
ImportsIronXLImportsIronXL.Drawing.ChartsPrivate workBook AsWorkBook = WorkBook.Load("chart.xlsx")Private workSheet AsWorkSheet = workBook.DefaultWorkSheet' Set the chart type and positionPrivate chart AsIChart = workSheet.CreateChart(ChartType.Column, 5, 5, 20, 10)Private xAxis AsString = "A2:A7"' Add the seriesPrivate series AsIChartSeries = chart.AddSeries(xAxis, "B2:B7")series.Title = workSheet("B1").StringValue' Add the seriesseries = chart.AddSeries(xAxis, "C2:C7")series.Title = workSheet("C1").StringValue' Add the seriesseries = chart.AddSeries(xAxis, "D2:D7")series.Title = workSheet("D1").StringValue' Set the chart titlechart.SetTitle("Column Chart")' Set the legend positionchart.SetLegendPosition(LegendPosition.Bottom)' Plot the chartchart.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")
using IronXL;using IronXL.Drawing.Charts;WorkBook workBook = WorkBook.Load("chart.xlsx");WorkSheet workSheet = workBook.DefaultWorkSheet;// Set the chart type and positionIChart chart = workSheet.CreateChart(ChartType.Pie, 5, 5, 20, 10);string xAxis = "A2:A7";// Add the seriesIChartSeries series = chart.AddSeries(xAxis, "B2:B7");series.Title = workSheet["B1"].StringValue;// Set the chart titlechart.SetTitle("Pie Chart");// Set the legend positionchart.SetLegendPosition(LegendPosition.Bottom);// Plot the chartchart.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");
ImportsIronXLImportsIronXL.Drawing.ChartsPrivate workBook AsWorkBook = WorkBook.Load("chart.xlsx")Private workSheet AsWorkSheet = workBook.DefaultWorkSheet' Set the chart type and positionPrivate chart AsIChart = workSheet.CreateChart(ChartType.Pie, 5, 5, 20, 10)Private xAxis AsString = "A2:A7"' Add the seriesPrivate series AsIChartSeries = chart.AddSeries(xAxis, "B2:B7")series.Title = workSheet("B1").StringValue' Set the chart titlechart.SetTitle("Pie Chart")' Set the legend positionchart.SetLegendPosition(LegendPosition.Bottom)' Plot the chartchart.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")
何時該使用圓餅圖?
圓形圖表用以顯示整體中的比例與百分比。 對於圓形圖,僅需一欄資料,因此更易於實作。 They're effective when you want to convert spreadsheet data into visual representations of market share, budget allocation, or category distribution:
using IronXL;using IronXL.Drawing.Charts;WorkBook workBook = WorkBook.Load("chart.xlsx");WorkSheet workSheet = workBook.DefaultWorkSheet;// Set the chart type and positionIChart chart = workSheet.CreateChart(ChartType.Pie, 5, 5, 20, 10);string xAxis = "A2:A7";// Add the seriesIChartSeries series = chart.AddSeries(xAxis, "B2:B7");series.Title = workSheet["B1"].StringValue;// Set the chart titlechart.SetTitle("Pie Chart");// Set the legend positionchart.SetLegendPosition(LegendPosition.Bottom);// Plot the chartchart.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");
ImportsIronXLImportsIronXL.Drawing.ChartsPrivate workBook AsWorkBook = WorkBook.Load("chart.xlsx")Private workSheet AsWorkSheet = workBook.DefaultWorkSheet' Set the chart type and positionPrivate chart AsIChart = workSheet.CreateChart(ChartType.Pie, 5, 5, 20, 10)Private xAxis AsString = "A2:A7"' Add the seriesPrivate series AsIChartSeries = chart.AddSeries(xAxis, "B2:B7")series.Title = workSheet("B1").StringValue' Set the chart titlechart.SetTitle("Pie Chart")' Set the legend positionchart.SetLegendPosition(LegendPosition.Bottom)' Plot the chartchart.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.
using IronXL;using IronXL.Drawing.Charts;WorkBook workBook = WorkBook.Load("pieChart.xlsx");WorkSheet workSheet = workBook.DefaultWorkSheet;// Retrieve the chartIChart chart = workSheet.Charts[0];// Edit the legend positionchart.SetLegendPosition(LegendPosition.Top);// Edit the chart titlechart.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");
ImportsIronXLImportsIronXL.Drawing.ChartsPrivate workBook AsWorkBook = WorkBook.Load("pieChart.xlsx")Private workSheet AsWorkSheet = workBook.DefaultWorkSheet' Retrieve the chartPrivate chart AsIChart = workSheet.Charts(0)' Edit the legend positionchart.SetLegendPosition(LegendPosition.Top)' Edit the chart titlechart.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:
using IronXL;using IronXL.Drawing.Charts;using System.Collections.Generic;WorkBook workBook = WorkBook.Load("pieChart.xlsx");WorkSheet workSheet = workBook.DefaultWorkSheet;// Retrieve the chartList<IChart> chart = workSheet.Charts;// Remove the chartworkSheet.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");
ImportsIronXLImportsIronXL.Drawing.ChartsImportsSystem.Collections.GenericPrivate workBook AsWorkBook = WorkBook.Load("pieChart.xlsx")Private workSheet AsWorkSheet = workBook.DefaultWorkSheet' Retrieve the chartPrivate chart AsList(OfIChart) = workSheet.Charts' Remove the chartworkSheet.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.
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.