Reg:Displaying Bar Charts

Hi,

Kindly let me know,if aspose supports the kind of charts present in attached excel sheet.

If so,Kindly provide me an example for reference.

Thanks in advance

Manjunath

Hi Manjunath,

I have observed your requirements for creating charts and like to share that you can create all MSO supported charts that are available in MS PowerPoint. The MSO supported charts are mainly common in both MS Excel and PowerPoint. That requested chart is of of clustered bar type. I have generated the sample code for your convenience as POC for your kind reference. Please try using the following sample code to serve the basis.

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

//Access first slide
SlideEx sld = pres.getSlides().get_Item(0);

// Add chart with default data
ChartEx chart = sld.getShapes().addChart(ChartTypeEx.ClusteredBar, 0, 0, 500, 500);

//Setting chart Title
chart.getChartTitle().getText().setText(“Chart Title”);
chart.getChartTitle().getText().setCenterText(true);
chart.getChartTitle().setHeight(20f);
chart.hasTitle(true);

//Set first series to Show Values
chart.getChartData().getSeries().get_Item(0).getLabels().setShowValue(true);


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

//Getting the chart data worksheet
ChartDataCellFactory fact = chart.getChartData().getChartDataCellFactory();

//Delete default generated series and categories
chart.getChartData().getSeries().clear();
chart.getChartData().getCategories().clear();
int s = chart.getChartData().getSeries().getCapacity();

//Adding new series
chart.getChartData().getSeries().add(fact.getCell(defaultWorksheetIndex, 0, 1, “Series 1”), chart.getType());
chart.getChartData().getSeries().add(fact.getCell(defaultWorksheetIndex, 0, 2, “Series 2”), chart.getType());
chart.getChartData().getSeries().add(fact.getCell(defaultWorksheetIndex, 0, 3, “Series 3”), chart.getType());

//Adding new categories
chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 1, 0, “Jan”));
chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 2, 0, “Feb”));
chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 3, 0, “Mar”));
chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 4, 0, “Apr”));

//Take first chart series
ChartSeriesEx series = chart.getChartData().getSeries().get_Item(0);

//Now populating series data
series.getValues().add(fact.getCell(defaultWorksheetIndex, 1, 1,1000));
series.getValues().add(fact.getCell(defaultWorksheetIndex, 2, 1,1001));
series.getValues().add(fact.getCell(defaultWorksheetIndex, 3, 1,1002));
series.getValues().add(fact.getCell(defaultWorksheetIndex, 4, 1,1003));

//Setting fill color for series

java.awt.Color firstSeriesColor=new java.awt.Color(79,129,189);
series.getFormat().getFill().setFillType(FillTypeEx.Solid);
series.getFormat().getFill().getSolidFillColor().setColor(firstSeriesColor);
series.getLabels().setShowValue(true);

//Take second chart series
series = chart.getChartData().getSeries().get_Item(1);

//Now populating series data
series.getValues().add(fact.getCell(defaultWorksheetIndex, 1, 2, 250));
series.getValues().add(fact.getCell(defaultWorksheetIndex, 2, 2, 251));
series.getValues().add(fact.getCell(defaultWorksheetIndex, 3, 2, 252));
series.getValues().add(fact.getCell(defaultWorksheetIndex, 4, 2, 253));

java.awt.Color secondSeriesColor=new java.awt.Color(192,80,77);
//Setting fill color for series
series.getFormat().getFill().setFillType(FillTypeEx.Solid);
series.getFormat().getFill().getSolidFillColor().setColor(secondSeriesColor);
series.getLabels().setShowValue(true);


//Take second chart series
series = chart.getChartData().getSeries().get_Item(2);

//Now populating series data
series.getValues().add(fact.getCell(defaultWorksheetIndex, 1, 3, 500));
series.getValues().add(fact.getCell(defaultWorksheetIndex, 2, 3, 501));
series.getValues().add(fact.getCell(defaultWorksheetIndex, 3, 3, 502));
series.getValues().add(fact.getCell(defaultWorksheetIndex, 4, 3, 503));

java.awt.Color thirdSeriesColor=new java.awt.Color(155,187,89);
//Setting fill color for series
series.getFormat().getFill().setFillType(FillTypeEx.Solid);
series.getFormat().getFill().getSolidFillColor().setColor(thirdSeriesColor);
series.getLabels().setShowValue(true);


chart.getValueAxis().getMajorGridLines().getFillFormat().setFillType(FillTypeEx.NoFill);
chart.getValueAxis().getMinorGridLines().getFillFormat().setFillType(FillTypeEx.NoFill);
chart.getLegend().setPosition(LegendPositionTypeEx.Top);
// Save presentation with chart
pres.write(“D:\Aspose Data\AsposeChart.pptx”);

You can also work on formatting different chart entities by your self. Please visit this documentation link for your kind reference in this regard.

Please share, if I may help you further in this regard.

Thank you for providing the information.