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);
該代碼產生以下結果: