
可視化長期趨勢是許多商業報告的核心需求。折線圖能夠清晰直觀地呈現連續軸上的數據序列,因此非常適合展示業績、銷售或任何基於時間的數據。在本指南中,我們將向您展示如何使用Aspose.Cells for .NET和 C# 以編程方式生成折線圖。
Aspose.Cells官方試用版免費下載,請聯繫慧都科技
加入Aspose技術交流QQ羣(1041253375),與更多小夥伴一起探討提升開發技能。
用於創建折線圖的 C# Excel 庫
Aspose.Cells for .NET是一款功能全面的 API,使開發人員無需 Microsoft Office 即可處理 Excel 文件。它支持創建、編輯和自定義各種類型的圖表,包括折線圖。主要優勢包括:
- 豐富的 API 接口——完全控制圖表類型、系列、座標軸和格式。
- 無 COM 依賴項– 可在任何支持 .NET 的平台上運行。
- 高性能——高效處理大型工作簿。
- 支持多種格式——讀/寫 XLSX、XLS、CSV、PDF 等格式。
入門
-
從慧都網下載最新的 Aspose.Cells for .NET 。
-
通過 NuGet安裝: PM> Install-Package Aspose.Cells
-
Aspose.Cells在你的項目中添加對它的引用。
使用 C# 在 Excel 中創建折線圖
以下是兩個實際例子:
- 簡單的單系列折線圖——非常適合快速可視化趨勢。
- 帶輔助軸的多系列折線圖——在比較不同尺度的數據集時非常有用。
這兩個例子都是完整的、可直接編譯的 C# 程序。
示例 1 – 簡單單系列折線圖
// 1. Create a new workbook and obtain the first worksheet.
var workbook = new Workbook();
var sheet = workbook.Worksheets[0];
// ------------------------------------------------------------
// 2. Populate worksheet with sample data.
// ------------------------------------------------------------
// A B
// 1 Month Sales
// 2 Jan 120
// 3 Feb 150
// 4 Mar 180
// 5 Apr 210
// ------------------------------------------------------------
string[] months = { "Jan", "Feb", "Mar", "Apr" };
double[] sales = { 120, 150, 180, 210 };
sheet.Cells["A1"].PutValue("Month");
sheet.Cells["B1"].PutValue("Sales");
for (int i = 0; i < months.Length; i++)
{
sheet.Cells[i + 1, 0].PutValue(months[i]); // Column A
sheet.Cells[i + 1, 1].PutValue(sales[i]); // Column B
}
// ------------------------------------------------------------
// 3. Add a Line chart object.
// ------------------------------------------------------------
// Parameters: (type, upper left row, upper left column, lower right row, lower right column)
int chartIndex = sheet.Charts.Add(ChartType.Line, 6, 0, 26, 10);
Chart chart = sheet.Charts[chartIndex];
chart.Title.Text = "Monthly Sales Trend";
// ------------------------------------------------------------
// 4. Add the series ¨C data range refers to the Sales column.
// ------------------------------------------------------------
int seriesIndex = chart.NSeries.Add("=Sheet1!$B$2:$B$5", true);
chart.NSeries[seriesIndex].Name = "Sales";
// ------------------------------------------------------------
// 5. Set category (X?axis) data ¨C months.
// ------------------------------------------------------------
chart.NSeries.CategoryData = "=Sheet1!$A$2:$A$5";
// ------------------------------------------------------------
// 6. Optional: customize axes titles.
// ------------------------------------------------------------
chart.CategoryAxis.Title.Text = "Month";
chart.ValueAxis.Title.Text = "Sales";
// ------------------------------------------------------------
// 7. Save the workbook.
// ------------------------------------------------------------
workbook.Save("SimpleLineChart.xlsx");
Console.WriteLine("Workbook with Line chart saved as SimpleLineChart.xlsx");
解釋
- 工作簿在內存中創建,並填充了月份和銷售數據。
- ChartType.Line創建基本折線圖。
- NSeries.Add定義數據系列;CategoryData分配 X 軸標籤。
- 生成的文件SimpleLineChart.xlsx包含一個格式完整的折線圖。
示例 2 – 帶輔助座標軸的多系列折線圖
// 1. Initialize workbook and worksheet.
var workbook = new Workbook();
var sheet = workbook.Worksheets[0];
// ------------------------------------------------------------
// 2. Add sample data for two metrics: Revenue (primary) and
// Profit Margin (secondary).
// ------------------------------------------------------------
// A B C
// 1 Month Revenue Profit%
// 2 Jan 3000 12
// 3 Feb 3500 15
// 4 Mar 4000 13
// 5 Apr 3800 14
// ------------------------------------------------------------
string[] months = { "Jan", "Feb", "Mar", "Apr" };
double[] revenue = { 3000, 3500, 4000, 3800 };
double[] profitPct = { 12, 15, 13, 14 };
sheet.Cells["A1"].PutValue("Month");
sheet.Cells["B1"].PutValue("Revenue");
sheet.Cells["C1"].PutValue("Profit%");
for (int i = 0; i < months.Length; i++)
{
sheet.Cells[i + 1, 0].PutValue(months[i]); // Month
sheet.Cells[i + 1, 1].PutValue(revenue[i]); // Revenue
sheet.Cells[i + 1, 2].PutValue(profitPct[i]); // Profit%
}
// ------------------------------------------------------------
// 3. Insert a Line chart.
// ------------------------------------------------------------
int chartIdx = sheet.Charts.Add(ChartType.Line, 7, 0, 27, 12);
Chart chart = sheet.Charts[chartIdx];
chart.Title.Text = "Revenue vs. Profit Margin";
chart.NSeries.CategoryData = "=Sheet1!$A$2:$A$5";
// ------------------------------------------------------------
// 4. Add Revenue series (primary axis).
// ------------------------------------------------------------
int revSeriesIdx = chart.NSeries.Add("=Sheet1!$B$2:$B$5", true);
chart.NSeries[revSeriesIdx].Name = "Revenue";
chart.NSeries[revSeriesIdx].Type = ChartType.Line; // Explicit, though default
// ------------------------------------------------------------
// 5. Add Profit% series (secondary axis) and set marker style.
// ------------------------------------------------------------
int profitSeriesIdx = chart.NSeries.Add("=Sheet1!$C$2:$C$5", true);
chart.NSeries[profitSeriesIdx].Name = "Profit%";
chart.NSeries[profitSeriesIdx].Type = ChartType.Line;
chart.NSeries[profitSeriesIdx].PlotOnSecondAxis = true; // Use secondary Y?axis
// Optional: customize marker for profit series
chart.NSeries[profitSeriesIdx].Marker.MarkerStyle = ChartMarkerType.Circle;
chart.NSeries[profitSeriesIdx].Marker.MarkerSize = 10;
chart.NSeries[profitSeriesIdx].Marker.Area.ForegroundColor = Color.Orange;
// ------------------------------------------------------------
// 6. Axis titles.
// ------------------------------------------------------------
chart.CategoryAxis.Title.Text = "Month";
chart.ValueAxis.Title.Text = "Revenue (USD)";
chart.SecondValueAxis.Title.Text = "Profit %";
// ------------------------------------------------------------
// 7. Save workbook.
// ------------------------------------------------------------
workbook.Save("MultiSeriesLineChart.xlsx");
Console.WriteLine("Workbook saved as MultiSeriesLineChart.xlsx");
要點
- 增加了兩個系列:收入(主軸)和利潤率(次軸),以説明不同的尺度。
- PlotOnSecondAxis = true將第二組數據移至 Y 軸右側。
- 標記自定義使次要系列在視覺上獨具特色。
- 最終文件MultiSeriesLineChart.xlsx包含一個功能齊全的多系列折線圖。
結論
使用 Aspose.Cells for .NET 創建折線圖既簡單又高度可定製。無論您需要快速繪製單系列趨勢線,還是需要創建帶有輔助座標軸的複雜多系列圖表,該庫都可通過直觀的 API 提供全面的控制。您可以以提供的代碼示例為起點,並根據您的具體報表需求進行調整。
Aspose.Cells官方試用版免費下載,請聯繫慧都科技
加入Aspose技術交流QQ羣(1041253375),與更多小夥伴一起探討提升開發技能。