组合图表中至少含有两种不同类型的图表样式,常见的组合图表如柱状图和折线图的组合,能将不同类型的数据分别显示到主轴和次轴。本文将展示如何使用spire.presentation在ppt中创建柱状加折线的组合图表。
c#
//创建presentation对象
presentation presentation = new presentation();
//添加一个柱状图
rectanglef rect = new rectanglef(40, 100, 550, 320);
ichart chart = presentation.slides[0].shapes.appendchart(charttype.columnclustered, rect);
//设置图表名称
chart.charttitle.textproperties.text = "月销售额图表";
chart.charttitle.textproperties.iscentered = true;
chart.charttitle.height = 30;
chart.hastitle = true;
//创建一个datatable对象,写入数据
datatable datatable = new datatable();
datatable.columns.add(new datacolumn("月份", type.gettype("system.string")));
datatable.columns.add(new datacolumn("销售额", type.gettype("system.int32")));
datatable.columns.add(new datacolumn("环比增长率", type.gettype("system.decimal")));
datatable.rows.add("一月", 200, 0);
datatable.rows.add("二月", 250, 0.25);
datatable.rows.add("三月", 300, 0.20);
datatable.rows.add("四月", 150, -0.5);
datatable.rows.add("五月", 200, 0.33);
datatable.rows.add("六月", 400, 1);
//将datatable数据导入图表的数据表
for (int c = 0; c < datatable.columns.count; c )
{
chart.chartdata[0, c].text = datatable.columns[c].caption;
}
for (int r = 0; r < datatable.rows.count; r )
{
object[] datas = datatable.rows[r].itemarray;
for (int c = 0; c < datas.length; c )
{
chart.chartdata[r 1, c].value = datas[c];
}
}
//设置系列标签的数据范围
chart.series.serieslabel = chart.chartdata["b1", "c1"];
//设置种类标签的数据范围
chart.categories.categorylabels = chart.chartdata["a2", "a7"];
//为系列设置数据
chart.series[0].values = chart.chartdata["b2", "b7"];
chart.series[1].values = chart.chartdata["c2", "c7"];
//将系列二的图表类型设置为了折线图
chart.series[1].type = charttype.linemarkers;
//将系列二显示到第二个轴
chart.series[1].usesecondaxis = true;
//设置第二个轴的数字格式为百分数
chart.secondaryvalueaxis.numberformat = "0%";
//隐藏第二个轴的主网格线
chart.secondaryvalueaxis.majorgridtextlines.filltype = fillformattype.none;
//设置系列重叠
chart.overlap = -50;
//设置分类间距
chart.gapwidth = 200;
//保存文档
presentation.savetofile("output.pptx", fileformat.pptx2010);
vb.net
'创建presentation对象
dim presentation as new presentation()
'添加一个柱状图
dim rect as new rectanglef(40, 100, 550, 320)
dim chart as ichart = presentation.slides(0).shapes.appendchart(charttype.columnclustered, rect)
'设置图表名称
chart.charttitle.textproperties.text = "月销售额图表"
chart.charttitle.textproperties.iscentered = true
chart.charttitle.height = 30
chart.hastitle = true
'创建一个datatable对象,写入数据
dim datatable as new datatable()
datatable.columns.add(new datacolumn("月份", type.[gettype]("system.string")))
datatable.columns.add(new datacolumn("销售额", type.[gettype]("system.int32")))
datatable.columns.add(new datacolumn("环比增长率", type.[gettype]("system.decimal")))
datatable.rows.add("一月", 200, 0)
datatable.rows.add("二月", 250, 0.25)
datatable.rows.add("三月", 300, 0.2)
datatable.rows.add("四月", 150, -0.5)
datatable.rows.add("五月", 200, 0.33)
datatable.rows.add("六月", 400, 1)
'将datatable数据导入图表的数据表
for c as integer = 0 to datatable.columns.count - 1
chart.chartdata(0, c).text = datatable.columns(c).caption
next
for r as integer = 0 to datatable.rows.count - 1
dim datas as object() = datatable.rows(r).itemarray
for c as integer = 0 to datas.length - 1
chart.chartdata(r 1, c).value = datas(c)
next
next
'设置系列标签的数据范围
chart.series.serieslabel = chart.chartdata("b1", "c1")
'设置种类标签的数据范围
chart.categories.categorylabels = chart.chartdata("a2", "a7")
'为系列设置数据
chart.series(0).values = chart.chartdata("b2", "b7")
chart.series(1).values = chart.chartdata("c2", "c7")
'将系列二的图表类型设置为了折线图
chart.series(1).type = charttype.linemarkers
'将系列二显示到第二个轴
chart.series(1).usesecondaxis = true
'设置第二个轴的数字格式为百分数
chart.secondaryvalueaxis.numberformat = "0%"
'隐藏第二个轴的主网格线
chart.secondaryvalueaxis.majorgridtextlines.filltype = fillformattype.none
'设置系列重叠
chart.overlap = -50
'设置分类间距
chart.gapwidth = 200
'保存文档
presentation.savetofile("output.pptx", fileformat.pptx2010)