如何设置类别轴
Contents
[
Hide
]
可能的使用场景
在工作表中创建图表后,您可以为其设置类别轴。在本文中,我们将向您展示如何使用Aspose.Cells为Excel图表设置类别轴,并提供示例代码。
示例代码中的步骤
-
创建一个新的工作簿。
-
在第一个工作表中创建一个新的图表。
-
在第一个工作表的单元格中添加一些值。
-
现在您可以设置分类轴,有两种方式:使用单元格数据或直接使用字符串,两者都在示例代码中展示。
-
设置值轴,保存工作簿以查看结果。
示例代码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//How to Set Category Axis | |
//Your local test path | |
String path = @""; | |
//Create a new workbook | |
Workbook workbook = new Workbook(); | |
Worksheet worksheet = workbook.Worksheets[0]; | |
worksheet.Name = ("CHART"); | |
// Add a chart to the worksheet | |
int chartIndex = worksheet.Charts.Add(ChartType.Column, 8, 0, 20, 10); | |
Chart chart = worksheet.Charts[chartIndex]; | |
//Add some values to cells | |
worksheet.Cells["A1"].PutValue("Sales"); | |
worksheet.Cells["A2"].PutValue(100); | |
worksheet.Cells["A3"].PutValue(150); | |
worksheet.Cells["A4"].PutValue(130); | |
worksheet.Cells["A5"].PutValue(160); | |
worksheet.Cells["A6"].PutValue(150); | |
worksheet.Cells["B1"].PutValue("Days"); | |
worksheet.Cells["B2"].PutValue(1); | |
worksheet.Cells["B3"].PutValue(2); | |
worksheet.Cells["B4"].PutValue(3); | |
worksheet.Cells["B5"].PutValue(4); | |
worksheet.Cells["B6"].PutValue(5); | |
//Some values in string | |
String Sales = "100,150,130,160,150"; | |
String Days = "1,2,3,4,5"; | |
//Set Category Axis Data with string | |
chart.NSeries.CategoryData = "{" + Days + "}"; | |
//Or you can set Category Axis Data with data in cells, try it! | |
//chart.NSeries.CategoryData = "B2:B6"; | |
//Add Series to the chart | |
chart.NSeries.Add("Demand1", true); | |
//Set value axis with string | |
chart.NSeries[0].Values = "{" + Sales + "}"; | |
chart.NSeries.Add("Demand2", true); | |
//Set value axis with data in cells | |
chart.NSeries[1].Values = "A2:A6"; | |
//Set some Category Axis properties | |
chart.CategoryAxis.TickLabels.RotationAngle = 45; | |
chart.CategoryAxis.TickLabels.Font.Size = 8; | |
chart.Legend.Position = LegendPositionType.Bottom; | |
//Save the workbook to view the result file | |
workbook.Save(path + "Output.xlsx"); |