Home > Code > C# > How to Create Normal & Scatter Charts in PowerPoint Slides Using C# & VB.NET

How to Create Normal & Scatter Charts in PowerPoint Slides Using C# & VB.NET

by sher azam   on Apr 22, 2014   Category: C#   |  Views: 4813    |  Points: 25   |  Gold 


This Tutorial shows how .NET developers can create normal and scatter charts inside their .NET Applications. Aspose.Slides for .NET lets developers add custom charts into slides from scratch. This topic, explains how to create normal and scatter charts with multiple series from scratch using Aspose.Slides for .NET. Aspose.Slides for .NET works independently of Aspose.Cells for .NET for chart creation. Aspose.Slides for .NET has provided the simplest API for creating charts in an easy way. To create a chart in a slide, please follow the steps below:

• Create an instance of the Presentation class.
• Obtain the reference of a slide by index.
• Add chart with default data along with desired type.
• Add a chart title.
• Access the chart data worksheet.
• Clear all the default series and categories.
• Add new series and categories.
• Add new chart data for chart series.
• Add fill color for chart series.
• Adding chart series labels.
• Write the modified presentation as a PPTX file.

Code Sample for how to add Chart a slide

[C# Code Sample]

//Instantiate Presentation class that represents PPTX file//Instantiate Presentation class that represents PPTX file
Presentation pres = new Presentation();

//Access first slide
ISlide sld = pres.Slides[0];

// Add chart with default data
IChart chart = sld.Shapes.AddChart(ChartType.ClusteredColumn, 0, 0, 500, 500);

//Setting chart Title
//chart.ChartTitle.TextFrameForOverriding.Text = "Sample Title";
chart.ChartTitle.AddTextFrameForOverriding("Sample Title");
chart.ChartTitle.TextFrameForOverriding.TextFrameFormat.CenterText = NullableBool.True;
chart.ChartTitle.Height = 20;
chart.HasTitle = true;

//Set first series to Show Values
chart.ChartData.Series[0].Labels.DefaultDataLabelFormat.ShowValue = true;

//Setting the index of chart data sheet
int defaultWorksheetIndex = 0;

//Getting the chart data worksheet
IChartDataWorkbook fact = chart.ChartData.ChartDataWorkbook;

//Delete default generated series and categories
chart.ChartData.Series.Clear();
chart.ChartData.Categories.Clear();
int s = chart.ChartData.Series.Count;
s = chart.ChartData.Categories.Count;

//Adding new series
chart.ChartData.Series.Add(fact.GetCell(defaultWorksheetIndex, 0, 1, "Series 1"), chart.Type);
chart.ChartData.Series.Add(fact.GetCell(defaultWorksheetIndex, 0, 2, "Series 2"), chart.Type);

//Adding new categories
chart.ChartData.Categories.Add(fact.GetCell(defaultWorksheetIndex, 1, 0, "Caetegoty 1"));
chart.ChartData.Categories.Add(fact.GetCell(defaultWorksheetIndex, 2, 0, "Caetegoty 2"));
chart.ChartData.Categories.Add(fact.GetCell(defaultWorksheetIndex, 3, 0, "Caetegoty 3"));

//Take first chart series
IChartSeries series = chart.ChartData.Series[0];

//Now populating series data

series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 1, 1, 20));
series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 2, 1, 50));
series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 3, 1, 30));

//Setting fill color for series
series.Format.Fill.FillType = FillType.Solid;
series.Format.Fill.SolidFillColor.Color = Color.Red

//Take second chart series
series = chart.ChartData.Series[1];

//Now populating series data
series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 1, 2, 30));
series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 2, 2, 10));
series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 3, 2, 60));

//Setting fill color for series
series.Format.Fill.FillType = FillType.Solid;
series.Format.Fill.SolidFillColor.Color = Color.Green;

//create custom labels for each of categories for new series

//first label will be show Category name
IDataLabel lbl = series.DataPoints[0].Label;
lbl.DataLabelFormat.ShowCategoryName = true;

lbl = series.DataPoints[1].Label;
lbl.DataLabelFormat.ShowSeriesName = true;

