Java 中的 PPT 格式圖表
使用 Java API 的服務器端 Aspose.Slides 的原生和高性能 PPT 文檔圖表,無需使用 Microsoft 或 Adobe PDF 等任何軟件。
如何使用 Java 創建 PPT 文件圖表
為了創建 PPT 文件圖表,我們將使用
API 是一個功能豐富、功能強大且易於使用的 Java 平台圖表 API。您可以直接從
並通過將以下配置添加到 pom.xml 將其安裝在基於 Maven 的項目中。
存儲庫
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://releases.aspose.com/java/repo/</url>
</repository>
依賴
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-slides</artifactId>
<version>version of aspose-slides API</version>
<classifier>jdk17</classifier>
</dependency>
在 Java 中創建 PPT 文件圖表的步驟
只需幾行代碼即可使用 Aspose.Slides for Java API 繪製基本文檔圖表。
實例化 Presentation 類對象。
獲取第一張幻燈片。
添加帶有默認數據的圖表。
設置圖表標題。
將第一個系列設置為顯示值。
設置圖表數據表的索引。
獲取圖表數據工作表。
刪除默認生成的系列和類別。
添加新的系列和類別。
採取第一個圖表系列。
填充系列數據。
為系列設置填充顏色。
為新系列的每個類別創建自定義標籤。
用圖表保存演示文稿。
系統要求
Aspose.Slides for Java 支持所有主要平台和操作系統。請確保您具有以下先決條件。
- Microsoft Windows 或與 JSP/JSF 應用程序和桌面應用程序的 Java 運行時環境兼容的操作系統。
- 直接從 Java 獲取最新版本的 Aspose.Slides Maven 。
創建 PPT 文件圖表 - Java
//Instantiate Presentation class
Presentation pres = new Presentation();
//Access first slide
ISlide sld = pres.getSlides().get_Item(0);
// Add chart with default data
IChart chart = sld.getShapes().addChart(ChartType.ClusteredColumn, 0, 0, 500, 500);
//Setting chart Title
//chart.getChartTitle().TextFrameForOverriding.Text = "Title";
chart.getChartTitle().addTextFrameForOverriding("Title");
chart.getChartTitle().getTextFrameForOverriding().getTextFrameFormat().setCenterText(NullableBool.True);
chart.getChartTitle().setHeight(20);
chart.hasTitle(true);
//Set first series to Show Values
chart.getChartData().getSeries().get_Item(0).getLabels().getDefaultDataLabelFormat().setShowValue(true);
//Setting the index of chart data sheet
int defaultWorksheetIndex = 0;
//Getting the chart data worksheet
IChartDataWorkbook fact = chart.getChartData().getChartDataWorkbook();
//Delete default generated series and categories
chart.getChartData().getSeries().clear();
chart.getChartData().getCategories().clear();
int s = chart.getChartData().getSeries().size();
s = chart.getChartData().getCategories().size();
//Adding new series
chart.getChartData().getSeries().add(fact.getCell(defaultWorksheetIndex, 0, 1, "Series 1"), chart.getType());
chart.getChartData().getSeries().add(fact.getCell(defaultWorksheetIndex, 0, 2, "Series 2"), chart.getType());
//Adding new categories
chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 1, 0, "Caetegoty 1"));
chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 2, 0, "Caetegoty 2"));
chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 3, 0, "Caetegoty 3"));
//Take first chart series
IChartSeries series = chart.getChartData().getSeries().get_Item(0);
//Now populating series data
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 1, 1, 20));
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 2, 1, 50));
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 3, 1, 30));
//Setting fill color for series
series.getFormat().getFill().setFillType(FillType.Solid);
series.getFormat().getFill().getSolidFillColor().setColor(java.awt.Color.RED);
//Take second chart series
series = chart.getChartData().getSeries().get_Item(1);
//Now populating series data
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 1, 2, 30));
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 2, 2, 10));
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 3, 2, 60));
//Setting fill color for series
series.getFormat().getFill().setFillType(FillType.Solid);
series.getFormat().getFill().getSolidFillColor().setColor(java.awt.Color.GREEN);
//create custom labels for each of categories for new series
//first label will be show Category name
IDataLabel lbl = series.getDataPoints().get_Item(0).getLabel();
lbl.getDataLabelFormat().setShowCategoryName(true);
lbl = series.getDataPoints().get_Item(1).getLabel();
lbl.getDataLabelFormat().setShowSeriesName(true);
//Show value for third label
lbl = series.getDataPoints().get_Item(2).getLabel();
lbl.getDataLabelFormat().setShowValue(true);
lbl.getDataLabelFormat().setShowSeriesName(true);
lbl.getDataLabelFormat().setSeparator("/");
//Save presentation with chart
pres.save("AsposeChart.ppt",SaveFormat.Ppt);
Online PPT Chart Creation Live Demos
Generate PPT documents charts right now by visiting our Live Demos website . The live demo has the following benefits
PPT 什麼是 PPT 文件格式
A file with PPT extension represents PowerPoint file that consists of a collection of slides for displaying as SlideShow. It specifies the Binary File Format used by Microsoft PowerPoint 97-2003. A PPT file can contain several different types of information such as text, bulleted points, images, multimedia and other embedded OLE objects. Microsoft came up with newer file format for PowerPoint, known as PPTX, from 2007 onwards that is based on Office OpenXML and is different from this binary file format. Several other application programs such as OpenOffice Impress and Apple Keynote can also create PPT files.
閱讀更多