Create PPTX Charts in Java
Use Aspose.Slides for Java to create and format charts in presentation files without Microsoft PowerPoint.
Create Charts in PPTX Files Using Java
Use
Aspose.Slides for Java
to create and format charts in PPTX presentations. The API provides chart objects such as IChart, IChartDataWorkbook, IChartSeries, and IDataLabel. You can install the aspose-slides package in a Maven-based project by adding the repository and dependency below.
Repository
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://releases.aspose.com/java/repo/</url>
</repository>Dependency
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-slides</artifactId>
<version>26.8</version>
<classifier>jdk16</classifier>
</dependency>Create a Chart in PPTX - Java
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IChart chart = slide.getShapes().addChart(ChartType.ClusteredColumn, 0, 0, 500, 500);
chart.getChartTitle().addTextFrameForOverriding("Sales by quarter");
chart.getChartTitle().getTextFrameForOverriding().getTextFrameFormat().setCenterText(NullableBool.True);
chart.getChartTitle().setHeight(20);
chart.setTitle(true);
chart.getChartData().getSeries().get_Item(0).getLabels().getDefaultDataLabelFormat().setShowValue(true);
int defaultWorksheetIndex = 0;
IChartDataWorkbook chartDataWorkbook = chart.getChartData().getChartDataWorkbook();
chart.getChartData().getSeries().clear();
chart.getChartData().getCategories().clear();
chart.getChartData().getSeries().add(chartDataWorkbook.getCell(defaultWorksheetIndex, 0, 1, "Series 1"), chart.getType());
chart.getChartData().getSeries().add(chartDataWorkbook.getCell(defaultWorksheetIndex, 0, 2, "Series 2"), chart.getType());
chart.getChartData().getCategories().add(chartDataWorkbook.getCell(defaultWorksheetIndex, 1, 0, "Category 1"));
chart.getChartData().getCategories().add(chartDataWorkbook.getCell(defaultWorksheetIndex, 2, 0, "Category 2"));
chart.getChartData().getCategories().add(chartDataWorkbook.getCell(defaultWorksheetIndex, 3, 0, "Category 3"));
IChartSeries firstSeries = chart.getChartData().getSeries().get_Item(0);
firstSeries.getDataPoints().addDataPointForBarSeries(chartDataWorkbook.getCell(defaultWorksheetIndex, 1, 1, 20));
firstSeries.getDataPoints().addDataPointForBarSeries(chartDataWorkbook.getCell(defaultWorksheetIndex, 2, 1, 50));
firstSeries.getDataPoints().addDataPointForBarSeries(chartDataWorkbook.getCell(defaultWorksheetIndex, 3, 1, 30));
firstSeries.getFormat().getFill().setFillType(FillType.Solid);
firstSeries.getFormat().getFill().getSolidFillColor().setColor(java.awt.Color.RED);
IChartSeries secondSeries = chart.getChartData().getSeries().get_Item(1);
secondSeries.getDataPoints().addDataPointForBarSeries(chartDataWorkbook.getCell(defaultWorksheetIndex, 1, 2, 30));
secondSeries.getDataPoints().addDataPointForBarSeries(chartDataWorkbook.getCell(defaultWorksheetIndex, 2, 2, 10));
secondSeries.getDataPoints().addDataPointForBarSeries(chartDataWorkbook.getCell(defaultWorksheetIndex, 3, 2, 60));
secondSeries.getFormat().getFill().setFillType(FillType.Solid);
secondSeries.getFormat().getFill().getSolidFillColor().setColor(java.awt.Color.GREEN);
IDataLabel firstDataLabel = secondSeries.getDataPoints().get_Item(0).getLabel();
firstDataLabel.getDataLabelFormat().setShowCategoryName(true);
IDataLabel secondDataLabel = secondSeries.getDataPoints().get_Item(1).getLabel();
secondDataLabel.getDataLabelFormat().setShowSeriesName(true);
IDataLabel thirdDataLabel = secondSeries.getDataPoints().get_Item(2).getLabel();
thirdDataLabel.getDataLabelFormat().setShowValue(true);
thirdDataLabel.getDataLabelFormat().setShowSeriesName(true);
thirdDataLabel.getDataLabelFormat().setSeparator("/");
presentation.save("chart-presentation.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}How to Create Charts in PPTX Files in Java
Create charts in PPTX files with Aspose.Slides for Java.
- Create a
Presentationobject. - Get the first
ISlidefrom the presentation. - Add an
IChartobject with default data. - Set the chart title and data label options.
- Get the
IChartDataWorkbookobject and clear default series and categories. - Add new chart series and categories.
- Populate series data and set series fill colors.
- Create custom data labels for the chart series.
- Save the presentation with the chart.
System Requirements
Aspose.Slides for Java supports major desktop and server platforms. Make sure that your project has these prerequisites.
- Microsoft Windows, Linux, macOS, or another OS with a supported Java Runtime Environment.
- Get the latest version of Aspose.Slides for Java from Maven .
Built with Aspose.Slides
Create PPTX Charts Online
The free Aspose.Slides Chart app is built with Aspose.Slides for Java, the same API as the code above. Open it and see what the API can do with your own PPTX files.
- Runs in your browser, nothing to install.
- See how Aspose.Slides builds charts before you write code.
- Build a chart from Excel or table data.
- Download it as a PPTX file or an image.
What is PPTX File Format?
A PPTX file is a Microsoft PowerPoint Open XML presentation file. It stores slides, text, images, charts, formatting, animations, and other presentation content in a zipped XML-based package.
Other Supported Chart Formats
Using Java, you can also create charts in other formats.