//Show value for third label
lbl = series.DataPoints[2].Label;
lbl.DataLabelFormat.ShowValue = true;
lbl.DataLabelFormat.ShowSeriesName = true;
lbl.DataLabelFormat.Separator = "/";

//Save presentation with chart
pres.Save("AsposeChart.pptx", SaveFormat.Pptx);



[VB Code Sample]

'Instantiate Presentation class that represents PPTX file
Dim pres As New Presentation()

'Access first slide
Dim sld As ISlide = pres.Slides(0)

' Add chart with default data
Dim chart As IChart = sld.Shapes.AddChart(ChartType.ClusteredColumn, 0, 0, 500, 500)

'Setting chart Title
'chart.ChartTitle.TextFrameForOverriding.Text = "Sample Title";
chart.ChartTitle.AddTextFrameForOverriding("Sample Title")
chart.ChartTitle.TextFrameForOverriding.TextFrameFormat.CenterText = NullableBool.True
chart.ChartTitle.Height = 20
chart.HasTitle = True

'Set first series to Show Values
chart.ChartData.Series(0).Labels.DefaultDataLabelFormat.ShowValue = True

'Setting the index of chart data sheet
Dim defaultWorksheetIndex As Integer = 0

'Getting the chart data worksheet
Dim fact As IChartDataWorkbook = chart.ChartData.ChartDataWorkbook

'Delete default generated series and categories
chart.ChartData.Series.Clear()
chart.ChartData.Categories.Clear()
Dim s As Integer = chart.ChartData.Series.Count
s = chart.ChartData.Categories.Count

'Adding new series
chart.ChartData.Series.Add(fact.GetCell(defaultWorksheetIndex, 0, 1, "Series 1"), chart.Type)
chart.ChartData.Series.Add(fact.GetCell(defaultWorksheetIndex, 0, 2, "Series 2"), chart.Type)

'Adding new categories
chart.ChartData.Categories.Add(fact.GetCell(defaultWorksheetIndex, 1, 0, "Caetegoty 1"))
chart.ChartData.Categories.Add(fact.GetCell(defaultWorksheetIndex, 2, 0, "Caetegoty 2"))
chart.ChartData.Categories.Add(fact.GetCell(defaultWorksheetIndex, 3, 0, "Caetegoty 3"))

'Take first chart series
Dim series As IChartSeries = chart.ChartData.Series(0)

'Now populating series data

series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 1, 1, 20))
series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 2, 1, 50))
series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 3, 1, 30))

'Setting fill color for series
series.Format.Fill.FillType = FillType.Solid
series.Format.Fill.SolidFillColor.Color = Color.Red

'Take second chart series
series = chart.ChartData.Series(1)

'Now populating series data
series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 1, 2, 30))
series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 2, 2, 10))
series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 3, 2, 60))

'Setting fill color for series
series.Format.Fill.FillType = FillType.Solid
series.Format.Fill.SolidFillColor.Color = Color.Green

'create custom labels for each of categories for new series

'first label will be show Category name
Dim lbl As IDataLabel = series.DataPoints(0).Label
lbl.DataLabelFormat.ShowCategoryName = True

lbl = series.DataPoints(1).Label
lbl.DataLabelFormat.ShowSeriesName = True

'Show value for third label
lbl = series.DataPoints(2).Label
lbl.DataLabelFormat.ShowValue = True
lbl.DataLabelFormat.ShowSeriesName = True
lbl.DataLabelFormat.Separator = "/"

'Save presentation with chart
pres.Save("AsposeChart.pptx", SaveFormat.Pptx)

Creating Scattered Chart with multiple series and different series markers



