Manage Titles of Excel Charts
In Excel charts, there are 2 kinds of title:
- Chart Title
- Axis Titles
Title Options
Aspose.Cells for Python via .NET also allows to manage chart’s titles at runtime,with Title object, you can change text, font and fill format for titles.
||
Setting the Titles of Charts or Axes
You can use Microsoft Excel to set the titles of a chart and its axes in a WYSIWYG environment. Aspose.Cells for Python via .NET also allows developers to set the titles of a chart and its axes at runtime. All charts and their axes contain a title property that can be used to set their titles as shown below in an example.
The following code snippet demonstrates how to set titles to charts and axes.
from aspose.cells import Workbook | |
from aspose.cells.charts import ChartType | |
from aspose.cells.drawing import GradientStyleType | |
from aspose.pydrawing import Color | |
from os import os, path | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Create directory if it is not already present. | |
IsExists = path.isdir(dataDir) | |
if notIsExists: | |
os.makedirs(dataDir) | |
# Instantiating a Workbook object | |
workbook = Workbook() | |
# Adding a new worksheet to the Workbook object | |
sheetIndex = workbook.worksheets.add() | |
# Obtaining the reference of the newly added worksheet by passing its sheet index | |
worksheet = workbook.worksheets[sheetIndex] | |
# Adding sample values to cells | |
worksheet.cells.get("A1").put_value(50) | |
worksheet.cells.get("A2").put_value(100) | |
worksheet.cells.get("A3").put_value(150) | |
worksheet.cells.get("B1").put_value(60) | |
worksheet.cells.get("B2").put_value(32) | |
worksheet.cells.get("B3").put_value(50) | |
# Adding a chart to the worksheet | |
chartIndex = worksheet.charts.add(ChartType.COLUMN, 5, 0, 15, 5) | |
# Accessing the instance of the newly added chart | |
chart = worksheet.charts[chartIndex] | |
# Adding SeriesCollection (chart data source) to the chart ranging from "A1" cell to "B3" | |
chart.n_series.add("A1:B3", True) | |
# Setting the foreground color of the plot area | |
chart.plot_area.area.foreground_color = Color.blue | |
# Setting the foreground color of the chart area | |
chart.chart_area.area.foreground_color = Color.yellow | |
# Setting the foreground color of the 1st SeriesCollection area | |
chart.n_series[0].area.foreground_color = Color.red | |
# Setting the foreground color of the area of the 1st SeriesCollection point | |
chart.n_series[0].points[0].area.foreground_color = Color.cyan | |
# Filling the area of the 2nd SeriesCollection with a gradient | |
chart.n_series[1].area.fill_format.set_one_color_gradient(Color.lime, 1, GradientStyleType.HORIZONTAL, 1) | |
# Setting the title of a chart | |
chart.title.text = "Title" | |
# Setting the font color of the chart title to blue | |
chart.title.font.color = Color.blue | |
# Setting the title of category axis of the chart | |
chart.category_axis.title.text = "Category" | |
# Setting the title of value axis of the chart | |
chart.value_axis.title.text = "Value" | |
# Saving the Excel file | |
workbook.save(dataDir + "book1.out.xls") |