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

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

Jar文件获取及导入:

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

Java 在PPT中添加混合图表-LMLPHP

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


Java代码示例(供参考)

  1. import com.spire.presentation.*;
  2. import com.spire.presentation.charts.ChartType;
  3. import com.spire.presentation.charts.IChart;
  4. import com.spire.presentation.drawing.FillFormatType;

  5. import java.awt.geom.Rectangle2D;

  6. public class Chart {
  7.     public static void main(String[] args) throws Exception{
  8.         //创建PowerPoint文档
  9.         Presentation presentation = new Presentation();

  10.         //添加一个柱状图
  11.         Rectangle2D.Double rect = new Rectangle2D.Double(60, 100, 600, 350);
  12.         IChart chart = presentation.getSlides().get(0).getShapes().appendChart(ChartType.COLUMN_CLUSTERED, rect);

  13.         //设置图表名称
  14.         chart.getChartTitle().getTextProperties().setText("上半年销量");
  15.         chart.getChartTitle().getTextProperties().isCentered(true);
  16.         chart.getChartTitle().setHeight(30);
  17.         chart.hasTitle(true);

  18.         //写入图表数据
  19.         chart.getChartData().get(0,0).setText("月份");
  20.         chart.getChartData().get(0,1).setText("销量");
  21.         chart.getChartData().get(0,2).setText("环比增长(%)");
  22.         chart.getChartData().get(1,0).setText("1月");
  23.         chart.getChartData().get(1,1).setNumberValue(120);
  24.         chart.getChartData().get(1,2).setNumberValue(12);
  25.         chart.getChartData().get(2,0).setText("2月");
  26.         chart.getChartData().get(2,1).setNumberValue(100);
  27.         chart.getChartData().get(2,2).setNumberValue(10);
  28.         chart.getChartData().get(3,0).setText("3月");
  29.         chart.getChartData().get(3,1).setNumberValue(80);
  30.         chart.getChartData().get(3,2).setNumberValue(9);
  31.         chart.getChartData().get(4,0).setText("4月");
  32.         chart.getChartData().get(4,1).setNumberValue(120);
  33.         chart.getChartData().get(4,2).setNumberValue(15);
  34.         chart.getChartData().get(5,0).setText("5月");
  35.         chart.getChartData().get(5,1).setNumberValue(90);
  36.         chart.getChartData().get(5,2).setNumberValue(11);
  37.         chart.getChartData().get(6,0).setText("6月");
  38.         chart.getChartData().get(6,1).setNumberValue(110);
  39.         chart.getChartData().get(6,2).setNumberValue(10.5);

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

  42.         //设置分类标签数据来源
  43.         chart.getCategories().setCategoryLabels(chart.getChartData().get("A2", "A7"));

  44.         //设置系列的数据来源
  45.         chart.getSeries().get(0).setValues(chart.getChartData().get("B2", "B7"));
  46.         chart.getSeries().get(1).setValues(chart.getChartData().get("C2", "C7"));
  47.         chart.getSeries().get(1).getDataLabels().setLabelValueVisible(true);//设置显示系列2的数据标签值
  48.         chart.getSeries().get(1).setType(ChartType.LINE_MARKERS);//将系列2的图表类型设置为折线图
  49.         chart.getSeries().get(1).setUseSecondAxis(true);//将系列2绘制在次坐标轴
  50.         chart.getSecondaryValueAxis().getMajorGridTextLines().setFillType(FillFormatType.NONE);//不显示次坐标轴的网格线

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

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

  55.         //保存文档
  56.         presentation.saveToFile("chart.pptx", FileFormat.PPTX_2013);
  57.         presentation.dispose();
  58.     }
  59. }

图表添加效果:

Java 在PPT中添加混合图表-LMLPHP
(本文完)

转载请注明出处!

09-18 08:31
查看更多