@Thilakbabu,
I’ve carefully reviewed and executed the first code example from the post above, and the result aligns with your requirements:
- The Stacked Column chart and Line chart are on the primary axis.
- The Clustered Column chart is on the secondary axis.
- The legend for the Clustered Column chart entry is invisible.
Code example:
Presentation presentation = new Presentation();
presentation.getSlideSize().setSize(SlideSizeType.Widescreen, SlideSizeScaleType.EnsureFit);
ISlide slide = presentation.getSlides().get_Item(0);
IChart chart = addStackedColumnChart(slide);
addClusteredColumnChart(chart);
addLineWithMarkersChart(chart);
configureHorizontalAxis(chart.getAxes().getHorizontalAxis());
configureVerticalAxis(chart.getAxes().getVerticalAxis());
configureSecondaryHorizontalAxis(chart.getAxes().getSecondaryHorizontalAxis());
configureSecondaryVerticalAxis(chart.getAxes().getSecondaryVerticalAxis());
configurePlotArea(chart.getPlotArea());
configureChartLegend(chart.getLegend());
chart.getChartData().setRange("Sheet1!$A$1:$D$18");
hideChartZeroValues(chart);
presentation.save("output.pptx", SaveFormat.Pptx);
presentation.dispose();
static IChart addStackedColumnChart(ISlide slide) {
IChart chart = slide.getShapes().addChart(ChartType.StackedColumn, 28.5f, 101f, 902.5f, 355f);
chart.getChartData().getSeries().clear();
chart.getChartData().getCategories().clear();
IChartDataWorkbook workbook = chart.getChartData().getChartDataWorkbook();
final int worksheetIndex = 0;
final int seriesColumnIndex = 1;
chart.getChartData().getCategories().add(workbook.getCell(worksheetIndex, 1, 0, "ABC"));
chart.getChartData().getCategories().add(workbook.getCell(worksheetIndex, 2, 0, "DEF"));
chart.getChartData().getCategories().add(workbook.getCell(worksheetIndex, 3, 0, "XYZ"));
chart.getChartData().getCategories().add(workbook.getCell(worksheetIndex, 4, 0, "RST"));
chart.getChartData().getCategories().add(workbook.getCell(worksheetIndex, 5, 0, "MNO"));
IChartSeries series = chart.getChartData().getSeries().add(
workbook.getCell(worksheetIndex, 0, seriesColumnIndex, "Fact 1"), ChartType.StackedColumn);
series.getDataPoints().addDataPointForBarSeries(workbook.getCell(worksheetIndex, 1, seriesColumnIndex, 59.4));
series.getDataPoints().addDataPointForBarSeries(workbook.getCell(worksheetIndex, 2, seriesColumnIndex, 16.2));
series.getDataPoints().addDataPointForBarSeries(workbook.getCell(worksheetIndex, 3, seriesColumnIndex, 6.2));
series.getDataPoints().addDataPointForBarSeries(workbook.getCell(worksheetIndex, 4, seriesColumnIndex, 2.6));
series.getDataPoints().addDataPointForBarSeries(workbook.getCell(worksheetIndex, 5, seriesColumnIndex, 2.3));
setSeriesDataFormat(workbook, worksheetIndex, seriesColumnIndex, series.getDataPoints().size());
series.getFormat().getFill().setFillType(FillType.Solid);
series.getFormat().getFill().getSolidFillColor().setColor(new Color(103, 88, 149));
series.getParentSeriesGroup().setOverlap((byte) 100);
series.getParentSeriesGroup().setGapWidth(40);
series.getLabels().getDefaultDataLabelFormat().setShowValue(true);
series.getLabels().getDefaultDataLabelFormat().getFormat().getFill().setFillType(FillType.Solid);
series.getLabels().getDefaultDataLabelFormat().getFormat().getFill().getSolidFillColor().setColor(Color.WHITE);
series.getLabels().getDefaultDataLabelFormat().getFormat().getLine().setWidth(0.75);
series.getLabels().getDefaultDataLabelFormat().getFormat().getLine().getFillFormat().setFillType(FillType.Solid);
series.getLabels().getDefaultDataLabelFormat().getFormat().getLine().getFillFormat().getSolidFillColor().setColor(new Color(89, 89, 89));
series.getLabels().getDefaultDataLabelFormat().getTextFormat().getPortionFormat().setFontHeight(10);
series.getLabels().getDefaultDataLabelFormat().getTextFormat().getPortionFormat().setLatinFont(new FontData("Arial"));
series.setOrder(0);
return chart;
}
static void addLineWithMarkersChart(IChart chart) {
IChartDataWorkbook workbook = chart.getChartData().getChartDataWorkbook();
final int worksheetIndex = 0;
final int seriesColumnIndex = 3;
IChartSeries series = chart.getChartData().getSeries().add(
workbook.getCell(worksheetIndex, 0, seriesColumnIndex, "Fact 2"), ChartType.LineWithMarkers);
series.getDataPoints().addDataPointForLineSeries(workbook.getCell(worksheetIndex, 1, seriesColumnIndex, 59.4));
series.getDataPoints().addDataPointForLineSeries(workbook.getCell(worksheetIndex, 2, seriesColumnIndex, 75.6));
series.getDataPoints().addDataPointForLineSeries(workbook.getCell(worksheetIndex, 3, seriesColumnIndex, 81.9));
series.getDataPoints().addDataPointForLineSeries(workbook.getCell(worksheetIndex, 4, seriesColumnIndex, 84.4));
series.getDataPoints().addDataPointForLineSeries(workbook.getCell(worksheetIndex, 5, seriesColumnIndex, 86.7));
setSeriesDataFormat(workbook, worksheetIndex, seriesColumnIndex, series.getDataPoints().size());
series.getFormat().getLine().getFillFormat().setFillType(FillType.Solid);
series.getFormat().getLine().getFillFormat().getSolidFillColor().setColor(new Color(70, 151, 226));
series.getMarker().setSymbol(MarkerStyleType.Circle);
series.getMarker().setSize(5);
series.getMarker().getFormat().getFill().setFillType(FillType.Solid);
series.getMarker().getFormat().getFill().getSolidFillColor().setColor(new Color(70, 151, 226));
series.getMarker().getFormat().getLine().getFillFormat().setFillType(FillType.Solid);
series.getMarker().getFormat().getLine().getFillFormat().getSolidFillColor().setColor(new Color(70, 151, 226));
series.getLabels().getDefaultDataLabelFormat().setShowValue(true);
series.getLabels().getDefaultDataLabelFormat().getFormat().getFill().setFillType(FillType.Solid);
series.getLabels().getDefaultDataLabelFormat().getFormat().getFill().getSolidFillColor().setColor(Color.WHITE);
series.getLabels().getDefaultDataLabelFormat().getFormat().getLine().setWidth(0.75);
series.getLabels().getDefaultDataLabelFormat().getFormat().getLine().getFillFormat().setFillType(FillType.Solid);
series.getLabels().getDefaultDataLabelFormat().getFormat().getLine().getFillFormat().getSolidFillColor().setColor(new Color(89, 89, 89));
series.getLabels().getDefaultDataLabelFormat().getTextFormat().getPortionFormat().setFontHeight(10);
series.getLabels().getDefaultDataLabelFormat().getTextFormat().getPortionFormat().setLatinFont(new FontData("Arial"));
series.setOrder(1);
}
static void addClusteredColumnChart(IChart chart) {
IChartDataWorkbook workbook = chart.getChartData().getChartDataWorkbook();
final int worksheetIndex = 0;
final int seriesColumnIndex = 2;
IChartSeries series = chart.getChartData().getSeries().add(
workbook.getCell(worksheetIndex, 0, seriesColumnIndex, "Fact 3"), ChartType.ClusteredColumn);
series.setPlotOnSecondAxis(true);
series.getDataPoints().addDataPointForBarSeries(workbook.getCell(worksheetIndex, 1, seriesColumnIndex, 59.4));
series.getDataPoints().addDataPointForBarSeries(workbook.getCell(worksheetIndex, 2, seriesColumnIndex, 52.8));
series.getDataPoints().addDataPointForBarSeries(workbook.getCell(worksheetIndex, 3, seriesColumnIndex, 41.6));
series.getDataPoints().addDataPointForBarSeries(workbook.getCell(worksheetIndex, 4, seriesColumnIndex, 29.0));
series.getDataPoints().addDataPointForBarSeries(workbook.getCell(worksheetIndex, 5, seriesColumnIndex, 24.6));
setSeriesDataFormat(workbook, worksheetIndex, seriesColumnIndex, series.getDataPoints().size());
series.getFormat().getFill().setFillType(FillType.NoFill);
series.getFormat().getLine().getFillFormat().setFillType(FillType.NoFill);
series.getParentSeriesGroup().setOverlap((byte) -100);
series.getParentSeriesGroup().setGapWidth(320);
series.getLabels().getDefaultDataLabelFormat().setShowValue(true);
series.getLabels().getDefaultDataLabelFormat().getFormat().getLine().setWidth(0.75);
series.getLabels().getDefaultDataLabelFormat().getFormat().getLine().getFillFormat().setFillType(FillType.Solid);
series.getLabels().getDefaultDataLabelFormat().getFormat().getLine().getFillFormat().getSolidFillColor().setColor(new Color(211, 211, 211));
series.getLabels().getDefaultDataLabelFormat().getTextFormat().getPortionFormat().getFillFormat().setFillType(FillType.Solid);
series.getLabels().getDefaultDataLabelFormat().getTextFormat().getPortionFormat().getFillFormat().getSolidFillColor().setColor(new Color(19, 130, 56));
series.getLabels().getDefaultDataLabelFormat().getTextFormat().getPortionFormat().setFontHeight(10);
series.getLabels().getDefaultDataLabelFormat().getTextFormat().getPortionFormat().setLatinFont(new FontData("Arial"));
series.getLabels().getDefaultDataLabelFormat().setPosition(LegendDataLabelPosition.InsideBase);
series.getRelatedLegendEntry().setHide(true);
series.setOrder(2);
}
static void configureHorizontalAxis(IAxis axis) {
axis.getFormat().getLine().setWidth(2);
axis.getFormat().getLine().getFillFormat().setFillType(FillType.Solid);
axis.getFormat().getLine().getFillFormat().getSolidFillColor().setColor(Color.BLACK);
axis.getTextFormat().getPortionFormat().getFillFormat().setFillType(FillType.Solid);
axis.getTextFormat().getPortionFormat().getFillFormat().getSolidFillColor().setColor(new Color(21, 25, 33));
axis.getTextFormat().getPortionFormat().setFontHeight(10);
axis.getTextFormat().getPortionFormat().setLatinFont(new FontData("Arial"));
axis.setMajorTickMark(TickMarkType.None);
axis.getMajorGridLinesFormat().getLine().setWidth(1);
axis.getMajorGridLinesFormat().getLine().getFillFormat().setFillType(FillType.Solid);
axis.getMajorGridLinesFormat().getLine().getFillFormat().getSolidFillColor().setColor(new Color(229, 229, 229));
}
static void configureVerticalAxis(IAxis axis) {
axis.getFormat().getLine().setWidth(2);
axis.getFormat().getLine().getFillFormat().setFillType(FillType.Solid);
axis.getFormat().getLine().getFillFormat().getSolidFillColor().setColor(Color.BLACK);
axis.getTextFormat().getPortionFormat().getFillFormat().setFillType(FillType.Solid);
axis.getTextFormat().getPortionFormat().getFillFormat().getSolidFillColor().setColor(new Color(21, 25, 33));
axis.getTextFormat().getPortionFormat().setFontHeight(10);
axis.getTextFormat().getPortionFormat().setLatinFont(new FontData("Arial"));
axis.setMajorTickMark(TickMarkType.None);
axis.setNumberFormatLinkedToSource(false);
axis.setNumberFormat("#,##0.0\\%");
axis.getMajorGridLinesFormat().getLine().setWidth(1);
axis.getMajorGridLinesFormat().getLine().getFillFormat().setFillType(FillType.Solid);
axis.getMajorGridLinesFormat().getLine().getFillFormat().getSolidFillColor().setColor(new Color(229, 229, 229));
}
static void configureSecondaryHorizontalAxis(IAxis axis) {
axis.setVisible(false);
axis.getMajorGridLinesFormat().getLine().getFillFormat().setFillType(FillType.NoFill);
axis.getMinorGridLinesFormat().getLine().getFillFormat().setFillType(FillType.NoFill);
}
static void configureSecondaryVerticalAxis(IAxis axis) {
axis.setVisible(false);
axis.setPlotOrderReversed(true);
axis.setCrossType(CrossesType.Maximum);
axis.getMajorGridLinesFormat().getLine().getFillFormat().setFillType(FillType.NoFill);
axis.getMinorGridLinesFormat().getLine().getFillFormat().setFillType(FillType.NoFill);
}
static void configurePlotArea(IChartPlotArea plotArea) {
plotArea.getFormat().getLine().getFillFormat().setFillType(FillType.Solid);
plotArea.getFormat().getLine().setDashStyle(LineDashStyle.Dot);
}
static void configureChartLegend(ILegend legend) {
legend.setPosition(LegendPositionType.Bottom);
legend.getTextFormat().getPortionFormat().setFontHeight(13);
legend.getTextFormat().getPortionFormat().setLatinFont(new FontData("Arial"));
legend.getTextFormat().getPortionFormat().getFillFormat().setFillType(FillType.Solid);
legend.getTextFormat().getPortionFormat().getFillFormat().getSolidFillColor().setColor(new Color(102, 102, 102));
}
static void setSeriesDataFormat(IChartDataWorkbook workbook, int worksheetIndex, int columnIndex, int pointCount) {
final String format = "#,##0.0\\%";
for (int i = 0; i < pointCount; i++) {
int rowIndex = 1 + i;
workbook.getCell(worksheetIndex, rowIndex, columnIndex).setCustomNumberFormat(format);
}
}
static void hideChartZeroValues(IChart chart) {
for (IChartSeries series : chart.getChartData().getSeries()) {
for (int i = 0; i < series.getDataPoints().size(); i++) {
IChartDataPoint dataPoint = series.getDataPoints().get_Item(i);
if (Double.isNaN(dataPoint.getValue().toDouble())) {
dataPoint.getFormat().getFill().setFillType(FillType.NoFill);
dataPoint.getFormat().getLine().getFillFormat().setFillType(FillType.NoFill);
dataPoint.getMarker().getFormat().getFill().setFillType(FillType.NoFill);
dataPoint.getMarker().getFormat().getLine().getFillFormat().setFillType(FillType.NoFill);
series.getLabels().get_Item(i).hide();
}
}
}
}
files.zip (231.4 KB)
Could you please check the result and clarify what is wrong?