Excel处置处罚控件Aspose.Cells教程:利用C#在Excel中创建气泡图

[复制链接]
发表于 2025-12-22 12:57:39 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×

多维数据可视化对于深入分析至关紧张。气泡图答应您在单个图表中体现三个数据系列——X 轴、Y 轴和睦泡巨细。在本指南中,您将学习怎样利用 C# 和 Aspose.Cells for .NET 在 Excel 中创建气泡图。无论您须要展示贩卖业绩、市场份额还是科学数据,以下示例都将资助您仅用几行代码即可天生专业且可直接导出的图表。
Aspose.Cells官方试用版免费下载,请接洽慧都科技
参加Aspose技能互换QQ群(1041253375),与更多小搭档一起探究提升开发技能。
用于创建气泡图的 C# Excel 库

Aspose.Cells for .NET是一个功能强大的 Excel 自动化库,可简化图表创建,包罗气泡图。它提供丰富的 API,用于添加数据、设置图表范例、自界说序列以及将工作簿导出为各种格式(XLSX、PDF、PNG 等)。
Aspose.Cells 的紧张特性使其成为气泡图的抱负之选:

  • 强大的范例安全 API – Visual Studio 中完全支持 IntelliSense。
  • 直接利用图表——通过编程方式更改序列范例、标志巨细、颜色和坐标轴设置。
  • 高性能——处置处罚大型数据集时不会产生额外的用户界面开销。
  • 支持多种格式——生存为 XLSX、XLS、CSV、PDF、HTML 或图像。
入门

现在你已经可以通过编程方式创建气泡图了。
利用 C# 在 Excel 中创建气泡图

下面有两个完备的、可运行的示例,分析怎样重新开始天生气泡图以及怎样自界说其外貌。
示例 1 – 根本气泡图

此示例创建了一个简朴的气泡图,体现产物贩卖额(X 轴)、利润率(Y 轴)和市场份额(气泡巨细)。
  1. // ------------------------------------------------------------
  2. // 1. Create a new workbook and obtain the first worksheet.
  3. // ------------------------------------------------------------
  4. var workbook = new Workbook();
  5. Worksheet sheet = workbook.Worksheets[0];
  6. // ------------------------------------------------------------
  7. // 2. Populate worksheet with sample data for the bubble chart.
  8. // ------------------------------------------------------------
  9. //    A       B        C        D
  10. // 1  Product Sales   Profit   MarketShare
  11. // 2  Product A   120      30       0.20
  12. // 3  Product B   150      45       0.35
  13. // 4  Product C   180      55       0.50
  14. // ------------------------------------------------------------
  15. sheet.Cells["A1"].PutValue("Product");
  16. sheet.Cells["B1"].PutValue("Sales");
  17. sheet.Cells["C1"].PutValue("Profit");
  18. sheet.Cells["D1"].PutValue("MarketShare");
  19. string[] products = { "Product A", "Product B", "Product C" };
  20. double[] sales = { 120, 150, 180 };
  21. double[] profit = { 30, 45, 55 };
  22. double[] marketShare = { 0.20, 0.35, 0.50 };
  23. for (int i = 0; i < products.Length; i++)
  24. {
  25.     int row = i + 1; // Excel rows are 0?based in the API
  26.     sheet.Cells[row, 0].PutValue(products[i]);   // Column A
  27.     sheet.Cells[row, 1].PutValue(sales[i]);      // Column B
  28.     sheet.Cells[row, 2].PutValue(profit[i]);     // Column C
  29.     sheet.Cells[row, 3].PutValue(marketShare[i]); // Column D
  30. }
  31. // ------------------------------------------------------------
  32. // 3. Add a Bubble chart to the worksheet.
  33. // ------------------------------------------------------------
  34. int chartIndex = sheet.Charts.Add(ChartType.Bubble, 5, 0, 25, 15);
  35. Chart bubbleChart = sheet.Charts[chartIndex];
  36. bubbleChart.Title.Text = "Product Sales vs Profit (Bubble Size = Market Share)";
  37. bubbleChart.ShowLegend = true;
  38. // ------------------------------------------------------------
  39. // 4. Define the data series for the Bubble chart.
  40. // ------------------------------------------------------------
  41. // Series 0 ¨C X values (Sales) ¨C Column B
  42. int seriesIdx = bubbleChart.NSeries.Add("=Sheet1!$B$2:$B$4", true);
  43. // Set Y values (Profit) ¨C Column C
  44. bubbleChart.NSeries[seriesIdx].Values = "=Sheet1!$C$2:$C$4";
  45. // Set Bubble Sizes ¨C Column D (must be a positive number)
  46. bubbleChart.NSeries[seriesIdx].BubbleSizes = "=Sheet1!$D$2:$D$4";
  47. bubbleChart.NSeries[seriesIdx].Name = "Products";
  48. // ------------------------------------------------------------
  49. // 5. Customize axes (optional).
  50. // ------------------------------------------------------------
  51. bubbleChart.CategoryAxis.Title.Text = "Sales";
  52. bubbleChart.ValueAxis.Title.Text = "Profit";
  53. bubbleChart.ValueAxis.MinValue = 0;
  54. bubbleChart.ValueAxis.MaxValue = 100;
  55. // ------------------------------------------------------------
  56. // 6. Save the workbook to an XLSX file.
  57. // ------------------------------------------------------------
  58. workbook.Save("BubbleChart_Basic.xlsx");
