博客 / 詳情

返回

在 PowerPoint 中創建柱狀圖和折線圖:用 Java 實現自動化數據可視化

在瞬息萬變的職場和學習環境中,高效的數據可視化是講述數據故事、傳遞核心洞察的關鍵。無論是製作項目報告、市場分析還是學術演示,柱狀圖和折線圖都是最常用且直觀的圖表類型。然而,手動在 PowerPoint 中創建和更新大量圖表不僅耗時,還容易出錯。本文將為您揭示如何利用 Spire.Presentation for Java 庫,輕鬆實現 PowerPoint 中柱狀圖和折線圖的自動化創建,徹底告別繁瑣的手動操作!

Spire.Presentation for Java 庫介紹與安裝

Spire.Presentation for Java 是一個功能強大的 Java API,專為創建、讀取、編輯和轉換 PowerPoint 演示文稿而設計。它支持 PPT、PPTX 等多種格式,提供了豐富的對象模型,讓開發者能夠以編程方式全面控制 PowerPoint 文檔的各個方面,包括幻燈片管理、形狀操作、文本處理以及本文重點關注的圖表創建與編輯。其優勢在於無需安裝 Microsoft Office 即可獨立運行,極大地提升了自動化處理的靈活性和效率。

Maven 依賴配置

在您的 pom.xml 文件中添加以下 Maven 依賴,即可將 Spire.Presentation 庫引入您的項目:

<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>
提示: 您可以訪問 E-iceblue 官方網站獲取最新版本的依賴信息或下載 JAR 包手動添加到項目中。

如何在 PowerPoint 中創建柱狀圖

柱狀圖是比較不同類別數據最常用的圖表類型。通過 Spire.Presentation,您可以輕鬆地自動化創建和填充柱狀圖。

詳細步驟

  • 創建演示文稿對象: 實例化 Presentation 類,代表一個新的 PowerPoint 文檔。
  • 添加圖表: 調用幻燈片對象的 getShapes().appendChart() 方法,指定圖表類型為 ChartType.COLUMN_CLUSTERED(簇狀柱形圖)或其他柱狀圖類型,並設置圖表的位置和大小。
  • 設置圖表數據:

    • 通過 chart.getChartData() 獲取圖表數據對象。
    • 使用 chart.getChartData().get(row, column).setText() 設置圖表的數據標籤,例如類別名稱和系列名稱。
    • 使用 chart.getCategories().setCategoryLabels() 設置橫座標(類別)標籤。
    • 使用 chart.getSeries().setSeriesLabel() 設置圖例(系列)標籤。
    • 使用 chart.getSeries().get(index).setValues() 設置每個系列的數據值。
  • 設置圖表屬性:

    • 設置圖表標題:chart.getChartTitle().getTextProperties().setText()。
    • 設置軸標籤等。
  • 保存演示文稿: 調用 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 {

        //實例化一個Presentation對象
        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("銷售報表");
        chart.getChartTitle().getTextProperties().isCentered(true);
        chart.getChartTitle().setHeight(30);
        chart.hasTitle(true);

        //創建後台數據表
        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("銷售額", "門店1");
        row1.setInt("穀物", 250);
        row1.setInt("糧油", 150);
        row1.setInt("百貨", 99);
        DataRow row2 = dataTable.newRow();
        row2.setString("銷售額", "門店2");
        row2.setInt("穀物", 270);
        row2.setInt("糧油", 150);
        row2.setInt("百貨", 99);
        DataRow row3 = dataTable.newRow();
        row3.setString("銷售額", "門店3");
        row3.setInt("穀物", 310);
        row3.setInt("糧油", 120);
        row3.setInt("百貨", 49);
        DataRow row4 = dataTable.newRow();
        row4.setString("銷售額", "門店4");
        row4.setInt("穀物", 330);
        row4.setInt("糧油", 120);
        row4.setInt("百貨", 49);
        DataRow row5 = dataTable.newRow();
        row5.setString("銷售額", "門店5");
        row5.setInt("穀物", 360);
        row5.setInt("糧油", 150);
        row5.setInt("百貨", 141);
        DataRow row6 = dataTable.newRow();
        row6.setString("銷售額", "門店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);

        //將數據寫入圖表
        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]);

            }
        }

        //設置系列標籤
        chart.getSeries().setSeriesLabel(chart.getChartData().get("B1", "D1"));

        //設置類別標籤
        chart.getCategories().setCategoryLabels(chart.getChartData().get("A2", "A7"));

        //為各個系列賦值
        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);

        //設置系列重疊
        chart.setOverLap(-50);

        //設置類別間距
        chart.setGapDepth(200);

        //保存文檔
        presentation.saveToFile("output/CreateChart.pptx", FileFormat.PPTX_2010);

    }
}

如何在 PowerPoint 中創建折線圖

折線圖常用於展示數據隨時間變化的趨勢。創建折線圖的步驟與柱狀圖類似,只需調整圖表類型和數據綁定方式。

詳細步驟

  • 創建演示文稿對象: 實例化 Presentation 類。
  • 添加圖表: 調用幻燈片對象的 getShapes().appendChart() 方法,指定圖表類型為 ChartType.LINE,並設置圖表的位置和大小。
  • 設置圖表數據:

    • 設置數據表頭、類別標籤和系列標籤。
    • 設置每個系列的數據值。
  • 設置圖表屬性:設置圖表標題、圖例等。
  • 保存演示文稿: 調用 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 {

        //創建Presentation對象
        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);

        //設置圖表標題
        chart.getChartTitle().getTextProperties().setText("產品月銷量趨勢");
        chart.getChartTitle().getTextProperties().isCentered(true);
        chart.getChartTitle().setHeight(30);
        chart.hasTitle(true);

        //設置軸標題
        chart.getPrimaryCategoryAxis().getTitle().getTextProperties().setText("月份");
        chart.getPrimaryCategoryAxis().hasTitle(true);
        chart.getPrimaryValueAxis().getTitle().getTextProperties().setText("銷量");
        chart.getPrimaryValueAxis().hasTitle(true);

        //寫入圖表數據
        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("台式機");
        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);

        //設置系列標籤
        chart.getSeries().setSeriesLabel(chart.getChartData().get("B1", "D1"));

        //設置分類標籤
        chart.getCategories().setCategoryLabels(chart.getChartData().get("A2", "A7"));

        //設置系列數據區域
        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(0).getDataLabels().setLabelValueVisible(true);
        chart.getSeries().get(1).getDataLabels().setLabelValueVisible(true);
        chart.getSeries().get(2).getDataLabels().setLabelValueVisible(true);

        //設置圖例位置
        chart.getChartLegend().setPosition(ChartLegendPositionType.TOP);

        //保存文檔
        presentation.saveToFile("LineChart.pptx", FileFormat.PPTX_2013);
    }
}

總結

通過本文的詳細教程,您已經掌握瞭如何利用 Spire.Presentation for Java 庫在 PowerPoint 中自動化創建柱狀圖和折線圖。這個強大的工具能夠將您從重複性的手動操作中解放出來,無論是生成每日報告、每月總結還是複雜的年度演示,都能顯著提高工作效率,確保數據展示的準確性和一致性。Spire.Presentation for Java 不僅限於圖表創建,其在PPT自動化處理方面的潛力巨大,鼓勵您進一步探索它在更復雜數據呈現場景中的應用,讓數據可視化變得前所未有的簡單和高效!

user avatar
0 位用戶收藏了這個故事!

發佈 評論

Some HTML is okay.