This article guides on how to create bar chart in PowerPoint with Java REST API. You will learn how to create a bar graph in PowerPoint with Java RESTful Service using a Java-based Cloud SDK. It contains complete information on adding a chart and setting data.
Prerequisite
- Create an account API credentials
- Download Aspose.Slides Cloud SDK for Java to add a chart
- Setup Java project with the above SDK to work with graphs in a presentation
Steps to Add Column Chart in PowerPoint with Java Java-based API
- Initialize the SlidesApi with a client ID and secret for adding a chart
- Upload the presentation file to the Cloud
- Create a new chart configuration object using the Chart class and set the chart type
- Add chart title and initialize categories for the chart
- Initialize the data series for the chart, create the data series, and add the first data series
- Initialize more data series for the chart if required
- Add the chart to the first slide of the presentation using the CreateShape() method and download it if required
These steps describe how to create bar chart in PowerPoint with Java Java-based API. Create the SlidesApi object, upload the presentation, create a Chart object, set chart type, position, dimensions, title, categories and desired data series. Add the data series to the chart and add the chart to the first slide.
Code to Create Bar Graph in PowerPoint with Java REST Interface
package KbExamples; | |
import com.aspose.slides.ApiException; | |
import com.aspose.slides.api.SlidesApi; | |
import com.aspose.slides.model.*; | |
import java.io.File; | |
import java.io.IOException; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.nio.file.StandardCopyOption; | |
import java.util.ArrayList; | |
public class Example_AddBarChartInPresentation { | |
protected static SlidesApi presentationApi; | |
public Example_AddBarChartInPresentation() { | |
if (presentationApi == null) { | |
presentationApi = new SlidesApi("appSid", "appKey"); | |
} | |
} | |
public void InsertBarChart() throws ApiException, IOException { | |
String localPath = "/home/downloads/"; | |
String fileName = "Sample.pptx"; | |
String storageFolderName = "TempTests"; | |
presentationApi.uploadFile(storageFolderName+"/"+fileName, readFileToByteArray(localPath + fileName),null); | |
// Create a new chart configuration object | |
Chart chartConfig = new Chart(); | |
// Define chart type as a stacked bar chart | |
chartConfig.setChartType(Chart.ChartTypeEnum.STACKEDBAR); | |
// Set the position and dimensions of the chart on the slide | |
chartConfig.setX(150.0); // Horizontal position (pixels) | |
chartConfig.setY(150.0); // Vertical position (pixels) | |
chartConfig.setWidth(500.0); // Chart width (pixels) | |
chartConfig.setHeight(350.0); // Chart height (pixels) | |
// Add a title to the chart | |
ChartTitle title = new ChartTitle(); | |
title.setText("Bar Chart Example"); | |
chartConfig.setTitle(title); | |
// Initialize categories (x-axis labels) for the chart | |
chartConfig.setCategories(new ArrayList<ChartCategory>()); | |
ChartCategory category = new ChartCategory(); | |
category.setValue("Group A"); | |
chartConfig.getCategories().add(category); // First category | |
category = new ChartCategory(); | |
category.setValue("Group B"); | |
chartConfig.getCategories().add(category); // Second category | |
category = new ChartCategory(); | |
category.setValue("Group C"); | |
chartConfig.getCategories().add(category); // Third category | |
category = new ChartCategory(); | |
category.setValue("Group D"); | |
chartConfig.getCategories().add(category); // Fourth category | |
// Initialize data series for the chart | |
chartConfig.setSeries(new ArrayList<Series>()); | |
// Create the first data series | |
OneValueSeries firstSeries = new OneValueSeries(); | |
firstSeries.setDataPoints(new ArrayList<OneValueChartDataPoint>()); | |
OneValueChartDataPoint oneValueChartDataPoint = new OneValueChartDataPoint(); | |
oneValueChartDataPoint.setValue(15.0); | |
firstSeries.getDataPoints().add(oneValueChartDataPoint); // Value for Group A | |
oneValueChartDataPoint = new OneValueChartDataPoint(); | |
oneValueChartDataPoint.setValue(40.0); | |
firstSeries.getDataPoints().add(oneValueChartDataPoint); // Value for Group B | |
oneValueChartDataPoint = new OneValueChartDataPoint(); | |
oneValueChartDataPoint.setValue(25.0); | |
firstSeries.getDataPoints().add(oneValueChartDataPoint); // Value for Group C | |
oneValueChartDataPoint = new OneValueChartDataPoint(); | |
oneValueChartDataPoint.setValue(10.0); | |
firstSeries.getDataPoints().add(oneValueChartDataPoint); // Value for Group D | |
// Add the first data series to the chart | |
chartConfig.getSeries().add(firstSeries); | |
// Create the second data series | |
OneValueSeries secondSeries = new OneValueSeries(); | |
secondSeries.setDataPoints(new ArrayList<OneValueChartDataPoint>()); | |
oneValueChartDataPoint = new OneValueChartDataPoint(); | |
oneValueChartDataPoint.setValue(10.0); | |
secondSeries.getDataPoints().add(oneValueChartDataPoint); // Value for Group A | |
oneValueChartDataPoint = new OneValueChartDataPoint(); | |
oneValueChartDataPoint.setValue(20.0); | |
secondSeries.getDataPoints().add(oneValueChartDataPoint); // Value for Group B | |
oneValueChartDataPoint = new OneValueChartDataPoint(); | |
oneValueChartDataPoint.setValue(45.0); | |
secondSeries.getDataPoints().add(oneValueChartDataPoint); // Value for Group C | |
oneValueChartDataPoint = new OneValueChartDataPoint(); | |
oneValueChartDataPoint.setValue(15.0); | |
secondSeries.getDataPoints().add(oneValueChartDataPoint); // Value for Group D | |
// Add the second data series to the chart | |
chartConfig.getSeries().add(secondSeries); | |
// Add the chart to the first slide of the presentation | |
Chart chart = (Chart)presentationApi.createShape(fileName, 1, chartConfig, null, | |
null, null, null, storageFolderName, null); | |
// Download the updated presentation with Bar chart from the server | |
File presentationFile = presentationApi.downloadFile(storageFolderName+"/"+fileName, null, null); | |
// Copy the downloaded presentation with Bar chart to the local directory | |
copyFile(presentationFile, new File(localPath, fileName)); | |
System.out.println("Presentation with Bar chart is copied to: " + localPath + fileName); | |
} | |
public static byte[] readFileToByteArray(String filePath) throws IOException { | |
Path path = new File(filePath).toPath(); | |
return Files.readAllBytes(path); | |
} | |
private void copyFile(File sourceFile, File targetFile) throws IOException { | |
if (sourceFile == null || !sourceFile.exists()) { | |
throw new IOException("Source file does not exist: " + sourceFile); | |
} | |
// Ensure the target directory exists | |
Path targetPath = targetFile.toPath(); | |
Files.createDirectories(targetPath.getParent()); | |
// Copy the file | |
Files.copy(sourceFile.toPath(), targetPath, StandardCopyOption.REPLACE_EXISTING); | |
} | |
} |
This code demonstrates the process of how to make a bar graph in PowerPoint with Java Low Code API. You may set different chart type such as ClusteredColumn, Column3D, ClusteredCone, and Line etc, using the ChartTypeEnum and other parameters in the Chart object including ShowDataLabelsOverMaximum, BackWall, SideWall, Floor, and Legend etc.
This article has taught us the creation of a bar chart in PowerPoint. To convert HTML to PowerPoint, refer to the article on Convert HTML to PowerPoint with NET REST API.