Chart — Aspose.Cells FOSS for Python API Reference
aspose-cells-foss 支持在工作表中嵌入图表。图表通过 ws.charts,,一个 ChartCollection 附加到每个 Worksheet.。系列数据通过 chart.n_series,,一个 NSeries 集合。.
Properties: aspose.cells_foss
from aspose.cells_foss import ChartTypeChartType
ChartType 是一个 IntEnum 用于标识图表类型的.
| Properties | Properties | Properties |
|---|---|---|
LINE | 0 | 完全支持保存。. |
BAR | 1 | 完全支持保存。. |
PIE | 2 | 完全支持保存。. |
AREA | 3 | 完全支持保存。. |
BOX_WHISKER | 4 | 使用 chartEx 格式保存。. |
WATERFALL | 5 | 使用 chartEx 格式保存。. |
COMBO | 6 | Properties NotImplementedError 在保存时。. |
SCATTER | 7 | Properties NotImplementedError 在保存时。. |
STOCK | 8 | 完全支持保存。. |
SURFACE | 9 | Properties NotImplementedError 在保存时。. |
RADAR | 10 | Properties NotImplementedError 在保存时。. |
TREEMAP | 11 | Properties NotImplementedError 在保存时。. |
SUNBURST | 12 | Properties NotImplementedError 保存时。. |
HISTOGRAM | 13 | Properties NotImplementedError 保存时。. |
FUNNEL | 14 | Properties NotImplementedError 保存时。. |
MAP | 15 | Properties NotImplementedError 保存时。. |
Properties
Chart 对象不能直接构造。使用 ws.charts.add(...) 或使用类型化的便利方法(add_bar(), add_line(), add_pie(), add_area()).
Properties
| Properties | Properties | Properties | Properties |
|---|---|---|---|
type | ChartType | Properties | 图表类型在创建时设置。. |
title | `str | None` | 读/写 |
category_data | `str | None` | 读/写 |
n_series | NSeries | Properties | 数据系列的集合。. |
show_legend | bool | 读/写 | True (default) 显示图例。. |
legend_position | str | 读/写 | 图例位置:: 'right', 'left', 'top', 'bottom', 'top_right' (也接受简写形式 'r', 'l', 't', 'b', 'tr'). |
smooth | bool | 读取/写入 | True 在折线图上使用平滑(样条)线。. |
grouping | str | Read/Write | 系列分组:: 'standard', 'stacked', 'percentStacked', 'clustered'. |
bar_direction | str | Read/Write | Properties BAR 图表:: 'bar' (水平)或 'col' (垂直/柱形)。. |
gap_width | int | Read/Write | 条形/柱形簇之间的间距,0–500(默认 150)。. |
overlap | int | Read/Write | 系列重叠百分比,-100 到 100(默认 0)。. |
vary_colors | bool | Read/Write | True 为每个数据点分配不同的颜色。. |
first_slice_angle | int | Read/Write | 第一块饼图的起始角度,0–360 度。. |
is_3d | bool | Read/Write | True 启用 3D 渲染。. |
view_3d | ChartView3D | Properties | 3D rotation and perspective settings. |
axes | list[ChartAxis] | Properties | 图表坐标轴列表。. |
upper_left_row | int | Properties | 0-based row index of the chart’s top-left anchor cell. |
upper_left_column | int | Properties | 0-based column index of the chart’s top-left anchor cell. |
lower_right_row | int | Properties | 0-based row index of the chart’s bottom-right anchor cell. |
lower_right_column | int | Properties | 0-based column index of the chart’s bottom-right anchor cell. |
ChartCollection
ChartCollection 可通过以下方式访问 ws.charts. 它包含工作表中嵌入的所有图表。.
Properties
add
ws.charts.add(
chart_type: ChartType,
upper_left_row: int,
upper_left_column: int,
lower_right_row: int,
lower_right_column: int
) -> Chart添加指定类型的图表。所有位置参数均为从 0 开始的行/列索引。.
add_bar
ws.charts.add_bar(upper_left_row, upper_left_column, lower_right_row, lower_right_column) -> Chart快捷键用于 add(ChartType.BAR, ...).
add_line
ws.charts.add_line(upper_left_row, upper_left_column, lower_right_row, lower_right_column) -> Chart快捷键 add(ChartType.LINE, ...).
add_pie
ws.charts.add_pie(upper_left_row, upper_left_column, lower_right_row, lower_right_column) -> Chart快捷键 add(ChartType.PIE, ...).
add_area
ws.charts.add_area(upper_left_row, upper_left_column, lower_right_row, lower_right_column) -> Chart快捷键 add(ChartType.AREA, ...).
属性和迭代
| Properties | Properties |
|---|---|
count | 集合中图表的数量(只读)。. |
__len__() | Properties count. |
__iter__() | 遍历所有 Chart 对象。. |
__getitem__(index) | 返回 Chart 在零基 index. |
NSeries
NSeries 通过…访问 chart.n_series.。它管理属于图表的数据系列。.
Properties
add
chart.n_series.add(
area: str,
is_vertical: bool = False,
category_data: str | None = None,
name: str | None = None,
chart_type: ChartType | None = None
) -> ChartSeries添加一个数据系列。. area 是一个范围字符串,例如 "Sheet1!B1:B5".。设置 is_vertical=True 当数据按列排列时。. chart_type 覆盖此系列的父图表类型(用于组合图表)。.
属性和迭代
| Properties | Properties |
|---|---|
count | 系列数量(只读)。. |
__len__() | Properties count. |
__iter__() | 遍历所有 ChartSeries 对象。. |
__getitem__(index) | 返回 ChartSeries 基于零索引 index. |
ChartSeries
ChartSeries 表示图表中的单个数据系列。通过以下方式获取: chart.n_series[index].
Properties
| Properties | Properties | Properties |
|---|---|---|
values | str | Y 值数据的范围字符串(例如,., "Sheet1!B2:B6"). |
category_data | str | 此系列的类别/X 轴标签的范围字符串。. |
name | `str | None` |
chart_type | `ChartType | None` |
x_values | str | 散点图 X 值的范围字符串。. |
error_bars | `ChartErrorBars | None` |
smooth | bool | True 为此系列使用平滑线。. |
Properties
add_error_bars
series.add_error_bars(
direction='y',
bar_type='both',
val_type='fixedVal',
val=1.0,
no_end_cap=False
) -> ChartErrorBars为此系列添加误差线并返回 ChartErrorBars 对象。.
| Properties | Properties |
|---|---|
direction | 'x', 'y', 'xy' |
bar_type | 'both', 'minus', 'plus' |
val_type | 'fixedVal', 'percentage', 'stdDev', 'stdErr' |
ChartAxis
ChartAxis 通过以下方式访问 chart.axes[index].
Properties
| Properties | Properties | Properties |
|---|---|---|
title | `str | None` |
number_format | `str | None` |
minimum_scale | `float | None` |
maximum_scale | `float | None` |
major_unit | `float | None` |
minor_unit | `float | None` |
is_logarithmic | bool | True 使用对数刻度。. |
log_base | float | 对数的底数,当 is_logarithmic 为 True. |
crosses | str | 此轴与垂直轴相交的位置:: 'autoZero', 'min', 'max'. |
crosses_at | `float | None` |
reverse_order | bool | True 反转轴方向。. |
axis_type | str | 轴类别类型:: 'catAx', 'valAx', 'dateAx', 'serAx'. |
orientation | str | 'minMax' 用于正常方向,, 'maxMin' 用于反向。. |
ChartView3D
ChartView3D 通过以下方式访问 chart.view_3d.。仅在以下情况下适用 chart.is_3d 为 True.
Properties
| Properties | Properties | Properties |
|---|---|---|
rotation_x | int | X 轴旋转(仰角),0–90 度。. |
rotation_y | int | Y 轴旋转,0–360 度。. |
right_angle_axes | bool | True 使用直角坐标轴(等轴视图)。. |
perspective | int | 透视畸变,0–100。忽略当 right_angle_axes 是 True. |
height_percent | int | 图表高度占基准的百分比,5–500。. |
depth_percent | int | 图表深度相对于基准的百分比,20–2000。. |
ChartErrorBars
ChartErrorBars 通过 series.error_bars.。使用以下方式创建 series.add_error_bars(...).
Properties
| Properties | Properties | Properties |
|---|---|---|
direction | str | 误差线方向:: 'x', 'y', 'xy'. |
bar_type | str | 要显示的误差线:: 'both', 'minus', 'plus'. |
val_type | str | 数值类型:: 'fixedVal', 'percentage', 'stdDev', 'stdErr'. |
val | float | 在以下情况下使用固定值或百分比值: val_type 是 'fixedVal' 或 'percentage'. |
no_end_cap | bool | True 以省略误差线的端帽线。. |
plus_formula | `str | None` |
minus_formula | `str | None` |
line_width | `int | None` |
line_color | `str | None` |
Properties
下面的示例写入月度收入数据,创建带标题和两个数据系列的簇状条形图,并保存工作簿。.
from aspose.cells_foss import Workbook, ChartType
wb = Workbook()
ws = wb.worksheets[0]
ws.name = "Revenue"
# Write headers and data
headers = ["Month", "Product A", "Product B"]
data = [
["Jan", 12000, 9500],
["Feb", 14500, 11000],
["Mar", 13200, 12800],
["Apr", 15800, 14100],
]
for col, h in enumerate(headers):
ws.cells[0][col].put_value(h)
for row, row_data in enumerate(data, start=1):
for col, val in enumerate(row_data):
ws.cells[row][col].put_value(val)
# Add a bar chart covering rows 6-20, columns 0-6
chart = ws.charts.add_bar(6, 0, 20, 6)
chart.title = "Monthly Revenue by Product"
chart.bar_direction = "col" # vertical (column chart)
chart.grouping = "clustered"
# Series: Product A and Product B
chart.n_series.add("Revenue!B2:B5", is_vertical=True, category_data="Revenue!A2:A5", name="Product A")
chart.n_series.add("Revenue!C2:C5", is_vertical=True, category_data="Revenue!A2:A5", name="Product B")
chart.show_legend = True
chart.legend_position = "bottom"
wb.save("revenue_chart.xlsx")