How to create the PowerPoint scatter with straight lines chart in C# and VB.NET?
This article explains how to create a scatter with straight line chart in PowerPoint using WinForms Presentation.
What is scatter with straight line chart
A scatter with straight line chart is used to compare at least two sets of values or pairs of data. The values are represented with a connected straight line.
Scatter with straight line chart created using Presentation library
Steps to create scatter with straight line chart
- Initialize chart
Create a chart object by calling the slide.Charts.AddChart method and specify the chart type to OfficeChartType.Scatter_Line enum value.
//Create the chart IPresentationChart chart = slide.Charts.AddChart(100, 10, 700, 500); //Set chart type to Scatter_Line chart.ChartType = OfficeChartType.Scatter_Line;
- Assign data
Set a range of data from the worksheet to chart’s DataRange property. To plot the series values in column and categories in row, set chart’s IsSeriesInRows property to false.
//Set region of chart data chart.DataRange = chart.ChartData[1,1,6,2]; //Set chart series in column for assigned data region chart.IsSeriesInRows = false;
- Apply basic chart elements
Add the basic elements like chart title, data labels, and legend.
- ChartTitle of chart object.
- Set DataLabels via DefaultDataPoint.
- Set TRUE to chart’s HasLegend property to show the legend, else FALSE.
//Apply chart elements //Set chart title chart.ChartTitle = "Scatter With Straight Line Chart"; //Set legend chart.HasLegend = false; //Set Datalabels IOfficeChartSerie serie = chart.Series[0]; serie.DataPoints.DefaultDataPoint.DataLabels.IsValue = true; serie.DataPoints.DefaultDataPoint.DataLabels.IsCategoryName = true;
Property applicable for a scatter with straight line chart along with the common line properties is IsVaryColorIsVaryColor
NOTE: Applying properties apart from the mentioned property and line properties may throw exception or the changes will not be reflected in the output document because those properties are not related to scatter chart.
To know 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 scatter with straight line 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); chart.ChartType = OfficeChartType.Scatter_Line; //Assign data chart.DataRange = chart.ChartData[1, 1, 6, 2]; chart.IsSeriesInRows = false; //Set data to the chart RowIndex, columnIndex, and data chart.ChartData.SetValue(1, 1, "Food"); chart.ChartData.SetValue(1, 2, "Count"); chart.ChartData.SetValue(2, 1, "Fruits"); chart.ChartData.SetValue(3, 1, "Vegitables"); chart.ChartData.SetValue(4, 1, "Dairy"); chart.ChartData.SetValue(5, 1, "Proptein"); chart.ChartData.SetValue(6, 1, "Grains"); chart.ChartData.SetValue(1, 2, "Percentage"); chart.ChartData.SetValue(2, 2, "36"); chart.ChartData.SetValue(3, 2, "14"); chart.ChartData.SetValue(4, 2, "13"); chart.ChartData.SetValue(5, 2, "28"); chart.ChartData.SetValue(6, 2, "9"); //Apply chart elements //Set chart title chart.ChartTitle = "Scatter With Straight Line Chart"; //Set legend chart.HasLegend = false; //Set Datalabels IOfficeChartSerie serie = chart.Series[0]; serie.DataPoints.DefaultDataPoint.DataLabels.IsValue = true; serie.DataPoints.DefaultDataPoint.DataLabels.IsCategoryName = true; //Saving and closing the presentation Stream stream = File.Create("Output.pptx"); pptxDoc.Save(stream); } } } }
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) chart.ChartType = OfficeChartType.Scatter_Line 'Assign data chart.DataRange = chart.ChartData(1, 1, 6, 2) chart.IsSeriesInRows = False 'Set data to the chart RowIndex, columnIndex, and data chart.ChartData.SetValue(1, 1, "Food") chart.ChartData.SetValue(1, 2, "Count") chart.ChartData.SetValue(2, 1, "Fruits") chart.ChartData.SetValue(3, 1, "Vegitables") chart.ChartData.SetValue(4, 1, "Dairy") chart.ChartData.SetValue(5, 1, "Proptein") chart.ChartData.SetValue(6, 1, "Grains") chart.ChartData.SetValue(1, 2, "Percentage") chart.ChartData.SetValue(2, 2, "36") chart.ChartData.SetValue(3, 2, "14") chart.ChartData.SetValue(4, 2, "13") chart.ChartData.SetValue(5, 2, "28") chart.ChartData.SetValue(6, 2, "9") 'Apply chart elements 'Set chart title chart.ChartTitle = "Scatter With Straight Line Chart" 'Set legend chart.HasLegend = False 'Set Datalabels Dim serie As IOfficeChartSerie = chart.Series(0) serie.DataPoints.DefaultDataPoint.DataLabels.IsValue = True serie.DataPoints.DefaultDataPoint.DataLabels.IsCategoryName = True 'Saving and closing the presentation Dim stream As Stream = File.Create("Output.pptx") pptxDoc.Save(stream) End Sub End Class End Namespace
Conclusion
I hope you enjoyed learning about how to create the PowerPoint scatter with straight lines chart in C# and VB.NET.
You can refer to our WinForms Presentation feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WinForms Presentation example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!