이 문서에서는 Java REST API를 사용하여 PowerPoint에서 막대 차트를 생성하는 방법을 안내합니다. Java 기반 Cloud SDK를 사용하여 Java RESTful Service로 PowerPoint에서 막대 그래프를 만드는 방법을 알아봅니다. 여기에는 차트 추가 및 데이터 설정에 대한 완전한 정보가 포함되어 있습니다.
전제조건
다운로드 Aspose.Slides Cloud SDK for Java to add a chart
프리젠테이션에서 그래프를 사용하려면 위 SDK를 사용하여 Java 프로젝트를 설정하세요.
Java Java 기반 API를 사용하여 PowerPoint에서 세로 막대형 차트를 추가하는 단계
- 차트 추가를 위해 클라이언트 ID와 비밀번호로 SlidesApi를 초기화합니다.
- 프레젠테이션 파일을 클라우드에 업로드
- Chart 클래스를 사용하여 새 차트 구성 개체를 만들고 차트 유형을 설정합니다.
- 차트 제목 추가 및 차트 카테고리 초기화
- 차트의 데이터 시리즈를 초기화하고 데이터 시리즈를 생성한 후 첫 번째 데이터 시리즈를 추가합니다.
- 필요한 경우 차트에 대한 추가 데이터 시리즈를 초기화하세요.
- CreateShape() 메소드를 사용하여 프레젠테이션의 첫 번째 슬라이드에 차트를 추가하고 필요한 경우 다운로드하세요.
이 단계에서는 Java Java 기반 API를 사용하여 PowerPoint에서 막대 차트를 만드는 방법을 설명합니다. SlidesApi 개체를 만들고, 프레젠테이션을 업로드하고, Chart 개체를 만들고, 차트 유형, 위치, 크기, 제목, 범주 및 원하는 데이터 시리즈를 설정합니다. 차트에 데이터 시리즈를 추가하고 첫 번째 슬라이드에 차트를 추가합니다.
Java REST 인터페이스를 사용하여 PowerPoint에서 막대 그래프를 만드는 코드
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); | |
} | |
} |
이 코드는 Java Low Code API를 사용하여 PowerPoint에서 막대 그래프를 만드는 방법 프로세스를 보여줍니다. ShowDataLabelsOverMaximum, BackWall, SideWall, Floor 및 Legend 등을 포함한 Chart 개체의 ChartTypeEnum 및 기타 매개 변수를 사용하여 ClusteredColumn, Column3D, ClusteredCone 및 Line 등과 같은 다양한 차트 유형을 설정할 수 있습니다.
이 기사에서는 PowerPoint에서 막대 차트를 만드는 방법을 배웠습니다. HTML을 PowerPoint로 변환하려면 NET REST API를 사용하여 HTML을 PowerPoint로 변환의 기사를 참조하세요.