在Java的PowerPoint中创建饼图或甜甜圈图表
#api #java #powerpoint #chart

饼图和甜甜圈图是两个相似图表,用于显示整体的相对比例。这两个图表都是在PowerPoint演示文稿中可视化数据的绝佳工具,并且还可以使文档更加专业。本文将分享如何使用免费的spire。

导入依赖性

以下是安装免费库的两种方法。
Download并解压缩它,然后将Spire.Presentation.jar文件添加到您的项目中。

通过将以下配置添加到pom.xml。

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.presentation.free</artifactId>
        <version>5.1.0</version>
    </dependency>
</dependencies>

在Java的PowerPoint演示文稿中创建饼图

java的free spire.presentation提供 iSlide.getShapeS()。附录图(ChartType Type,Rectangle2D Rectangle,boolean Init)方法方法,以在指定的显示幻灯片中添加一定类型的图表。以下是用于在PowerPoint中添加饼图的完整示例代码。

import com.spire.presentation.*;
import com.spire.presentation.charts.*;
import com.spire.presentation.charts.entity.*;
import com.spire.presentation.drawing.*;
import java.awt.*;
import java.awt.geom.Rectangle2D;

public class createPieChart {
    public static void main(String[] args) throws Exception {

        //Create a Presentation object
        Presentation presentation = new Presentation();

        //Insert a Pie chart to the first slide and set the chart title
        Rectangle2D rect1 = new Rectangle2D.Double(40, 100, 550, 320);
        IChart chart = presentation.getSlides().get(0).getShapes().appendChart(ChartType.PIE, rect1, false);
        chart.getChartTitle().getTextProperties().setText("Sales by Quarter");
        chart.getChartTitle().getTextProperties().isCentered(true);
        chart.getChartTitle().setHeight(30);
        chart.hasTitle(true);

        //Set the chart data
        String[] quarters = new String[] { "1st Qtr", "2nd Qtr", "3rd Qtr", "4th Qtr" };
        int[] sales = new int[] { 210, 320, 180, 500 };

        //Append data to ChartData, which represents a data table where the chart data is stored
        chart.getChartData().get(0, 0).setText("Quarters");
        chart.getChartData().get(0, 1).setText("Sales");
        for (int i = 0; i < quarters.length; ++i)
        {
            chart.getChartData().get(i + 1, 0).setValue(quarters[i]);
            chart.getChartData().get(i + 1, 1).setValue(sales[i]);
        }

        //Set category labels, series label and series data
        chart.getSeries().setSeriesLabel(chart.getChartData().get("B1", "B1"));
        chart.getCategories().setCategoryLabels(chart.getChartData().get("A2", "A5"));
        chart.getSeries().get(0).setValues(chart.getChartData().get("B2", "B5"));

        //Add data points to series and fill each data point with different color
        for (int i = 0; i < chart.getSeries().get(0).getValues().getCount(); i++)
        {
            ChartDataPoint cdp = new ChartDataPoint(chart.getSeries().get(0));
            cdp.setIndex(i);
            chart.getSeries().get(0).getDataPoints().add(cdp);
        }
        chart.getSeries().get(0).getDataPoints().get(0).getFill().setFillType( FillFormatType.SOLID);
        chart.getSeries().get(0).getDataPoints().get(0).getFill().getSolidColor().setColor(Color.ORANGE);
        chart.getSeries().get(0).getDataPoints().get(1).getFill().setFillType( FillFormatType.SOLID);
        chart.getSeries().get(0).getDataPoints().get(1).getFill().getSolidColor().setColor(Color.CYAN);
        chart.getSeries().get(0).getDataPoints().get(2).getFill().setFillType( FillFormatType.SOLID);
        chart.getSeries().get(0).getDataPoints().get(2).getFill().getSolidColor().setColor(Color.PINK);
        chart.getSeries().get(0).getDataPoints().get(3).getFill().setFillType( FillFormatType.SOLID);
        chart.getSeries().get(0).getDataPoints().get(3).getFill().getSolidColor().setColor(Color.GRAY);

        //Set the data labels to display label value
        chart.getSeries().get(0).getDataLabels().setLabelValueVisible(true);

        //Save the result document
        presentation.saveToFile("createPieChart.pptx", FileFormat.PPTX_2013);
    }
}

