图表是指将数据用图形的形式表示出来,常见的图表类型有柱形图、饼状图、条形图、折线图和组合图等。spire.presentation支持多达70种不同样式的图表类型,本文将以柱形图、圆环图(饼状图)和组合图为例,介绍如何在幻灯片上创建图表。
创建柱形图
c#
//创建一个powerpoint文档
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;
//定义一个sting[,]数组
string[,] data = new string[,]
{
{"部门名称","男性员工","女性员工" },
{"开发部","25","15"},
{"测试部","5","10" },
{"销售部","7","3" },
{"k8凯发天生赢家的技术支持部","20","5" }
};
//将数据写入图表后台数据表
for (int i = 0; i < data.getlength(0); i )
{
for (int j = 0; j < data.getlength(1); j )
{
//将数字类型的字符串转换为整数
int number;
bool result = int32.tryparse(data[i, j],out number);
if (result)
{
chart.chartdata[i, j].value = number;
}
else
{
chart.chartdata[i, j].value = data[i, j];
}
}
}
//设置系列标签
chart.series.serieslabel = chart.chartdata["b1", "c1"];
//设置类别标签
chart.categories.categorylabels = chart.chartdata["a2", "a5"];
//为各个系列赋值
chart.series[0].values = chart.chartdata["b2", "b5"];
chart.series[1].values = chart.chartdata["c2", "c5"];
//应用内置图标样式
chart.chartstyle = chartstyle.style11;
//设置系列重叠
chart.overlap = -50;
//设置类别间距
chart.gapwidth = 200;
//保存文档
presentation.savetofile("柱形图.pptx", fileformat.pptx2010);
vb.net
'创建一个powerpoint文档
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
'定义一个sting[,]数组
dim data as string(,) = new string(,) {{"部门名称", "男性员工", "女性员工"}, {"开发部", "25", "15"}, {"测试部", "5", "10"}, {"销售部", "7", "3"}, {"k8凯发天生赢家的技术支持部", "20", "5"}}
'将数据写入图表后台数据表
for i as integer = 0 to data.getlength(0) - 1
for j as integer = 0 to data.getlength(1) - 1
'将数字类型的字符串转换为整数
dim number as integer
dim result as boolean = int32.tryparse(data(i, j), number)
if result then
chart.chartdata(i, j).value = number
else
chart.chartdata(i, j).value = data(i, j)
end if
next
next
'设置系列标签
chart.series.serieslabel = chart.chartdata("b1", "c1")
'设置类别标签
chart.categories.categorylabels = chart.chartdata("a2", "a5")
'为各个系列赋值
chart.series(0).values = chart.chartdata("b2", "b5")
chart.series(1).values = chart.chartdata("c2", "c5")
'应用内置图标样式
chart.chartstyle = chartstyle.style11
'设置系列重叠
chart.overlap = -50
'设置类别间距
chart.gapwidth = 200
'保存文档
presentation.savetofile("柱形图.pptx", fileformat.pptx2010)
创建圆环图
c#
//创建一个powerpoint文件
presentation presentation = new presentation();
//插入圆环图
rectanglef rect = new rectanglef(40, 100, 550, 320);
ichart chart = presentation.slides[0].shapes.appendchart(charttype.doughnut, rect, false);
//设置图表名
chart.charttitle.textproperties.text = "市场份额";
chart.charttitle.textproperties.iscentered = true;
chart.charttitle.height = 30;
chart.hastitle = true;
//定义数据
string[] countries = new string[] { "古巴", "墨西哥", "法国", "德国" };
int[] sales = new int[] { 1800, 3000, 5100, 6200 };
//将数据写入图表后台数据表
chart.chartdata[0, 0].text = "国家";
chart.chartdata[0, 1].text = "销售额";
for (int i = 0; i < countries.length; i)
{
chart.chartdata[i 1, 0].value = countries[i];
chart.chartdata[i 1, 1].value = sales[i];
}
//设置系列标签
chart.series.serieslabel = chart.chartdata["b1", "b1"];
//设置分类标签
chart.categories.categorylabels = chart.chartdata["a2", "a5"];
//为系列赋值
chart.series[0].values = chart.chartdata["b2", "b5"];
//添加点到系列
for (int i = 0; i < chart.series[0].values.count; i )
{
chartdatapoint cdp = new chartdatapoint(chart.series[0]);
cdp.index = i;
chart.series[0].datapoints.add(cdp);
}
//为系列里的个点添加背景颜色
chart.series[0].datapoints[0].fill.filltype = fillformattype.solid;
chart.series[0].datapoints[0].fill.solidcolor.color = color.lightblue;
chart.series[0].datapoints[1].fill.filltype = fillformattype.solid;
chart.series[0].datapoints[1].fill.solidcolor.color = color.mediumpurple;
chart.series[0].datapoints[2].fill.filltype = fillformattype.solid;
chart.series[0].datapoints[2].fill.solidcolor.color = color.darkgray;
chart.series[0].datapoints[3].fill.filltype = fillformattype.solid;
chart.series[0].datapoints[3].fill.solidcolor.color = color.darkorange;
//设置标签显示数值
chart.series[0].datalabels.labelvaluevisible = true;
//设置标签显示百分比
chart.series[0].datalabels.percentvaluevisible = true;
//设置圆环内径大小
chart.series[0].doughnutholesize = 60;
//保存文档
presentation.savetofile("圆环图.pptx", fileformat.pptx2013);
vb.net
'创建一个powerpoint文件
dim presentation as new presentation()
'插入圆环图
dim rect as new rectanglef(40, 100, 550, 320)
dim chart as ichart = presentation.slides(0).shapes.appendchart(charttype.doughnut, rect, false)
'设置图表名
chart.charttitle.textproperties.text = "市场份额"
chart.charttitle.textproperties.iscentered = true
chart.charttitle.height = 30
chart.hastitle = true
'定义数据
dim countries as string() = new string() {"古巴", "墨西哥", "法国", "德国"}
dim sales as integer() = new integer() {1800, 3000, 5100, 6200}
'将数据写入图表后台数据表
chart.chartdata(0, 0).text = "国家"
chart.chartdata(0, 1).text = "销售额"
for i as integer = 0 to countries.length - 1
chart.chartdata(i 1, 0).value = countries(i)
chart.chartdata(i 1, 1).value = sales(i)
next
'设置系列标签
chart.series.serieslabel = chart.chartdata("b1", "b1")
'设置分类标签
chart.categories.categorylabels = chart.chartdata("a2", "a5")
'为系列赋值
chart.series(0).values = chart.chartdata("b2", "b5")
'添加点到系列
for i as integer = 0 to chart.series(0).values.count - 1
dim cdp as new chartdatapoint(chart.series(0))
cdp.index = i
chart.series(0).datapoints.add(cdp)
next
'为系列里的个点添加背景颜色
chart.series(0).datapoints(0).fill.filltype = fillformattype.solid
chart.series(0).datapoints(0).fill.solidcolor.color = color.lightblue
chart.series(0).datapoints(1).fill.filltype = fillformattype.solid
chart.series(0).datapoints(1).fill.solidcolor.color = color.mediumpurple
chart.series(0).datapoints(2).fill.filltype = fillformattype.solid
chart.series(0).datapoints(2).fill.solidcolor.color = color.darkgray
chart.series(0).datapoints(3).fill.filltype = fillformattype.solid
chart.series(0).datapoints(3).fill.solidcolor.color = color.darkorange
'设置标签显示数值
chart.series(0).datalabels.labelvaluevisible = true
'设置标签显示百分比
chart.series(0).datalabels.percentvaluevisible = true
'设置圆环内径大小
chart.series(0).doughnutholesize = 60
'保存文档
presentation.savetofile("圆环图.pptx", fileformat.pptx2013)
创建混合图表
c#
//新建一个powerpoint文档
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("1月", 200, 0.6);
datatable.rows.add("2月", 250, 0.8);
datatable.rows.add("3月", 300, 0.6);
datatable.rows.add("4月", 150, 0.2);
datatable.rows.add("5月", 200, 0.5);
datatable.rows.add("6月", 400, 0.9);
//将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"];
//将系列2的图表类型改为折线图
chart.series[1].type = charttype.linemarkers;
//将系列2显示到第二根轴
chart.series[1].usesecondaxis = true;
//显示百分比数据
chart.secondaryvalueaxis.numberformat = "0%";
//不显示第二根轴的网格线
chart.secondaryvalueaxis.majorgridtextlines.filltype = fillformattype.none;
//设置系列重叠
chart.overlap = -50;
//设置类别间距
chart.gapwidth = 200;
//保存文档
presentation.savetofile("混合图表.pptx", fileformat.pptx2010);
vb.net
'新建一个powerpoint文档
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("1月", 200, 0.6)
datatable.rows.add("2月", 250, 0.8)
datatable.rows.add("3月", 300, 0.6)
datatable.rows.add("4月", 150, 0.2)
datatable.rows.add("5月", 200, 0.5)
datatable.rows.add("6月", 400, 0.9)
'将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")
'将系列2的图表类型改为折线图
chart.series(1).type = charttype.linemarkers
'将系列2显示到第二根轴
chart.series(1).usesecondaxis = true
'显示百分比数据
chart.secondaryvalueaxis.numberformat = "0%"
'不显示第二根轴的网格线
chart.secondaryvalueaxis.majorgridtextlines.filltype = fillformattype.none
'设置系列重叠
chart.overlap = -50
'设置类别间距
chart.gapwidth = 200
'保存文档
presentation.savetofile("混合图表.pptx", fileformat.pptx2010)