Chart — Aspose.Cells FOSS for Python API Reference

aspose-cells-foss hỗ trợ nhúng biểu đồ trong các bảng tính. Biểu đồ được quản lý thông qua ws.charts, một ChartCollection đính kèm vào mỗi Worksheet. Dữ liệu series được thêm thông qua chart.n_series, một NSeries bộ sưu tập.

Enumerations: aspose.cells_foss

from aspose.cells_foss import ChartType

ChartType

ChartType là một IntEnum xác định loại biểu đồ.

EnumerationsEnumerationsEnumerations
LINE0Hoàn toàn được hỗ trợ khi lưu.
BAR1Hoàn toàn được hỗ trợ khi lưu.
PIE2Được hỗ trợ đầy đủ cho việc lưu.
AREA3Được hỗ trợ đầy đủ cho việc lưu.
BOX_WHISKER4Đã lưu bằng định dạng chartEx.
WATERFALL5Đã lưu bằng định dạng chartEx.
COMBO6Enumerations NotImplementedError Khi lưu.
SCATTER7Enumerations NotImplementedError Khi lưu.
STOCK8Được hỗ trợ đầy đủ cho việc lưu.
SURFACE9Enumerations NotImplementedError Khi lưu.
RADAR10Enumerations NotImplementedError Khi lưu.
TREEMAP11Enumerations NotImplementedError Khi lưu.
SUNBURST12Enumerations NotImplementedError khi lưu.
HISTOGRAM13Enumerations NotImplementedError khi lưu.
FUNNEL14Enumerations NotImplementedError khi lưu.
MAP15Enumerations NotImplementedError khi lưu.

Enumerations

Chart các đối tượng không được tạo trực tiếp. Sử dụng ws.charts.add(...) hoặc các phương thức tiện lợi đã định kiểu (add_bar(), add_line(), add_pie(), add_area()).

Enumerations

EnumerationsEnumerationsEnumerationsEnumerations
typeChartTypeEnumerationsLoại biểu đồ được đặt khi tạo.
title`strNone`Đọc/Ghi
category_data`strNone`Đọc/Ghi
n_seriesNSeriesEnumerationsBộ sưu tập các chuỗi dữ liệu.
show_legendboolĐọc/GhiTrue (mặc định) để hiển thị chú giải biểu đồ.
legend_positionstrĐọc/GhiVị trí chú giải: 'right', 'left', 'top', 'bottom', 'top_right' (cũng chấp nhận dạng ngắn 'r', 'l', 't', 'b', 'tr').
smoothboolĐọc/GhiTrue để sử dụng các đường mượt (spline) trên biểu đồ đường.
groupingstrĐọc/GhiNhóm series: 'standard', 'stacked', 'percentStacked', 'clustered'.
bar_directionstrĐọc/GhiEnumerations BAR biểu đồ: 'bar' (ngang) hoặc 'col' (dọc/cột).
gap_widthintĐọc/GhiKhoảng cách giữa các cụm thanh/cột, 0–500 (mặc định 150).
overlapintĐọc/GhiPhần trăm chồng lấn series, -100 đến 100 (mặc định 0).
vary_colorsboolĐọc/GhiTrue để gán màu riêng biệt cho mỗi điểm dữ liệu.
first_slice_angleintĐọc/GhiGóc bắt đầu cho lát bánh pie đầu tiên, 0–360 độ.
is_3dboolĐọc/GhiTrue để bật chế độ render 3D.
view_3dChartView3DEnumerations3D rotation and perspective settings.
axeslist[ChartAxis]EnumerationsDanh sách các trục biểu đồ.
upper_left_rowintEnumerations0-based row index of the chart’s top-left anchor cell.
upper_left_columnintEnumerations0-based column index of the chart’s top-left anchor cell.
lower_right_rowintEnumerations0-based row index of the chart’s bottom-right anchor cell.
lower_right_columnintEnumerations0-based column index of the chart’s bottom-right anchor cell.

ChartCollection

ChartCollection được truy cập như ws.charts. Nó chứa tất cả các biểu đồ được nhúng trong một bảng tính.

Enumerations

add

ws.charts.add(
    chart_type: ChartType,
    upper_left_row: int,
    upper_left_column: int,
    lower_right_row: int,
    lower_right_column: int
) -> Chart

Thêm một biểu đồ loại được chỉ định. Tất cả các đối số vị trí đều là chỉ số hàng/cột bắt đầu từ 0.

add_bar

ws.charts.add_bar(upper_left_row, upper_left_column, lower_right_row, lower_right_column) -> Chart

Phím tắt cho add(ChartType.BAR, ...).

add_line

ws.charts.add_line(upper_left_row, upper_left_column, lower_right_row, lower_right_column) -> Chart

Phím tắt cho add(ChartType.LINE, ...).

add_pie

ws.charts.add_pie(upper_left_row, upper_left_column, lower_right_row, lower_right_column) -> Chart

Phím tắt cho add(ChartType.PIE, ...).

