How to create the PowerPoint bubble chart in C# and VB.NET?
This article explains how to create a bubble chart in WinForms PowerPoint using Presentation library.
What is a bubble chart
A bubble chart represents data points with bubbles along with an additional dimension of the data, the size of bubbles. Like a scatter chart, a bubble chart does not use a category axis. Both x (horizontal) and y (vertical) axes are value axes in addition with z (size) values.
Bubble chart created using Presentation library
Steps to create bubble chart in PowerPoint using Presentation library.
- Initialize chart
Create a chart object by calling the slide.Charts.AddChart method and specify the chart type to OfficeChartType.Bubble enum value.
//Create the chart IPresentationChart chart = slide.Charts.AddChart(100, 10, 700, 500); //Set chart type to bubble chart.ChartType = OfficeChartType.Bubble;
- Assign data and chart elements
Add the basic elements like chart title, data labels, and legend.
- ChartTitle of chart object.
- Set DataLabels via DefaultDataPoint.
- Add CategoryLabels and Values
- Set TRUE to chart’s HasLegend property to show the legend, else FALSE.
//Apply chart elements //Set chart title chart.ChartTitle = "Bubble Chart"; //Set legend chart.HasLegend = false; //Set Datalabels, CategoryLabels, and Values IOfficeChartSerie serie = chart.Series.Add(); serie.CategoryLabels = chart.ChartData[2,1,11,1]; serie.Values = chart.ChartData[2, 2, 11, 2]; serie.Bubbles = chart.ChartData[2, 3, 11, 3]; serie.DataPoints.DefaultDataPoint.DataLabels.IsValue = true; serie.DataPoints.DefaultDataPoint.DataLabels.Position = OfficeDataLabelPosition.Center;
Applicable properties of bubble chart
NOTE:
- Applying properties apart from the mentioned list might throw exception or the changes will not be reflected in the output document because those properties are not related to bubble chart.
- Bubbles with negative size are said to be the negative bubbles.
To learn more about creating charts with various settings using Presentation library, please refer the documentation.
The following C# and VB.NET complete code snippet shows the creation of bubble chart using Presentation library.
C#
using Syncfusion.Presentation; using Syncfusion.OfficeChart; using System.IO; namespace ChartSample { class Program { static void Main(string[] args) { //Create an instance for PowerPoint using (IPresentation pptxDoc = Presentation.Create()) { //Add a blank slide to Presentation ISlide slide = pptxDoc.Slides.Add(SlideLayoutType.Blank); //Adds chart to the slide with position and size IPresentationChart chart = slide.Charts.AddChart(100, 10, 700, 500); //Set the chart type chart.ChartType = OfficeChartType.Bubble; //Apply chart elements //Set chart title chart.ChartTitle = "Bubble Chart"; //Set data to the chart SetChartData(chart); //Set Datalabels, CategoryLabels, and Values IOfficeChartSerie serie = chart.Series.Add(); serie.CategoryLabels = chart.ChartData[2, 1, 11, 1]; serie.Values = chart.ChartData[2, 2, 11, 2]; serie.Bubbles = chart.ChartData[2, 3, 11, 3]; serie.DataPoints.DefaultDataPoint.DataLabels.IsValue = true; serie.DataPoints.DefaultDataPoint.DataLabels.Position = OfficeDataLabelPosition.Center; //Saving and closing the presentation Stream stream = File.Create("Output.pptx"); pptxDoc.Save(stream); } } /// <summary> /// Set the values for the chart /// </summary> /// <param name="chart">Represent the instance of the Presentation chart</param> private static void SetChartData(IPresentationChart chart) { chart.ChartData.SetValue(1, 1, "X"); chart.ChartData.SetValue(2, 1, -10); chart.ChartData.SetValue(3, 1, -20); chart.ChartData.SetValue(4, 1, -30); chart.ChartData.SetValue(5, 1, -40); chart.ChartData.SetValue(6, 1, -50); chart.ChartData.SetValue(7, 1, 10); chart.ChartData.SetValue(8, 1, 20); chart.ChartData.SetValue(9, 1, 30); chart.ChartData.SetValue(10, 1, 40); chart.ChartData.SetValue(11, 1, 50); chart.ChartData.SetValue(1, 2, "Y"); chart.ChartData.SetValue(2, 2, -100); chart.ChartData.SetValue(3, 2, -200); chart.ChartData.SetValue(4, 2, -300); chart.ChartData.SetValue(5, 2, -400); chart.ChartData.SetValue(6, 2, -500); chart.ChartData.SetValue(7, 2, 100); chart.ChartData.SetValue(8, 2, 200); chart.ChartData.SetValue(9, 2, 300); chart.ChartData.SetValue(10, 2, 400); chart.ChartData.SetValue(11, 2, 500); chart.ChartData.SetValue(1, 3, "Size"); chart.ChartData.SetValue(2, 3, 1); chart.ChartData.SetValue(3, 3, -1); chart.ChartData.SetValue(4, 3, 1); chart.ChartData.SetValue(5, 3, -1); chart.ChartData.SetValue(6, 3, 1); chart.ChartData.SetValue(7, 3, -1); chart.ChartData.SetValue(8, 3, 1); chart.ChartData.SetValue(9, 3, -1); chart.ChartData.SetValue(10, 3, 1); chart.ChartData.SetValue(11, 3, -1); } } }
VB
Imports Syncfusion.Presentation Imports Syncfusion.OfficeChart Imports System.IO Namespace ChartSample Class Program Public Shared Sub Main(ByVal args() As String) 'Create an instance for PowerPoint Dim pptxDoc As IPresentation = Presentation.Create() 'Add a blank slide to Presentation Dim slide As ISlide = pptxDoc.Slides.Add(SlideLayoutType.Blank) 'Adds chart to the slide with position and size Dim chart As IPresentationChart = slide.Charts.AddChart(100, 10, 700, 500) 'Set he chart type chart.ChartType = OfficeChartType.Bubble 'Apply chart elements 'Set chart title chart.ChartTitle = "Bubble Chart" 'Set data to the chart Program.SetChartData(chart) 'Set Datalabels, CategoryLabels, and Values Dim serie As IOfficeChartSerie = chart.Series.Add() serie.CategoryLabels = chart.ChartData(2, 1, 11, 1) serie.Values = chart.ChartData(2, 2, 11, 2) serie.Bubbles = chart.ChartData(2, 3, 11, 3) serie.DataPoints.DefaultDataPoint.DataLabels.IsValue = True serie.DataPoints.DefaultDataPoint.DataLabels.Position = OfficeDataLabelPosition.Center 'Saving and closing the presentation Dim stream As Stream = File.Create("Output.pptx") pptxDoc.Save(stream) End Sub ''' <summary> ''' Set the values for the chart ''' </summary> ''' <param name="chart">Represent the instance of the Presentation chart</param> Private Shared Sub SetChartData(ByVal chart As IPresentationChart) chart.ChartData.SetValue(1, 1, "X") chart.ChartData.SetValue(2, 1, -10) chart.ChartData.SetValue(3, 1, -20) chart.ChartData.SetValue(4, 1, -30) chart.ChartData.SetValue(5, 1, -40) chart.ChartData.SetValue(6, 1, -50) chart.ChartData.SetValue(7, 1, 10) chart.ChartData.SetValue(8, 1, 20) chart.ChartData.SetValue(9, 1, 30) chart.ChartData.SetValue(10, 1, 40) chart.ChartData.SetValue(11, 1, 50) chart.ChartData.SetValue(1, 2, "Y") chart.ChartData.SetValue(2, 2, -100) chart.ChartData.SetValue(3, 2, -200) chart.ChartData.SetValue(4, 2, -300) chart.ChartData.SetValue(5, 2, -400) chart.ChartData.SetValue(6, 2, -500) chart.ChartData.SetValue(7, 2, 100) chart.ChartData.SetValue(8, 2, 200) chart.ChartData.SetValue(9, 2, 300) chart.ChartData.SetValue(10, 2, 400) chart.ChartData.SetValue(11, 2, 500) chart.ChartData.SetValue(1, 3, "Size") chart.ChartData.SetValue(2, 3, 1) chart.ChartData.SetValue(3, 3, -1) chart.ChartData.SetValue(4, 3, 1) chart.ChartData.SetValue(5, 3, -1) chart.ChartData.SetValue(6, 3, 1) chart.ChartData.SetValue(7, 3, -1) chart.ChartData.SetValue(8, 3, 1) chart.ChartData.SetValue(9, 3, -1) chart.ChartData.SetValue(10, 3, 1) chart.ChartData.SetValue(11, 3, -1) End Sub End Class End Namespace