C# 在PPT幻灯片中创建图表
程序员文章站
2022-12-25 18:50:15
图表能够很直观的表现数据在某个时间段的变化趋势,或者呈现数据的整体和局部之间的相互关系,相较于大篇幅的文本数据,图表更增加了我们分析数据时选择的多样性,是我们挖掘数据背后潜在价值的一种更为有效地方式。在做数据汇报时,常用到PPT幻灯片来辅助工作,下面的示例中将演示如何通过C#编程在PPT幻灯片中创建 ......
图表能够很直观的表现数据在某个时间段的变化趋势,或者呈现数据的整体和局部之间的相互关系,相较于大篇幅的文本数据,图表更增加了我们分析数据时选择的多样性,是我们挖掘数据背后潜在价值的一种更为有效地方式。在做数据汇报时,常用到ppt幻灯片来辅助工作,下面的示例中将演示如何通过c#编程在ppt幻灯片中创建图表。示例中主要介绍了三种图表的创建方法,如下:
1. 创建柱形图表
2. 创建饼状图表
3. 创建混合型图表(柱形图、折线图)
使用工具:spire.presentation for .net
ps:下载安装后,注意添加引用spire.presentation.dll到程序,dll文件可在安装路径下的bin文件夹中获取。
【示例 1 】创建柱形图表
步骤 1 :添加using指令
using spire.presentation; using spire.presentation.charts; using system; using system.drawing;
步骤 2 :创建一个powerpoint文档
presentation presentation = new presentation();
步骤 3 :在幻灯片指定位置绘入指定大小和类型的图表
rectanglef rect = new rectanglef(40, 50, 680, 500); ichart chart = presentation.slides[0].shapes.appendchart(charttype.column3dclustered, rect);
步骤 4 :添加图表数据
//添加图表名 chart.charttitle.textproperties.text = "2018年上半年销量"; chart.charttitle.textproperties.iscentered = true; chart.charttitle.height = 30; chart.hastitle = true; //定义一个sting[,]数组 string[,] data = new string[,] { {"产品大类","1月","2月","3月","4月","5月","6月" }, {"dw10","1542","1057","1223","1302","1145","1336"}, {"zq13","4587","3658","2515","3154","2984","3890" }, {"yi73","558","458","369","576","334","482" }, {"tr11","2011","2485" ,"3010" ,"2785" ,"2225" ,"2476" } }; //将数据写入图表后台数据表 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", "g1"]; //设置类别标签 chart.categories.categorylabels = chart.chartdata["a2", "a5"]; //为各个系列赋值 chart.series[0].values = chart.chartdata["b2", "b5"]; chart.series[1].values = chart.chartdata["c2", "c5"]; chart.series[2].values = chart.chartdata["d2", "d5"]; chart.series[3].values = chart.chartdata["e2", "e5"]; chart.series[4].values = chart.chartdata["f2", "f5"]; chart.series[5].values = chart.chartdata["g2", "g5"];
步骤 5 :应用图表样式
//应用内置图标样式 chart.chartstyle = chartstyle.style12; //设置系列重叠 chart.overlap = -50; //设置类别间距 chart.gapwidth = 200;
步骤 6 :保存文档
presentation.savetofile("柱形图.pptx", fileformat.pptx2010); system.diagnostics.process.start("柱形图.pptx");
调试运行程序后,生成图表,如下图:
全部代码:
using spire.presentation; using spire.presentation.charts; using system; using system.drawing; namespace columnchart { class program { static void main(string[] args) { //创建一个powerpoint文档 presentation presentation = new presentation(); //插入柱形图 rectanglef rect = new rectanglef(40, 50, 680, 500); ichart chart = presentation.slides[0].shapes.appendchart(charttype.column3dclustered, rect); //添加图表名 chart.charttitle.textproperties.text = "2018年上半年销量"; chart.charttitle.textproperties.iscentered = true; chart.charttitle.height = 30; chart.hastitle = true; //定义一个sting[,]数组 string[,] data = new string[,] { {"产品大类","1月","2月","3月","4月","5月","6月" }, {"dw10","1542","1057","1223","1302","1145","1336"}, {"zq13","4587","3658","2515","3154","2984","3890" }, {"yi73","558","458","369","576","334","482" }, {"tr11","2011","2485" ,"3010" ,"2785" ,"2225" ,"2476" } }; //将数据写入图表后台数据表 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", "g1"]; //设置类别标签 chart.categories.categorylabels = chart.chartdata["a2", "a5"]; //为各个系列赋值 chart.series[0].values = chart.chartdata["b2", "b5"]; chart.series[1].values = chart.chartdata["c2", "c5"]; chart.series[2].values = chart.chartdata["d2", "d5"]; chart.series[3].values = chart.chartdata["e2", "e5"]; chart.series[4].values = chart.chartdata["f2", "f5"]; chart.series[5].values = chart.chartdata["g2", "g5"]; //应用内置图标样式 chart.chartstyle = chartstyle.style12; //设置系列重叠 chart.overlap = -50; //设置类别间距 chart.gapwidth = 200; //保存并打开文档 presentation.savetofile("柱形图.pptx", fileformat.pptx2010); system.diagnostics.process.start("柱形图.pptx"); } } }
【示例 2 】创建环形图表
步骤 1 :添加using指令
using spire.presentation; using spire.presentation.charts; using spire.presentation.drawing; using system.drawing;
步骤 2 :新建一个ppt文件
presentation presentation = new presentation();
步骤 3 :插入圆环形图表
rectanglef rect = new rectanglef(40, 100, 550, 320); ichart chart = presentation.slides[0].shapes.appendchart(charttype.doughnut, rect, false);
步骤 4 :添加图表数据内容
//设置图表名 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]; }
步骤 5 :应用图表标签
//设置系列标签 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;
步骤 6 :保存文档
presentation.savetofile("环形图.pptx", fileformat.pptx2013); system.diagnostics.process.start("环形图.pptx");
圆环图表创建效果:
全部代码:
using spire.presentation; using spire.presentation.charts; using spire.presentation.drawing; using system.drawing; namespace doughnutchart { class program { static void main(string[] args) { //创建一个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); system.diagnostics.process.start("环形图.pptx"); } } }
【示例 3 】创建混合型图表
步骤 1 :添加using指令
using spire.presentation; using spire.presentation.charts; using spire.presentation.drawing; using system; using system.data; using system.drawing;
步骤 2 :新建文档
presentation presentation = new presentation();
步骤 3 :创建图表1:柱形图表
//插入柱形图 rectanglef rect = new rectanglef(40, 100, 650, 320); ichart chart = presentation.slides[0].shapes.appendchart(charttype.columnclustered, rec //添加表名 chart.charttitle.textproperties.text = "2017季度销售情况"; 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数据导入图表后台数据表 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", "a5"]; //为系列赋值 chart.series[0].values = chart.chartdata["b2", "b5"]; chart.series[1].values = chart.chartdata["c2", "c5"];
步骤 4 :添加折线图
//将系列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;
步骤 5 :保存文件
presentation.savetofile("混合图表.pptx", fileformat.pptx2010); system.diagnostics.process.start("混合图表.pptx");
混合型图表生成效果:
全部代码:
using spire.presentation; using spire.presentation.charts; using spire.presentation.drawing; using system; using system.data; using system.drawing; namespace 混合图表 { class program { static void main(string[] args) { //新建一个powerpoint文档 presentation presentation = new presentation(); //插入柱形图 rectanglef rect = new rectanglef(40, 100, 650, 320); ichart chart = presentation.slides[0].shapes.appendchart(charttype.columnclustered, rect); //添加表名 chart.charttitle.textproperties.text = "2017季度销售情况"; 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数据导入图表后台数据表 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", "a5"]; //为系列赋值 chart.series[0].values = chart.chartdata["b2", "b5"]; chart.series[1].values = chart.chartdata["c2", "c5"]; //将系列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); system.diagnostics.process.start("混合图表.pptx"); } } }
注:spire.presentation 支持创建多大73种不同的图表样式,如下图
以上是本次关于“c# 创建ppt图表”的全部内容。
如需转载,请注明出处。