[C# Code Sample]

Presentation pres = new Presentation();

ISlide slide = pres.Slides[0];

//Creating the default chart
IChart chart = slide.Shapes.AddChart(ChartType.ScatterWithSmoothLines, 0, 0, 400, 400);

//Getting the default chart data worksheet index
int defaultWorksheetIndex = 0;

//Getting the chart data worksheet
IChartDataWorkbook fact = chart.ChartData.ChartDataWorkbook;

//Delete demo series
chart.ChartData.Series.Clear();

//Add new series
chart.ChartData.Series.Add(fact.GetCell(defaultWorksheetIndex, 1, 1, "Series 1"), chart.Type);
chart.ChartData.Series.Add(fact.GetCell(defaultWorksheetIndex, 1, 3, "Series 2"), chart.Type);

//Take first chart series
IChartSeries series = chart.ChartData.Series[0];

//Add new point (1:3) there.
series.DataPoints.AddDataPointForScatterSeries(fact.GetCell(defaultWorksheetIndex, 2, 1, 1), fact.GetCell(defaultWorksheetIndex, 2, 2, 3));

//Add new point (2:10)
series.DataPoints.AddDataPointForScatterSeries(fact.GetCell(defaultWorksheetIndex, 3, 1, 2), fact.GetCell(defaultWorksheetIndex, 3, 2, 10));

//Edit the type of series
series.Type = ChartType.ScatterWithStraightLinesAndMarkers;

//Changing the chart series marker
series.Marker.Size = 10;
series.Marker.Symbol = MarkerStyleType.Star;

//Take second chart series
series = chart.ChartData.Series[1];

//Add new point (5:2) there.
series.DataPoints.AddDataPointForScatterSeries(fact.GetCell(defaultWorksheetIndex, 2, 3, 5), fact.GetCell(defaultWorksheetIndex, 2, 4, 2));

//Add new point (3:1)
series.DataPoints.AddDataPointForScatterSeries(fact.GetCell(defaultWorksheetIndex, 3, 3, 3), fact.GetCell(defaultWorksheetIndex, 3, 4, 1));

//Add new point (2:2)
series.DataPoints.AddDataPointForScatterSeries(fact.GetCell(defaultWorksheetIndex, 4, 3, 2), fact.GetCell(defaultWorksheetIndex, 4, 4, 2));

//Add new point (5:1)
series.DataPoints.AddDataPointForScatterSeries(fact.GetCell(defaultWorksheetIndex, 5, 3, 5), fact.GetCell(defaultWorksheetIndex, 5, 4, 1));

//Changing the chart series marker
series.Marker.Size = 10;
series.Marker.Symbol = MarkerStyleType.Circle;

pres.Save("AsposeScatterChart.pptx", SaveFormat.Pptx);

[VB.NET Code Sample]

Dim pres As New Presentation()
Dim slide As ISlide = pres.Slides(0)

'Creating the default chart
Dim chart As IChart = slide.Shapes.AddChart(ChartType.ScatterWithSmoothLines, 0, 0, 400, 400)

'Getting the default chart data worksheet index
Dim defaultWorksheetIndex As Integer = 0

'Getting the chart data worksheet
Dim fact As IChartDataWorkbook = chart.ChartData.ChartDataWorkbook

'Delete demo series
chart.ChartData.Series.Clear()

'Add new series
chart.ChartData.Series.Add(fact.GetCell(defaultWorksheetIndex, 1, 1, "Series 1"), chart.Type)
chart.ChartData.Series.Add(fact.GetCell(defaultWorksheetIndex, 1, 3, "Series 2"), chart.Type)

'Take first chart series
Dim series As IChartSeries = chart.ChartData.Series(0)

'Add new point (1:3) there.
series.DataPoints.AddDataPointForScatterSeries(fact.GetCell(defaultWorksheetIndex, 2, 1, 1), fact.GetCell(defaultWorksheetIndex, 2, 2, 3))
'Add new point (2:10)
series.DataPoints.AddDataPointForScatterSeries(fact.GetCell(defaultWorksheetIndex, 3, 1, 2), fact.GetCell(defaultWorksheetIndex, 3, 2, 10))

'Edit the type of series
series.Type = ChartType.ScatterWithStraightLinesAndMarkers

'Changing the chart series marker
series.Marker.Size = 10
series.Marker.Symbol = MarkerStyleType.Star

'Take second chart series
series = chart.ChartData.Series(1)

'Add new point (5:2) there.
series.DataPoints.AddDataPointForScatterSeries(fact.GetCell(defaultWorksheetIndex, 2, 3, 5), fact.GetCell(defaultWorksheetIndex, 2, 4, 2))

'Add new point (3:1)
series.DataPoints.AddDataPointForScatterSeries(fact.GetCell(defaultWorksheetIndex, 3, 3, 3), fact.GetCell(defaultWorksheetIndex, 3, 4, 1))

'Add new point (2:2)
series.DataPoints.AddDataPointForScatterSeries(fact.GetCell(defaultWorksheetIndex, 4, 3, 2), fact.GetCell(defaultWorksheetIndex, 4, 4, 2))

'Add new point (5:1)
series.DataPoints.AddDataPointForScatterSeries(fact.GetCell(defaultWorksheetIndex, 5, 3, 5), fact.GetCell(defaultWorksheetIndex, 5, 4, 1))

'Changing the chart series marker
series.Marker.Size = 10
series.Marker.Symbol = MarkerStyleType.Circle
pres.Save("AsposeScatterChart.pptx", SaveFormat.Pptx)



Overview: Aspose.Slides for .NET

Aspose.Slides is a .NET component to read, write and modify a PowerPoint document without using MS PowerPoint. PowerPoint versions from 97-2007 and all three PowerPoint formats: PPT, POT, PPS are also supported. Now you can create, access, copy, clone, edit and delete slides in your presentations. Other features include PPT/PPTX printing, PPT to XPS format, saving PowerPoint slides into PDF, adding & modifying audio & video frames, using shapes like rectangles or ellipses and saving presentations in SVG format, streams or images.

More about Aspose.Slides for .NET

- Homepage of Aspose.Slides for .NET: http://www.aspose.com/.net/powerpoint-component.aspx

- Download Aspose.Slides for .NET: http://www.aspose.com/community/files/51/.net-components/aspose.slides-for-.net/default.aspx



Post Code  |  Code Snippet Home

User Responses


  Re :How to Create Normal & Scatter Charts in PowerPoint Slides Using C# & VB.NET   
Posted by Leo_21
on 21/05/2014
Points : 5

Hi.

How about creating charts in PowerPoint document with below codes?Here is the full guidelines - http://www.e-iceblue.com/Tutorials/Spire.Presentation/Program-Guide/Insert-chart-in-PPT-document.html

Presentation presentation = new Presentation();

//set background Image
string ImageFile = @"bg.png";
RectangleF rect2 = new RectangleF(0, 0, presentation.SlideSize.Size.Width,
presentation.SlideSize.Size.Height);
presentation.Slides[0].Shapes.AppendEmbedImage(ShapeType.Rectangle, ImageFile, rect2);
presentation.Slides[0].Shapes[0].Line.FillFormat.SolidFillColor.Color = Color.FloralWhite;

//insert chart
RectangleF rect = new RectangleF(50, 100, 400, 400);
IChart chart = presentation.Slides[0].Shapes.AppendChart(ChartType.Cylinder3DClustered, rect);

//add chart Title
chart.ChartTitle.TextProperties.Text = "Report";
chart.ChartTitle.TextProperties.IsCentered = true;
chart.ChartTitle.Height = 30;
chart.HasTitle = true;

//load data from XML file to datatable
DataTable dataTable = LoadData();

//load data from datatable to chart
InitChartData(chart, dataTable);

//save the document
presentation.SaveToFile("chart.pptx", FileFormat.Pptx2010);

System.Diagnostics.Process.Start("chart.pptx");
}

//function to load data from XML file to DataTable

private DataTable LoadData()
{
DataSet ds = new DataSet();
ds.ReadXmlSchema(@"data-schema.xml");
ds.ReadXml(@"data.xml");

return ds.Tables[0];
}

//function to load data from DataTable to IChart
private void InitChartData(IChart chart, DataTable dataTable)
{
for (int c = 0; c < dataTable.Columns.Count; c++)
{
chart.ChartData[0, c].Text = dataTable.Columns[c].Caption;
}

for (int r = 0; r < dataTable.Rows.Count; r++)
{
object[] data = dataTable.Rows[r].ItemArray;
for (int c = 0; c < data.Length; c++)
{
chart.ChartData[r + 1, c].Value = data[c];
}
}

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts