0

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: output presentation

Resize chart to show that it's generating: enter image description here

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.

adi
  • 31
  • 5
  • 1
    For units of rectangle dimensions of `XSLFChart` see https://stackoverflow.com/questions/59848453/resizing-a-xslfchart/59853872#59853872. But not clear what meant with: "find a way to add anchor and place chart from above code in predefined container in presentation". What predefined container? How would you do this using PowerPoint GUI? Please make the question more clear. – Axel Richter Aug 03 '23 at 04:24
  • Thanks @AxelRichter for the comment. I was following the thread you provided before but the problem is that when I'm passing rectangle object chart is not reacting to the rectangle values. I'm always receiving chart on (x,y) -> (0,0) and size (w, h)-> 0,0. Adding images to main thread. – adi Aug 04 '23 at 09:57
  • My answer in https://stackoverflow.com/questions/45373942/java-create-a-chart-in-a-powerpoint-using-apache-poi now provides an additional example using `XDDF`. Works for me using current Apache POI 5.2.3. Produces a properly dimesioned chart using `Rectangle chartDimensions = new Rectangle(100*Units.EMU_PER_POINT, 50*Units.EMU_PER_POINT, 400*Units.EMU_PER_POINT, 400*Units.EMU_PER_POINT); slide.addChart(chart, chartDimensions);` – Axel Richter Aug 04 '23 at 10:05

0 Answers0