Aspose.Words for .NET图表教程——如何设置图表轴属性

本文将进入关于“使用图表”的介绍,在Aspose.Words中学会如何设置图表轴属性,包括设置轴的日期时间值、设置轴的界限、在标签之间设置间隔单位、隐藏图表轴和对齐图表标签。

Aspose.Words For .Net是一种高级Word文档处理API,用于执行各种文档管理和操作任务。API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word。此外,API支持所有流行的Word处理文件格式,并允许将Word文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。

接下来我们将进入关于“使用图表”的介绍,在Aspose.Words中学会如何设置图表轴属性,包括设置轴的日期时间值、设置轴的界限、在标签之间设置间隔单位、隐藏图表轴和对齐图表标签。

>>Aspose.Words for .NET更新至最新版v19.12,支持转换为PDF 1.7标准,体验


如何设置图表轴属性

如果要使用图表轴,缩放比例和值轴的显示单位,请使用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);

还想要更多吗可以点击阅读【2019 · Aspose最新资源整合】查找需要的教程资源。让人兴奋的是Aspose.Total限时直降10000元!java版另送IDE开发工具一套!联系客服立马1分钟了解全部咨询!


如何设置轴的日期时间值

以下代码示例显示如何为轴属性设置日期/时间值。

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);

如何设置轴的界限

AxisBound类表示轴值的最小或最大范围。绑定可以指定为数字,日期时间或特殊的“自动”值。以下代码示例显示如何设置轴的边界。

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);

如何在标签之间设置间隔单位

下面的代码示例演示如何设置轴上标签之间的间隔单位。

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);

如何隐藏图表轴

如果要显示或隐藏图表轴,只需设置ChartAxis.Hidden属性的值即可实现。下面的代码示例显示如何隐藏图表的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);

如何对齐图表标签

如果要为多行标签设置文本对齐方式,只需设置ChartAxis.TickLabelAlignment属性的值即可实现。下面的代码示例演示如何对标签对齐进行打勾。

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");

标签:

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2019年11月6日
下一篇 2019年11月7日

相关推荐

发表回复

登录后才能评论