Այս հոդվածը ցույց է տալիս, թե ինչպես ստեղծել գծային գծապատկեր PowerPoint-ում Java REST API-ով: Դուք կսովորեք ինչպես ստեղծել գծապատկեր PowerPoint-ում Java RESTful ծառայության միջոցով Java-ի վրա հիմնված Cloud SDK-ի միջոցով: Այն պարունակում է ամբողջական տեղեկատվություն գծապատկեր ավելացնելու և տվյալներ կարգավորելու վերաբերյալ:
Նախապայման
[Ստեղծեք հաշվի API հավատարմագրեր] ( https://kb.aspose.cloud/ hy/total/java/how-to-create-aspose-cloud-apis-account/)
Բեռնել Aspose.Slides Cloud SDK for Java to add a chart
Կարգավորեք Java նախագիծը վերը նշված SDK-ով` շնորհանդեսում գրաֆիկների հետ աշխատելու համար
Java Java-ի վրա հիմնված API-ով PowerPoint-ում սյունակների գծապատկեր ավելացնելու քայլեր
- Նախաձեռնեք SlidesApi-ը հաճախորդի ID-ով և գծապատկեր ավելացնելու գաղտնիքով
- Ներբեռնեք ներկայացման ֆայլը Cloud-ում
- Ստեղծեք նոր գծապատկերի կազմաձևման օբյեկտ՝ օգտագործելով Chart դասը և սահմանեք գծապատկերի տեսակը
- Ավելացրեք գծապատկերի անվանումը և սկզբնավորեք կատեգորիաները գծապատկերի համար
- Նախաձեռնեք տվյալների շարքը գծապատկերի համար, ստեղծեք տվյալների շարքը և ավելացրեք առաջին տվյալների շարքը
- Անհրաժեշտության դեպքում սկզբնավորեք ավելի շատ տվյալների շարքեր գծապատկերի համար
- Ավելացրեք գծապատկերը ներկայացման առաջին սլայդին՝ օգտագործելով CreateShape() մեթոդը և անհրաժեշտության դեպքում ներբեռնեք այն։
Այս քայլերը նկարագրում են ինչպես ստեղծել գծային գծապատկեր PowerPoint-ում Java Java-ի վրա հիմնված API-ով: Ստեղծեք 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); | |
} | |
} |
Այս ծածկագիրը ցույց է տալիս ինչպես գծապատկեր ստեղծել PowerPoint-ում Java Low Code API-ով: Դուք կարող եք սահմանել գծապատկերի տարբեր տեսակներ, ինչպիսիք են ClusteredColumn, Column3D, ClusteredCone և Line և այլն, օգտագործելով ChartTypeEnum և այլ պարամետրեր Chart օբյեկտում, ներառյալ ShowDataLabelsOverMaximum, BackWall, SideWall, Floor և Legend և այլն:
Այս հոդվածը մեզ սովորեցրել է PowerPoint-ում գծային գծապատկերի ստեղծումը: HTML-ը PowerPoint-ի փոխարկելու համար տես Փոխարկեք HTML-ը PowerPoint-ի NET REST API-ի միջոցով-ի հոդվածը: