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 ChartTypeChartType
ChartType là một IntEnum xác định loại biểu đồ.
| Enumerations | Enumerations | Enumerations |
|---|---|---|
LINE | 0 | Hoàn toàn được hỗ trợ khi lưu. |
BAR | 1 | Hoàn toàn được hỗ trợ khi lưu. |
PIE | 2 | Được hỗ trợ đầy đủ cho việc lưu. |
AREA | 3 | Được hỗ trợ đầy đủ cho việc lưu. |
BOX_WHISKER | 4 | Đã lưu bằng định dạng chartEx. |
WATERFALL | 5 | Đã lưu bằng định dạng chartEx. |
COMBO | 6 | Enumerations NotImplementedError Khi lưu. |
SCATTER | 7 | Enumerations NotImplementedError Khi lưu. |
STOCK | 8 | Được hỗ trợ đầy đủ cho việc lưu. |
SURFACE | 9 | Enumerations NotImplementedError Khi lưu. |
RADAR | 10 | Enumerations NotImplementedError Khi lưu. |
TREEMAP | 11 | Enumerations NotImplementedError Khi lưu. |
SUNBURST | 12 | Enumerations NotImplementedError khi lưu. |
HISTOGRAM | 13 | Enumerations NotImplementedError khi lưu. |
FUNNEL | 14 | Enumerations NotImplementedError khi lưu. |
MAP | 15 | Enumerations 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
| Enumerations | Enumerations | Enumerations | Enumerations |
|---|---|---|---|
type | ChartType | Enumerations | Loại biểu đồ được đặt khi tạo. |
title | `str | None` | Đọc/Ghi |
category_data | `str | None` | Đọc/Ghi |
n_series | NSeries | Enumerations | Bộ sưu tập các chuỗi dữ liệu. |
show_legend | bool | Đọc/Ghi | True (mặc định) để hiển thị chú giải biểu đồ. |
legend_position | str | Đọc/Ghi | Vị 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'). |
smooth | bool | Đọc/Ghi | True để sử dụng các đường mượt (spline) trên biểu đồ đường. |
grouping | str | Đọc/Ghi | Nhóm series: 'standard', 'stacked', 'percentStacked', 'clustered'. |
bar_direction | str | Đọc/Ghi | Enumerations BAR biểu đồ: 'bar' (ngang) hoặc 'col' (dọc/cột). |
gap_width | int | Đọc/Ghi | Khoảng cách giữa các cụm thanh/cột, 0–500 (mặc định 150). |
overlap | int | Đọc/Ghi | Phần trăm chồng lấn series, -100 đến 100 (mặc định 0). |
vary_colors | bool | Đọc/Ghi | True để gán màu riêng biệt cho mỗi điểm dữ liệu. |
first_slice_angle | int | Đọc/Ghi | Góc bắt đầu cho lát bánh pie đầu tiên, 0–360 độ. |
is_3d | bool | Đọc/Ghi | True để bật chế độ render 3D. |
view_3d | ChartView3D | Enumerations | 3D rotation and perspective settings. |
axes | list[ChartAxis] | Enumerations | Danh sách các trục biểu đồ. |
upper_left_row | int | Enumerations | 0-based row index of the chart’s top-left anchor cell. |
upper_left_column | int | Enumerations | 0-based column index of the chart’s top-left anchor cell. |
lower_right_row | int | Enumerations | 0-based row index of the chart’s bottom-right anchor cell. |
lower_right_column | int | Enumerations | 0-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
) -> ChartThê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) -> ChartPhí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) -> ChartPhí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) -> ChartPhí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) -> ChartPhím tắt cho add(ChartType.AREA, ...).
Thuộc tính và Lặp lại
| Enumerations | Enumerations |
|---|---|
count | Số 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
) -> ChartSeriesThê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
| Enumerations | Enumerations |
|---|---|
count | Số 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
| Enumerations | Enumerations | Enumerations |
|---|---|---|
values | str | Chuỗi phạm vi cho dữ liệu giá trị Y (ví dụ,., "Sheet1!B2:B6"). |
category_data | str | Chuỗi phạm vi cho các nhãn danh mục/trục X cho chuỗi này. |
name | `str | None` |
chart_type | `ChartType | None` |
x_values | str | Chuỗi phạm vi cho các giá trị X của biểu đồ phân tán. |
error_bars | `ChartErrorBars | None` |
smooth | bool | True để 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
) -> ChartErrorBarsThêm thanh lỗi vào chuỗi này và trả về ChartErrorBars đối tượng.
| Enumerations | Enumerations |
|---|---|
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
| Enumerations | Enumerations | Enumerations |
|---|---|---|
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 để sử dụng thang đo logarit. |
log_base | float | Cơ số của logarit khi is_logarithmic là True. |
crosses | str | Vị trí mà trục này cắt trục vuông góc: 'autoZero', 'min', 'max'. |
crosses_at | `float | None` |
reverse_order | bool | True để đảo ngược hướng trục. |
axis_type | str | Loại danh mục trục: 'catAx', 'valAx', 'dateAx', 'serAx'. |
orientation | str | '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_3d là True.
Enumerations
| Enumerations | Enumerations | Enumerations |
|---|---|---|
rotation_x | int | Xoay trục X (độ cao), 0–90 độ. |
rotation_y | int | Xoay trục Y, 0–360 độ. |
right_angle_axes | bool | True để sử dụng các trục góc vuông (isometric view). |
perspective | int | Biến dạng phối cảnh, 0–100. Bỏ qua khi right_angle_axes là True. |
height_percent | int | Chiều cao biểu đồ dưới dạng phần trăm của cơ sở, 5–500. |
depth_percent | int | Độ 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
| Enumerations | Enumerations | Enumerations |
|---|---|---|
direction | str | Hướng thanh lỗi: 'x', 'y', 'xy'. |
bar_type | str | Thanh nào sẽ hiển thị: 'both', 'minus', 'plus'. |
val_type | str | Kiểu giá trị: 'fixedVal', 'percentage', 'stdDev', 'stdErr'. |
val | float | Giá trị cố định hoặc phần trăm được sử dụng khi val_type là 'fixedVal' hoặc 'percentage'. |
no_end_cap | bool | True để bỏ qua đường đầu mút trên các thanh lỗi. |
plus_formula | `str | None` |
minus_formula | `str | None` |
line_width | `int | None` |
line_color | `str | None` |
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")