Java實(shí)現(xiàn)在PowerPoint中創(chuàng)建柱狀圖和折線圖實(shí)現(xiàn)數(shù)據(jù)可視化
在瞬息萬(wàn)變的職場(chǎng)和學(xué)習(xí)環(huán)境中,高效的數(shù)據(jù)可視化是講述數(shù)據(jù)故事、傳遞核心洞察的關(guān)鍵。無(wú)論是制作項(xiàng)目報(bào)告、市場(chǎng)分析還是學(xué)術(shù)演示,柱狀圖和折線圖都是最常用且直觀的圖表類型。然而,手動(dòng)在 PowerPoint 中創(chuàng)建和更新大量圖表不僅耗時(shí),還容易出錯(cuò)。本文將為您揭示如何利用 Spire.Presentation for Java 庫(kù),輕松實(shí)現(xiàn) PowerPoint 中柱狀圖和折線圖的自動(dòng)化創(chuàng)建,徹底告別繁瑣的手動(dòng)操作!
Spire.Presentation for Java 庫(kù)介紹與安裝
Spire.Presentation for Java 是一個(gè)功能強(qiáng)大的 Java API,專為創(chuàng)建、讀取、編輯和轉(zhuǎn)換 PowerPoint 演示文稿而設(shè)計(jì)。它支持 PPT、PPTX 等多種格式,提供了豐富的對(duì)象模型,讓開(kāi)發(fā)者能夠以編程方式全面控制 PowerPoint 文檔的各個(gè)方面,包括幻燈片管理、形狀操作、文本處理以及本文重點(diǎn)關(guān)注的圖表創(chuàng)建與編輯。其優(yōu)勢(shì)在于無(wú)需安裝 Microsoft Office 即可獨(dú)立運(yùn)行,極大地提升了自動(dòng)化處理的靈活性和效率。
Maven 依賴配置
在您的 pom.xml 文件中添加以下 Maven 依賴,即可將 Spire.Presentation 庫(kù)引入您的項(xiàng)目:
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.presentation</artifactId>
<version>10.11.4</version>
</dependency>
</dependencies>
提示: 您可以訪問(wèn) E-iceblue 官方網(wǎng)站獲取最新版本的依賴信息或下載 JAR 包手動(dòng)添加到項(xiàng)目中。
如何在 PowerPoint 中創(chuàng)建柱狀圖
柱狀圖是比較不同類別數(shù)據(jù)最常用的圖表類型。通過(guò) Spire.Presentation,您可以輕松地自動(dòng)化創(chuàng)建和填充柱狀圖。
詳細(xì)步驟
創(chuàng)建演示文稿對(duì)象: 實(shí)例化 Presentation 類,代表一個(gè)新的 PowerPoint 文檔。
添加圖表: 調(diào)用幻燈片對(duì)象的 getShapes().appendChart() 方法,指定圖表類型為 ChartType.COLUMN_CLUSTERED(簇狀柱形圖)或其他柱狀圖類型,并設(shè)置圖表的位置和大小。
設(shè)置圖表數(shù)據(jù):
- 通過(guò) chart.getChartData() 獲取圖表數(shù)據(jù)對(duì)象。
- 使用 chart.getChartData().get(row, column).setText() 設(shè)置圖表的數(shù)據(jù)標(biāo)簽,例如類別名稱和系列名稱。
- 使用 chart.getCategories().setCategoryLabels() 設(shè)置橫坐標(biāo)(類別)標(biāo)簽。
- 使用 chart.getSeries().setSeriesLabel() 設(shè)置圖例(系列)標(biāo)簽。
- 使用 chart.getSeries().get(index).setValues() 設(shè)置每個(gè)系列的數(shù)據(jù)值。
設(shè)置圖表屬性:
- 設(shè)置圖表標(biāo)題:chart.getChartTitle().getTextProperties().setText()。
- 設(shè)置軸標(biāo)簽等。
保存演示文稿: 調(diào)用 presentation.saveToFile() 方法將 PPT 文檔保存到指定路徑。
Java 代碼示例 (柱狀圖)
import com.spire.presentation.*;
import com.spire.pdf.tables.table.*;
import com.spire.presentation.charts.*;
import com.spire.presentation.drawing.FillFormatType;
import java.awt.geom.Rectangle2D;
import java.lang.Object;
public class CreateChart {
public static void main(String[] args) throws Exception {
//實(shí)例化一個(gè)Presentation對(duì)象
Presentation presentation = new Presentation();
//插入柱形圖
Rectangle2D.Double rect = new Rectangle2D.Double(40, 100, 550, 320);
IChart chart = null;
chart = presentation.getSlides().get(0).getShapes().appendChart(ChartType.COLUMN_CLUSTERED, rect);
//添加表名
chart.getChartTitle().getTextProperties().setText("銷售報(bào)表");
chart.getChartTitle().getTextProperties().isCentered(true);
chart.getChartTitle().setHeight(30);
chart.hasTitle(true);
//創(chuàng)建后臺(tái)數(shù)據(jù)表
DataTable dataTable = new DataTable();
dataTable.getColumns().add(new DataColumn("銷售額", DataTypes.DATATABLE_STRING));
dataTable.getColumns().add(new DataColumn("谷物", DataTypes.DATATABLE_INT));
dataTable.getColumns().add(new DataColumn("糧油", DataTypes.DATATABLE_INT));
dataTable.getColumns().add(new DataColumn("百貨", DataTypes.DATATABLE_INT));
DataRow row1 = dataTable.newRow();
row1.setString("銷售額", "門(mén)店1");
row1.setInt("谷物", 250);
row1.setInt("糧油", 150);
row1.setInt("百貨", 99);
DataRow row2 = dataTable.newRow();
row2.setString("銷售額", "門(mén)店2");
row2.setInt("谷物", 270);
row2.setInt("糧油", 150);
row2.setInt("百貨", 99);
DataRow row3 = dataTable.newRow();
row3.setString("銷售額", "門(mén)店3");
row3.setInt("谷物", 310);
row3.setInt("糧油", 120);
row3.setInt("百貨", 49);
DataRow row4 = dataTable.newRow();
row4.setString("銷售額", "門(mén)店4");
row4.setInt("谷物", 330);
row4.setInt("糧油", 120);
row4.setInt("百貨", 49);
DataRow row5 = dataTable.newRow();
row5.setString("銷售額", "門(mén)店5");
row5.setInt("谷物", 360);
row5.setInt("糧油", 150);
row5.setInt("百貨", 141);
DataRow row6 = dataTable.newRow();
row6.setString("銷售額", "門(mén)店6");
row6.setInt("谷物", 380);
row6.setInt("糧油", 150);
row6.setInt("百貨", 135);
dataTable.getRows().add(row1);
dataTable.getRows().add(row2);
dataTable.getRows().add(row3);
dataTable.getRows().add(row4);
dataTable.getRows().add(row5);
dataTable.getRows().add(row6);
//將數(shù)據(jù)寫(xiě)入圖表
for (int c = 0; c < dataTable.getColumns().size(); c++) {
chart.getChartData().get(0, c).setText(dataTable.getColumns().get(c).getColumnName());
}
for (int r = 0; r < dataTable.getRows().size(); r++) {
Object[] datas = dataTable.getRows().get(r).getArrayList();
for (int c = 0; c < datas.length; c++) {
chart.getChartData().get(r + 1, c).setValue(datas[c]);
}
}
//設(shè)置系列標(biāo)簽
chart.getSeries().setSeriesLabel(chart.getChartData().get("B1", "D1"));
//設(shè)置類別標(biāo)簽
chart.getCategories().setCategoryLabels(chart.getChartData().get("A2", "A7"));
//為各個(gè)系列賦值
chart.getSeries().get(0).setValues(chart.getChartData().get("B2", "B7"));
chart.getSeries().get(1).setValues(chart.getChartData().get("C2", "C7"));
chart.getSeries().get(2).setValues(chart.getChartData().get("D2", "D7"));
chart.getSeries().get(2).getFill().setFillType(FillFormatType.SOLID);
chart.getSeries().get(2).getFill().getSolidColor().setKnownColor(KnownColors.LIGHT_BLUE);
//設(shè)置系列重疊
chart.setOverLap(-50);
//設(shè)置類別間距
chart.setGapDepth(200);
//保存文檔
presentation.saveToFile("output/CreateChart.pptx", FileFormat.PPTX_2010);
}
}
如何在 PowerPoint 中創(chuàng)建折線圖
折線圖常用于展示數(shù)據(jù)隨時(shí)間變化的趨勢(shì)。創(chuàng)建折線圖的步驟與柱狀圖類似,只需調(diào)整圖表類型和數(shù)據(jù)綁定方式。
詳細(xì)步驟
創(chuàng)建演示文稿對(duì)象: 實(shí)例化 Presentation 類。
添加圖表: 調(diào)用幻燈片對(duì)象的 getShapes().appendChart() 方法,指定圖表類型為 ChartType.LINE,并設(shè)置圖表的位置和大小。
設(shè)置圖表數(shù)據(jù):
- 設(shè)置數(shù)據(jù)表頭、類別標(biāo)簽和系列標(biāo)簽。
- 設(shè)置每個(gè)系列的數(shù)據(jù)值。
設(shè)置圖表屬性:設(shè)置圖表標(biāo)題、圖例等。
保存演示文稿: 調(diào)用 presentation.saveToFile() 方法保存 PPT 文檔。
Java 代碼示例 (折線圖)
import com.spire.presentation.FileFormat;
import com.spire.presentation.Presentation;
import com.spire.presentation.SlideSizeType;
import com.spire.presentation.charts.ChartLegendPositionType;
import com.spire.presentation.charts.ChartType;
import com.spire.presentation.charts.IChart;
import java.awt.geom.Rectangle2D;
public class LineChart {
public static void main(String[] args) throws Exception {
//創(chuàng)建Presentation對(duì)象
Presentation presentation = new Presentation();
presentation.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9);
//插入折線圖
Rectangle2D.Double rect = new Rectangle2D.Double(100, 50, 600, 430);
IChart chart = presentation.getSlides().get(0).getShapes().appendChart(ChartType.LINE, rect);
//設(shè)置圖表標(biāo)題
chart.getChartTitle().getTextProperties().setText("產(chǎn)品月銷量趨勢(shì)");
chart.getChartTitle().getTextProperties().isCentered(true);
chart.getChartTitle().setHeight(30);
chart.hasTitle(true);
//設(shè)置軸標(biāo)題
chart.getPrimaryCategoryAxis().getTitle().getTextProperties().setText("月份");
chart.getPrimaryCategoryAxis().hasTitle(true);
chart.getPrimaryValueAxis().getTitle().getTextProperties().setText("銷量");
chart.getPrimaryValueAxis().hasTitle(true);
//寫(xiě)入圖表數(shù)據(jù)
chart.getChartData().get(0,0).setText("月份");
chart.getChartData().get(1,0).setText("一月");
chart.getChartData().get(2,0).setText("二月");
chart.getChartData().get(3,0).setText("三月");
chart.getChartData().get(4,0).setText("四月");
chart.getChartData().get(5,0).setText("五月");
chart.getChartData().get(6,0).setText("六月");
chart.getChartData().get(0,1).setText("臺(tái)式機(jī)");
chart.getChartData().get(1,1).setNumberValue(80);
chart.getChartData().get(2,1).setNumberValue(45);
chart.getChartData().get(3,1).setNumberValue(25);
chart.getChartData().get(4,1).setNumberValue(20);
chart.getChartData().get(5,1).setNumberValue(10);
chart.getChartData().get(6,1).setNumberValue(5);
chart.getChartData().get(0,2).setText("筆記本");
chart.getChartData().get(1,2).setNumberValue(30);
chart.getChartData().get(2,2).setNumberValue(25);
chart.getChartData().get(3,2).setNumberValue(35);
chart.getChartData().get(4,2).setNumberValue(50);
chart.getChartData().get(5,2).setNumberValue(45);
chart.getChartData().get(6,2).setNumberValue(55);
chart.getChartData().get(0,3).setText("平板");
chart.getChartData().get(1,3).setNumberValue(10);
chart.getChartData().get(2,3).setNumberValue(15);
chart.getChartData().get(3,3).setNumberValue(20);
chart.getChartData().get(4,3).setNumberValue(35);
chart.getChartData().get(5,3).setNumberValue(60);
chart.getChartData().get(6,3).setNumberValue(95);
//設(shè)置系列標(biāo)簽
chart.getSeries().setSeriesLabel(chart.getChartData().get("B1", "D1"));
//設(shè)置分類標(biāo)簽
chart.getCategories().setCategoryLabels(chart.getChartData().get("A2", "A7"));
//設(shè)置系列數(shù)據(jù)區(qū)域
chart.getSeries().get(0).setValues(chart.getChartData().get("B2", "B7"));
chart.getSeries().get(1).setValues(chart.getChartData().get("C2", "C7"));
chart.getSeries().get(2).setValues(chart.getChartData().get("D2", "D7"));
//在數(shù)據(jù)標(biāo)簽中顯示數(shù)據(jù)
chart.getSeries().get(0).getDataLabels().setLabelValueVisible(true);
chart.getSeries().get(1).getDataLabels().setLabelValueVisible(true);
chart.getSeries().get(2).getDataLabels().setLabelValueVisible(true);
//設(shè)置圖例位置
chart.getChartLegend().setPosition(ChartLegendPositionType.TOP);
//保存文檔
presentation.saveToFile("LineChart.pptx", FileFormat.PPTX_2013);
}
}
總結(jié)
通過(guò)本文的詳細(xì)教程,您已經(jīng)掌握了如何利用 Spire.Presentation for Java 庫(kù)在 PowerPoint 中自動(dòng)化創(chuàng)建柱狀圖和折線圖。這個(gè)強(qiáng)大的工具能夠?qū)⒛鷱闹貜?fù)性的手動(dòng)操作中解放出來(lái),無(wú)論是生成每日?qǐng)?bào)告、每月總結(jié)還是復(fù)雜的年度演示,都能顯著提高工作效率,確保數(shù)據(jù)展示的準(zhǔn)確性和一致性。Spire.Presentation for Java 不僅限于圖表創(chuàng)建,其在PPT自動(dòng)化處理方面的潛力巨大,鼓勵(lì)您進(jìn)一步探索它在更復(fù)雜數(shù)據(jù)呈現(xiàn)場(chǎng)景中的應(yīng)用,讓數(shù)據(jù)可視化變得前所未有的簡(jiǎn)單和高效!
以上就是Java實(shí)現(xiàn)在PowerPoint中創(chuàng)建柱狀圖和折線圖實(shí)現(xiàn)數(shù)據(jù)可視化的詳細(xì)內(nèi)容,更多關(guān)于Java PPT創(chuàng)建柱狀圖和折線圖的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Jmeter對(duì)響應(yīng)數(shù)據(jù)實(shí)現(xiàn)斷言代碼實(shí)例
這篇文章主要介紹了Jmeter對(duì)響應(yīng)數(shù)據(jù)實(shí)現(xiàn)斷言代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
Java中char數(shù)組(字符數(shù)組)與字符串String類型的轉(zhuǎn)換方法
這篇文章主要介紹了Java中char數(shù)組(字符數(shù)組)與字符串String類型的轉(zhuǎn)換方法,涉及Java中toCharArray與valueOf方法的使用技巧,需要的朋友可以參考下2015-12-12
JAVA調(diào)用SAP WEBSERVICE服務(wù)實(shí)現(xiàn)流程圖解
這篇文章主要介紹了JAVA調(diào)用SAP WEBSERVICE服務(wù)實(shí)現(xiàn)流程圖解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10
springBoot整合Eureka啟動(dòng)失敗的解決方案
這篇文章主要介紹了springBoot整合Eureka啟動(dòng)失敗的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07