PPTPieChart

在Java的PowerPoint演示文稿中创建甜甜圈图表

甜甜圈图在功能上与饼图相同,只是它在图表的中间有一个圆形的,未填充的区域。以下是用于在PowerPoint中添加甜甜圈图表的完整示例代码。

import com.spire.presentation.*;
import com.spire.presentation.charts.*;
import com.spire.presentation.charts.entity.*;
import com.spire.presentation.drawing.*;
import java.awt.*;
import java.awt.geom.Rectangle2D;

public class createDoughnutChart {
    public static void main(String[] args) throws Exception {
        //create a Presentation instance
        Presentation presentation = new Presentation();

        //Add a Doughnut chart to the first slide and set the chart title
        Rectangle2D rect1 = new Rectangle2D.Double(40, 100, 550, 320);
        IChart chart = presentation.getSlides().get(0).getShapes().appendChart(ChartType.DOUGHNUT, rect1, false);
        chart.getChartTitle().getTextProperties().setText("Market share by country");
        chart.getChartTitle().getTextProperties().isCentered(true);
        chart.getChartTitle().setHeight(30);

        //Set the chart data
        String[] countries = new String[]{"Cuba", "Mexico", "France", "German"};
        int[] sales = new int[]{1800, 3000, 5100, 6200};
        chart.getChartData().get(0, 0).setText("Countries");
        chart.getChartData().get(0, 1).setText("Sales");
        for (int i = 0; i < countries.length; ++i) {
            chart.getChartData().get(i + 1, 0).setValue(countries[i]);
            chart.getChartData().get(i + 1, 1).setValue(sales[i]);
        }

        //Set category labels, series label and series data
        chart.getSeries().setSeriesLabel(chart.getChartData().get("B1", "B1"));
        chart.getCategories().setCategoryLabels(chart.getChartData().get("A2", "A5"));
        chart.getSeries().get(0).setValues(chart.getChartData().get("B2", "B5"));

        //Add data points to series and fill each data point with different color
        for (int i = 0; i < chart.getSeries().get(0).getValues().getCount(); i++) {
            ChartDataPoint cdp = new ChartDataPoint(chart.getSeries().get(0));
            cdp.setIndex(i);
            chart.getSeries().get(0).getDataPoints().add(cdp);
        }
        chart.getSeries().get(0).getDataPoints().get(0).getFill().setFillType(FillFormatType.SOLID);
        chart.getSeries().get(0).getDataPoints().get(0).getFill().getSolidColor().setColor(Color.CYAN);
        chart.getSeries().get(0).getDataPoints().get(1).getFill().setFillType(FillFormatType.SOLID);
        chart.getSeries().get(0).getDataPoints().get(1).getFill().getSolidColor().setColor(Color.pink);
        chart.getSeries().get(0).getDataPoints().get(2).getFill().setFillType(FillFormatType.SOLID);
        chart.getSeries().get(0).getDataPoints().get(2).getFill().getSolidColor().setColor(Color.gray);
        chart.getSeries().get(0).getDataPoints().get(3).getFill().setFillType(FillFormatType.SOLID);
        chart.getSeries().get(0).getDataPoints().get(3).getFill().getSolidColor().setColor(Color.orange);

        //Set the data labels to display percentage value
        chart.getSeries().get(0).getDataLabels().setPercentValueVisible(true);

        //Save the result document
        presentation.saveToFile("createDoughnutChart.pptx", FileFormat.PPTX_2013);
    }
}

PPTDoughnutChart