Aspose.Words for .NET圖表教程——如何設(shè)置圖表軸屬性
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é)會如何設(shè)置圖表軸屬性,包括設(shè)置軸的日期時間值、設(shè)置軸的界限、在標(biāo)簽之間設(shè)置間隔單位、隱藏圖表軸和對齊圖表標(biāo)簽。
>>Aspose.Words for .NET更新至最新版v19.12,支持轉(zhuǎn)換為PDF 1.7標(biāo)準(zhǔn),點(diǎn)擊下載體驗(yàn)
如何設(shè)置圖表軸屬性
如果要使用圖表軸,縮放比例和值軸的顯示單位,請使用ChartAxis,AxisDisplayUnit和AxisScaling類。下面給出的代碼示例顯示了如何定義X和Y軸屬性。
Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Insert chart. Shape shape = builder.InsertChart(ChartType.Area, 432, 252); Chart chart = shape.Chart; // Clear demo data. chart.Series.Clear(); // Fill data. chart.Series.Add("AW Series 1", new DateTime[] { new DateTime(2002, 01, 01), new DateTime(2002, 06, 01), new DateTime(2002, 07, 01), new DateTime(2002, 08, 01), new DateTime(2002, 09, 01) }, new double[] { 640, 320, 280, 120, 150 }); ChartAxis xAxis = chart.AxisX; ChartAxis yAxis = chart.AxisY; // Change the X axis to be category instead of date, so all the points will be put with equal interval on the X axis. xAxis.CategoryType = AxisCategoryType.Category; // Define X axis properties. xAxis.Crosses = AxisCrosses.Custom; xAxis.CrossesAt = 3; // measured in display units of the Y axis (hundreds) xAxis.ReverseOrder = true; xAxis.MajorTickMark = AxisTickMark.Cross; xAxis.MinorTickMark = AxisTickMark.Outside; xAxis.TickLabelOffset = 200; // Define Y axis properties. yAxis.TickLabelPosition = AxisTickLabelPosition.High; yAxis.MajorUnit = 100; yAxis.MinorUnit = 50; yAxis.DisplayUnit.Unit = AxisBuiltInUnit.Hundreds; yAxis.Scaling.Minimum = new AxisBound(100); yAxis.Scaling.Maximum = new AxisBound(700); dataDir = dataDir + @"SetAxisProperties_out.docx"; doc.Save(dataDir);
還想要更多嗎?您可以點(diǎn)擊閱讀【2019 · Aspose最新資源整合】,查找需要的教程資源。讓人興奮的是Aspose.Total限時直降10000元!java版另送IDE開發(fā)工具一套!聯(lián)系慧都客服立馬1分鐘了解全部咨詢!
如何設(shè)置軸的日期時間值
以下代碼示例顯示如何為軸屬性設(shè)置日期/時間值。
Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Insert chart. Shape shape = builder.InsertChart(ChartType.Column, 432, 252); Chart chart = shape.Chart; // Clear demo data. chart.Series.Clear(); // Fill data. chart.Series.Add("AW Series 1", new DateTime[] { new DateTime(2017, 11, 06), new DateTime(2017, 11, 09), new DateTime(2017, 11, 15), new DateTime(2017, 11, 21), new DateTime(2017, 11, 25), new DateTime(2017, 11, 29) }, new double[] { 1.2, 0.3, 2.1, 2.9, 4.2, 5.3 }); // Set X axis bounds. ChartAxis xAxis = chart.AxisX; xAxis.Scaling.Minimum = new AxisBound((new DateTime(2017, 11, 05)).ToOADate()); xAxis.Scaling.Maximum = new AxisBound((new DateTime(2017, 12, 03)).ToOADate()); // Set major units to a week and minor units to a day. xAxis.MajorUnit = 7; xAxis.MinorUnit = 1; xAxis.MajorTickMark = AxisTickMark.Cross; xAxis.MinorTickMark = AxisTickMark.Outside; dataDir = dataDir + @"SetDateTimeValuesToAxis_out.docx"; doc.Save(dataDir);
doc.Save(dataDir);
如何設(shè)置軸的界限
AxisBound類表示軸值的最小或最大范圍。綁定可以指定為數(shù)字,日期時間或特殊的“自動”值。以下代碼示例顯示如何設(shè)置軸的邊界。
Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Insert chart. Shape shape = builder.InsertChart(ChartType.Column, 432, 252); Chart chart = shape.Chart; // Clear demo data. chart.Series.Clear(); // Fill data. chart.Series.Add("AW Series 1", new string[] { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" }, new double[] { 1.2, 0.3, 2.1, 2.9, 4.2 }); chart.AxisY.Scaling.Minimum = new AxisBound(0); chart.AxisY.Scaling.Maximum = new AxisBound(6); dataDir = dataDir + @"SetboundsOfAxis_out.docx"; doc.Save(dataDir);
如何在標(biāo)簽之間設(shè)置間隔單位
下面的代碼示例演示如何設(shè)置軸上標(biāo)簽之間的間隔單位。
Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Insert chart. Shape shape = builder.InsertChart(ChartType.Column, 432, 252); Chart chart = shape.Chart; // Clear demo data. chart.Series.Clear(); // Fill data. chart.Series.Add("AW Series 1", new string[] { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" }, new double[] { 1.2, 0.3, 2.1, 2.9, 4.2 }); chart.AxisX.TickLabelSpacing = 2; dataDir = dataDir + @"SetIntervalUnitBetweenLabelsOnAxis_out.docx"; doc.Save(dataDir);
如何隱藏圖表軸
如果要顯示或隱藏圖表軸,只需設(shè)置ChartAxis.Hidden屬性的值即可實(shí)現(xiàn)。下面的代碼示例顯示如何隱藏圖表的Y軸。
Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Insert chart. Shape shape = builder.InsertChart(ChartType.Column, 432, 252); Chart chart = shape.Chart; // Clear demo data. chart.Series.Clear(); // Fill data. chart.Series.Add("AW Series 1", new string[] { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" }, new double[] { 1.2, 0.3, 2.1, 2.9, 4.2 }); // Hide the Y axis. chart.AxisY.Hidden = true; dataDir = dataDir + @"HideChartAxis_out.docx"; doc.Save(dataDir);
如何對齊圖表標(biāo)簽
如果要為多行標(biāo)簽設(shè)置文本對齊方式,只需設(shè)置ChartAxis.TickLabelAlignment屬性的值即可實(shí)現(xiàn)。下面的代碼示例演示如何對標(biāo)簽對齊進(jìn)行打勾。
Document doc = new Document(dataDir + "Document.docx"); Shape shape = (Shape)doc.GetChild(NodeType.Shape, 0, true); ChartAxis axis = shape.Chart.AxisX; //This property has effect only for multi-line labels. axis.TickLabelAlignment = ParagraphAlignment.Right; doc.Save(dataDir + "Document_out.docx");