Java 在PPT中創(chuàng)建散點(diǎn)圖的實(shí)現(xiàn)示例
本文將以Java代碼示例展示如何在PPT幻燈片中創(chuàng)建散點(diǎn)圖表。
創(chuàng)建圖表前
需要在Java程序中導(dǎo)入用于操作PPT的jar包 Free Spire.Presentation for Java。可參考如下兩種方法導(dǎo)入:
方法1:手動(dòng)導(dǎo)入jar包。需下載jar包到本地,并解壓,找到lib文件夾下的jar文件。然后按照如下步驟執(zhí)行,導(dǎo)入:
方法2:maven倉(cāng)庫(kù)下載導(dǎo)入。需在pom.xml文件中配置maven倉(cāng)庫(kù)路徑,并指定依賴。配置內(nèi)容如下:
<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>
創(chuàng)建圖表時(shí)
通過指定數(shù)據(jù)源,并在幻燈片中的指定坐標(biāo)位置插入圖表。該Jar包提供了ShapeCollection.appendChart(ChartType type, Rectangle2D rectangle, boolean init)方法向幻燈片添加特定類型的圖表,ChartType枚舉預(yù)定義了73種圖表類型,包括但不限于散點(diǎn)圖、柱圖、餅圖等。
本次創(chuàng)建散點(diǎn)圖,主要通過以下步驟完成:
- 創(chuàng)建 Presentation 類的實(shí)例。
- 使用 ShapeCollection.appendChart() 方法將散點(diǎn)圖附加到特定的幻燈片。
- 通過 ChartData.get().setValue() 方法設(shè)置圖表數(shù)據(jù)。
- 使用 IChart 接口提供的方法設(shè)置圖表標(biāo)題、坐標(biāo)軸標(biāo)題、系列標(biāo)簽等。
- 設(shè)置網(wǎng)格線樣式和數(shù)據(jù)點(diǎn)線樣式。
- 使用 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{ //創(chuàng)建Presentation類的實(shí)例 Presentation presentation = new Presentation(); presentation.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9); //添加散點(diǎn)圖表到第一張幻燈片 IChart chart = presentation.getSlides().get(0).getShapes().appendChart(ChartType.SCATTER_MARKERS,new Rectangle2D.Float(180, 80, 550, 320),false); //設(shè)置圖表標(biāo)題 chart.getChartTitle().getTextProperties().setText("散點(diǎn)圖表"); chart.getChartTitle().getTextProperties().isCentered(true); chart.getChartTitle().setHeight(20f); chart.hasTitle(true); //設(shè)置圖表數(shù)據(jù)源 Double[] xData = 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]); } //設(shè)置系列標(biāo)簽 chart.getSeries().setSeriesLabel(chart.getChartData().get("B1","B1")); //設(shè)置X和Y軸值 chart.getSeries().get(0).setXValues(chart.getChartData().get("A2","A5")); chart.getSeries().get(0).setYValues(chart.getChartData().get("B2","B5")); //添加數(shù)據(jù)標(biāo)簽 for (int i = 0; i < 4; i++) { ChartDataLabel dataLabel = chart.getSeries().get(0).getDataLabels().add(); dataLabel.setLabelValueVisible(true); } //設(shè)置主軸標(biāo)題和次軸標(biāo)題 chart.getPrimaryValueAxis().hasTitle(true); chart.getPrimaryValueAxis().getTitle().getTextProperties().setText("X-軸 標(biāo)題"); chart.getSecondaryValueAxis().hasTitle(true); chart.getSecondaryValueAxis().getTitle().getTextProperties().setText("Y-軸 標(biāo)題"); //設(shè)置網(wǎng)格線 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); //設(shè)置數(shù)據(jù)點(diǎn)線 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(); } }
圖表效果圖:
其他注意事項(xiàng)
本次代碼中的PPT結(jié)果文檔路徑為IDEA程序項(xiàng)目文件夾路徑,如F:\IDEAProject\Chart_PPT\ScatterChart.pptx ,路徑也可以自定義。
到此這篇關(guān)于Java 在PPT中創(chuàng)建散點(diǎn)圖的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)Java PPT創(chuàng)建散點(diǎn)圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Java 將PPT幻燈片轉(zhuǎn)為HTML文件的實(shí)現(xiàn)思路
- Java 給PPT添加動(dòng)畫效果的示例
- Java如何為 PPT 中的圖形添加陰影效果
- Java 在PPT中添加文本和圖片超鏈接的實(shí)現(xiàn)方法
- Java 在PPT中添加混合圖表過程詳解
- Java 添加文本框到PPT幻燈片過程解析
- 如何使用Java讀取PPT文本和圖片
- Java如何在PPT中繪制圖形
- java實(shí)現(xiàn)PPT轉(zhuǎn)PDF出現(xiàn)中文亂碼問題的解決方法
- java使用poi讀取ppt文件和poi讀取excel、word示例
- java讀取word-excel-ppt文件代碼
相關(guān)文章
SpringBoot 利用RestTemplate http測(cè)試
這篇文章主要介紹了SpringBoot 利用RestTemplate http測(cè)試,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08IDEA?一直scanning?files?to?index的四種完美解決方法(VIP典藏版)
這篇文章主要介紹了IDEA?一直scanning?files?to?index的四種完美解決方法(VIP典藏版),推薦第四種方法,第四種方法摸索研究后得出,親測(cè)好用,需要的朋友參考下吧2023-10-10深入淺出重構(gòu)Mybatis與Spring集成的SqlSessionFactoryBean(上)
通常來講,重構(gòu)是指不改變功能的情況下優(yōu)化代碼,但本文所說的重構(gòu)也包括了添加功能。這篇文章主要介紹了重構(gòu)Mybatis與Spring集成的SqlSessionFactoryBean(上)的相關(guān)資料,需要的朋友可以參考下2016-11-11SpringBoot項(xiàng)目實(shí)現(xiàn)統(tǒng)一異常處理的最佳方案
在前后端分離的項(xiàng)目開發(fā)過程中,我們通常會(huì)對(duì)數(shù)據(jù)返回格式進(jìn)行統(tǒng)一的處理,這樣可以方便前端人員取數(shù)據(jù),后端發(fā)生異常時(shí)同樣會(huì)使用此格式將異常信息返回給前端,本文介紹了如何在SpringBoot項(xiàng)目中實(shí)現(xiàn)統(tǒng)一異常處理,如有錯(cuò)誤,還望批評(píng)指正2024-02-02springboot?vue接口測(cè)試定義編輯功能的實(shí)現(xiàn)
這篇文章主要為大家介紹了springboot?vue接口測(cè)試定義編輯功能的實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05java.io.File的renameTo方法移動(dòng)文件失敗的解決方案
這篇文章主要介紹了java.io.File的renameTo方法移動(dòng)文件失敗的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07SpringBoot實(shí)現(xiàn)簡(jiǎn)單的登錄注冊(cè)的項(xiàng)目實(shí)戰(zhàn)
本文主要介紹了SpringBoot實(shí)現(xiàn)簡(jiǎn)單的登錄注冊(cè)的項(xiàng)目實(shí)戰(zhàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03