管理Excel图表的标题
Contents
[
Hide
]
在Excel图表中,有两种标题:
- 图表标题
- 轴标题
标题选项
Aspose.Cells for Python via .NET 还允许在运行时管理图表的标题,借助 Title 对象,可以更改标题的文本、字体和填充格式。
||
设置图表或坐标轴的标题
你可以使用 Microsoft Excel 在可视化环境中设置图表及其轴的标题。Aspose.Cells for Python via .NET 也允许开发者在运行时设置图表和轴的标题。所有图表及其轴都包含一个 标题 属性,可以用来设置它们的标题,如下面的示例所示。
以下代码段演示了如何设置图表和轴的标题。
This file contains hidden or 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
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") |