Chart — Aspose.Cells FOSS for Python API Reference
aspose-cells-foss รองรับการฝังแผนภูมิในแผ่นงาน. แผนภูมิจะถูกจัดการผ่าน ws.charts, a 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 | อ่าน/เขียน | การจัดกลุ่มซีรีส์: 'standard', 'stacked', 'percentStacked', 'clustered'. |
bar_direction | str | อ่าน/เขียน | Properties BAR แผนภูมิ: 'bar' (แนวนอน) หรือ 'col' (แนวตั้ง/คอลัมน์). |
gap_width | int | อ่าน/เขียน | ช่องว่างระหว่างกลุ่มแถบ/คอลัมน์, 0–500 (ค่าเริ่มต้น 150). |
overlap | int | อ่าน/เขียน | เปอร์เซ็นต์การทับซ้อนของซีรีส์, -100 ถึง 100 (ค่าเริ่มต้น 0). |
vary_colors | bool | อ่าน/เขียน | True เพื่อกำหนดสีที่แตกต่างให้กับแต่ละจุดข้อมูล. |
first_slice_angle | int | อ่าน/เขียน | มุมเริ่มต้นสำหรับชิ้นพายชิ้นแรก, 0–360 องศา. |
is_3d | bool | อ่าน/เขียน | True เพื่อเปิดใช้งานการเรนเดอร์ 3 มิติ. |
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")