How to Create a Combination Chart in PowerPoint in C#

A combination chart is a chart that combines two or more chart types in a single chart. In this article, we’ll introduce how to create a chart that combines column chart and line chart in PowerPoint using Spire.Presentation.

Code Snippet:

Step 1: Create a PowerPoint document.

Presentation presentation = new Presentation();

Step 2: Insert a column chart in the first slide.

RectangleF rect = new RectangleF(40, 100, 550, 320);
IChart chart = presentation.Slides[0].Shapes.AppendChart(ChartType.ColumnClustered, rect);

Step 3: Create an instance of DataTable.

DataTable dataTable = new DataTable();
dataTable.Columns.Add(new DataColumn("Month", Type.GetType("System.String")));
dataTable.Columns.Add(new DataColumn("Sales", Type.GetType("System.Int32")));
dataTable.Columns.Add(new DataColumn("Growth rate", Type.GetType("System.Decimal")));
dataTable.Rows.Add("January", 200, 0.6);
dataTable.Rows.Add("February", 250, 0.8);
dataTable.Rows.Add("March", 300, 0.6);
dataTable.Rows.Add("April", 150, 0.2);
dataTable.Rows.Add("May", 200, 0.5);
dataTable.Rows.Add("June", 400, 0.9);

Step 4: Import data from DataTable to ChartData.

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[] datas = dataTable.Rows[r].ItemArray;
    for (int c = 0; c < datas.Length; c++)
    {
        chart.ChartData[r + 1, c].Value = datas[c];

    }
}

Step 5: Set the data range of series labels, category labels and series values.

chart.Series.SeriesLabel = chart.ChartData["B1", "C1"];       
chart.Categories.CategoryLabels = chart.ChartData["A2", "A7"];
chart.Series[0].Values = chart.ChartData["B2","B7"];
chart.Series[1].Values = chart.ChartData["C2","C7"];

Step 6: Change the chart type of series 2 to line chart and plot data to the secondary axis.

chart.Series[1].Type = ChartType.LineMarkers;
chart.Series[1].UseSecondAxis = true;
chart.SecondaryValueAxis.NumberFormat = "0%";

Step 7: Save the file.

presentation.SaveToFile("Combination_Chart.pptx", FileFormat.Pptx2010);

Output:

How to Create a Combination Chart in PowerPoint in C#

Full Code:

//create a presentation instance
Presentation presentation = new Presentation();

//insert a column clustered chart
RectangleF rect = new RectangleF(40, 100, 550, 320);
IChart chart = presentation.Slides[0].Shapes.AppendChart(ChartType.ColumnClustered, rect);

//set chart title
chart.ChartTitle.TextProperties.Text = "Monthly Sales Report";
chart.ChartTitle.TextProperties.IsCentered = true;
chart.ChartTitle.Height = 30;
chart.HasTitle = true;

//create a datatable
DataTable dataTable = new DataTable();
dataTable.Columns.Add(new DataColumn("Month", Type.GetType("System.String")));
dataTable.Columns.Add(new DataColumn("Sales", Type.GetType("System.Int32")));
dataTable.Columns.Add(new DataColumn("Growth rate", Type.GetType("System.Decimal")));
dataTable.Rows.Add("January", 200, 0.6);
dataTable.Rows.Add("February", 250, 0.8);
dataTable.Rows.Add("March", 300, 0.6);
dataTable.Rows.Add("April", 150, 0.2);
dataTable.Rows.Add("May", 200, 0.5);
dataTable.Rows.Add("June", 400, 0.9);

//import data from datatable to chart data
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[] datas = dataTable.Rows[r].ItemArray;
    for (int c = 0; c < datas.Length; c++)
    {
        chart.ChartData[r + 1, c].Value = datas[c];

    }
}

//set series labels
chart.Series.SeriesLabel = chart.ChartData["B1", "C1"];
 
//set categories labels    
chart.Categories.CategoryLabels = chart.ChartData["A2", "A7"];

//assign data to series values
chart.Series[0].Values = chart.ChartData["B2","B7"];
chart.Series[1].Values = chart.ChartData["C2","C7"];

//change the chart type of serie 2 to line with markers
chart.Series[1].Type = ChartType.LineMarkers;

//plot data of series 2 on the secondary axis
chart.Series[1].UseSecondAxis = true;

//set the number format as percentage 
chart.SecondaryValueAxis.NumberFormat = "0%";

//hide gridlinkes of secondary axis
chart.SecondaryValueAxis.MajorGridTextLines.FillType = FillFormatType.None;

//set overlap
chart.OverLap = -50;

//set gapwidth
chart.GapWidth = 200;

//save to file
presentation.SaveToFile("Combination_Chart.pptx", FileFormat.Pptx2010);