Bài viết này hướng dẫn bạn cách tạo biểu đồ hình tròn trong PowerPoint bằng Java REST API. Bạn sẽ tìm hiểu cách tạo biểu đồ hình tròn trong PowerPoint bằng Java RESTful Service bằng SDK đám mây dựa trên Java. Tất cả các bước được thực hiện trong mã mẫu cùng với các nhận xét mô tả.
Điều kiện tiên quyết
Tải xuống Aspose.Slides Cloud SDK for Java to make a Pie chart
Thiết lập dự án Java với SDK trên để hoạt động với biểu đồ Pie trong slide thuyết trình
Các bước để tạo biểu đồ hình tròn trong PowerPoint bằng Java API dựa trên Java
- Khởi tạo ứng dụng khách API bằng ID và KEY bằng cách sử dụng lớp SlidesApi
- Đọc tệp bản trình bày cục bộ và tải nó lên bộ lưu trữ Đám mây
- Tạo biểu đồ Hình tròn bằng cách xác định vị trí, kích thước và loại để định cấu hình biểu đồ
- Thêm nhãn vào biểu đồ và tạo chuỗi điểm dữ liệu cho biểu đồ
- Thêm chuỗi vào biểu đồ và thêm biểu đồ này vào slide mục tiêu trong bản trình bày đã tải bằng phương thức CreateShape()
- Tải bản trình bày xuống một tệp cục bộ
Các bước này mô tả cách tạo biểu đồ hình tròn trong PowerPoint bằng Java RESTful Service. Khởi tạo đối tượng biểu đồ, đặt cấu hình các thuộc tính của nó, thêm danh mục và tạo chuỗi điểm dữ liệu để thêm vào Biểu đồ hình tròn. Cuối cùng, thêm biểu đồ Hình tròn vào trang chiếu đích trong bản trình bày và lưu vào đĩa cục bộ.
Mã để thêm biểu đồ hình tròn trong PowerPoint với dịch vụ RESTful Java
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); | |
} | |
} |
Đoạn mã trên cho thấy cách tạo biểu đồ hình tròn trong PPT bằng API dựa trên Java Java. Bạn có thể đảm bảo rằng bạn thêm nhiều chuỗi thu thập dữ liệu bằng số lượng danh mục được thêm vào biểu đồ. Cũng lưu ý rằng việc tải bản trình bày ngay từ đầu là bắt buộc nếu không bạn có thể gặp ngoại lệ trong khi thực thi mã mà không tải lên bản trình bày nguồn.
Bài viết này đã dạy chúng tôi cách thêm biểu đồ Pie vào bản trình bày. Để thêm biểu đồ thanh, hãy tham khảo bài viết trên Tạo biểu đồ thanh trong PowerPoint bằng API Java REST.