C# 创建Excel气泡图

  • A+
所属分类:.NET技术
摘要

气泡图(Bubble Chart)是可用于展示三个变量之间的关系。通过绘制x 值, y 值和大小值即可确定图表中气泡的坐标及大小。下面通过后端C#代码及VB.NET代码展示如何来实现在Excel中创建气泡图的方法。

气泡图(Bubble Chart)是可用于展示三个变量之间的关系。通过绘制x 值, y 值和大小值即可确定图表中气泡的坐标及大小。下面通过后端C#代码及VB.NET代码展示如何来实现在Excel中创建气泡图的方法。

程序环境:

  • Visual Studio
  • .Net FrameWork 4.5.1
  • Spire.XLS for .NET Version 10.12.0

注:编辑代码前先将Spire.Xls.dll(dll文件可在解压包Bin文件夹下获取)添加引用至VS程序,可通过官网下载或者Nuget下载。参考如下引用效果:

C# 创建Excel气泡图

 

 

【C#】

using Spire.Xls; using Spire.Xls.Charts; using System.Drawing;  namespace CreateBubbleChart_XLS {     class Program     {         static void Main(string[] args)         {             //创建一个Excel工作簿,并获取第一张工作表             Workbook wb = new Workbook();             wb.Version = ExcelVersion.Version2013;             Worksheet sheet = wb.Worksheets[0];                        //添加图表,指定图表类型             Chart chart = sheet.Charts.Add();             chart.ChartType = ExcelChartType.Bubble;             chart.PrimaryCategoryAxis.Title = "人均消费";             chart.PrimaryValueAxis.Title = "点评数";              //设置图表的名称和数据             Spire.Xls.Charts.ChartSerie cs1 = chart.Series.Add("气泡图");             cs1.EnteredDirectlyValues = new object[]{2, 5, 3, 6, 7};             cs1.EnteredDirectlyCategoryLabels = new object[]{1, 4.5, 1.2, 3.3, 2};             cs1.EnteredDirectlyBubbles = new object[]{3, 6, 1, 4, 7};                         //设置图表位置             chart.LeftColumn = 4;             chart.TopRow = 2;             chart.RightColumn = 12;             chart.BottomRow = 22;              //设置图表标题             chart.ChartTitle = "人均消费VS点评数";             chart.ChartTitleArea.IsBold = true;             chart.ChartTitleArea.Size = 12;                 chart.Legend.Position = LegendPositionType.Top;              //保存文档             wb.SaveToFile("BubbleChart.xlsx", ExcelVersion.Version2013);             System.Diagnostics.Process.Start("BubbleChart.xlsx");         }     } }

气泡图效果:

C# 创建Excel气泡图

 

【vb.net

Imports Spire.Xls Imports Spire.Xls.Charts Imports System.Drawing  Namespace CreateBubbleChart_XLS     Class Program         Private Shared Sub Main(args As String())             '创建一个Excel工作簿,并获取第一张工作表             Dim wb As New Workbook()             wb.Version = ExcelVersion.Version2013             Dim sheet As Worksheet = wb.Worksheets(0)              '添加图表,指定图表类型             Dim chart As Chart = sheet.Charts.Add()             chart.ChartType = ExcelChartType.Bubble             chart.PrimaryCategoryAxis.Title = "人均消费"             chart.PrimaryValueAxis.Title = "点评数"              '设置图表的名称和数据             Dim cs1 As Spire.Xls.Charts.ChartSerie = chart.Series.Add("气泡图")             cs1.EnteredDirectlyValues = New Object() {2, 5, 3, 6, 7}             cs1.EnteredDirectlyCategoryLabels = New Object() {1, 4.5, 1.2, 3.3, 2}             cs1.EnteredDirectlyBubbles = New Object() {3, 6, 1, 4, 7}              '设置图表位置             chart.LeftColumn = 4             chart.TopRow = 2             chart.RightColumn = 12             chart.BottomRow = 22              '设置图表标题             chart.ChartTitle = "人均消费VS点评数"             chart.ChartTitleArea.IsBold = True             chart.ChartTitleArea.Size = 12             chart.Legend.Position = LegendPositionType.Top              '保存文档             wb.SaveToFile("BubbleChart.xlsx", ExcelVersion.Version2010)             System.Diagnostics.Process.Start("BubbleChart.xlsx")         End Sub     End Class End Namespace