![create charts in powerpoint ppt java](images/create-charts.jpg#center)
Charts are used to summarize and visually represent the data within the presentations. To visualize the data, MS PowerPoint provides a wide range of chart types. Among all, the most commonly used chart types include pie charts, line charts, bar charts, histograms, stock charts, and etc. In this article, you will learn how to create charts in PowerPoint presentations using Java.
- API to Create Charts in PowerPoint
- Create Column Chart in PowerPoint using Java
- Create Scattered Chart in PowerPoint using Java
- Add Pie Chart in PowerPoint using Java
- Add Histogram Chart in PowerPoint using Java
- Create a Stock Chart in PowerPoint using Java
Java API to Create Charts in PowerPoint
In order to create different types of charts in presentations, we will use Aspose.Slides for Java. The said API lets you create and manipulate PowerPoint presentations from within your Java applications. Furthermore, it allows you to create and add charts to the presentations seamlessly. You can either download the API or install it using the following Maven configurations.
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>http://repository.aspose.com/repo/</url>
</repository>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-slides</artifactId>
<version>21.7</version>
<classifier>jdk16</classifier>
</dependency>
Create Column Chart in PowerPoint PPT using Java
The following are the steps to create a column chart in a PowerPoint presentation using Java.
- First, create an instance of the Presentation class (specify path of the file in the constructor in case of loading exisitng presentation).
- Obtain the slide’s reference by specifying it’s index.
- Add a chart with default data along with the type ChartType.ClusteredColumn.
- Clear the default series and categories.
- Access the chart data workbook in a IChartDataWorkbook object.
- Add new series and categories of the chart to the workbook.
- Add new chart data to the chart series.
- Finally, save the presentation using Presentation.save(String, SaveFormat) method.
For demonstration, the following code sample shows how to create a column chart in a PowerPoint presentation using Java.
// Create a new presentation | |
Presentation pres = new Presentation(); | |
try { | |
// Add a new column chart and clear the default categories and series | |
IChart ch = pres.getSlides().get_Item(0).getShapes().addChart(ChartType.ClusteredColumn, 100, 100, 600, 450); | |
ch.getChartData().getSeries().clear(); | |
ch.getChartData().getCategories().clear(); | |
// Access the chart data | |
IChartDataWorkbook fact = ch.getChartData().getChartDataWorkbook(); | |
fact.clear(0); | |
int defaultWorksheetIndex = 0; | |
// Add categories | |
IChartCategory category = ch.getChartData().getCategories().add(fact.getCell(0, "c2", "A")); | |
category.getGroupingLevels().setGroupingItem(1, "Group1"); | |
category = ch.getChartData().getCategories().add(fact.getCell(0, "c3", "B")); | |
category = ch.getChartData().getCategories().add(fact.getCell(0, "c4", "C")); | |
category.getGroupingLevels().setGroupingItem(1, "Group2"); | |
category = ch.getChartData().getCategories().add(fact.getCell(0, "c5", "D")); | |
category = ch.getChartData().getCategories().add(fact.getCell(0, "c6", "E")); | |
category.getGroupingLevels().setGroupingItem(1, "Group3"); | |
category = ch.getChartData().getCategories().add(fact.getCell(0, "c7", "F")); | |
category = ch.getChartData().getCategories().add(fact.getCell(0, "c8", "G")); | |
category.getGroupingLevels().setGroupingItem(1, "Group4"); | |
category = ch.getChartData().getCategories().add(fact.getCell(0, "c9", "H")); | |
// Add Series | |
IChartSeries series = ch.getChartData().getSeries().add(fact.getCell(0, "D1", "Series 1"), | |
ChartType.ClusteredColumn); | |
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, "D2", 10)); | |
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, "D3", 20)); | |
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, "D4", 30)); | |
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, "D5", 40)); | |
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, "D6", 50)); | |
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, "D7", 60)); | |
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, "D8", 70)); | |
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, "D9", 80)); | |
// Save presentation with chart | |
pres.save("column-chart.pptx", SaveFormat.Pptx); | |
} finally { | |
if (pres != null) pres.dispose(); | |
} |
The following is the screenshot of the resultant column chart.
![Create Column Chart in PowerPoint Java](images/column-chart-2.jpg#center)
Create Scattered Chart in PowerPoint PPT using Java
The following are the steps to create a scattered chart in the PowerPoint presentation using Java.
- Create a new presentation or load an existing one using the Presentation class.
- Get the reference of the slide in the ISlide object.
- Add a ScatterWithSmoothLines chart type with default data and get its reference in IChart object.
- Access the chart data workbook into the IChartDataWorkbook object and clear the default series.
- Add new series to the chart data.
- Access each series into the IChartSeries object and add data points to the series.
- Access the marker of the series using IChartSeries.getMarker() and set its properties.
- Save the presentation using Presentation.save(String, SaveFormat) method.
The following code sample shows how to create a scattered chart in PowerPoint presentations using Java.
// Instantiate Presentation class that represents PPTX file | |
Presentation pres = new Presentation(); | |
try { | |
// Access first slide | |
ISlide slide = pres.getSlides().get_Item(0); | |
// Create the default chart | |
IChart chart = slide.getShapes().addChart(ChartType.ScatterWithSmoothLines, 0, 0, 400, 400); | |
// Get the default chart data worksheet index | |
int defaultWorksheetIndex = 0; | |
// Get the chart data worksheet | |
IChartDataWorkbook fact = chart.getChartData().getChartDataWorkbook(); | |
// Delete demo series | |
chart.getChartData().getSeries().clear(); | |
// Add new series | |
chart.getChartData().getSeries().add(fact.getCell(defaultWorksheetIndex, 1, 1, "Series 1"), chart.getType()); | |
chart.getChartData().getSeries().add(fact.getCell(defaultWorksheetIndex, 1, 3, "Series 2"), chart.getType()); | |
// Take first chart series | |
IChartSeries series = chart.getChartData().getSeries().get_Item(0); | |
// Add new point (1:3) there. | |
series.getDataPoints().addDataPointForScatterSeries(fact.getCell(defaultWorksheetIndex, 2, 1, 1), fact.getCell(defaultWorksheetIndex, 2, 2, 3)); | |
// Add new point (2:10) | |
series.getDataPoints().addDataPointForScatterSeries(fact.getCell(defaultWorksheetIndex, 3, 1, 2), fact.getCell(defaultWorksheetIndex, 3, 2, 10)); | |
// Edit the type of series | |
series.setType(ChartType.ScatterWithStraightLinesAndMarkers); | |
// Change the chart series marker | |
series.getMarker().setSize(10); | |
series.getMarker().setSymbol(MarkerStyleType.Star); | |
// Take second chart series | |
series = chart.getChartData().getSeries().get_Item(1); | |
// Add new point (5:2) there. | |
series.getDataPoints().addDataPointForScatterSeries(fact.getCell(defaultWorksheetIndex, 2, 3, 5), fact.getCell(defaultWorksheetIndex, 2, 4, 2)); | |
// Add new point (3:1) | |
series.getDataPoints().addDataPointForScatterSeries(fact.getCell(defaultWorksheetIndex, 3, 3, 3), fact.getCell(defaultWorksheetIndex, 3, 4, 1)); | |
// Add new point (2:2) | |
series.getDataPoints().addDataPointForScatterSeries(fact.getCell(defaultWorksheetIndex, 4, 3, 2), fact.getCell(defaultWorksheetIndex, 4, 4, 2)); | |
// Add new point (5:1) | |
series.getDataPoints().addDataPointForScatterSeries(fact.getCell(defaultWorksheetIndex, 5, 3, 5), fact.getCell(defaultWorksheetIndex, 5, 4, 1)); | |
// Change the chart series marker | |
series.getMarker().setSize(10); | |
series.getMarker().setSymbol(MarkerStyleType.Circle); | |
// Save the presentation | |
pres.save("scatter-chart.pptx", SaveFormat.Pptx); | |
} finally { | |
if (pres != null) pres.dispose(); | |
} |
The following screenshot shows the resultant scattered chart.
![create scattered chart in powerpoint in C#](images/scattered-chart.jpg#center)
Create Pie Chart in PowerPoint PPT using Java
The following are the steps to create a pie chart in a PowerPoint presentation using Java.
- Create a new presentation or load an existing one using the Presentation class.
- Get reference of the slide by providing its index.
- Add a chart with default data along with the type ChartType.Pie.
- Access the chart data in a IChartDataWorkbook object.
- Clear the default series and categories.
- Create new series and categories.
- Add new chart data for the chart series.
- Add new points for charts and add custom colors for the pie chart’s sectors.
- Set labels for series and leader lines for labels.
- Set the rotation angle for pie chart slides.
- Save the presentation using Presentation.save(String, SaveFormat) method.
The following code sample shows how to create a pie chart in PowerPoint presentation using Java.
// Instantiate Presentation class that represents PPTX file | |
Presentation pres = new Presentation(); | |
try { | |
// Access first slide | |
ISlide slides = pres.getSlides().get_Item(0); | |
// Add chart with default data | |
IChart chart = slides.getShapes().addChart(ChartType.Pie, 100, 100, 400, 400); | |
// Set chart Title | |
chart.getChartTitle().addTextFrameForOverriding("Sample Title"); | |
chart.getChartTitle().getTextFrameForOverriding().getTextFrameFormat().setCenterText(NullableBool.True); | |
chart.getChartTitle().setHeight(20); | |
chart.setTitle(true); | |
// Set first series to Show Values | |
chart.getChartData().getSeries().get_Item(0).getLabels().getDefaultDataLabelFormat().setShowValue(true); | |
// Set the index of chart data sheet | |
int defaultWorksheetIndex = 0; | |
// Get the chart data worksheet | |
IChartDataWorkbook fact = chart.getChartData().getChartDataWorkbook(); | |
// Delete default generated series and categories | |
chart.getChartData().getSeries().clear(); | |
chart.getChartData().getCategories().clear(); | |
// Add new categories | |
chart.getChartData().getCategories().add(fact.getCell(0, 1, 0, "First Qtr")); | |
chart.getChartData().getCategories().add(fact.getCell(0, 2, 0, "2nd Qtr")); | |
chart.getChartData().getCategories().add(fact.getCell(0, 3, 0, "3rd Qtr")); | |
// Add new series | |
IChartSeries series = chart.getChartData().getSeries().add(fact.getCell(0, 0, 1, "Series 1"), chart.getType()); | |
// Now populating series data | |
series.getDataPoints().addDataPointForPieSeries(fact.getCell(defaultWorksheetIndex, 1, 1, 20)); | |
series.getDataPoints().addDataPointForPieSeries(fact.getCell(defaultWorksheetIndex, 2, 1, 50)); | |
series.getDataPoints().addDataPointForPieSeries(fact.getCell(defaultWorksheetIndex, 3, 1, 30)); | |
// Not working in new version | |
// Adding new points and setting sector color | |
// series.IsColorVaried = true; | |
chart.getChartData().getSeriesGroups().get_Item(0).setColorVaried(true); | |
IChartDataPoint point = series.getDataPoints().get_Item(0); | |
point.getFormat().getFill().setFillType(FillType.Solid); | |
point.getFormat().getFill().getSolidFillColor().setColor(Color.CYAN); | |
// Set Sector border | |
point.getFormat().getLine().getFillFormat().setFillType(FillType.Solid); | |
point.getFormat().getLine().getFillFormat().getSolidFillColor().setColor(Color.GRAY); | |
point.getFormat().getLine().setWidth(3.0); | |
//point.getFormat().getLine().setStyle(LineStyle.ThinThick); | |
//point.getFormat().getLine().setDashStyle(LineDashStyle.DashDot); | |
IChartDataPoint point1 = series.getDataPoints().get_Item(1); | |
point1.getFormat().getFill().setFillType(FillType.Solid); | |
point1.getFormat().getFill().getSolidFillColor().setColor(Color.ORANGE); | |
// Set Sector border | |
point1.getFormat().getLine().getFillFormat().setFillType(FillType.Solid); | |
point1.getFormat().getLine().getFillFormat().getSolidFillColor().setColor(Color.BLUE); | |
point1.getFormat().getLine().setWidth(3.0); | |
//point1.getFormat().getLine().setStyle(LineStyle.Single); | |
//point1.getFormat().getLine().setDashStyle(LineDashStyle.LargeDashDot); | |
IChartDataPoint point2 = series.getDataPoints().get_Item(2); | |
point2.getFormat().getFill().setFillType(FillType.Solid); | |
point2.getFormat().getFill().getSolidFillColor().setColor(Color.YELLOW); | |
// Set Sector border | |
point2.getFormat().getLine().getFillFormat().setFillType(FillType.Solid); | |
point2.getFormat().getLine().getFillFormat().getSolidFillColor().setColor(Color.RED); | |
point2.getFormat().getLine().setWidth(2.0); | |
//point2.getFormat().getLine().setStyle(LineStyle.ThinThin); | |
//point2.getFormat().getLine().setDashStyle(LineDashStyle.LargeDashDotDot); | |
// Create custom labels for each of categories for new series | |
IDataLabel lbl1 = series.getDataPoints().get_Item(0).getLabel(); | |
// lbl.ShowCategoryName = true; | |
lbl1.getDataLabelFormat().setShowValue(true); | |
IDataLabel lbl2 = series.getDataPoints().get_Item(1).getLabel(); | |
lbl2.getDataLabelFormat().setShowValue(true); | |
lbl2.getDataLabelFormat().setShowLegendKey(true); | |
lbl2.getDataLabelFormat().setShowPercentage(true); | |
IDataLabel lbl3 = series.getDataPoints().get_Item(2).getLabel(); | |
lbl3.getDataLabelFormat().setShowSeriesName(true); | |
lbl3.getDataLabelFormat().setShowPercentage(true); | |
// Show Leader Lines for Chart | |
series.getLabels().getDefaultDataLabelFormat().setShowLeaderLines(true); | |
// Set Rotation Angle for Pie Chart Sectors | |
chart.getChartData().getSeriesGroups().get_Item(0).setFirstSliceAngle(180); | |
// Save presentation with chart | |
pres.save("pie-chart.pptx", SaveFormat.Pptx); | |
} finally { | |
if (pres != null) pres.dispose(); | |
} |
The following is the screenshot of the generated pie chart.
![Creat Pie Chart in PowerPoint Java](images/pie-chart-2.jpg#center)
Add Histogram Chart in PowerPoint PPTX using Java
The following are the steps to create a histogram chart in PowerPoint presentations using Java.
- Create a new presentation or load existing one using Presentation class.
- Obtain reference of the slide by providing its index.
- Add a chart with default data along with the type ChartType.Histogram.
- Access the chart data in IChartDataWorkbook object.
- Clear the default series and categories.
- Add new series and categories.
- Save the presentation using Presentation.save(String, SaveFormat) method.
The following code sample shows how to create a histogram chart using Java.
// Create a new presentation | |
Presentation pres = new Presentation(); | |
try { | |
// Add a new histogram chart and clear its categories and series | |
IChart chart = pres.getSlides().get_Item(0).getShapes().addChart(ChartType.Histogram, 50, 50, 500, 400); | |
chart.getChartData().getCategories().clear(); | |
chart.getChartData().getSeries().clear(); | |
// Access chart data | |
IChartDataWorkbook wb = chart.getChartData().getChartDataWorkbook(); | |
wb.clear(0); | |
// Add series | |
IChartSeries series = chart.getChartData().getSeries().add(ChartType.Histogram); | |
series.getDataPoints().addDataPointForHistogramSeries(wb.getCell(0, "A1", 15)); | |
series.getDataPoints().addDataPointForHistogramSeries(wb.getCell(0, "A2", -41)); | |
series.getDataPoints().addDataPointForHistogramSeries(wb.getCell(0, "A3", 16)); | |
series.getDataPoints().addDataPointForHistogramSeries(wb.getCell(0, "A4", 10)); | |
series.getDataPoints().addDataPointForHistogramSeries(wb.getCell(0, "A5", -23)); | |
series.getDataPoints().addDataPointForHistogramSeries(wb.getCell(0, "A6", 16)); | |
chart.getAxes().getHorizontalAxis().setAggregationType(AxisAggregationType.Automatic); | |
// Save the presentation | |
pres.save("histogram-chart.pptx", SaveFormat.Pptx); | |
} finally { | |
if (pres != null) pres.dispose(); | |
} |
The following is the screenshot of the created histogram chart.
![create histogram chart in powerpoint in C#](images/histogram-chart.jpg#center)
Create a Stock Chart in PowerPoint using Java
Stock chart is also one of the commonly used chart types within PowerPoint presentations. The following are the steps to create a stock chart in Java.
- Create an instance of the Presentation class.
- Get the reference of the slide by providing its index.
- Add a chart with default data along with the type ChartType.OpenHighLowClose.
- Access the chart data in IChartDataWorkbook object.
- Clear the default series and categories.
- Add new series and categories.
- Add new chart data for the chart series.
- Specify HiLowLines format.
- Save the presentation using Presentation.save(String, SaveFormat) method.
The following code sample shows how to add a stock chart to PowerPoint presentation using C#.
// Create a new presentation | |
Presentation pres = new Presentation(); | |
try { | |
// Add chart | |
IChart chart = pres.getSlides().get_Item(0).getShapes().addChart(ChartType.OpenHighLowClose, 50, 50, 600, 400, false); | |
// Clear categories and series | |
chart.getChartData().getSeries().clear(); | |
chart.getChartData().getCategories().clear(); | |
// Access chart data | |
IChartDataWorkbook wb = chart.getChartData().getChartDataWorkbook(); | |
// Add categories | |
chart.getChartData().getCategories().add(wb.getCell(0, 1, 0, "A")); | |
chart.getChartData().getCategories().add(wb.getCell(0, 2, 0, "B")); | |
chart.getChartData().getCategories().add(wb.getCell(0, 3, 0, "C")); | |
chart.getChartData().getSeries().add(wb.getCell(0, 0, 1, "Open"), chart.getType()); | |
chart.getChartData().getSeries().add(wb.getCell(0, 0, 2, "High"), chart.getType()); | |
chart.getChartData().getSeries().add(wb.getCell(0, 0, 3, "Low"), chart.getType()); | |
chart.getChartData().getSeries().add(wb.getCell(0, 0, 4, "Close"), chart.getType()); | |
// Add series data | |
IChartSeries series = chart.getChartData().getSeries().get_Item(0); | |
series.getDataPoints().addDataPointForStockSeries(wb.getCell(0, 1, 1, 72)); | |
series.getDataPoints().addDataPointForStockSeries(wb.getCell(0, 2, 1, 25)); | |
series.getDataPoints().addDataPointForStockSeries(wb.getCell(0, 3, 1, 38)); | |
series = chart.getChartData().getSeries().get_Item(1); | |
series.getDataPoints().addDataPointForStockSeries(wb.getCell(0, 1, 2, 172)); | |
series.getDataPoints().addDataPointForStockSeries(wb.getCell(0, 2, 2, 57)); | |
series.getDataPoints().addDataPointForStockSeries(wb.getCell(0, 3, 2, 57)); | |
series = chart.getChartData().getSeries().get_Item(2); | |
series.getDataPoints().addDataPointForStockSeries(wb.getCell(0, 1, 3, 12)); | |
series.getDataPoints().addDataPointForStockSeries(wb.getCell(0, 2, 3, 12)); | |
series.getDataPoints().addDataPointForStockSeries(wb.getCell(0, 3, 3, 13)); | |
series = chart.getChartData().getSeries().get_Item(3); | |
series.getDataPoints().addDataPointForStockSeries(wb.getCell(0, 1, 4, 25)); | |
series.getDataPoints().addDataPointForStockSeries(wb.getCell(0, 2, 4, 38)); | |
series.getDataPoints().addDataPointForStockSeries(wb.getCell(0, 3, 4, 50)); | |
chart.getChartData().getSeriesGroups().get_Item(0).getUpDownBars().setUpDownBars(true); | |
chart.getChartData().getSeriesGroups().get_Item(0).getHiLowLinesFormat().getLine().getFillFormat().setFillType(FillType.Solid); | |
for (IChartSeries ser : chart.getChartData().getSeries()) | |
{ | |
ser.getFormat().getLine().getFillFormat().setFillType(FillType.NoFill); | |
} | |
// Save the presentation | |
pres.save("output.pptx", SaveFormat.Pptx); | |
} finally { | |
if (pres != null) pres.dispose(); | |
} |
The following is the screenshot of the created stock chart.
![create stock chart in powerpoint in C#](images/stock-chart.jpg#center)
More Chart Types
In addition to the above-mentioned charts, there are other types of charts as well that you can add to the PowerPoint presentations. In order to read more about the supported chart types, you can visit this documentation article.
Get a Free API License
You can get a free temporary license in order to use the API without evaluation limitations.
Try Online
You can also try the free online tool for creating charts in PowerPoint presentations, which is based on Aspose.Slides.
Conclusion
In this article, you have learned how to create charts in PowerPoint presentations using Java. The step-by-step guide and code samples have shown how to add column charts, scatter charts, pie charts, histograms, and stock charts. You can explore more about Aspose.Slides for Java using documentation. In case you would have any queries, inform us via our forum.