add_area

ws.charts.add_area(upper_left_row, upper_left_column, lower_right_row, lower_right_column) -> Chart

Phím tắt cho add(ChartType.AREA, ...).

Thuộc tính và Lặp lại

EnumerationsEnumerations
countSố lượng biểu đồ trong bộ sưu tập (chỉ đọc).
__len__()Enumerations count.
__iter__()Lặp qua tất cả Chart đối tượng.
__getitem__(index)Trả về Chart tại chỉ số bắt đầu từ 0 index.

NSeries

NSeries được truy cập qua chart.n_series. Nó quản lý các chuỗi dữ liệu thuộc về một biểu đồ.

Enumerations

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

Thêm một chuỗi dữ liệu. area là một chuỗi phạm vi như "Sheet1!B1:B5". Đặt is_vertical=True khi dữ liệu được sắp xếp theo cột. chart_type ghi đè loại biểu đồ cha cho chuỗi này (được sử dụng trong biểu đồ kết hợp).

Thuộc tính và Lặp lại

EnumerationsEnumerations
countSố lượng chuỗi (chỉ đọc).
__len__()Enumerations count.
__iter__()Duyệt qua tất cả ChartSeries đối tượng.
__getitem__(index)Trả về ChartSeries ở chỉ số bắt đầu từ 0 index.

ChartSeries

ChartSeries đại diện cho một chuỗi dữ liệu duy nhất trong biểu đồ. Được lấy thông qua chart.n_series[index].

Enumerations

EnumerationsEnumerationsEnumerations
valuesstrChuỗi phạm vi cho dữ liệu giá trị Y (ví dụ,., "Sheet1!B2:B6").
category_datastrChuỗi phạm vi cho các nhãn danh mục/trục X cho chuỗi này.
name`strNone`
chart_type`ChartTypeNone`
x_valuesstrChuỗi phạm vi cho các giá trị X của biểu đồ phân tán.
error_bars`ChartErrorBarsNone`
smoothboolTrue để sử dụng các đường mượt cho chuỗi này.

Enumerations

add_error_bars

series.add_error_bars(
    direction='y',
    bar_type='both',
    val_type='fixedVal',
    val=1.0,
    no_end_cap=False
) -> ChartErrorBars

Thêm thanh lỗi vào chuỗi này và trả về ChartErrorBars đối tượng.

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

ChartAxis

ChartAxis được truy cập qua chart.axes[index].

Enumerations

EnumerationsEnumerationsEnumerations
title`strNone`
number_format`strNone`
minimum_scale`floatNone`
maximum_scale`floatNone`
major_unit`floatNone`
minor_unit`floatNone`
is_logarithmicboolTrue để sử dụng thang đo logarit.
log_basefloatCơ số của logarit khi is_logarithmicTrue.
crossesstrVị trí mà trục này cắt trục vuông góc: 'autoZero', 'min', 'max'.
crosses_at`floatNone`
reverse_orderboolTrue để đảo ngược hướng trục.
axis_typestrLoại danh mục trục: 'catAx', 'valAx', 'dateAx', 'serAx'.
orientationstr'minMax' cho hướng bình thường, 'maxMin' cho hướng ngược.

ChartView3D

ChartView3D được truy cập qua chart.view_3d. Chỉ áp dụng khi chart.is_3dTrue.

Enumerations

EnumerationsEnumerationsEnumerations
rotation_xintXoay trục X (độ cao), 0–90 độ.
rotation_yintXoay trục Y, 0–360 độ.
right_angle_axesboolTrue để sử dụng các trục góc vuông (isometric view).
perspectiveintBiến dạng phối cảnh, 0–100. Bỏ qua khi right_angle_axesTrue.
height_percentintChiều cao biểu đồ dưới dạng phần trăm của cơ sở, 5–500.
depth_percentintĐộ sâu biểu đồ tính theo phần trăm của cơ sở, 20–2000.

ChartErrorBars

ChartErrorBars được truy cập qua series.error_bars. Tạo bằng series.add_error_bars(...).

Enumerations

EnumerationsEnumerationsEnumerations
directionstrHướng thanh lỗi: 'x', 'y', 'xy'.
bar_typestrThanh nào sẽ hiển thị: 'both', 'minus', 'plus'.
val_typestrKiểu giá trị: 'fixedVal', 'percentage', 'stdDev', 'stdErr'.
valfloatGiá trị cố định hoặc phần trăm được sử dụng khi val_type'fixedVal' hoặc 'percentage'.
no_end_capboolTrue để bỏ qua đường đầu mút trên các thanh lỗi.
plus_formula`strNone`
minus_formula`strNone`
line_width`intNone`
line_color`strNone`

Enumerations

Ví dụ sau ghi dữ liệu doanh thu hàng tháng, tạo một biểu đồ cột nhóm có tiêu đề và hai chuỗi dữ liệu, và lưu workbook.

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

Xem Thêm

 Tiếng Việt