复制代码
代码的作用

  • 创建工作簿并添补示例数据。
  • 添加ChartType.Bubble图表。
  • 将序列的 X 值、Y 值和睦泡巨细与添补范围关联起来。
  • (可选)设置坐标轴标题和限定。
  • 将工作簿生存为BubbleChart_Basic.xlsx。
示例 2 – 自界说气泡图(颜色、标志样式和辅助坐标轴)

此示例演示了高级自界说功能,比方自界说气泡颜色、边框样式,以及将图表放置在辅助坐标轴上以进步可读性。
  1. // ------------------------------------------------------------
  2. // 1. Initialize workbook and worksheet.
  3. // ------------------------------------------------------------
  4. var wb = new Workbook();
  5. var ws = wb.Worksheets[0];
  6. // ------------------------------------------------------------
  7. // 2. Insert data for two product groups.
  8. // ------------------------------------------------------------
  9. //    A         B        C        D        E        F
  10. // 1  Category  Sales1   Profit1  Share1   Sales2   Profit2   Share2
  11. // 2  Jan       80      20       0.15     65       30        0.10
  12. // 3  Feb       95      35       0.25     70       40        0.20
  13. // 4  Mar       110     50       0.30     85       55        0.35
  14. // ------------------------------------------------------------
  15. ws.Cells["A1"].PutValue("Month");
  16. ws.Cells["B1"].PutValue("Sales Group 1");
  17. ws.Cells["C1"].PutValue("Profit Group 1");
  18. ws.Cells["D1"].PutValue("Share Group 1");
  19. ws.Cells["E1"].PutValue("Sales Group 2");
  20. ws.Cells["F1"].PutValue("Profit Group 2");
  21. ws.Cells["G1"].PutValue("Share Group 2");
  22. string[] months = { "Jan", "Feb", "Mar" };
  23. double[] sales1 = { 80, 95, 110 };
  24. double[] profit1 = { 20, 35, 50 };
  25. double[] share1 = { 0.15, 0.25, 0.30 };
  26. double[] sales2 = { 65, 70, 85 };
  27. double[] profit2 = { 30, 40, 55 };
  28. double[] share2 = { 0.10, 0.20, 0.35 };
  29. for (int i = 0; i < months.Length; i++)
  30. {
  31.     int row = i + 1;
  32.     ws.Cells[row, 0].PutValue(months[i]);     // A column
  33.     ws.Cells[row, 1].PutValue(sales1[i]);     // B column
  34.     ws.Cells[row, 2].PutValue(profit1[i]);    // C column
  35.     ws.Cells[row, 3].PutValue(share1[i]);     // D column
  36.     ws.Cells[row, 4].PutValue(sales2[i]);     // E column
  37.     ws.Cells[row, 5].PutValue(profit2[i]);    // F column
  38.     ws.Cells[row, 6].PutValue(share2[i]);     // G column
  39. }
  40. // ------------------------------------------------------------
  41. // 3. Add a Bubble chart (combined for two groups).
  42. // ------------------------------------------------------------
  43. int chartIdx = ws.Charts.Add(ChartType.Bubble, 6, 0, 26, 15);
  44. Chart bubbleChart = ws.Charts[chartIdx];
  45. bubbleChart.Title.Text = "Quarterly Sales vs Profit (Bubble = Market Share)";
  46. bubbleChart.ShowLegend = true;
  47. // ------------------------------------------------------------
  48. // 4. First series ¨C Group 1
  49. // ------------------------------------------------------------
  50. int series1 = bubbleChart.NSeries.Add("=Sheet1!$B$2:$B$4", true);
  51. bubbleChart.NSeries[series1].Values = "=Sheet1!$C$2:$C$4";
  52. bubbleChart.NSeries[series1].BubbleSizes = "=Sheet1!$D$2:$D$4";
  53. bubbleChart.NSeries[series1].Name = "Group 1";
  54. // Apply custom formatting to series 1 bubbles
  55. bubbleChart.NSeries[series1].Marker.MarkerStyle = ChartMarkerType.Circle;
  56. bubbleChart.NSeries[series1].Marker.MarkerSize = 12; // Size of marker (not bubble size)
  57. bubbleChart.NSeries[series1].Marker.Area.Formatting = FormattingType.Custom;
  58. bubbleChart.NSeries[series1].Marker.Area.ForegroundColor = Color.CornflowerBlue;
  59. bubbleChart.NSeries[series1].Marker.Border.IsVisible = true;
  60. bubbleChart.NSeries[series1].Marker.Border.Color = Color.DarkBlue;
  61. bubbleChart.NSeries[series1].Marker.Border.Weight = WeightType.WideLine;
  62. // ------------------------------------------------------------
  63. // 5. Second series ¨C Group 2 (placed on secondary axis)
  64. // ------------------------------------------------------------
  65. int series2 = bubbleChart.NSeries.Add("=Sheet1!$E$2:$E$4", true);
  66. bubbleChart.NSeries[series2].Values = "=Sheet1!$F$2:$F$4";
  67. bubbleChart.NSeries[series2].BubbleSizes = "=Sheet1!$G$2:$G$4";
  68. bubbleChart.NSeries[series2].Name = "Group 2";
  69. bubbleChart.NSeries[series2].PlotOnSecondAxis = true; // Use secondary axis
  70. // Custom style for series 2 bubbles
  71. bubbleChart.NSeries[series2].Marker.MarkerStyle = ChartMarkerType.Square;
  72. bubbleChart.NSeries[series2].Marker.MarkerSize = 12;
  73. bubbleChart.NSeries[series2].Marker.Area.Formatting = FormattingType.Custom;
  74. bubbleChart.NSeries[series2].Marker.Area.ForegroundColor = Color.IndianRed;
  75. bubbleChart.NSeries[series2].Marker.Border.IsVisible = true;
  76. bubbleChart.NSeries[series2].Marker.Border.Color = Color.Maroon;
  77. bubbleChart.NSeries[series2].Marker.Border.Weight = WeightType.WideLine;
  78. // ------------------------------------------------------------
  79. // 6. Axis titles and formatting (optional)
  80. // ------------------------------------------------------------
  81. bubbleChart.CategoryAxis.Title.Text = "Sales";
  82. bubbleChart.ValueAxis.Title.Text = "Profit";
  83. bubbleChart.ValueAxis.MinValue = 0;
  84. bubbleChart.ValueAxis.MaxValue = 120;
  85. // Secondary axis ¨C make its title distinguishable
  86. bubbleChart.SecondValueAxis.Title.Text = "Profit (Group 2)";
  87. bubbleChart.SecondValueAxis.Title.Font.Color = Color.IndianRed;
  88. // ------------------------------------------------------------
  89. // 7. Save the workbook to XLSX and as PNG image (optional export)
  90. // ------------------------------------------------------------
  91. wb.Save("BubbleChart_Customized.xlsx");
  92. // Export the chart area as an image (requires Aspose.Cells rendering license)
  93. bubbleChart.ToImage("BubbleChart_Customized.png");
复制代码
定制亮点

  • 两组独立的泡泡系列(第 1 组和第 2 组),颜色和标志外形各不雷同。
  • 第二组数据绘制在辅助坐标轴上,当数据范围差异时,这非常有用。
  • 自界说标志格式(添补颜色、边框颜色、粗细)。
  • 可选择将图表导出为图像,以便将其包罗在陈诉或网页中。
Aspose.Cells官方试用版免费下载,请接洽慧都科技
参加Aspose技能互换QQ群(1041253375),与更多小搭档一起探究提升开发技能。
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表