蓝指尖尖 發表於 2025-12-22 10:35:00

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

<p><img src="https://image.evget.com/attachment/keditor/image/20251222/095249_4.png"></p>
<p>多维数据可视化对于深入分析至关重要。气泡图允许您在单个图表中显示三个数据系列——X 轴、Y 轴和气泡大小。在本指南中,您将学习如何使用 C# 和&nbsp;<strong>Aspose.Cells for .NET&nbsp;</strong>在 Excel 中创建气泡图。无论您需要展示销售业绩、市场份额还是科学数据,以下示例都将帮助您仅用几行代码即可生成专业且可直接导出的图表。</p>
<p style="text-align: center"><span style="color: rgba(230, 126, 35, 1)"><strong>Aspose.Cells官方试用版免费下载,请联系慧都科技</strong></span></p>
<p style="text-align: center"><span style="color: rgba(230, 126, 35, 1)"><strong><em>加入Aspose技术交流QQ群(1041253375),与更多小伙伴一起探讨提升开发技能。</em></strong></span></p>
<h2 id="csharp-excel-library">用于创建气泡图的 C# Excel 库</h2>
<p><strong>Aspose.Cells for .NET</strong>是一个功能强大的 Excel 自动化库,可简化图表创建,包括气泡图。它提供丰富的 API,用于添加数据、配置图表类型、自定义序列以及将工作簿导出为各种格式(XLSX、PDF、PNG 等)。</p>
<p><strong>Aspose.Cells</strong>&nbsp;的主要特性使其成为气泡图的理想之选:</p>
<ul>
<li><strong>强大的类型安全 API</strong>&nbsp;– Visual Studio 中完全支持 IntelliSense。</li>
<li><strong>直接操作图表</strong>——通过编程方式更改序列类型、标记大小、颜色和坐标轴设置。</li>
<li><strong>高性能</strong>——处理大型数据集时不会产生额外的用户界面开销。</li>
<li><strong>支持多种格式</strong>——保存为 XLSX、XLS、CSV、PDF、HTML 或图像。</li>
</ul>
<h3 id="getting-started">入门</h3>
<ol>
<li>从<strong>慧都网Aspose.Cells页面下载</strong>该库。</li>
<li>从NuGet安装
<pre class="prettyprint highlighter-hljs"><code>PM&gt; Install-Package Aspose.Cells</code></pre>
</li>
<li><strong>Aspose.Cells</strong>在你的C#项目中添加对它的引用。</li>
</ol>
<p>现在你已经可以通过编程方式创建气泡图了。</p>
<h2 id="create-bubble-chart-in-excel">使用 C# 在 Excel 中创建气泡图</h2>
<p>下面有两个完整的、可运行的示例,说明如何从头开始生成气泡图以及如何自定义其外观。</p>
<h3 id="example-1--basic-bubble-chart">示例 1 – 基本气泡图</h3>
<p>此示例创建了一个简单的气泡图,显示产品销售额(X 轴)、利润率(Y 轴)和市场份额(气泡大小)。</p>
<pre class="prettyprint lang-cs highlighter-hljs"><code>// ------------------------------------------------------------
// 1. Create a new workbook and obtain the first worksheet.
// ------------------------------------------------------------
var workbook = new Workbook();
Worksheet sheet = workbook.Worksheets;

