
Overview
Creating visually appealing presentations is essential for effective communication, and charts are a powerful tool to represent data clearly. So, creating a bar chart in PowerPoint using Java allows developers to automate this process, saving time and ensuring consistency. However, in this blog post, we will explore how to use Aspose.Slides for Java to generate bar charts in PowerPoint presentations programmatically.
Bar Chart Generator API Installation
To get started, you will need to install the library that enables bar chart creation in PowerPoint. So, this step-by-step guide will walk you through the process.
So, you can include the following dependency in your pom.xml file.
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://repository.aspose.com/repo/</url>
</repository>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-slides</artifactId>
<version>24.4</version>
<classifier>jdk16</classifier>
</dependency>
The other option is to download the JAR file. In fact, Aspose.Slides for Java provides comprehensive installation instructions and features for creating and manipulating PPTX/PPT files.
Create a Bar Chart in PowerPoint using Java - Code Snippet
Now, let us dive into the code to create a bar chart in PowerPoint using Java programmatically.
The following are the steps:
- Instantiate Presentation class that represents PPTX file.
- Access the first slide by calling the get_Item method.
- Add a bar chart with default data by calling the addChart method.
- Get the chart data worksheet by calling the getChartDataWorkbook method.
- Call add method to add new series.
- Now populating series data by calling the addDataPointForBarSeries method.
- Invoke the setFillType method to set the fill color for the series.
- The save method will save the presentation with a bar chart.
The following code sample demonstrates how to create a bar chart in PowerPoint using this bar chart generator API:
public class main | |
{ | |
public static void main(String[] args) | |
{ | |
// The path to the documents directory. | |
String dataDir = "/Desktop/"; | |
// Create directory if it is not already present. | |
boolean IsExists = new File(dataDir).exists(); | |
if (!IsExists) | |
new File(dataDir).mkdirs(); | |
// Instantiate Presentation class that represents PPTX file. | |
Presentation pres = new Presentation(); | |
// Access first slide by calling the get_Item method. | |
ISlide sld = pres.getSlides().get_Item(0); | |
// Add a bar chart with default data by calling the addChart method. | |
IChart chart = sld.getShapes().addChart(ChartType.ClusteredColumn, 0, 0, 500, 500); | |
// Set the chart title with the custom size and placement. | |
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); | |
// Setting the index of chart data sheet | |
int defaultWorksheetIndex = 0; | |
// Get the chart data worksheet by calling the getChartDataWorkbook method. | |
IChartDataWorkbook fact = chart.getChartData().getChartDataWorkbook(); | |
// Delete default generated series and categories | |
chart.getChartData().getSeries().clear(); | |
chart.getChartData().getCategories().clear(); | |
int s = chart.getChartData().getSeries().size(); | |
s = chart.getChartData().getCategories().size(); | |
// Call add method to add new series. | |
chart.getChartData().getSeries().add(fact.getCell(defaultWorksheetIndex, 0, 1, "Series 1"), chart.getType()); | |
chart.getChartData().getSeries().add(fact.getCell(defaultWorksheetIndex, 0, 2, "Series 2"), chart.getType()); | |
// Adding new categories | |
chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 1, 0, "Caetegoty 1")); | |
chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 2, 0, "Caetegoty 2")); | |
chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 3, 0, "Caetegoty 3")); | |
// Take first chart series | |
IChartSeries series = chart.getChartData().getSeries().get_Item(0); | |
// Now populating series data by calling the addDataPointForBarSeries method. | |
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 1, 1, 20)); | |
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 2, 1, 50)); | |
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 3, 1, 30)); | |
// Invoke the setFillType method to set the fill color for series. | |
series.getFormat().getFill().setFillType(FillType.Solid); | |
// Take second chart series | |
series = chart.getChartData().getSeries().get_Item(1); | |
// Now populating series data | |
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 1, 2, 30)); | |
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 2, 2, 10)); | |
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 3, 2, 60)); | |
// Setting fill color for series | |
series.getFormat().getFill().setFillType(FillType.Solid); | |
// series.getFormat().getFill().getSolidFillColor().setColor(Color.GREEN); | |
// First label will be show Category name | |
IDataLabel lbl = series.getDataPoints().get_Item(0).getLabel(); | |
lbl.getDataLabelFormat().setShowCategoryName(true); | |
lbl = series.getDataPoints().get_Item(1).getLabel(); | |
lbl.getDataLabelFormat().setShowSeriesName(true); | |
// Show value for third label | |
lbl = series.getDataPoints().get_Item(2).getLabel(); | |
lbl.getDataLabelFormat().setShowValue(true); | |
lbl.getDataLabelFormat().setShowSeriesName(true); | |
lbl.getDataLabelFormat().setSeparator("/"); | |
// The save method will save the presentation with bar chart. | |
pres.save(dataDir + "AsposeChart_out.pptx", SaveFormat.Pptx); | |
} | |
} |
Output:

For more information on how to use this library, you can check out some popular platforms like Stack Overflow and GitHub for community discussions and examples.
Get a Free License
Aspose.Slides for Java offers a free temporary license to explore its features. Try it now and see how it can streamline your presentation creation process.
Final Touch
Creating a bar chart in PowerPoint using Java is a straightforward process with Aspose.Slides for Java. This Java API not only saves time but also enhances the quality of your presentations. Whether you are a developer looking to automate your workflow or simply want to add dynamic charts to your slides, this solution is both efficient and effective.
Check out the API documentation and API references to start using this bar chart generator API. Stay updated with aspose.com for the latest news and features.
Feel Free to Reach Out
If you have any questions, feel free to ask on our Forum.
Frequently Asked Questions – FAQs
Is there a free trial available to try Aspose.Slides for Java?
Yes, there is a free temporary license available to try out the library.