I'm trying to create bar chart as POC and place it in presentation slide container. I'm able to create chart but it's appearing in top right corner and it's very small. When I'm adding dimensions it's appearing even smaller (not visible). Here is code:
XMLSlideShow ppt = new XMLSlideShow();
// Create a slide
XSLFSlide slide = ppt.createSlide();
XSLFChart chart = ppt.createChart(slide);
chart.setTitleText("Test Chart");
// bar chart
XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
leftAxis.setCrossBetween(AxisCrossBetween.BETWEEN);
// Define chart data
XDDFChartData data = chart.createData(ChartTypes.BAR, bottomAxis, leftAxis);
// Add chart categories (x-axis data)
String[] categories = new String[] { "Category 1", "Category 2", "Category 3" };
XDDFCategoryDataSource categoryData = XDDFDataSourcesFactory.fromArray(categories);
// Add chart values (y-axis data)
Double[] values = new Double[] { 10.0, 20.0, 15.0 };
XDDFNumericalDataSource<Double> valueData = XDDFDataSourcesFactory.fromArray(values);
XDDFBarChartData bar = (XDDFBarChartData) data;
bar.setBarDirection(BarDirection.BAR);
XDDFChartData.Series series = data.addSeries(categoryData, valueData);
series.setTitle("Series 1", null);
chart.plot(data);
slide.addChart(chart);
// Save the presentation to a file
FileOutputStream out = new FileOutputStream("ChartPresentation.pptx");
ppt.write(out);
out.close();
I was also trying to pass dimensions but then chart is even smaller.
slide.addChart(chart, new Rectangle2D.Float(10, 10, 400, 400));
I was also following sample from java-create-a-chart-in-a-powerpoint-using-apache-poi but that one is more complex and not ideal.
it's not showing charts in preview of the file. File looks like empty.
Output presentation:
Resize chart to show that it's generating:
Also I can't find a way to add anchor and place chart from above code in predefined container in presentation. What I mean, I'd like to place chart in predefined container that already exists in template.