// ------------------------------------------------------------
// 2. Populate worksheet with sample data for the bubble chart.
// ------------------------------------------------------------
//    A       B      C      D
// 1Product Sales   Profit   MarketShare
// 2Product A   120      30       0.20
// 3Product B   150      45       0.35
// 4Product 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 &lt; products.Length; i++)
{
    int row = i + 1; // Excel rows are 0?based in the API
    sheet.Cells.PutValue(products);   // Column A
    sheet.Cells.PutValue(sales);      // Column B
    sheet.Cells.PutValue(profit);   // Column C
    sheet.Cells.PutValue(marketShare); // 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;

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.Values = "=Sheet1!$C$2:$C$4";

// Set Bubble Sizes ¨C Column D (must be a positive number)
bubbleChart.NSeries.BubbleSizes = "=Sheet1!$D$2:$D$4";

bubbleChart.NSeries.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");</code></pre>
<p><strong>代码的作用</strong></p>
<ol>
<li>创建工作簿并填充示例数据。</li>
<li>添加ChartType.Bubble图表。</li>
<li>将序列的 X 值、Y 值和气泡大小与填充范围关联起来。</li>
<li>(可选)设置坐标轴标题和限制。</li>
<li>将工作簿保存为<em>BubbleChart_Basic.xlsx</em>。</li>
</ol>
<h3 id="example-2--customized-bubble-chart-colors-marker-styles-and-secondary-axis">示例 2 – 自定义气泡图(颜色、标记样式和辅助坐标轴)</h3>
<p>此示例演示了高级自定义功能,例如自定义气泡颜色、边框样式,以及将图表放置在辅助坐标轴上以提高可读性。</p>
<pre class="prettyprint lang-cs highlighter-hljs"><code>// ------------------------------------------------------------
// 1. Initialize workbook and worksheet.
// ------------------------------------------------------------
var wb = new Workbook();
var ws = wb.Worksheets;

// ------------------------------------------------------------
// 2. Insert data for two product groups.
// ------------------------------------------------------------
//    A         B      C      D      E      F
// 1CategorySales1   Profit1Share1   Sales2   Profit2   Share2
// 2Jan       80      20       0.15   65       30      0.10
// 3Feb       95      35       0.25   70       40      0.20
// 4Mar       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 &lt; months.Length; i++)
{
    int row = i + 1;
    ws.Cells.PutValue(months);   // A column
    ws.Cells.PutValue(sales1);   // B column
    ws.Cells.PutValue(profit1);    // C column
    ws.Cells.PutValue(share1);   // D column
    ws.Cells.PutValue(sales2);   // E column
    ws.Cells.PutValue(profit2);    // F column
    ws.Cells.PutValue(share2);   // 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;
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.Values = "=Sheet1!$C$2:$C$4";
bubbleChart.NSeries.BubbleSizes = "=Sheet1!$D$2:$D$4";
bubbleChart.NSeries.Name = "Group 1";

// Apply custom formatting to series 1 bubbles
bubbleChart.NSeries.Marker.MarkerStyle = ChartMarkerType.Circle;
bubbleChart.NSeries.Marker.MarkerSize = 12; // Size of marker (not bubble size)
bubbleChart.NSeries.Marker.Area.Formatting = FormattingType.Custom;
bubbleChart.NSeries.Marker.Area.ForegroundColor = Color.CornflowerBlue;
bubbleChart.NSeries.Marker.Border.IsVisible = true;
bubbleChart.NSeries.Marker.Border.Color = Color.DarkBlue;
bubbleChart.NSeries.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.Values = "=Sheet1!$F$2:$F$4";
bubbleChart.NSeries.BubbleSizes = "=Sheet1!$G$2:$G$4";
bubbleChart.NSeries.Name = "Group 2";
bubbleChart.NSeries.PlotOnSecondAxis = true; // Use secondary axis

// Custom style for series 2 bubbles
bubbleChart.NSeries.Marker.MarkerStyle = ChartMarkerType.Square;
bubbleChart.NSeries.Marker.MarkerSize = 12;
bubbleChart.NSeries.Marker.Area.Formatting = FormattingType.Custom;
bubbleChart.NSeries.Marker.Area.ForegroundColor = Color.IndianRed;
bubbleChart.NSeries.Marker.Border.IsVisible = true;
bubbleChart.NSeries.Marker.Border.Color = Color.Maroon;
bubbleChart.NSeries.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");</code></pre>
<p><strong>定制亮点</strong></p>
<ul>
<li>两组独立的泡泡系列(第 1 组和第 2 组),颜色和标记形状各不相同。</li>
<li>第二组数据绘制在辅助坐标轴上,当数据范围不同时,这非常有用。</li>
<li>自定义标记格式(填充颜色、边框颜色、粗细)。</li>
<li>可选择将图表导出为图像,以便将其包含在报告或网页中。</li>
</ul>
<p style="text-align: center"><span style="color: rgba(230, 126, 35, 1)"><strong>Aspose.Cells官方试用版免费下载,请联系慧都科技</strong></span></p>
<p style="text-align: center"><span style="color: rgba(230, 126, 35, 1)"><strong><em>加入Aspose技术交流QQ群(1041253375),与更多小伙伴一起探讨提升开发技能。</em></strong></span></p><br><br>
来源:https://www.cnblogs.com/software-Development/p/19381161
頁: [1]
查看完整版本: Excel处理控件Aspose.Cells教程:使用C#在Excel中创建气泡图