马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
多维数据可视化对于深入分析至关紧张。气泡图答应您在单个图表中体现三个数据系列——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. Create a new workbook and obtain the first worksheet.
- // ------------------------------------------------------------
- var workbook = new Workbook();
- Worksheet sheet = workbook.Worksheets[0];
- // ------------------------------------------------------------
- // 2. Populate worksheet with sample data for the bubble chart.
- // ------------------------------------------------------------
- // A B C D
- // 1 Product Sales Profit MarketShare
- // 2 Product A 120 30 0.20
- // 3 Product B 150 45 0.35
- // 4 Product C 180 55 0.50
- // ------------------------------------------------------------
- sheet.Cells["A1"].PutValue("Product");
- sheet.Cells["B1"].PutValue("Sales");
- sheet.Cells["C1"].PutValue("Profit");
- sheet.Cells["D1"].PutValue("MarketShare");
- string[] products = { "Product A", "Product B", "Product C" };
- double[] sales = { 120, 150, 180 };
- double[] profit = { 30, 45, 55 };
- double[] marketShare = { 0.20, 0.35, 0.50 };
- for (int i = 0; i < products.Length; i++)
- {
- int row = i + 1; // Excel rows are 0?based in the API
- sheet.Cells[row, 0].PutValue(products[i]); // Column A
- sheet.Cells[row, 1].PutValue(sales[i]); // Column B
- sheet.Cells[row, 2].PutValue(profit[i]); // Column C
- sheet.Cells[row, 3].PutValue(marketShare[i]); // Column D
- }
- // ------------------------------------------------------------
- // 3. Add a Bubble chart to the worksheet.
- // ------------------------------------------------------------
- int chartIndex = sheet.Charts.Add(ChartType.Bubble, 5, 0, 25, 15);
- Chart bubbleChart = sheet.Charts[chartIndex];
- bubbleChart.Title.Text = "Product Sales vs Profit (Bubble Size = Market Share)";
- bubbleChart.ShowLegend = true;
- // ------------------------------------------------------------
- // 4. Define the data series for the Bubble chart.
- // ------------------------------------------------------------
- // Series 0 ¨C X values (Sales) ¨C Column B
- int seriesIdx = bubbleChart.NSeries.Add("=Sheet1!$B$2:$B$4", true);
- // Set Y values (Profit) ¨C Column C
- bubbleChart.NSeries[seriesIdx].Values = "=Sheet1!$C$2:$C$4";
- // Set Bubble Sizes ¨C Column D (must be a positive number)
- bubbleChart.NSeries[seriesIdx].BubbleSizes = "=Sheet1!$D$2:$D$4";
- bubbleChart.NSeries[seriesIdx].Name = "Products";
- // ------------------------------------------------------------
- // 5. Customize axes (optional).
- // ------------------------------------------------------------
- bubbleChart.CategoryAxis.Title.Text = "Sales";
- bubbleChart.ValueAxis.Title.Text = "Profit";
- bubbleChart.ValueAxis.MinValue = 0;
- bubbleChart.ValueAxis.MaxValue = 100;
- // ------------------------------------------------------------
- // 6. Save the workbook to an XLSX file.
- // ------------------------------------------------------------
- workbook.Save("BubbleChart_Basic.xlsx");
复制代码 代码的作用
- 创建工作簿并添补示例数据。
- 添加ChartType.Bubble图表。
- 将序列的 X 值、Y 值和睦泡巨细与添补范围关联起来。
- (可选)设置坐标轴标题和限定。
- 将工作簿生存为BubbleChart_Basic.xlsx。
示例 2 – 自界说气泡图(颜色、标志样式和辅助坐标轴)
此示例演示了高级自界说功能,比方自界说气泡颜色、边框样式,以及将图表放置在辅助坐标轴上以进步可读性。- // ------------------------------------------------------------
- // 1. Initialize workbook and worksheet.
- // ------------------------------------------------------------
- var wb = new Workbook();
- var ws = wb.Worksheets[0];
- // ------------------------------------------------------------
- // 2. Insert data for two product groups.
- // ------------------------------------------------------------
- // A B C D E F
- // 1 Category Sales1 Profit1 Share1 Sales2 Profit2 Share2
- // 2 Jan 80 20 0.15 65 30 0.10
- // 3 Feb 95 35 0.25 70 40 0.20
- // 4 Mar 110 50 0.30 85 55 0.35
- // ------------------------------------------------------------
- ws.Cells["A1"].PutValue("Month");
- ws.Cells["B1"].PutValue("Sales Group 1");
- ws.Cells["C1"].PutValue("Profit Group 1");
- ws.Cells["D1"].PutValue("Share Group 1");
- ws.Cells["E1"].PutValue("Sales Group 2");
- ws.Cells["F1"].PutValue("Profit Group 2");
- ws.Cells["G1"].PutValue("Share Group 2");
- string[] months = { "Jan", "Feb", "Mar" };
- double[] sales1 = { 80, 95, 110 };
- double[] profit1 = { 20, 35, 50 };
- double[] share1 = { 0.15, 0.25, 0.30 };
- double[] sales2 = { 65, 70, 85 };
- double[] profit2 = { 30, 40, 55 };
- double[] share2 = { 0.10, 0.20, 0.35 };
- for (int i = 0; i < months.Length; i++)
- {
- int row = i + 1;
- ws.Cells[row, 0].PutValue(months[i]); // A column
- ws.Cells[row, 1].PutValue(sales1[i]); // B column
- ws.Cells[row, 2].PutValue(profit1[i]); // C column
- ws.Cells[row, 3].PutValue(share1[i]); // D column
- ws.Cells[row, 4].PutValue(sales2[i]); // E column
- ws.Cells[row, 5].PutValue(profit2[i]); // F column
- ws.Cells[row, 6].PutValue(share2[i]); // G column
- }
- // ------------------------------------------------------------
- // 3. Add a Bubble chart (combined for two groups).
- // ------------------------------------------------------------
- int chartIdx = ws.Charts.Add(ChartType.Bubble, 6, 0, 26, 15);
- Chart bubbleChart = ws.Charts[chartIdx];
- bubbleChart.Title.Text = "Quarterly Sales vs Profit (Bubble = Market Share)";
- bubbleChart.ShowLegend = true;
- // ------------------------------------------------------------
- // 4. First series ¨C Group 1
- // ------------------------------------------------------------
- int series1 = bubbleChart.NSeries.Add("=Sheet1!$B$2:$B$4", true);
- bubbleChart.NSeries[series1].Values = "=Sheet1!$C$2:$C$4";
- bubbleChart.NSeries[series1].BubbleSizes = "=Sheet1!$D$2:$D$4";
- bubbleChart.NSeries[series1].Name = "Group 1";
- // Apply custom formatting to series 1 bubbles
- bubbleChart.NSeries[series1].Marker.MarkerStyle = ChartMarkerType.Circle;
- bubbleChart.NSeries[series1].Marker.MarkerSize = 12; // Size of marker (not bubble size)
- bubbleChart.NSeries[series1].Marker.Area.Formatting = FormattingType.Custom;
- bubbleChart.NSeries[series1].Marker.Area.ForegroundColor = Color.CornflowerBlue;
- bubbleChart.NSeries[series1].Marker.Border.IsVisible = true;
- bubbleChart.NSeries[series1].Marker.Border.Color = Color.DarkBlue;
- bubbleChart.NSeries[series1].Marker.Border.Weight = WeightType.WideLine;
- // ------------------------------------------------------------
- // 5. Second series ¨C Group 2 (placed on secondary axis)
- // ------------------------------------------------------------
- int series2 = bubbleChart.NSeries.Add("=Sheet1!$E$2:$E$4", true);
- bubbleChart.NSeries[series2].Values = "=Sheet1!$F$2:$F$4";
- bubbleChart.NSeries[series2].BubbleSizes = "=Sheet1!$G$2:$G$4";
- bubbleChart.NSeries[series2].Name = "Group 2";
- bubbleChart.NSeries[series2].PlotOnSecondAxis = true; // Use secondary axis
- // Custom style for series 2 bubbles
- bubbleChart.NSeries[series2].Marker.MarkerStyle = ChartMarkerType.Square;
- bubbleChart.NSeries[series2].Marker.MarkerSize = 12;
- bubbleChart.NSeries[series2].Marker.Area.Formatting = FormattingType.Custom;
- bubbleChart.NSeries[series2].Marker.Area.ForegroundColor = Color.IndianRed;
- bubbleChart.NSeries[series2].Marker.Border.IsVisible = true;
- bubbleChart.NSeries[series2].Marker.Border.Color = Color.Maroon;
- bubbleChart.NSeries[series2].Marker.Border.Weight = WeightType.WideLine;
- // ------------------------------------------------------------
- // 6. Axis titles and formatting (optional)
- // ------------------------------------------------------------
- bubbleChart.CategoryAxis.Title.Text = "Sales";
- bubbleChart.ValueAxis.Title.Text = "Profit";
- bubbleChart.ValueAxis.MinValue = 0;
- bubbleChart.ValueAxis.MaxValue = 120;
- // Secondary axis ¨C make its title distinguishable
- bubbleChart.SecondValueAxis.Title.Text = "Profit (Group 2)";
- bubbleChart.SecondValueAxis.Title.Font.Color = Color.IndianRed;
- // ------------------------------------------------------------
- // 7. Save the workbook to XLSX and as PNG image (optional export)
- // ------------------------------------------------------------
- wb.Save("BubbleChart_Customized.xlsx");
- // Export the chart area as an image (requires Aspose.Cells rendering license)
- bubbleChart.ToImage("BubbleChart_Customized.png");
复制代码 定制亮点
- 两组独立的泡泡系列(第 1 组和第 2 组),颜色和标志外形各不雷同。
- 第二组数据绘制在辅助坐标轴上,当数据范围差异时,这非常有用。
- 自界说标志格式(添补颜色、边框颜色、粗细)。
- 可选择将图表导出为图像,以便将其包罗在陈诉或网页中。
Aspose.Cells官方试用版免费下载,请接洽慧都科技
参加Aspose技能互换QQ群(1041253375),与更多小搭档一起探究提升开发技能。
|