Is there a way to create a combination chart?

Hi Team,

I want a chart which is a combination of clustered bars and line series. Ex: If a chart has series 1,series 2 then I want series 1 to be a bar and the series 2 to be a line.

Is there a way to set this ? If so, please provide me the code samples to show a series as a line and another as a bar.

Thanks,
Priya

Hi Priya,


I have observed your requirement and like to share that Aspose.Slides does offer to add a chart with multiple series of different types. Please try using the following sample code on your end to develop understanding of concept. Please share, if I may help you further in this regard.

public static void chartwithmultipleTypes()
{
//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 = “/”;


/////Addding second type
//Add new series
series=chart.ChartData.Series.Add(fact.GetCell(defaultWorksheetIndex, 0, 3, “Series 3”), ChartType.ScatterWithSmoothLines);


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

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



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

//Take second chart series
series=chart.ChartData.Series.Add(fact.GetCell(defaultWorksheetIndex, 0, 5, “Series 4”), ChartType.ScatterWithStraightLinesAndMarkers);

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

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

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

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

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

series.PlotOnSecondAxis = true;


//Save presentation with chart
pres.Save(“C:\Presentations\AsposeChart.pptx”, SaveFormat.Pptx);
}

Many Thanks,

Hi,

This throws an error that the parent series in not bar/ column. Could you please let me know if I can do this in 14.8 version also as I currently have 14.8?

I have some how achieved the same results by setting trendlines for the series and filling no color to the series and filling colors for trend lines. The text that I wanted above the lines , I had overriden the textframe for the trend lines.

What should be the chart type for this?

But having this as a combo chart would be better and code would look neat.

Thanks,
Priya


Are you missing anything in the above code? Although the series are added without any issues. I am getting an error while trying to save the ppt.


Thanks!

Hey,

Sorry .Missed to add plot on secondary axis to true. Seems to be working. Will get back to you if I have questions.

Hi Priya,

Is your issue resolved on your end. By the way, I tested using Aspose.Slides for .NET 15.1.0.

Many Thanks,

Working like wonder.


Would like to know if I can set the series line height? Let me know if there is an option.

Thanks,
Priya

Hi Priya,

In line chart the line is rendered in accordance to points set in chart. The length of of line is not an option. In fact there are series data points that are joined together in the form of line.

Many Thanks,

Alright!! Thanks alot for the immediate response. Much appreciated!