溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Java在PPT中創(chuàng)建散點(diǎn)圖的實(shí)現(xiàn)方法是什么

發(fā)布時(shí)間:2021-11-05 09:05:13 來(lái)源:億速云 閱讀:136 作者:iii 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要講解了“Java在PPT中創(chuàng)建散點(diǎn)圖的實(shí)現(xiàn)方法是什么”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“Java在PPT中創(chuàng)建散點(diǎn)圖的實(shí)現(xià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)入:

Java在PPT中創(chuàng)建散點(diǎn)圖的實(shí)現(xiàn)方法是什么

Java在PPT中創(chuàng)建散點(diǎn)圖的實(shí)現(xiàn)方法是什么

Java在PPT中創(chuàng)建散點(diǎn)圖的實(shí)現(xiàn)方法是什么

方法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í)

通過(guò)指定數(shù)據(jù)源,并在幻燈片中的指定坐標(biāo)位置插入圖表。該Jar包提供了ShapeCollection.appendChart(ChartType type, Rectangle2D rectangle, boolean init)方法向幻燈片添加特定類型的圖表,ChartType枚舉預(yù)定義了73種圖表類型,包括但不限于散點(diǎn)圖、柱圖、餅圖等。

本次創(chuàng)建散點(diǎn)圖,主要通過(guò)以下步驟完成:

  • 創(chuàng)建 Presentation 類的實(shí)例。

  • 使用 ShapeCollection.appendChart() 方法將散點(diǎn)圖附加到特定的幻燈片。

  • 通過(guò) 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();
    }
}

圖表效果圖:

Java在PPT中創(chuàng)建散點(diǎn)圖的實(shí)現(xiàn)方法是什么

感謝各位的閱讀,以上就是“Java在PPT中創(chuàng)建散點(diǎn)圖的實(shí)現(xiàn)方法是什么”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)Java在PPT中創(chuàng)建散點(diǎn)圖的實(shí)現(xiàn)方法是什么這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI