1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

C#を使用してExcelでバブルチャートを追加する方法

Last updated at Posted at 2022-01-28

バブルチャートとは、3つの変数間の関係を示すために使用するものます。x値、y値、およびサイズ値をプロットすることにより、チャート内のバブルの座標とサイズを決定できます。以下に、バックエンドのC#コードとVB.NETコードを使用してExcelでバブルチャートを作成する方法を紹介します。

##プログラム環境:

Visual Studio
.Net Framework 4.5.1
Spire.XLS for.NETバージョン10.12.0

注:コードを編集する前に、Spire.Xls.dll(dllファイルは解凍されたパッケージのBinフォルダーにあります)への参照をVSプログラムに追加します。VSプログラムは、公式WebサイトまたはNugetからダウンロードできます。次の引用結果を参照してください。
02.png

##コード一覧
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");
        }
    }
}

バブルチャートの追加した結果は以下のようになります:
01.png

またVB.NETのコードは以下の通りです:

Imports Spire.Xls
Imports Spire.Xls.Charts
Imports System.Drawing
 
Namespace CreateBubbleChart_XLS
    Class Program
        Shared  Sub Main(ByVal args() As String)
            'Excelブックを作成し、最初のシートを取得する
            Dim wb As Workbook =  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.Version2013)
            System.Diagnostics.Process.Start("BubbleChart.xlsx")
        End Sub
    End Class
End Namespace

##結語
以上は今回のExcelにバブルチャートを追加する方法でした、最後まで読んでいただきありがとうございます。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?