我们前面介绍了如何使用spire.presentation 创建 powerpoint图表。示例里面的柱形图为簇状柱形图。该文将详细介绍如何使用spire.presentation创建堆积图并以堆积柱形图为例。
柱形图主要是为了用图形的样式显示繁多的数据。但是堆积柱形图和簇状柱形图也有差别。
簇状柱形图用于显示一段时间内的数据变化及比较各单项之间的数据差异情况。比较各个类别的数值。
堆积柱形图用于显示单个项目与整体之间的关系。当有多个数据系列并且希望强调总数值时,我们通常使用堆积柱形图来分析总体,比较比例。
创建堆积柱形图
c#
//创建一个powerpoint文档
presentation presentation = new presentation();
//插入堆积柱形图
rectanglef rect = new rectanglef(0, 50, 650, 500);
ichart chart = presentation.slides[0].shapes.appendchart(charttype.columnstacked, rect, false);
//将数据写入图表数据表
chart.chartdata[0, 1].text = "直接访问";
chart.chartdata[0, 2].text = "邮件营销";
chart.chartdata[0, 3].text = "联盟广告";
chart.chartdata[0, 4].text = "视频广告";
chart.chartdata[0, 5].text = "搜索引擎";
chart.chartdata[0, 6].text = "百度";
chart.chartdata[0, 7].text = "谷歌";
chart.chartdata[0, 8].text = "必应";
chart.chartdata[0, 9].text = "其他";
//输入数据
chart.chartdata[1, 0].text = "周一";
chart.chartdata[6, 0].text = "周二";
chart.chartdata[11, 0].text = "周三";
chart.chartdata[16, 0].text = "周四";
chart.chartdata[21, 0].text = "周五";
chart.chartdata[26, 0].text = "周六";
chart.chartdata[31, 0].text = "周七";
//输入各系列的数值
chart.chartdata[1, 1].value = 320;
chart.chartdata[6, 1].value = 332;
chart.chartdata[11, 1].value = 301;
chart.chartdata[16, 1].value = 334;
chart.chartdata[21, 1].value = 390;
chart.chartdata[26, 1].value = 330;
chart.chartdata[31, 1].value = 320;
//data for series 2
chart.chartdata[2, 2].value = 120;
chart.chartdata[7, 2].value = 132;
chart.chartdata[12, 2].value = 101;
chart.chartdata[17, 2].value = 134;
chart.chartdata[22, 2].value = 90;
chart.chartdata[27, 2].value = 230;
chart.chartdata[32, 2].value = 210;
//data for series 3
chart.chartdata[2, 3].value = 220;
chart.chartdata[7, 3].value = 182;
chart.chartdata[12, 3].value = 191;
chart.chartdata[17, 3].value = 234;
chart.chartdata[22, 3].value = 290;
chart.chartdata[27, 3].value = 330;
chart.chartdata[32, 3].value = 310;
//data for series 4
chart.chartdata[2, 4].value = 150;
chart.chartdata[7, 4].value = 232;
chart.chartdata[12, 4].value = 201;
chart.chartdata[17, 4].value = 154;
chart.chartdata[22, 4].value = 190;
chart.chartdata[27, 4].value = 330;
chart.chartdata[32, 4].value = 410;
//data for series 5
chart.chartdata[3, 5].value = 862;
chart.chartdata[8, 5].value = 1018;
chart.chartdata[13, 5].value = 964;
chart.chartdata[18, 5].value = 1026;
chart.chartdata[23, 5].value = 1679;
chart.chartdata[28, 5].value = 1600;
chart.chartdata[33, 5].value = 1570;
//data for series 6
chart.chartdata[4, 6].value = 620;
chart.chartdata[9, 6].value = 732;
chart.chartdata[14, 6].value = 701;
chart.chartdata[19, 6].value = 734;
chart.chartdata[24, 6].value = 1090;
chart.chartdata[29, 6].value = 1130;
chart.chartdata[34, 6].value = 1120;
//data for series 7
chart.chartdata[4, 7].value = 120;
chart.chartdata[9, 7].value = 132;
chart.chartdata[14, 7].value = 101;
chart.chartdata[19, 7].value = 134;
chart.chartdata[24, 7].value = 290;
chart.chartdata[29, 7].value = 230;
chart.chartdata[34, 7].value = 220;
//data for series 8
chart.chartdata[4, 8].value = 60;
chart.chartdata[9, 8].value = 72;
chart.chartdata[14, 8].value = 71;
chart.chartdata[19, 8].value = 74;
chart.chartdata[24, 8].value = 190;
chart.chartdata[29, 8].value = 130;
chart.chartdata[34, 8].value = 110;
//data for series 9
chart.chartdata[4, 9].value = 62;
chart.chartdata[9, 9].value = 82;
chart.chartdata[14, 9].value = 91;
chart.chartdata[19, 9].value = 84;
chart.chartdata[24, 9].value = 109;
chart.chartdata[29, 9].value = 110;
chart.chartdata[34, 9].value = 120;
//设置分类标签
chart.categories.categorylabels = chart.chartdata["a1", "a35"];
//设置图例位置
chart.chartlegend.position = chartlegendpositiontype.top;
//设置系列标签
chart.series.serieslabel = chart.chartdata["b1", "j1"];
//为各个系列赋值
chart.series[0].values = chart.chartdata["b2", "b35"];
chart.series[1].values = chart.chartdata["c2", "c35"];
chart.series[2].values = chart.chartdata["d2", "d35"];
chart.series[3].values = chart.chartdata["e2", "e35"];
chart.series[4].values = chart.chartdata["f2", "f35"];
chart.series[5].values = chart.chartdata["g2", "g35"];
chart.series[6].values = chart.chartdata["h2", "h35"];
chart.series[7].values = chart.chartdata["i2", "i35"];
chart.series[8].values = chart.chartdata["j2", "j35"];
//保存文档
presentation.savetofile( "堆积柱形图.pptx", fileformat.pptx2007);
vb.net
'创建一个powerpoint文档
dim presentation as new presentation()
'插入堆积柱形图
dim rect as new rectanglef(0, 50, 650, 500)
dim chart as ichart = presentation.slides(0).shapes.appendchart(charttype.columnstacked, rect, false)
'将数据写入图表数据表
chart.chartdata(0, 1).text = "直接访问"
chart.chartdata(0, 2).text = "邮件营销"
chart.chartdata(0, 3).text = "联盟广告"
chart.chartdata(0, 4).text = "视频广告"
chart.chartdata(0, 5).text = "搜索引擎"
chart.chartdata(0, 6).text = "百度"
chart.chartdata(0, 7).text = "谷歌"
chart.chartdata(0, 8).text = "必应"
chart.chartdata(0, 9).text = "其他"
'输入数据
chart.chartdata(1, 0).text = "周一"
chart.chartdata(6, 0).text = "周二"
chart.chartdata(11, 0).text = "周三"
chart.chartdata(16, 0).text = "周四"
chart.chartdata(21, 0).text = "周五"
chart.chartdata(26, 0).text = "周六"
chart.chartdata(31, 0).text = "周七"
'输入各系列的数值
chart.chartdata(1, 1).value = 320
chart.chartdata(6, 1).value = 332
chart.chartdata(11, 1).value = 301
chart.chartdata(16, 1).value = 334
chart.chartdata(21, 1).value = 390
chart.chartdata(26, 1).value = 330
chart.chartdata(31, 1).value = 320
'data for series 2
chart.chartdata(2, 2).value = 120
chart.chartdata(7, 2).value = 132
chart.chartdata(12, 2).value = 101
chart.chartdata(17, 2).value = 134
chart.chartdata(22, 2).value = 90
chart.chartdata(27, 2).value = 230
chart.chartdata(32, 2).value = 210
'data for series 3
chart.chartdata(2, 3).value = 220
chart.chartdata(7, 3).value = 182
chart.chartdata(12, 3).value = 191
chart.chartdata(17, 3).value = 234
chart.chartdata(22, 3).value = 290
chart.chartdata(27, 3).value = 330
chart.chartdata(32, 3).value = 310
'data for series 4
chart.chartdata(2, 4).value = 150
chart.chartdata(7, 4).value = 232
chart.chartdata(12, 4).value = 201
chart.chartdata(17, 4).value = 154
chart.chartdata(22, 4).value = 190
chart.chartdata(27, 4).value = 330
chart.chartdata(32, 4).value = 410
'data for series 5
chart.chartdata(3, 5).value = 862
chart.chartdata(8, 5).value = 1018
chart.chartdata(13, 5).value = 964
chart.chartdata(18, 5).value = 1026
chart.chartdata(23, 5).value = 1679
chart.chartdata(28, 5).value = 1600
chart.chartdata(33, 5).value = 1570
'data for series 6
chart.chartdata(4, 6).value = 620
chart.chartdata(9, 6).value = 732
chart.chartdata(14, 6).value = 701
chart.chartdata(19, 6).value = 734
chart.chartdata(24, 6).value = 1090
chart.chartdata(29, 6).value = 1130
chart.chartdata(34, 6).value = 1120
'data for series 7
chart.chartdata(4, 7).value = 120
chart.chartdata(9, 7).value = 132
chart.chartdata(14, 7).value = 101
chart.chartdata(19, 7).value = 134
chart.chartdata(24, 7).value = 290
chart.chartdata(29, 7).value = 230
chart.chartdata(34, 7).value = 220
'data for series 8
chart.chartdata(4, 8).value = 60
chart.chartdata(9, 8).value = 72
chart.chartdata(14, 8).value = 71
chart.chartdata(19, 8).value = 74
chart.chartdata(24, 8).value = 190
chart.chartdata(29, 8).value = 130
chart.chartdata(34, 8).value = 110
'data for series 9
chart.chartdata(4, 9).value = 62
chart.chartdata(9, 9).value = 82
chart.chartdata(14, 9).value = 91
chart.chartdata(19, 9).value = 84
chart.chartdata(24, 9).value = 109
chart.chartdata(29, 9).value = 110
chart.chartdata(34, 9).value = 120
'设置分类标签
chart.categories.categorylabels = chart.chartdata("a1", "a35")
'设置图例位置
chart.chartlegend.position = chartlegendpositiontype.top
'设置系列标签
chart.series.serieslabel = chart.chartdata("b1", "j1")
'为各个系列赋值
chart.series(0).values = chart.chartdata("b2", "b35")
chart.series(1).values = chart.chartdata("c2", "c35")
chart.series(2).values = chart.chartdata("d2", "d35")
chart.series(3).values = chart.chartdata("e2", "e35")
chart.series(4).values = chart.chartdata("f2", "f35")
chart.series(5).values = chart.chartdata("g2", "g35")
chart.series(6).values = chart.chartdata("h2", "h35")
chart.series(7).values = chart.chartdata("i2", "i35")
chart.series(8).values = chart.chartdata("j2", "j35")
'保存文档
presentation.savetofile("堆积柱形图.pptx", fileformat.pptx2007)
堆积柱形图效果图: