Chart

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 ChartType

ChartType

ChartType 是一个 IntEnum 用于标识图表类型的.

PropertiesPropertiesProperties
LINE0完全支持保存。.
BAR1完全支持保存。.
PIE2完全支持保存。.
AREA3完全支持保存。.
BOX_WHISKER4使用 chartEx 格式保存。.
WATERFALL5使用 chartEx 格式保存。.
COMBO6Properties NotImplementedError 在保存时。.
SCATTER7Properties NotImplementedError 在保存时。.
STOCK8完全支持保存。.
SURFACE9Properties NotImplementedError 在保存时。.
RADAR10Properties NotImplementedError 在保存时。.
TREEMAP11Properties NotImplementedError 在保存时。.
SUNBURST12Properties NotImplementedError 保存时。.
HISTOGRAM13Properties NotImplementedError 保存时。.
FUNNEL14Properties NotImplementedError 保存时。.
MAP15Properties NotImplementedError 保存时。.

Properties

Chart 对象不能直接构造。使用 ws.charts.add(...) 或使用类型化的便利方法(add_bar(), add_line(), add_pie(), add_area()).

Properties

PropertiesPropertiesPropertiesProperties
typeChartTypeProperties图表类型在创建时设置。.
title`strNone`读/写
category_data`strNone`读/写
n_seriesNSeriesProperties数据系列的集合。.
show_legendbool读/写True (default) 显示图例。.
legend_positionstr读/写图例位置:: 'right', 'left', 'top', 'bottom', 'top_right' (也接受简写形式 'r', 'l', 't', 'b', 'tr').
smoothbool读取/写入True 在折线图上使用平滑(样条)线。.
groupingstrRead/Write系列分组:: 'standard', 'stacked', 'percentStacked', 'clustered'.
bar_directionstrRead/WriteProperties BAR 图表:: 'bar' (水平)或 'col' (垂直/柱形)。.
gap_widthintRead/Write条形/柱形簇之间的间距,0–500(默认 150)。.
overlapintRead/Write系列重叠百分比,-100 到 100(默认 0)。.
vary_colorsboolRead/WriteTrue 为每个数据点分配不同的颜色。.
first_slice_angleintRead/Write第一块饼图的起始角度,0–360 度。.
is_3dboolRead/WriteTrue 启用 3D 渲染。.
view_3dChartView3DProperties3D rotation and perspective settings.
axeslist[ChartAxis]Properties图表坐标轴列表。.
upper_left_rowintProperties0-based row index of the chart’s top-left anchor cell.
upper_left_columnintProperties0-based column index of the chart’s top-left anchor cell.
lower_right_rowintProperties0-based row index of the chart’s bottom-right anchor cell.
lower_right_columnintProperties0-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, ...).

属性和迭代

PropertiesProperties
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 覆盖此系列的父图表类型(用于组合图表)。.

属性和迭代

PropertiesProperties
count系列数量(只读)。.
__len__()Properties count.
__iter__()遍历所有 ChartSeries 对象。.
__getitem__(index)返回 ChartSeries 基于零索引 index.

ChartSeries

ChartSeries 表示图表中的单个数据系列。通过以下方式获取: chart.n_series[index].

Properties

PropertiesPropertiesProperties
valuesstrY 值数据的范围字符串(例如,., "Sheet1!B2:B6").
category_datastr此系列的类别/X 轴标签的范围字符串。.
name`strNone`
chart_type`ChartTypeNone`
x_valuesstr散点图 X 值的范围字符串。.
error_bars`ChartErrorBarsNone`
smoothboolTrue 为此系列使用平滑线。.

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 对象。.

PropertiesProperties
direction'x', 'y', 'xy'
bar_type'both', 'minus', 'plus'
val_type'fixedVal', 'percentage', 'stdDev', 'stdErr'

ChartAxis

ChartAxis 通过以下方式访问 chart.axes[index].

Properties

PropertiesPropertiesProperties
title`strNone`
number_format`strNone`
minimum_scale`floatNone`
maximum_scale`floatNone`
major_unit`floatNone`
minor_unit`floatNone`
is_logarithmicboolTrue 使用对数刻度。.
log_basefloat对数的底数,当 is_logarithmicTrue.
crossesstr此轴与垂直轴相交的位置:: 'autoZero', 'min', 'max'.
crosses_at`floatNone`
reverse_orderboolTrue 反转轴方向。.
axis_typestr轴类别类型:: 'catAx', 'valAx', 'dateAx', 'serAx'.
orientationstr'minMax' 用于正常方向,, 'maxMin' 用于反向。.

ChartView3D

ChartView3D 通过以下方式访问 chart.view_3d.。仅在以下情况下适用 chart.is_3dTrue.

Properties

PropertiesPropertiesProperties
rotation_xintX 轴旋转(仰角),0–90 度。.
rotation_yintY 轴旋转,0–360 度。.
right_angle_axesboolTrue 使用直角坐标轴(等轴视图)。.
perspectiveint透视畸变,0–100。忽略当 right_angle_axesTrue.
height_percentint图表高度占基准的百分比,5–500。.
depth_percentint图表深度相对于基准的百分比,20–2000。.

ChartErrorBars

ChartErrorBars 通过 series.error_bars.。使用以下方式创建 series.add_error_bars(...).

Properties

PropertiesPropertiesProperties
directionstr误差线方向:: 'x', 'y', 'xy'.
bar_typestr要显示的误差线:: 'both', 'minus', 'plus'.
val_typestr数值类型:: 'fixedVal', 'percentage', 'stdDev', 'stdErr'.
valfloat在以下情况下使用固定值或百分比值: val_type'fixedVal''percentage'.
no_end_capboolTrue 以省略误差线的端帽线。.
plus_formula`strNone`
minus_formula`strNone`
line_width`intNone`
line_color`strNone`

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")

另见

 中文