Den här artikeln guidar dig om hur du skapar ett cirkeldiagram i PowerPoint med Java REST API. Du kommer att lära dig hur man gör ett cirkeldiagram i PowerPoint med Java RESTful Service med hjälp av en Java-baserad moln-SDK. Alla steg utförs i exempelkoden tillsammans med de beskrivande kommentarerna.
Nödvändig förutsättning
Ladda ner Aspose.Slides Cloud SDK for Java to make a Pie chart
Ställ in Java-projekt med ovanstående SDK för att arbeta med cirkeldiagram i en presentationsbild
Steg för att skapa ett cirkeldiagram i PowerPoint med Java Java-baserat API
- Initiera API-klienten med ID och KEY med klassen SlidesApi
- Läs den lokala presentationsfilen och ladda upp den till molnlagringen
- Skapa cirkeldiagrammet genom att definiera dess position, storlek och typ för att konfigurera det
- Lägg till etiketter i diagrammet och skapa en serie datapunkter för diagrammet
- Lägg till serier i diagrammet och lägg till detta diagram till målbilden i den laddade presentationen med metoden CreateShape()
- Ladda ner presentationen till en lokal fil
Dessa steg beskriver hur gör du ett cirkeldiagram i PowerPoint med Java RESTful Service. Instantiera diagramobjektet, konfigurera dess egenskaper, lägg till kategorier och skapa datapunktsserier för att läggas till i cirkeldiagrammet. Lägg slutligen till cirkeldiagrammet till målbilden i presentationen och spara på den lokala disken.
Kod för att lägga till cirkeldiagram i PowerPoint med 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); | |
} | |
} |
Ovanstående kod visar hur man skapar cirkeldiagram i PPT med Java Java-baserat API. Du kan se till att du lägger till lika många serier av datainsamling som antalet kategorier som lagts till i diagrammet. Observera också att det måste krävas att presentationen laddas i början, annars kan du få undantag när du kör kod utan att ladda upp källpresentationen.
Den här artikeln har lärt oss att lägga till ett cirkeldiagram i en presentation. För att lägga till ett stapeldiagram, se artikeln om Skapa stapeldiagram i PowerPoint med Java REST API.