How to create bubble chart in Word document using C# and VB.NET
What is a bubble chart?
A bubble chart represents data points with bubbles along with an additional dimension of the data, size of the 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 Word library (Essential® DocIO)
Steps to create bubble chart in Word document using DocIO library
- Initialize chart
Create a chart object by calling the paragraph.AppendChart(446,270) method and specify the chart type to OfficeChartType.Bubble enum value.
//Creates and Appends chart to the paragraph.
WChart chart = paragraph.AppendChart(446, 270);
//Sets chart type.
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
- SizeRepresents
- BubbleScale
- ShowNegativeBubbles
- 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.
The following C# and VB.NET complete code snippet shows the creation of bubble chart using Word library.
C#
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using Syncfusion.OfficeChart;
using System.IO;
namespace ChartSample
{
class Program
{
static void Main(string[] args)
{
//Loads the template document.
WordDocument document = new WordDocument();
// Adds section to the document.
IWSection sec = document.AddSection();
//Adds paragraph to the section.
IWParagraph paragraph = sec.AddParagraph();
//Creates and Appends chart to the paragraph.
WChart chart = paragraph.AppendChart(446, 270);
//Sets 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;
//Saves and closes the document.
document.Save("sample.docx", FormatType.Docx);
document.Close();
}
/// <summary>
/// Set the values for the chart.
/// </summary>
/// <param name="chart">Represent the instance of the WordDocument chart</param>
private static void SetChartData(WChart 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.DocIO
Imports Syncfusion.DocIO.DLS
Imports Syncfusion.OfficeChart
Namespace ChartSample
Class Program
Private Shared Sub Main(ByVal args As String())
'Loads the template document.
Dim document As WordDocument = New WordDocument()
' Adds section to the document.
Dim sec As IWSection = document.AddSection()
'Adds paragraph to the section.
Dim paragraph As IWParagraph = sec.AddParagraph()
'Creates and Appends chart to the paragraph.
Dim chart As WChart = paragraph.AppendChart(446, 270)
'Sets 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
'Saves and closes the document.
document.Save("sample.docx", FormatType.Docx)
document.Close()
End Sub
''' <summary>
''' Set the values for the chart
''' </summary>
''' <paramname="chart">Represent the instance of the WordDocument chart</param>
Private Shared Sub SetChartData(ByVal chart As WChart)
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
A complete working example of how to create bubble chart in Word document in C# can be downloaded from Create-bubble-chart-in-Word.zip.
To learn more about creating charts with various settings using Syncfusion® Word library (Essential® DocIO), please refer the documentation.