Aspose.Words for .NET圖表教程——創(chuàng)建四種OOXML圖表
Aspose.Words For .Net是一種高級Word文檔處理API,用于執(zhí)行各種文檔管理和操作任務(wù)。API支持生成,修改,轉(zhuǎn)換,呈現(xiàn)和打印文檔,而無需在跨平臺應(yīng)用程序中直接使用Microsoft Word。此外,API支持所有流行的Word處理文件格式,并允許將Word文檔導(dǎo)出或轉(zhuǎn)換為固定布局文件格式和最常用的圖像/多媒體格式。
接下來我們將進(jìn)入關(guān)于“使用圖表”的介紹,在Aspose.Words中學(xué)會從頭開始創(chuàng)建OOXML圖表,包括柱狀圖、散點(diǎn)圖、面積圖和氣泡圖。
>>Aspose.Words for .NET更新至最新版v19.11,歡迎下載體驗(yàn)
插入柱形圖
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);
該代碼產(chǎn)生以下結(jié)果:
(正版優(yōu)惠進(jìn)行時(shí),Aspose.Total爆款授權(quán)直降10000元,想要購買Aspose正版授權(quán)抓緊時(shí)間聯(lián)系慧都客服哦~)
添加方法有四種不同的重載,可以覆蓋所有圖表類型的數(shù)據(jù)源的所有可能變體:
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);
該代碼產(chǎn)生以下結(jié)果:
插入散點(diǎn)圖
// 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);
該代碼產(chǎn)生以下結(jié)果:
插入面積圖
// 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);
該代碼產(chǎn)生以下結(jié)果:
插入氣泡圖
// 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);
該代碼產(chǎn)生以下結(jié)果: