Java 在PPT中添加混合图表过程详解

所属分类: 软件编程 / java 阅读数: 51
收藏 0 赞 0 分享

本文将介绍通过Java程序在PPT幻灯片中添加混合图表的方法,即,将不同类型的图表类型放置在同一图表中,用于展示同一时期或阶段的数据在不同参数标准下的变化情况,便于对数据的综合分析。

使用工具:Free Spire.Presentation for Java(免费版)

Jar文件获取及导入:

方法1:通过官网下载jar文件包。下载后,解压文件,并将lib文件夹下的Spire.Presentation.jar导入java程序。参考如下导入效果:

方法2:通过maven仓库安装导入。可参考导入方法

Java代码示例(供参考)

import com.spire.presentation.*;
import com.spire.presentation.charts.ChartType;
import com.spire.presentation.charts.IChart;
import com.spire.presentation.drawing.FillFormatType;
import java.awt.geom.Rectangle2D;
public class Chart {
  public static void main(String[] args) throws Exception{
    //创建PowerPoint文档
    Presentation presentation = new Presentation();

    //添加一个柱状图
    Rectangle2D.Double rect = new  Rectangle2D.Double(60, 100, 600, 350);
    IChart 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);

    //写入图表数据
    chart.getChartData().get(0,0).setText("月份");
    chart.getChartData().get(0,1).setText("销量");
    chart.getChartData().get(0,2).setText("环比增长(%)");
    chart.getChartData().get(1,0).setText("1月");
    chart.getChartData().get(1,1).setNumberValue(120);
    chart.getChartData().get(1,2).setNumberValue(12);
    chart.getChartData().get(2,0).setText("2月");
    chart.getChartData().get(2,1).setNumberValue(100);
    chart.getChartData().get(2,2).setNumberValue(10);
    chart.getChartData().get(3,0).setText("3月");
    chart.getChartData().get(3,1).setNumberValue(80);
    chart.getChartData().get(3,2).setNumberValue(9);
    chart.getChartData().get(4,0).setText("4月");
    chart.getChartData().get(4,1).setNumberValue(120);
    chart.getChartData().get(4,2).setNumberValue(15);
    chart.getChartData().get(5,0).setText("5月");
    chart.getChartData().get(5,1).setNumberValue(90);
    chart.getChartData().get(5,2).setNumberValue(11);
    chart.getChartData().get(6,0).setText("6月");
    chart.getChartData().get(6,1).setNumberValue(110);
    chart.getChartData().get(6,2).setNumberValue(10.5);

    //设置系列标签数据来源
    chart.getSeries().setSeriesLabel(chart.getChartData().get("B1", "C1"));

    //设置分类标签数据来源
    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(1).getDataLabels().setLabelValueVisible(true);//设置显示系列2的数据标签值
    chart.getSeries().get(1).setType(ChartType.LINE_MARKERS);//将系列2的图表类型设置为折线图
    chart.getSeries().get(1).setUseSecondAxis(true);//将系列2绘制在次坐标轴
    chart.getSecondaryValueAxis().getMajorGridTextLines().setFillType(FillFormatType.NONE);//不显示次坐标轴的网格线

    //设置系列重叠
    chart.setOverLap(-30);

    //设置分类间距
    chart.setGapDepth(200);

    //保存文档
    presentation.saveToFile("chart.pptx", FileFormat.PPTX_2013);
    presentation.dispose();
  }
}

图表添加效果:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

JavaWeb项目部署到服务器详细步骤详解

这篇文章主要介绍了JavaWeb项目如何部署到服务器,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA基于支付宝小程序搭建springboot项目的详细步骤

这篇文章主要介绍了IDEA基于支付宝小程序搭建springboot项目的详细步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解SpringBoot应用服务启动与安全终止

这篇文章主要介绍了SpringBoot应用服务启动与安全终止,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Boot启动及退出加载项的方法

这篇文章主要介绍了Spring Boot启动及退出加载项的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Data Jpa 自动生成表结构的方法示例

这篇文章主要介绍了Spring Data Jpa 自动生成表结构的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA中osgi的开发应用指南详解

这篇文章主要介绍了IDEA中osgi的开发应用指南详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解用maven将dubbo工程打成jar包运行

这篇文章主要介绍了详解用maven将dubbo工程打成jar包运行,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解Java合并数组的两种实现方式

这篇文章主要介绍了Java合并数组的两种实现方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

使用Jenkins Pipeline自动化构建发布Java项目的方法

这篇文章主要介绍了使用Jenkins Pipeline自动化构建发布Java项目的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

使用Maven配置Spring的方法步骤

这篇文章主要介绍了使用Maven配置Spring的方法步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多