How to set category axis
Possible Usage Scenarios
After you create a chart in a worksheet, then you can set category axis for it. In this article, we will show you how to set category axis for Excel chart, using Aspose.Cells for Python via .NET with sample code.
The steps in sample code
-
Create a new workbook.
-
Create a new chart in the first worksheet.
-
Add some values to cells in the first worksheet.
-
Now you can set the category axis, there are two ways: using cell data, or using strings directly, both of which are shown in the sample code.
-
Set value axis, save the workbook to view the result.
Sample Code
from aspose.cells import Workbook | |
from aspose.cells.charts import ChartType, LegendPositionType | |
# How to Set Category Axis | |
# Your local test path | |
path = r"" | |
# Create a new workbook | |
workbook = Workbook() | |
worksheet = workbook.worksheets[0] | |
worksheet.name = "CHART" | |
# Add a chart to the worksheet | |
chartIndex = worksheet.charts.add(ChartType.COLUMN, 8, 0, 20, 10) | |
chart = worksheet.charts[chartIndex] | |
# Add some values to cells | |
worksheet.cells.get("A1").put_value("Sales") | |
worksheet.cells.get("A2").put_value(100) | |
worksheet.cells.get("A3").put_value(150) | |
worksheet.cells.get("A4").put_value(130) | |
worksheet.cells.get("A5").put_value(160) | |
worksheet.cells.get("A6").put_value(150) | |
worksheet.cells.get("B1").put_value("Days") | |
worksheet.cells.get("B2").put_value(1) | |
worksheet.cells.get("B3").put_value(2) | |
worksheet.cells.get("B4").put_value(3) | |
worksheet.cells.get("B5").put_value(4) | |
worksheet.cells.get("B6").put_value(5) | |
# Some values in string | |
Sales = "100,150,130,160,150" | |
Days = "1,2,3,4,5" | |
# Set Category Axis Data with string | |
chart.n_series.category_data = "{" + 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.n_series.add("Demand1", True) | |
# Set value axis with string | |
chart.n_series[0].values = "{" + Sales + "}" | |
chart.n_series.add("Demand2", True) | |
# Set value axis with data in cells | |
chart.n_series[1].values = "A2:A6" | |
# Set some Category Axis properties | |
chart.category_axis.tick_labels.rotation_angle = 45 | |
chart.category_axis.tick_labels.font.size = 8 | |
chart.legend.position = LegendPositionType.BOTTOM | |
# Save the workbook to view the result file | |
workbook.save(path + "Output.xlsx") |