Aspose.Words for .NET图表教程——创建四种OOXML图表
程序员文章站
2022-04-10 14:36:57
Aspose.Words For .Net是一种高级Word文档处理API,用于执行各种文档管理和操作任务。API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word。此外,API支持所有流行的Word处理文件格式,并允许将Word文档导出或转换为固定布 ......
aspose.words for .net是一种高级word文档处理api,用于执行各种文档管理和操作任务。api支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用microsoft word。此外,api支持所有流行的word处理文件格式,并允许将word文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。
接下来我们进入关于“使用图表”的介绍,在aspose.words中学会从头开始创建ooxml图表,包括柱状图、散点图、面积图和气泡图。
(如果您想下载aspose.words或了解正版价格,请随时加入aspose技术交流群(642018183)。)
插入柱形图
document doc = new document(); documentbuilder builder = new documentbuilder(doc); // add chart with default data. you can specify different chart types and sizes. shape shape = builder.insertchart(charttype.column, 432, 252); // chart property of shape contains all chart related options. chart chart = shape.chart; // get chart series collection. chartseriescollection seriescoll = chart.series; // check series count. console.writeline(seriescoll.count); // delete default generated series. seriescoll.clear(); // create category names array, in this example we have two categories. string[] categories = new string[] { "aw category 1", "aw category 2" }; // adding new series. please note, data arrays must not be empty and arrays must be the same size. seriescoll.add("aw series 1", categories, new double[] { 1, 2 }); seriescoll.add("aw series 2", categories, new double[] { 3, 4 }); seriescoll.add("aw series 3", categories, new double[] { 5, 6 }); seriescoll.add("aw series 4", categories, new double[] { 7, 8 }); seriescoll.add("aw series 5", categories, new double[] { 9, 10 }); datadir = datadir + @"testinsertsimplechartcolumn_out.doc"; doc.save(datadir);
该代码产生以下结果:
添加方法有四种不同的重载,可以覆盖所有图表类型的数据源的所有可能变体:
document doc = new document(); documentbuilder builder = new documentbuilder(doc); // insert column chart. shape shape = builder.insertchart(charttype.column, 432, 252); chart chart = shape.chart; // use this overload to add series to any type of bar, column, line and surface charts. chart.series.add("aw series 1", new string[] { "aw category 1", "aw category 2" }, new double[] { 1, 2 }); datadir = datadir + @"testinsertchartcolumn_out.doc"; doc.save(datadir);
该代码产生以下结果:
插入散点图
// the path to the documents directory. string datadir = runexamples.getdatadir_workingwithcharts(); document doc = new document(); documentbuilder builder = new documentbuilder(doc); // insert scatter chart. shape shape = builder.insertchart(charttype.scatter, 432, 252); chart chart = shape.chart; // use this overload to add series to any type of scatter charts. chart.series.add("aw series 1", new double[] { 0.7, 1.8, 2.6 }, new double[] { 2.7, 3.2, 0.8 }); datadir = datadir + "testinsertscatterchart_out.docx"; doc.save(datadir);
该代码产生以下结果:
插入面积图
// the path to the documents directory. string datadir = runexamples.getdatadir_workingwithcharts(); document doc = new document(); documentbuilder builder = new documentbuilder(doc); // insert area chart. shape shape = builder.insertchart(charttype.area, 432, 252); chart chart = shape.chart; // use this overload to add series to any type of area, radar and stock charts. chart.series.add("aw series 1", new datetime[] { new datetime(2002, 05, 01), new datetime(2002, 06, 01), new datetime(2002, 07, 01), new datetime(2002, 08, 01), new datetime(2002, 09, 01)}, new double[] { 32, 32, 28, 12, 15 }); datadir = datadir + @"testinsertareachart_out.docx"; doc.save(datadir);
该代码产生以下结果:
插入气泡图
// the path to the documents directory. string datadir = runexamples.getdatadir_workingwithcharts(); document doc = new document(); documentbuilder builder = new documentbuilder(doc); // insert bubble chart. shape shape = builder.insertchart(charttype.bubble, 432, 252); chart chart = shape.chart; // use this overload to add series to any type of bubble charts. chart.series.add("aw series 1", new double[] { 0.7, 1.8, 2.6 }, new double[] { 2.7, 3.2, 0.8 }, new double[] { 10, 4, 8 }); datadir = datadir + @"testinsertbubblechart_out.docx"; doc.save(datadir);
该代码产生以下结果:
上一篇: 数据库入门