Crea un grafico a torta in PowerPoint con l'API REST Java

Questo articolo ti guida su come creare un grafico a torta in PowerPoint con l’API REST Java. Imparerai come creare un grafico a torta in PowerPoint con il servizio Java RESTful utilizzando un Cloud SDK basato su Java. Tutti i passaggi vengono eseguiti nel codice di esempio insieme ai commenti descrittivi.

Prerequisito

Passaggi per creare un grafico a torta in PowerPoint con Java API basata su Java

  1. Inizializza il client API con ID e CHIAVE utilizzando la classe SlidesApi
  2. Leggi il file di presentazione locale e caricalo nell’archivio cloud
  3. Crea il grafico a torta definendone posizione, dimensione e tipo per configurarlo
  4. Aggiungi etichette al grafico e crea una serie di punti dati per il grafico
  5. Aggiungi serie al grafico e aggiungi questo grafico alla diapositiva di destinazione nella presentazione caricata con il metodo CreateShape()
  6. Scarica la presentazione in un file locale

Questi passaggi descrivono come creare un grafico a torta in PowerPoint con il servizio Java RESTful. Crea un’istanza dell’oggetto grafico, configura le sue proprietà, aggiungi categorie e crea serie di punti dati da aggiungere al grafico a torta. Infine, aggiungi il grafico a torta alla diapositiva di destinazione nella presentazione e salva sul disco locale.

Codice per aggiungere un grafico a torta in PowerPoint con il servizio Java RESTful

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);
}
}

Il codice sopra mostra come creare un grafico a torta in PPT con API Java basate su Java. Puoi assicurarti di aggiungere tante serie di raccolte dati quante sono le categorie aggiunte nel grafico. Tieni inoltre presente che è necessario caricare la presentazione all’inizio, altrimenti potresti ottenere eccezioni durante l’esecuzione del codice senza caricare la presentazione sorgente.

Questo articolo ci ha insegnato ad aggiungere un grafico a torta in una presentazione. Per aggiungere un grafico a barre, fai riferimento all’articolo su Crea un grafico a barre in PowerPoint con l’API REST Java.

 Italiano