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>version of aspose-slides API</version>
<classifier>jdk17</classifier>
</dependency>
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 .
Create a PPTX Chart - 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();
}
About Aspose.Slides for Java API
CreatePPTX charts online by visiting our
Live Demos website
. The live demo has the following benefitsOnline PPTX Chart Creation Live Demos
Create PPTX charts online by visiting our Live Demos website . The live demo has the following benefits
PPTX 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.
Read MoreOther Supported Charting Formats
You can also create charts in other presentation formats with Java.