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氣泡圖內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: