我们前面介绍了如何使用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)
堆积柱形图效果图: