本文介绍如何使用spire.presentation for java在powerpoint文档中插入折线图。
import com.spire.presentation.fileformat;
import com.spire.presentation.presentation;
import com.spire.presentation.slidesizetype;
import com.spire.presentation.charts.chartlegendpositiontype;
import com.spire.presentation.charts.charttype;
import com.spire.presentation.charts.ichart;
import java.awt.geom.rectangle2d;
public class linechart {
public static void main(string[] args) throws exception {
//创建presentation对象
presentation presentation = new presentation();
presentation.getslidesize().settype(slidesizetype.screen_16_x_9);
//插入折线图
rectangle2d.double rect = new rectangle2d.double(100, 50, 600, 430);
ichart chart = presentation.getslides().get(0).getshapes().appendchart(charttype.line, rect);
//设置图表标题
chart.getcharttitle().gettextproperties().settext("产品月销量趋势");
chart.getcharttitle().gettextproperties().iscentered(true);
chart.getcharttitle().setheight(30);
chart.hastitle(true);
//设置轴标题
chart.getprimarycategoryaxis().gettitle().gettextproperties().settext("月份");
chart.getprimarycategoryaxis().hastitle(true);
chart.getprimaryvalueaxis().gettitle().gettextproperties().settext("销量");
chart.getprimaryvalueaxis().hastitle(true);
//写入图表数据
chart.getchartdata().get(0,0).settext("月份");
chart.getchartdata().get(1,0).settext("一月");
chart.getchartdata().get(2,0).settext("二月");
chart.getchartdata().get(3,0).settext("三月");
chart.getchartdata().get(4,0).settext("四月");
chart.getchartdata().get(5,0).settext("五月");
chart.getchartdata().get(6,0).settext("六月");
chart.getchartdata().get(0,1).settext("台式机");
chart.getchartdata().get(1,1).setnumbervalue(80);
chart.getchartdata().get(2,1).setnumbervalue(45);
chart.getchartdata().get(3,1).setnumbervalue(25);
chart.getchartdata().get(4,1).setnumbervalue(20);
chart.getchartdata().get(5,1).setnumbervalue(10);
chart.getchartdata().get(6,1).setnumbervalue(5);
chart.getchartdata().get(0,2).settext("笔记本");
chart.getchartdata().get(1,2).setnumbervalue(30);
chart.getchartdata().get(2,2).setnumbervalue(25);
chart.getchartdata().get(3,2).setnumbervalue(35);
chart.getchartdata().get(4,2).setnumbervalue(50);
chart.getchartdata().get(5,2).setnumbervalue(45);
chart.getchartdata().get(6,2).setnumbervalue(55);
chart.getchartdata().get(0,3).settext("平板");
chart.getchartdata().get(1,3).setnumbervalue(10);
chart.getchartdata().get(2,3).setnumbervalue(15);
chart.getchartdata().get(3,3).setnumbervalue(20);
chart.getchartdata().get(4,3).setnumbervalue(35);
chart.getchartdata().get(5,3).setnumbervalue(60);
chart.getchartdata().get(6,3).setnumbervalue(95);
//设置系列标签
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(0).getdatalabels().setlabelvaluevisible(true);
chart.getseries().get(1).getdatalabels().setlabelvaluevisible(true);
chart.getseries().get(2).getdatalabels().setlabelvaluevisible(true);
//设置图例位置
chart.getchartlegend().setposition(chartlegendpositiontype.top);
//保存文档
presentation.savetofile("linechart.pptx", fileformat.pptx_2013);
}
}