多语言展示
当前在线:801今日阅读:23今日分享:25

Java 创建 PowerPoint 图表

图表能弥补单用文字表达的缺欠,对某些事物解说更加直接、更加具体、更加完善。本条经验将向大家详细介绍如何使用Spire.Presentation for Java在PowerPoint中创建图表。
工具/原料

Free Spire.Presentation for Java2.2.3(免费版)

jar包导入:
1

步骤一:下载安装好后,解压,将解压后的文件夹下中子文件夹lib中的jar包导入到project中。

2

步骤二:添加jar包,完成引用。

代码示例:

import com.spire.pdf.tables.table.DataColumn;import com.spire.pdf.tables.table.DataRow;import com.spire.pdf.tables.table.DataTable;import com.spire.pdf.tables.table.DataTypes;import com.spire.presentation.FileFormat;import com.spire.presentation.KnownColors;import com.spire.presentation.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 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('成绩', '2016年');       row1.setInt('良好', 150);       row1.setInt('及格', 100);       row1.setInt('不及格', 79);       DataRow row2 = dataTable.newRow();       row2.setString('成绩', '2017年');       row2.setInt('良好', 270);       row2.setInt('及格', 150);       row2.setInt('不及格', 99);       DataRow row3 = dataTable.newRow();       row3.setString('成绩', '2018年');       row3.setInt('良好', 310);       row3.setInt('及格', 120);       row3.setInt('不及格', 49);       DataRow row4 = dataTable.newRow();       row4.setString('成绩', '2019年');       row4.setInt('良好', 330);       row4.setInt('及格', 120);       row4.setInt('不及格', 49);       DataRow row5 = dataTable.newRow();       dataTable.getRows().add(row1);       dataTable.getRows().add(row2);       dataTable.getRows().add(row3);       dataTable.getRows().add(row4);       //将数据写入图表       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);   }}

效果展示:

代码运行后,效果如下:

推荐信息