This article guides you on how to create a Pie chart in PowerPoint with Java REST API. You will learn how to make a Pie chart in PowerPoint with Java RESTful Service using a Java-based Cloud SDK. All the steps are performed in the sample code along with the descriptive comments.
Prerequisite
- Create an account API credentials
- Download Aspose.Slides Cloud SDK for Java to make a Pie chart
- Setup Java project with the above SDK to work with Pie graphs in a presentation slide
Steps to Make a Pie Chart in PowerPoint with Java Java-based API
- Initialize the API client with ID and KEY using the SlidesApi class
- Read the local presentation file and upload it to the Cloud storage
- Create the Pie chart by defining its position, size, and type for configuring it
- Add labels to the chart and create a series of data points for the chart
- Add series to the chart and add this chart to the target slide in the loaded presentation with the CreateShape() method
- Download the presentation to a local file
These steps describe how do you make a Pie chart in PowerPoint with Java RESTful Service. Instantiate the chart object, configure its properties, add categories, and create data points series for adding to the Pie chart. Finally, add the Pie chart to the target slide in the presentation and save on the local disk.
Code to Add Pie Graph in PowerPoint with Java RESTful Service
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; | |
import java.util.List; | |
public class Example_AddPieChartInPresentation { | |
protected static SlidesApi presentationApi; | |
public Example_AddPieChartInPresentation() { | |
if (presentationApi == null) { | |
presentationApi = new SlidesApi("appSid", "appKey"); | |
} | |
} | |
public void InsertPieChart() throws ApiException, IOException { | |
String localPath = "/home/downloads/"; | |
String fileName = "Sample.pptx"; | |
String storageFolderName = "TempTests"; | |
presentationApi.uploadFile(storageFolderName+"/"+fileName, readFileToByteArray(localPath + fileName),null); | |
// Configure chart properties | |
// Creating a new chart object and defining its position, size, and type | |
Chart chartConfig = new Chart(); | |
chartConfig.setX(150.0); // Horizontal position (pixels) | |
chartConfig.setY(1520.0); // Vertical position (pixels) | |
chartConfig.setWidth(450.0); // Chart width (pixels) | |
chartConfig.setHeight(350.0); // Chart height (pixels) | |
// Add a title to the chart | |
ChartTitle title = new ChartTitle(); | |
title.setText("Pie Chart"); | |
chartConfig.setTitle(title); | |
// Initialize categories (x-axis labels) for the chart | |
chartConfig.setCategories(new ArrayList<ChartCategory>()); | |
ChartCategory category = new ChartCategory(); | |
category.setValue("Category A"); | |
chartConfig.getCategories().add(category); // First category | |
category = new ChartCategory(); | |
category.setValue("Category B"); | |
chartConfig.getCategories().add(category); // Second category | |
category = new ChartCategory(); | |
category.setValue("Category C"); | |
chartConfig.getCategories().add(category); // Third category | |
// Define data series and data points | |
// Creating a series of data points for the chart | |
OneValueSeries chartSeries = new OneValueSeries(); | |
chartSeries.setIsColorVaried(true); // Allows different colors for each data point | |
chartSeries.setDataPoints(new ArrayList<OneValueChartDataPoint>()); | |
OneValueChartDataPoint oneValueChartDataPoint = new OneValueChartDataPoint(); | |
oneValueChartDataPoint.setValue(35.0);// Data point for Category A | |
chartSeries.getDataPoints().add(oneValueChartDataPoint); | |
oneValueChartDataPoint = new OneValueChartDataPoint(); | |
oneValueChartDataPoint.setValue(45.0);// Data point for Category B | |
chartSeries.getDataPoints().add(oneValueChartDataPoint); | |
oneValueChartDataPoint = new OneValueChartDataPoint(); | |
oneValueChartDataPoint.setValue(20.0);// Data point for Category C | |
chartSeries.getDataPoints().add(oneValueChartDataPoint); | |
List<Series> chartSeriesCollection = new ArrayList<Series>(); | |
chartSeriesCollection.add(chartSeries); | |
// Adding the series to the chart | |
chartConfig.setSeries(chartSeriesCollection); | |
// Add the PIE chart to the first slide of the presentation | |
Chart chart = (Chart)presentationApi.createShape(fileName, 1, chartConfig, null, | |
null, null, null, storageFolderName, null); | |
// Download the presentation with PIE chart from the server | |
File presentationFile = presentationApi.downloadFile(storageFolderName+"/"+fileName, null, null); | |
// Copy the downloaded presentation with Pie chart to the local directory | |
copyFile(presentationFile, new File(localPath, fileName)); | |
System.out.println("Presentation with PIE 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); | |
} | |
} |
The above code shows how to create Pie chart in PPT with Java Java-based API. You may ensure that you add as many series of data collection as the number of categories added in the chart. Also note that loading the presentation in he beginning is must required otherwise you may get exceptions while executing code without uploading the source presentation.
This article has taught us adding a Pie chart in a presentation. To add a bar chart, refer to the article on Create Bar chart in PowerPoint with Java REST API.