C# 创建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#】

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");

}

}

}

气泡图效果:

【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

到此这篇关于C# 创建Excel气泡图的实例代码的文章就介绍到这了,更多相关C# Excel气泡图内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

以上是 C# 创建Excel气泡图的实例代码 的全部内容, 来源链接: utcz.com/z/335746.html

回到顶部