博客 / 詳情

返回

Excel處理控件Aspose.Cells教程:使用C#在Exce中創建帕累託線圖

使用帕累託線圖可視化缺陷、銷售額或任何指標的分佈情況,有助於您快速識別最重要的影響因素。本指南將指導您如何使用Aspose.Cells for .NET和 C# 在 Excel 中創建帕累託線圖。示例涵蓋數據準備、圖表創建、帕累託線配置以及將工作簿保存為各種格式。

Aspose.Cells官方試用版免費下載,請聯繫Aspose官方授權代理商慧都科技

加入Aspose技術交流QQ羣(1041253375),與更多小夥伴一起探討提升開發技能。

用於創建帕累託線圖的 C# Excel 庫

Aspose.Cells for .NET是一個功能全面的 Excel 操作庫,它允許開發人員在無需安裝 Microsoft Office 的情況下創建、修改和渲染 Excel 文件。它支持多種圖表類型,包括帕累託線圖(ParetoLine chart),該圖表將柱狀圖與累計百分比線相結合。

使用 Aspose.Cells for .NET 的主要優勢:

  • 豐富的 API – 完全訪問 Excel 功能,包括高級圖表類型。
  • 高性能——高效處理大型工作簿和數據集。
  • 多種輸出格式– 保存為 XLSX、XLS、CSV、PDF、PNG 等格式。
  • 不支持 COM 互操作– 可在任何支持 .NET 的平台上運行。

快速入門:

PM> Install-Package Aspose.Cells

使用 C# 在 Excel 中創建帕累託線圖

概述

帕累託圖本質上是由柱狀圖(顯示原始值)和折線圖(顯示這些值的累計百分比)組成。步驟如下:

  1. 創建Workbook並獲取第一個工作表。
  2. 在工作表中填充分類數據及其對應的數值。
  3. 添加柱狀圖
  4. 添加第二個系列(累計百分比),並將其圖表類型設置為折線圖
  5. 啓用線條系列的輔助座標軸。
  6. 格式化座標軸、標題和數據標籤。
  7. 保存工作簿。

完整的 C# 代碼示例

// 1. Create a new workbook and get the first worksheet.
var workbook = new Workbook();
var sheet = workbook.Worksheets[0];
sheet.Name = "Defects";

// ------------------------------------------------------------
// 2. Populate worksheet with sample data (Category | Defect Count)
// ------------------------------------------------------------
//    A          B
// 1  Defect     Count
// 2  A          120
// 3  B          90
// 4  C          45
// 5  D          30
// 6  E          15
// ------------------------------------------------------------
sheet.Cells["A1"].Value = "Defect";
sheet.Cells["B1"].Value = "Count";

string[] categories = { "A", "B", "C", "D", "E" };
double[] counts = { 120, 90, 45, 30, 15 };

for (int i = 0; i < categories.Length; i++)
{
    sheet.Cells[i + 1, 0].Value = categories[i];
    sheet.Cells[i + 1, 1].Value = counts[i];
}

// 3. Calculate cumulative percentages (required for Pareto line)
double total = 0;
foreach (var c in counts) total += c;

double cumulative = 0;
for (int i = 0; i < counts.Length; i++)
{
    cumulative += counts[i];
    // Store cumulative percentage in column C (as a decimal, e.g., 0.75)
    sheet.Cells[i + 1, 2].Value = cumulative / total;
}
sheet.Cells["C1"].Value = "Cumulative %";

// 4. Add a Column chart (base type)
int chartIndex = sheet.Charts.Add(ChartType.Column, 7, 0, 25, 15);
Chart chart = sheet.Charts[chartIndex];
chart.Title.Text = "ParetoLine Chart ¨C Defect Distribution";

// 5. Add the primary series (Column) ¨C Defect Count
int columnSeriesIdx = chart.NSeries.Add("=Defects!$B$2:$B$6", true);
chart.NSeries[columnSeriesIdx].Name = "Count";
chart.NSeries[columnSeriesIdx].Type = ChartType.Column; // Explicitly set Column

// 6. Add the secondary series (Line) ¨C Cumulative Percentage
int lineSeriesIdx = chart.NSeries.Add("=Defects!$C$2:$C$6", true);
chart.NSeries[lineSeriesIdx].Name = "Cumulative %";
chart.NSeries[lineSeriesIdx].Type = ChartType.Line; // Set to Line chart
chart.NSeries[lineSeriesIdx].PlotOnSecondAxis = true; // Use secondary axis

// 7. Configure the secondary axis to display percentage format
chart.SecondValueAxis.TickLabels.NumberFormat = "0%";
chart.SecondValueAxis.Title.Text = "Cumulative Percentage";

// 8. Optional: Show data labels for better readability
chart.NSeries[columnSeriesIdx].DataLabels.ShowValue = true;
chart.NSeries[lineSeriesIdx].DataLabels.ShowValue = true;
chart.NSeries[lineSeriesIdx].DataLabels.NumberFormat = "0%";

// 9. Set the primary axis title
chart.CategoryAxis.Title.Text = "Defect Type";
chart.ValueAxis.Title.Text = "Count";

// 10. Save the workbook in XLSX format (can also be saved as PDF, PNG, etc.)
string outputPath = "ParetoLineChart_Output.xlsx";
workbook.Save(outputPath);
Console.WriteLine($"ParetoLine chart created successfully. File saved at: {outputPath}");

關鍵步驟説明

目的
2 插入帕累託圖將要表示的原始數據。
3 計算該系列所需的累計百分比。
4-5 創建基本柱形圖並添加第一個系列(缺陷計數)。
6-7 添加第二個系列,將其類型設置為“線”,並將其放置在輔助座標軸上。
8 將輔助座標軸格式設置為百分比,並添加數據標籤以提高清晰度。
10 保存工作簿;Aspose.Cells 還可以通過更改文件擴展名將圖表渲染為 PDF、PNG 等格式。

結論

使用 Aspose.Cells for .NET 創建帕累託折線圖非常簡單:準備數據、添加柱狀圖、疊加用於顯示累計百分比的折線圖,並微調外觀即可。提供的代碼可直接編譯運行,生成專業美觀的帕累託分析圖,並可保存為任何受支持的格式。

Aspose.Cells官方試用版免費下載,請聯繫Aspose官方授權代理商慧都科技

加入Aspose技術交流QQ羣(1041253375),與更多小夥伴一起探討提升開發技能。

user avatar
0 位用戶收藏了這個故事!

發佈 評論

Some HTML is okay.