本文将介绍如何使用spire.presentation for java在幻灯片上创建图表。该示例以最常见的柱形图为例。
import com.spire.presentation.*;
import com.spire.pdf.tables.table.*;
import com.spire.presentation.charts.*;
import com.spire.presentation.drawing.fillformattype;
import java.awt.geom.rectangle2d;
import java.lang.object;
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("销售额", "门店1");
row1.setint("谷物", 250);
row1.setint("粮油", 150);
row1.setint("百货", 99);
datarow row2 = datatable.newrow();
row2.setstring("销售额", "门店2");
row2.setint("谷物", 270);
row2.setint("粮油", 150);
row2.setint("百货", 99);
datarow row3 = datatable.newrow();
row3.setstring("销售额", "门店3");
row3.setint("谷物", 310);
row3.setint("粮油", 120);
row3.setint("百货", 49);
datarow row4 = datatable.newrow();
row4.setstring("销售额", "门店4");
row4.setint("谷物", 330);
row4.setint("粮油", 120);
row4.setint("百货", 49);
datarow row5 = datatable.newrow();
row5.setstring("销售额", "门店5");
row5.setint("谷物", 360);
row5.setint("粮油", 150);
row5.setint("百货", 141);
datarow row6 = datatable.newrow();
row6.setstring("销售额", "门店6");
row6.setint("谷物", 380);
row6.setint("粮油", 150);
row6.setint("百货", 135);
datatable.getrows().add(row1);
datatable.getrows().add(row2);
datatable.getrows().add(row3);
datatable.getrows().add(row4);
datatable.getrows().add(row5);
datatable.getrows().add(row6);
//将数据写入图表
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);
}
}
生成的powerpoint 图表截图: