组合图表是至少包含两种图表类型的图表,组合图表比单一形式的图表更方便显示多种不同类型的数据。本文将展示如何使用spire.presentation for java创建柱状-折线组合图表。
import com.spire.presentation.fileformat;
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 combinationchart {
public static void main(string[] args) throws exception {
//创建powerpoint文档
presentation presentation = new presentation();
//添加一个柱状图
rectangle2d.double rect = new rectangle2d.double(50, 100, 550, 300);
ichart chart = presentation.getslides().get(0).getshapes().appendchart(charttype.column_clustered, rect);
//设置图表名称
chart.getcharttitle().gettextproperties().settext("销售额vs单价");
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("一月");
chart.getchartdata().get(1,1).setnumbervalue(120);
chart.getchartdata().get(1,2).setnumbervalue(5600);
chart.getchartdata().get(2,0).settext("二月");
chart.getchartdata().get(2,1).setnumbervalue(100);
chart.getchartdata().get(2,2).setnumbervalue(7300);
chart.getchartdata().get(3,0).settext("三月");
chart.getchartdata().get(3,1).setnumbervalue(80);
chart.getchartdata().get(3,2).setnumbervalue(10200);
chart.getchartdata().get(4,0).settext("四月");
chart.getchartdata().get(4,1).setnumbervalue(120);
chart.getchartdata().get(4,2).setnumbervalue(5900);
chart.getchartdata().get(5,0).settext("五月");
chart.getchartdata().get(5,1).setnumbervalue(90);
chart.getchartdata().get(5,2).setnumbervalue(9500);
chart.getchartdata().get(6,0).settext("六月");
chart.getchartdata().get(6,1).setnumbervalue(110);
chart.getchartdata().get(6,2).setnumbervalue(7200);
//设置系列标签数据来源
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"));
//将系列2的图表类型设置为折线图
chart.getseries().get(1).settype(charttype.line_markers);
//将系列2绘制在次坐标轴
chart.getseries().get(1).setusesecondaxis(true);
//不显示次坐标轴的网格线
chart.getsecondaryvalueaxis().getmajorgridtextlines().setfilltype(fillformattype.none);
//设置系列重叠
chart.setoverlap(-50);
//设置分类间距
chart.setgapdepth(200);
//保存文档
presentation.savetofile("output/combinationchart-cn.pptx", fileformat.pptx_2010);
}
}