本文將以Java代碼示例展示如何在PPT幻燈片中創建散點圖表,
創建圖表前
需要在Java程式中匯入用于操作PPT的jar包 Free Spire.Presentation for Java,可參考如下兩種方法匯入:
方法1:手動匯入jar包,需下載jar包到本地,并解壓,找到lib檔案夾下的jar檔案,然后按照如下步驟執行,匯入:



方法2:maven倉庫下載匯入,需在pom.xml檔案中配置maven倉庫路徑,并指定依賴,配置內容如下:
<repositories> <repository> <id>com.e-iceblue</id> <url>https://repo.e-iceblue.cn/repository/maven-public/</url> </repository> </repositories> <dependencies> <dependency> <groupId> e-iceblue </groupId> <artifactId>spire.presentation.free</artifactId> <version>3.9.0</version> </dependency> </dependencies>
創建圖表時
通過指定資料源,并在幻燈片中的指定坐標位置插入圖表,該Jar包提供了ShapeCollection.appendChart(ChartType type, Rectangle2D rectangle, boolean init)方法向幻燈片添加特定型別的圖表,ChartType列舉預定義了73種圖表型別,包括但不限于散點圖、柱圖、餅圖等,
本次創建散點圖,主要通過以下步驟完成:
- 創建 Presentation 類的實體,
- 使用 ShapeCollection.appendChart() 方法將散點圖附加到特定的幻燈片,
- 通過 ChartData.get().setValue() 方法設定圖表資料,
- 使用 IChart 介面提供的方法設定圖表標題、坐標軸標題、系列標簽等,
- 設定網格線樣式和資料點線樣式,
- 使用 Presentation.saveToFile() 方法將檔案保存到指定路徑,
Java代碼示例
import com.spire.presentation.FileFormat; import com.spire.presentation.Presentation; import com.spire.presentation.SlideSizeType; import com.spire.presentation.TextLineStyle; import com.spire.presentation.charts.ChartType; import com.spire.presentation.charts.IChart; import com.spire.presentation.charts.entity.ChartDataLabel; import com.spire.presentation.drawing.FillFormatType; import java.awt.*; import java.awt.geom.Rectangle2D; public class ScatterChart { public static void main(String[] args) throws Exception{ //創建Presentation類的實體 Presentation presentation = new Presentation(); presentation.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9); //添加散點圖表到第一張幻燈片 IChart chart = presentation.getSlides().get(0).getShapes().appendChart(ChartType.SCATTER_MARKERS,new Rectangle2D.Float(180, 80, 550, 320),false); //設定圖表標題 chart.getChartTitle().getTextProperties().setText("散點圖表"); chart.getChartTitle().getTextProperties().isCentered(true); chart.getChartTitle().setHeight(20f); chart.hasTitle(true); //設定圖表資料源 Double[] xData = https://www.cnblogs.com/Yesi/p/new Double[] { 1.0, 2.4, 5.0, 8.9 }; Double[] yData = new Double[] { 5.3, 15.2, 6.7, 8.0 }; chart.getChartData().get(0,0).setText("X-值"); chart.getChartData().get(0,1).setText("Y-值"); for (int i = 0; i < xData.length; i++) { chart.getChartData().get(i+1,0).setValue(xData[i]); chart.getChartData().get(i+1,1).setValue(yData[i]); } //設定系列標簽 chart.getSeries().setSeriesLabel(chart.getChartData().get("B1","B1")); //設定X和Y軸值 chart.getSeries().get(0).setXValues(chart.getChartData().get("A2","A5")); chart.getSeries().get(0).setYValues(chart.getChartData().get("B2","B5")); //添加資料標簽 for (int i = 0; i < 4; i++) { ChartDataLabel dataLabel = chart.getSeries().get(0).getDataLabels().add(); dataLabel.setLabelValueVisible(true); } //設定主軸標題和次軸標題 chart.getPrimaryValueAxis().hasTitle(true); chart.getPrimaryValueAxis().getTitle().getTextProperties().setText("X-軸 標題"); chart.getSecondaryValueAxis().hasTitle(true); chart.getSecondaryValueAxis().getTitle().getTextProperties().setText("Y-軸 標題"); //設定網格線 chart.getSecondaryValueAxis().getMajorGridTextLines().setFillType(FillFormatType.SOLID); chart.getSecondaryValueAxis().getMajorGridTextLines().setStyle(TextLineStyle.THIN_THIN); chart.getSecondaryValueAxis().getMajorGridTextLines().getSolidFillColor().setColor(Color.GRAY); chart.getPrimaryValueAxis().getMajorGridTextLines().setFillType(FillFormatType.NONE); //設定資料點線 chart.getSeries().get(0).getLine().setFillType(FillFormatType.SOLID); chart.getSeries().get(0).getLine().setWidth(0.1f); chart.getSeries().get(0).getLine().getSolidFillColor().setColor(Color.BLUE); //保存檔案 presentation.saveToFile("ScatterChart.pptx", FileFormat.PPTX_2013); presentation.dispose(); } }
圖表效果圖:

其他注意事項
本次代碼中的PPT結果檔案路徑為IDEA程式專案檔案夾路徑,如F:\IDEAProject\Chart_PPT\ScatterChart.pptx ,路徑也可以自定義,
—End—
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/348148.html
標籤:Java
