Tạo hoặc Cập nhật Biểu đồ Bản trình bày PowerPoint bằng Python
Tổng quan
Bài viết này cung cấp hướng dẫn toàn diện về cách tạo và tùy chỉnh biểu đồ bằng Aspose.Slides cho Python thông qua Java. Bạn sẽ học cách lập trình thêm biểu đồ vào một slide, điền dữ liệu vào biểu đồ và áp dụng các tùy chọn định dạng khác nhau để đáp ứng yêu cầu thiết kế cụ thể của mình. Trong suốt bài viết, các ví dụ mã chi tiết minh họa từng bước, từ khởi tạo bản trình bày và đối tượng biểu đồ đến cấu hình chuỗi, trục và chú giải. Khi làm theo hướng dẫn này, bạn sẽ nắm vững cách tích hợp việc tạo biểu đồ động vào các ứng dụng Python, giúp đơn giản hoá quá trình tạo bản trình bày dựa trên dữ liệu.
Tạo biểu đồ
Biểu đồ giúp người dùng nhanh chóng hình dung dữ liệu và rút ra những thông tin không hiển nhiên ngay từ bảng hoặc bảng tính.
Tại sao nên tạo biểu đồ?
Sử dụng biểu đồ, bạn có thể:
- tổng hợp, nén hoặc tóm tắt một lượng lớn dữ liệu trên một slide trong bản trình bày;
- hiển thị các mẫu và xu hướng trong dữ liệu;
- suy ra hướng và động lực của dữ liệu theo thời gian hoặc so với một đơn vị đo lường cụ thể;
- phát hiện các giá trị ngoại lệ, sai lệch, lỗi và dữ liệu vô nghĩa;
- truyền tải hoặc trình bày dữ liệu phức tạp.
Trong PowerPoint, bạn có thể tạo biểu đồ qua chức năng Insert, cung cấp các mẫu để thiết kế nhiều loại biểu đồ. Sử dụng Aspose.Slides, bạn có thể tạo cả các biểu đồ thông thường (dựa trên các loại biểu đồ phổ biến) và các biểu đồ tùy chỉnh.
Note
Sử dụng enumeration ChartType trong gói asposeslides.api . Các giá trị trong enumeration này tương ứng với các loại biểu đồ khác nhau.Tạo biểu đồ Cột Nhóm
Ví dụ này sử dụng addChart để tạo một biểu đồ cột nhóm so sánh hai chuỗi qua ba danh mục. Các chuỗi sử dụng màu nền đỏ và xanh lá, và các nhãn riêng lẻ hiển thị tên danh mục, tên chuỗi và giá trị. Chỉ số worksheet 0 đề cập tới sheet đầu tiên trong workbook biểu đồ được nhúng.
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import ChartType, FillType, NullableBool, Presentation, SaveFormat
Color = jpype.JClass("java.awt.Color")
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
chart = slide.getShapes().addChart(ChartType.ClusteredColumn, 20, 20, 500, 300)
chart.getChartTitle().addTextFrameForOverriding("Sample Title")
chart.getChartTitle().getTextFrameForOverriding().getTextFrameFormat().setCenterText(NullableBool.True_)
chart.getChartTitle().setHeight(20)
chart.setTitle(True)
workbook = chart.getChartData().getChartDataWorkbook()
worksheet_index = 0
chart.getChartData().getSeries().clear()
chart.getChartData().getCategories().clear()
series_name_cell = workbook.getCell(worksheet_index, 0, 1, "Series 1")
chart.getChartData().getSeries().add(series_name_cell, chart.getType())
series_name_cell = workbook.getCell(worksheet_index, 0, 2, "Series 2")
chart.getChartData().getSeries().add(series_name_cell, chart.getType())
category_cell = workbook.getCell(worksheet_index, 1, 0, "Category 1")
chart.getChartData().getCategories().add(category_cell)
category_cell = workbook.getCell(worksheet_index, 2, 0, "Category 2")
chart.getChartData().getCategories().add(category_cell)
category_cell = workbook.getCell(worksheet_index, 3, 0, "Category 3")
chart.getChartData().getCategories().add(category_cell)
series = chart.getChartData().getSeries().get_Item(0)
value_cell = workbook.getCell(worksheet_index, 1, 1, 20)
series.getDataPoints().addDataPointForBarSeries(value_cell)
value_cell = workbook.getCell(worksheet_index, 2, 1, 50)
series.getDataPoints().addDataPointForBarSeries(value_cell)
value_cell = workbook.getCell(worksheet_index, 3, 1, 30)
series.getDataPoints().addDataPointForBarSeries(value_cell)
series.getFormat().getFill().setFillType(FillType.Solid)
series.getFormat().getFill().getSolidFillColor().setColor(Color.RED)
series = chart.getChartData().getSeries().get_Item(1)
value_cell = workbook.getCell(worksheet_index, 1, 2, 30)
series.getDataPoints().addDataPointForBarSeries(value_cell)
value_cell = workbook.getCell(worksheet_index, 2, 2, 10)
series.getDataPoints().addDataPointForBarSeries(value_cell)
value_cell = workbook.getCell(worksheet_index, 3, 2, 60)
series.getDataPoints().addDataPointForBarSeries(value_cell)
series.getFormat().getFill().setFillType(FillType.Solid)
color = Color(0, 128, 0)
series.getFormat().getFill().getSolidFillColor().setColor(color)
label = series.getDataPoints().get_Item(0).getLabel()
label.getDataLabelFormat().setShowCategoryName(True)
label = series.getDataPoints().get_Item(1).getLabel()
label.getDataLabelFormat().setShowSeriesName(True)
label = series.getDataPoints().get_Item(2).getLabel()
label.getDataLabelFormat().setShowValue(True)
label.getDataLabelFormat().setShowSeriesName(True)
label.getDataLabelFormat().setSeparator("/")
presentation.save("AsposeChart.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
Kết quả:

Tạo biểu đồ Scatter
Biểu đồ scatter (còn gọi là biểu đồ phân tán hoặc đồ thị x‑y) thường được dùng để kiểm tra các mẫu hoặc chứng minh mối tương quan giữa hai biến.
Sử dụng biểu đồ scatter khi:
- Bạn có dữ liệu số cặp.
- Bạn có hai biến có quan hệ chặt chẽ với nhau.
- Bạn muốn xác định liệu hai biến có liên quan hay không.
- Bạn có một biến độc lập có nhiều giá trị cho một biến phụ thuộc.
Ví dụ này vẽ hai tập giá trị x‑y. Chuỗi đầu tiên dùng đường thẳng với dấu sao, trong khi chuỗi thứ hai dùng đường mượt với dấu tròn; cả hai đều có kích thước dấu 10, được cấu hình bằng setSize. Phương thức setSymbol kiểm soát hình dạng dấu.
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import ChartType, MarkerStyleType, Presentation, SaveFormat
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
chart = slide.getShapes().addChart(ChartType.ScatterWithSmoothLines, 20, 20, 500, 300)
workbook = chart.getChartData().getChartDataWorkbook()
worksheet_index = 0
chart.getChartData().getSeries().clear()
series_name_cell = workbook.getCell(worksheet_index, 1, 1, "Series 1")
chart.getChartData().getSeries().add(series_name_cell, chart.getType())
series_name_cell = workbook.getCell(worksheet_index, 1, 3, "Series 2")
chart.getChartData().getSeries().add(series_name_cell, chart.getType())
series = chart.getChartData().getSeries().get_Item(0)
x_value_cell = workbook.getCell(worksheet_index, 2, 1, 1)
y_value_cell = workbook.getCell(worksheet_index, 2, 2, 3)
series.getDataPoints().addDataPointForScatterSeries(x_value_cell, y_value_cell)
x_value_cell = workbook.getCell(worksheet_index, 3, 1, 2)
y_value_cell = workbook.getCell(worksheet_index, 3, 2, 10)
series.getDataPoints().addDataPointForScatterSeries(x_value_cell, y_value_cell)
series.setType(ChartType.ScatterWithStraightLinesAndMarkers)
series.getMarker().setSize(10)
series.getMarker().setSymbol(MarkerStyleType.Star)
series = chart.getChartData().getSeries().get_Item(1)
x_value_cell = workbook.getCell(worksheet_index, 2, 3, 5)
y_value_cell = workbook.getCell(worksheet_index, 2, 4, 2)
series.getDataPoints().addDataPointForScatterSeries(x_value_cell, y_value_cell)
x_value_cell = workbook.getCell(worksheet_index, 3, 3, 3)
y_value_cell = workbook.getCell(worksheet_index, 3, 4, 1)
series.getDataPoints().addDataPointForScatterSeries(x_value_cell, y_value_cell)
x_value_cell = workbook.getCell(worksheet_index, 4, 3, 2)
y_value_cell = workbook.getCell(worksheet_index, 4, 4, 2)
series.getDataPoints().addDataPointForScatterSeries(x_value_cell, y_value_cell)
x_value_cell = workbook.getCell(worksheet_index, 5, 3, 5)
y_value_cell = workbook.getCell(worksheet_index, 5, 4, 1)
series.getDataPoints().addDataPointForScatterSeries(x_value_cell, y_value_cell)
series.getMarker().setSize(10)
series.getMarker().setSymbol(MarkerStyleType.Circle)
presentation.save("AsposeChart.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
Kết quả:

Tạo biểu đồ Pie
Biểu đồ tròn thích hợp để hiển thị mối quan hệ phần‑với‑toàn trong dữ liệu, đặc biệt khi dữ liệu chứa các nhãn phân loại kèm giá trị số. Tuy nhiên, nếu dữ liệu của bạn có nhiều phần hoặc nhãn, bạn có thể cân nhắc sử dụng biểu đồ cột thay thế.
Ví dụ này tạo một biểu đồ tròn với ba giá trị quý. Mỗi miếng có màu nền và viền tùy chỉnh, và các nhãn hiển thị các kết hợp khác nhau của giá trị, phần trăm và thông tin chuỗi. Phương thức setFirstSliceAngle đặt miếng đầu tiên bắt đầu ở góc 180 độ.
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import ChartType, FillType, LineDashStyle, LineStyle, NullableBool, Presentation, SaveFormat
Color = jpype.JClass("java.awt.Color")
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
chart = slide.getShapes().addChart(ChartType.Pie, 20, 20, 500, 300)
chart.getChartTitle().addTextFrameForOverriding("Sample Title")
chart.getChartTitle().getTextFrameForOverriding().getTextFrameFormat().setCenterText(NullableBool.True_)
chart.getChartTitle().setHeight(20)
chart.setTitle(True)
chart.getChartData().getSeries().get_Item(0).getLabels().getDefaultDataLabelFormat().setShowValue(True)
workbook = chart.getChartData().getChartDataWorkbook()
worksheet_index = 0
chart.getChartData().getSeries().clear()
chart.getChartData().getCategories().clear()
category_cell = workbook.getCell(0, 1, 0, "1st Qtr")
chart.getChartData().getCategories().add(category_cell)
category_cell = workbook.getCell(0, 2, 0, "2nd Qtr")
chart.getChartData().getCategories().add(category_cell)
category_cell = workbook.getCell(0, 3, 0, "3rd Qtr")
chart.getChartData().getCategories().add(category_cell)
series_name_cell = workbook.getCell(0, 0, 1, "Series 1")
series = chart.getChartData().getSeries().add(series_name_cell, chart.getType())
value_cell = workbook.getCell(worksheet_index, 1, 1, 20)
series.getDataPoints().addDataPointForPieSeries(value_cell)
value_cell = workbook.getCell(worksheet_index, 2, 1, 50)
series.getDataPoints().addDataPointForPieSeries(value_cell)
value_cell = workbook.getCell(worksheet_index, 3, 1, 30)
series.getDataPoints().addDataPointForPieSeries(value_cell)
chart.getChartData().getSeriesGroups().get_Item(0).setColorVaried(True)
first_slice = series.getDataPoints().get_Item(0)
first_slice.getFormat().getFill().setFillType(FillType.Solid)
first_slice.getFormat().getFill().getSolidFillColor().setColor(Color.CYAN)
first_slice.getFormat().getLine().getFillFormat().setFillType(FillType.Solid)
first_slice.getFormat().getLine().getFillFormat().getSolidFillColor().setColor(Color.GRAY)
first_slice.getFormat().getLine().setWidth(3.0)
first_slice.getFormat().getLine().setStyle(LineStyle.ThinThick)
first_slice.getFormat().getLine().setDashStyle(LineDashStyle.LargeDash)
second_slice = series.getDataPoints().get_Item(1)
second_slice.getFormat().getFill().setFillType(FillType.Solid)
color = Color(165, 42, 42)
second_slice.getFormat().getFill().getSolidFillColor().setColor(color)
second_slice.getFormat().getLine().getFillFormat().setFillType(FillType.Solid)
second_slice.getFormat().getLine().getFillFormat().getSolidFillColor().setColor(Color.BLUE)
second_slice.getFormat().getLine().setWidth(3.0)
second_slice.getFormat().getLine().setStyle(LineStyle.Single)
second_slice.getFormat().getLine().setDashStyle(LineDashStyle.LargeDashDot)
third_slice = series.getDataPoints().get_Item(2)
third_slice.getFormat().getFill().setFillType(FillType.Solid)
color = Color(255, 127, 80)
third_slice.getFormat().getFill().getSolidFillColor().setColor(color)
third_slice.getFormat().getLine().getFillFormat().setFillType(FillType.Solid)
third_slice.getFormat().getLine().getFillFormat().getSolidFillColor().setColor(Color.RED)
third_slice.getFormat().getLine().setWidth(2.0)
third_slice.getFormat().getLine().setStyle(LineStyle.ThinThin)
third_slice.getFormat().getLine().setDashStyle(LineDashStyle.LargeDashDotDot)
first_label = series.getDataPoints().get_Item(0).getLabel()
first_label.getDataLabelFormat().setShowValue(True)
second_label = series.getDataPoints().get_Item(1).getLabel()
second_label.getDataLabelFormat().setShowValue(True)
second_label.getDataLabelFormat().setShowLegendKey(True)
second_label.getDataLabelFormat().setShowPercentage(True)
third_label = series.getDataPoints().get_Item(2).getLabel()
third_label.getDataLabelFormat().setShowSeriesName(True)
third_label.getDataLabelFormat().setShowPercentage(True)
series.getLabels().getDefaultDataLabelFormat().setShowLeaderLines(True)
chart.getChartData().getSeriesGroups().get_Item(0).setFirstSliceAngle(180)
presentation.save("PieChart.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
Kết quả:

Để cải thiện khả năng đọc các nhãn bên ngoài biểu đồ tròn, xem Format Leader Lines để biết ví dụ về cách định vị nhãn và tùy chỉnh màu, độ rộng, độ trong suốt và kiểu gạch của các đường dẫn.
Tạo biểu đồ Đường
Biểu đồ đường (còn gọi là đồ thị đường) thích hợp trong các tình huống bạn muốn minh họa sự thay đổi giá trị theo thời gian. Sử dụng biểu đồ đường, bạn có thể so sánh một lượng lớn dữ liệu cùng lúc, theo dõi các thay đổi và xu hướng theo thời gian, làm nổi bật các bất thường trong chuỗi dữ liệu, và nhiều hơn nữa.
Ví dụ này sử dụng addChart để tạo một biểu đồ đường với chuỗi và danh mục mặc định, sau đó lưu bản trình bày dưới dạng PPTX.
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import ChartType, Presentation, SaveFormat
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
line_chart = slide.getShapes().addChart(ChartType.Line, 20, 20, 500, 300)
presentation.save("line_chart.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
Mặc định, các điểm trên biểu đồ đường được nối bằng các đoạn thẳng liên tục. Nếu bạn muốn các điểm được nối bằng nét gạch, gọi setDashStyle với giá trị từ LineDashStyle :
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import ChartType, LineDashStyle, Presentation
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
line_chart = slide.getShapes().addChart(ChartType.Line, 20, 20, 500, 300)
for series in line_chart.getChartData().getSeries():
series.getFormat().getLine().setDashStyle(LineDashStyle.Dash)
finally:
presentation.dispose()
Kết quả:

Tạo biểu đồ Tree Map
Biểu đồ tree map thích hợp cho dữ liệu bán hàng khi bạn muốn hiển thị kích thước tương đối của các danh mục dữ liệu và nhanh chóng thu hút sự chú ý đến những mục đóng góp lớn trong mỗi danh mục.
Ví dụ này tạo một tree map với tám lá được nhóm thành bốn nhánh và hai nhánh phụ. Phương thức setGroupingItem gán các nhánh ở mức nhóm 1 và các nhánh phụ ở mức 2. Giá trị xác định kích thước hình chữ nhật tương đối, trong khi các nhãn danh mục và nhãn cha chồng lên nhau xác định phân cấp.
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import ChartType, ParentLabelLayoutType, Presentation, SaveFormat
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
chart = slide.getShapes().addChart(ChartType.Treemap, 20, 20, 500, 300)
chart.getChartData().getCategories().clear()
chart.getChartData().getSeries().clear()
workbook = chart.getChartData().getChartDataWorkbook()
workbook.clear(0)
# Nhánh 1
category_cell = workbook.getCell(0, "C1", "Leaf1")
leaf = chart.getChartData().getCategories().add(category_cell)
leaf.getGroupingLevels().setGroupingItem(1, "Stem1")
leaf.getGroupingLevels().setGroupingItem(2, "Branch1")
category_cell = workbook.getCell(0, "C2", "Leaf2")
chart.getChartData().getCategories().add(category_cell)
category_cell = workbook.getCell(0, "C3", "Leaf3")
leaf = chart.getChartData().getCategories().add(category_cell)
leaf.getGroupingLevels().setGroupingItem(1, "Stem2")
category_cell = workbook.getCell(0, "C4", "Leaf4")
chart.getChartData().getCategories().add(category_cell)
# Nhánh 2
category_cell = workbook.getCell(0, "C5", "Leaf5")
leaf = chart.getChartData().getCategories().add(category_cell)
leaf.getGroupingLevels().setGroupingItem(1, "Stem3")
leaf.getGroupingLevels().setGroupingItem(2, "Branch2")
category_cell = workbook.getCell(0, "C6", "Leaf6")
chart.getChartData().getCategories().add(category_cell)
category_cell = workbook.getCell(0, "C7", "Leaf7")
leaf = chart.getChartData().getCategories().add(category_cell)
leaf.getGroupingLevels().setGroupingItem(1, "Stem4")
category_cell = workbook.getCell(0, "C8", "Leaf8")
chart.getChartData().getCategories().add(category_cell)
series = chart.getChartData().getSeries().add(ChartType.Treemap)
series.getLabels().getDefaultDataLabelFormat().setShowCategoryName(True)
value_cell = workbook.getCell(0, "D1", 4)
series.getDataPoints().addDataPointForTreemapSeries(value_cell)
value_cell = workbook.getCell(0, "D2", 5)
series.getDataPoints().addDataPointForTreemapSeries(value_cell)
value_cell = workbook.getCell(0, "D3", 3)
series.getDataPoints().addDataPointForTreemapSeries(value_cell)
value_cell = workbook.getCell(0, "D4", 6)
series.getDataPoints().addDataPointForTreemapSeries(value_cell)
value_cell = workbook.getCell(0, "D5", 9)
series.getDataPoints().addDataPointForTreemapSeries(value_cell)
value_cell = workbook.getCell(0, "D6", 9)
series.getDataPoints().addDataPointForTreemapSeries(value_cell)
value_cell = workbook.getCell(0, "D7", 4)
series.getDataPoints().addDataPointForTreemapSeries(value_cell)
value_cell = workbook.getCell(0, "D8", 3)
series.getDataPoints().addDataPointForTreemapSeries(value_cell)
series.setParentLabelLayout(ParentLabelLayoutType.Overlapping)
presentation.save("Treemap.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
Kết quả:

Tạo biểu đồ Waterfall
Biểu đồ waterfall cho thấy cách các thay đổi dương và âm đóng góp vào giá trị cuối cùng. Ví dụ, một phân tích lợi nhuận có thể bắt đầu từ doanh thu, trừ chi phí và cộng thêm thu nhập khác. Các cột thay đổi nổi giữa giá trị chạy trước và sau, trong khi các cột tổng mở rộng từ 0 tới giá trị tích lũy.
Ví dụ dưới đây sử dụng các khoản tiền tính bằng nghìn USD:
| Category | Workbook value | Column type |
|---|---|---|
| Revenue | 1000 | Increase |
| Cost of sales | -400 | Decrease |
| Gross profit | 600 | Intermediate total |
| Operating expenses | -200 | Decrease |
| Other income | 50 | Increase |
| Net profit | 450 | Final total |
Để đánh dấu một điểm là tổng trung gian hoặc tổng cuối cùng, gọi phương thức setSetAsTotal của nó với True. Cung cấp số tiền tích lũy thực tế trong ô workbook của điểm đó: 600 cho lợi nhuận gộp (1000 - 400) và 450 cho lợi nhuận ròng (600 - 200 + 50). Phương thức này chỉ đánh dấu cột là tổng; nó không tính toán hoặc điền giá trị ô. Không sử dụng 0 làm chỗ giữ chỗ hoặc cung cấp thay đổi khác cho điểm tổng. Một cột tổng hiển thị giá trị tích lũy mà không cộng lại vào giá trị chạy.
Ví dụ Python đầy đủ này tạo một biểu đồ bằng thành phần Waterfall của ChartType, xóa dữ liệu mặc định và điền workbook được nhúng. Chỉ số worksheet 0 chọn sheet đầu tiên; cột A chứa tên danh mục và cột B chứa giá trị. Mỗi ô giá trị được thêm bằng addDataPointForWaterfallSeries. Các chỉ số điểm không dựa trên 0 2 và 5 xác định các tổng trung gian và tổng cuối cùng. Gọi setShowValue với True để hiển thị số tiền trên các cột. Không cần bản trình bày hoặc workbook đầu vào.
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import ChartType, Presentation, SaveFormat
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
chart = slide.getShapes().addChart(ChartType.Waterfall, 20, 20, 680, 200)
chart.setTitle(True)
chart.getChartTitle().addTextFrameForOverriding("Profit breakdown (USD thousands)")
chart.getChartTitle().setOverlay(False)
chart.setLegend(False)
chart.getChartData().getSeries().clear()
chart.getChartData().getCategories().clear()
workbook = chart.getChartData().getChartDataWorkbook()
worksheet_index = 0
workbook.clear(worksheet_index)
series_name_cell = workbook.getCell(worksheet_index, 0, 1, "Profit")
series = chart.getChartData().getSeries().add(series_name_cell, ChartType.Waterfall)
categories = ["Revenue", "Cost of sales", "Gross profit", "Operating expenses", "Other income", "Net profit"]
values = [1000, -400, 600, -200, 50, 450]
for i in range(len(categories)):
category_cell = workbook.getCell(worksheet_index, i + 1, 0, categories[i])
chart.getChartData().getCategories().add(category_cell)
value_cell = workbook.getCell(worksheet_index, i + 1, 1, values[i])
series.getDataPoints().addDataPointForWaterfallSeries(value_cell)
# Các điểm thứ ba và thứ sáu đại diện cho tổng, không phải các thay đổi bổ sung.
series.getDataPoints().get_Item(2).setSetAsTotal(True)
series.getDataPoints().get_Item(5).setSetAsTotal(True)
series.setShowConnectorLines(True)
series.getLabels().getDefaultDataLabelFormat().setShowValue(True)
presentation.save("WaterfallChart.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
Gọi phương thức cấp độ chuỗi setShowConnectorLines với True để hiển thị các đường nối các cột liên tiếp và giúp giá trị chạy dễ theo dõi hơn. Để ẩn các đường này, thay thế lời gọi phương thức trong ví dụ bằng series.setShowConnectorLines(False) trước khi lưu. Thao tác này chỉ thay đổi hiển thị đường, không ảnh hưởng đến dữ liệu hay các tổng.
Bản trình bày đã lưu hiển thị lợi nhuận gộp 600 và lợi nhuận ròng 450, với cả hai tổng được vẽ từ 0:

Tạo biểu đồ Stock
Biểu đồ stock được dùng để hiển thị dữ liệu tài chính như giá mở cửa, cao nhất, thấp nhất và đóng cửa, giúp phân tích xu hướng thị trường và độ biến động. Chúng cung cấp những hiểu biết quan trọng về hiệu suất cổ phiếu, hỗ trợ nhà đầu tư và nhà phân tích đưa ra quyết định thông minh.
Ví dụ này tạo một biểu đồ open‑high‑low‑close với bốn chuỗi theo thứ tự đó và ba danh mục. Phương thức setUpDownBars kích hoạt các thanh lên/xuống, và getHiLowLinesFormat điều khiển các đường cao‑thấp hiển thị khoảng giá, trong khi các đường chuỗi riêng và các đường lưới ngang phụ được ẩn.
Trình quản lý trả về bởi getUpDownBars mở ra các đối tượng định dạng riêng qua getUpBarsFormat và getDownBarsFormat. Sử dụng các phương thức getFill và getLine của từng đối tượng để đặt màu nền và viền một cách độc lập. Ví dụ này dùng các thanh xanh lá với viền xanh đậm cho các khoảng tăng và các thanh cam‑đỏ với viền đỏ đậm cho các khoảng giảm.
Cung cấp giá trị số cho cả bốn chuỗi ở mỗi danh mục, với High và Low bao quanh Open và Close. Các thanh so sánh Close với Open trong cùng một danh mục, không phải với danh mục trước: A giảm từ 72 xuống 25, trong khi B tăng từ 25 lên 38 và C tăng từ 38 lên 50. Các giá trị Open và Close bằng nhau không tạo ra khoảng màu nào. Các thanh lên/xuống bao phủ Open‑Close; các đường high‑low bao phủ Low‑High. Các thanh này thuộc nhóm chuỗi và không phải là các chuỗi biểu đồ cột thông thường.
Định dạng này cũng hoạt động với các biểu đồ Line and LineWithMarkers có ít nhất hai chuỗi trong cùng một nhóm và các giá trị số tương ứng cho mỗi danh mục. Đối với các biểu đồ đường không xếp chồng này, các thanh lên/xuống so sánh chuỗi đầu và chuỗi cuối: giá trị cuối cao hơn tạo thanh lên, giá trị cuối thấp hơn tạo thanh xuống. Đảo ngược các chuỗi sẽ đảo ngược phân loại. Ví dụ dưới đây sử dụng OpenHighLowClose; không giả định các biến thể stock, stacked‑line hay 3D khác sử dụng cùng cách sắp xếp dữ liệu hoặc cách diễn giải khoảng.
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import ChartType, FillType, Presentation, SaveFormat
Color = jpype.JClass("java.awt.Color")
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
chart = slide.getShapes().addChart(ChartType.OpenHighLowClose, 20, 20, 500, 150, False)
workbook = chart.getChartData().getChartDataWorkbook()
category_cell = workbook.getCell(0, 1, 0, "A")
chart.getChartData().getCategories().add(category_cell)
category_cell = workbook.getCell(0, 2, 0, "B")
chart.getChartData().getCategories().add(category_cell)
category_cell = workbook.getCell(0, 3, 0, "C")
chart.getChartData().getCategories().add(category_cell)
series_name_cell = workbook.getCell(0, 0, 1, "Open")
chart.getChartData().getSeries().add(series_name_cell, ChartType.OpenHighLowClose)
series_name_cell = workbook.getCell(0, 0, 2, "High")
chart.getChartData().getSeries().add(series_name_cell, ChartType.OpenHighLowClose)
series_name_cell = workbook.getCell(0, 0, 3, "Low")
chart.getChartData().getSeries().add(series_name_cell, ChartType.OpenHighLowClose)
series_name_cell = workbook.getCell(0, 0, 4, "Close")
chart.getChartData().getSeries().add(series_name_cell, ChartType.OpenHighLowClose)
series = chart.getChartData().getSeries().get_Item(0)
value_cell = workbook.getCell(0, 1, 1, 72)
series.getDataPoints().addDataPointForStockSeries(value_cell)
value_cell = workbook.getCell(0, 2, 1, 25)
series.getDataPoints().addDataPointForStockSeries(value_cell)
value_cell = workbook.getCell(0, 3, 1, 38)
series.getDataPoints().addDataPointForStockSeries(value_cell)
series = chart.getChartData().getSeries().get_Item(1)
value_cell = workbook.getCell(0, 1, 2, 172)
series.getDataPoints().addDataPointForStockSeries(value_cell)
value_cell = workbook.getCell(0, 2, 2, 57)
series.getDataPoints().addDataPointForStockSeries(value_cell)
value_cell = workbook.getCell(0, 3, 2, 57)
series.getDataPoints().addDataPointForStockSeries(value_cell)
series = chart.getChartData().getSeries().get_Item(2)
value_cell = workbook.getCell(0, 1, 3, 12)
series.getDataPoints().addDataPointForStockSeries(value_cell)
value_cell = workbook.getCell(0, 2, 3, 12)
series.getDataPoints().addDataPointForStockSeries(value_cell)
value_cell = workbook.getCell(0, 3, 3, 13)
series.getDataPoints().addDataPointForStockSeries(value_cell)
series = chart.getChartData().getSeries().get_Item(3)
value_cell = workbook.getCell(0, 1, 4, 25)
series.getDataPoints().addDataPointForStockSeries(value_cell)
value_cell = workbook.getCell(0, 2, 4, 38)
series.getDataPoints().addDataPointForStockSeries(value_cell)
value_cell = workbook.getCell(0, 3, 4, 50)
series.getDataPoints().addDataPointForStockSeries(value_cell)
up_down_bars = chart.getChartData().getSeriesGroups().get_Item(0).getUpDownBars()
up_down_bars.setUpDownBars(True)
up_down_bars.getUpBarsFormat().getFill().setFillType(FillType.Solid)
up_fill_color = Color(50, 205, 50)
up_down_bars.getUpBarsFormat().getFill().getSolidFillColor().setColor(up_fill_color)
up_down_bars.getUpBarsFormat().getLine().getFillFormat().setFillType(FillType.Solid)
up_line_color = Color(0, 100, 0)
up_down_bars.getUpBarsFormat().getLine().getFillFormat().getSolidFillColor().setColor(up_line_color)
up_down_bars.getUpBarsFormat().getLine().setWidth(2)
up_down_bars.getDownBarsFormat().getFill().setFillType(FillType.Solid)
down_fill_color = Color(255, 69, 0)
up_down_bars.getDownBarsFormat().getFill().getSolidFillColor().setColor(down_fill_color)
up_down_bars.getDownBarsFormat().getLine().getFillFormat().setFillType(FillType.Solid)
down_line_color = Color(139, 0, 0)
up_down_bars.getDownBarsFormat().getLine().getFillFormat().getSolidFillColor().setColor(down_line_color)
up_down_bars.getDownBarsFormat().getLine().setWidth(2)
chart.getChartData().getSeriesGroups().get_Item(0).getHiLowLinesFormat().getLine().getFillFormat().setFillType(FillType.Solid)
for stock_series in chart.getChartData().getSeries():
stock_series.getFormat().getLine().getFillFormat().setFillType(FillType.NoFill)
chart.getAxes().getVerticalAxis().getMinorGridLinesFormat().getLine().getFillFormat().setFillType(FillType.NoFill)
presentation.save("Stock-chart.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
Kết quả, xuất từ PowerPoint:

Tạo biểu đồ Box và Whisker
Biểu đồ Box và Whisker được dùng để hiển thị phân bố dữ liệu bằng cách tóm tắt các chỉ tiêu thống kê chính, như trung vị, các phần tư và các điểm ngoại lệ tiềm năng. Chúng đặc biệt hữu ích trong phân tích dữ liệu khám phá và các nghiên cứu thống kê để nhanh chóng hiểu được tính biến thiên của dữ liệu và nhận diện các bất thường.
Ví dụ này tạo một biểu đồ Box và Whisker từ sáu giá trị. Phương thức setQuartileMethod chọn cách tính phần tư loại trừ. Ví dụ cũng bật các đường trung bình, dấu trung bình, các điểm nội và các điểm ngoại lệ.
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import ChartType, Presentation, QuartileMethodType, SaveFormat
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
chart = slide.getShapes().addChart(ChartType.BoxAndWhisker, 20, 20, 500, 300)
chart.getChartData().getCategories().clear()
chart.getChartData().getSeries().clear()
workbook = chart.getChartData().getChartDataWorkbook()
workbook.clear(0)
category_cell = workbook.getCell(0, "A1", "Category 1")
chart.getChartData().getCategories().add(category_cell)
category_cell = workbook.getCell(0, "A2", "Category 2")
chart.getChartData().getCategories().add(category_cell)
category_cell = workbook.getCell(0, "A3", "Category 3")
chart.getChartData().getCategories().add(category_cell)
category_cell = workbook.getCell(0, "A4", "Category 4")
chart.getChartData().getCategories().add(category_cell)
category_cell = workbook.getCell(0, "A5", "Category 5")
chart.getChartData().getCategories().add(category_cell)
category_cell = workbook.getCell(0, "A6", "Category 6")
chart.getChartData().getCategories().add(category_cell)
series = chart.getChartData().getSeries().add(ChartType.BoxAndWhisker)
series.setQuartileMethod(QuartileMethodType.Exclusive)
series.setShowMeanLine(True)
series.setShowMeanMarkers(True)
series.setShowInnerPoints(True)
series.setShowOutlierPoints(True)
value_cell = workbook.getCell(0, "B1", 15)
series.getDataPoints().addDataPointForBoxAndWhiskerSeries(value_cell)
value_cell = workbook.getCell(0, "B2", 41)
series.getDataPoints().addDataPointForBoxAndWhiskerSeries(value_cell)
value_cell = workbook.getCell(0, "B3", 16)
series.getDataPoints().addDataPointForBoxAndWhiskerSeries(value_cell)
value_cell = workbook.getCell(0, "B4", 10)
series.getDataPoints().addDataPointForBoxAndWhiskerSeries(value_cell)
value_cell = workbook.getCell(0, "B5", 23)
series.getDataPoints().addDataPointForBoxAndWhiskerSeries(value_cell)
value_cell = workbook.getCell(0, "B6", 16)
series.getDataPoints().addDataPointForBoxAndWhiskerSeries(value_cell)
presentation.save("BoxAndWhisker.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
Tạo biểu đồ Funnel
Biểu đồ funnel được dùng để trực quan hoá các quy trình có các giai đoạn tuần tự, trong đó khối lượng dữ liệu giảm dần khi tiến từ bước này sang bước tiếp theo. Chúng đặc biệt hữu ích cho việc phân tích tỷ lệ chuyển đổi, xác định các nút thắt và theo dõi hiệu suất của quy trình bán hàng hoặc tiếp thị.
Ví dụ này sử dụng addChart để thêm một biểu đồ funnel vào slide đầu tiên của một bản trình bày hiện có. Nó thay thế dữ liệu mặc định bằng sáu danh mục và các giá trị từ 50 tới 500, sau đó lưu bản trình bày đã cập nhật. Bản trình bày đầu vào phải có ít nhất một slide.
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import ChartType, Presentation, SaveFormat
presentation = Presentation("test.pptx")
try:
slide = presentation.getSlides().get_Item(0)
chart = slide.getShapes().addChart(ChartType.Funnel, 50, 50, 500, 400)
chart.getChartData().getCategories().clear()
chart.getChartData().getSeries().clear()
workbook = chart.getChartData().getChartDataWorkbook()
workbook.clear(0)
category_cell = workbook.getCell(0, "A1", "Category 1")
chart.getChartData().getCategories().add(category_cell)
category_cell = workbook.getCell(0, "A2", "Category 2")
chart.getChartData().getCategories().add(category_cell)
category_cell = workbook.getCell(0, "A3", "Category 3")
chart.getChartData().getCategories().add(category_cell)
category_cell = workbook.getCell(0, "A4", "Category 4")
chart.getChartData().getCategories().add(category_cell)
category_cell = workbook.getCell(0, "A5", "Category 5")
chart.getChartData().getCategories().add(category_cell)
category_cell = workbook.getCell(0, "A6", "Category 6")
chart.getChartData().getCategories().add(category_cell)
series = chart.getChartData().getSeries().add(ChartType.Funnel)
value_cell = workbook.getCell(0, "B1", 50)
series.getDataPoints().addDataPointForFunnelSeries(value_cell)
value_cell = workbook.getCell(0, "B2", 100)
series.getDataPoints().addDataPointForFunnelSeries(value_cell)
value_cell = workbook.getCell(0, "B3", 200)
series.getDataPoints().addDataPointForFunnelSeries(value_cell)
value_cell = workbook.getCell(0, "B4", 300)
series.getDataPoints().addDataPointForFunnelSeries(value_cell)
value_cell = workbook.getCell(0, "B5", 400)
series.getDataPoints().addDataPointForFunnelSeries(value_cell)
value_cell = workbook.getCell(0, "B6", 500)
series.getDataPoints().addDataPointForFunnelSeries(value_cell)
presentation.save("Funnel.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
Kết quả:

Tạo biểu đồ Sunburst
Biểu đồ Sunburst được dùng để trực quan hoá dữ liệu phân cấp, hiển thị các cấp độ dưới dạng các vòng đồng tâm. Chúng giúp minh họa mối quan hệ phần‑với‑toàn và lý tưởng cho việc biểu diễn các danh mục và danh mục con lồng nhau một cách rõ ràng, gọn gàng.
Ví dụ này tạo một biểu đồ Sunburst với tám lá được nhóm thành bốn nhánh và hai nhánh phụ. Phương thức setGroupingItem gán các nhánh ở mức nhóm 1 và các nhánh phụ ở mức 2. Các nhãn danh mục xác định các đoạn, và các giá trị quyết định kích thước tương đối của chúng.
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import ChartType, Presentation, SaveFormat
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
chart = slide.getShapes().addChart(ChartType.Sunburst, 20, 20, 500, 300)
chart.getChartData().getCategories().clear()
chart.getChartData().getSeries().clear()
workbook = chart.getChartData().getChartDataWorkbook()
workbook.clear(0)
# Nhánh 1
category_cell = workbook.getCell(0, "C1", "Leaf1")
leaf = chart.getChartData().getCategories().add(category_cell)
leaf.getGroupingLevels().setGroupingItem(1, "Stem1")
leaf.getGroupingLevels().setGroupingItem(2, "Branch1")
category_cell = workbook.getCell(0, "C2", "Leaf2")
chart.getChartData().getCategories().add(category_cell)
category_cell = workbook.getCell(0, "C3", "Leaf3")
leaf = chart.getChartData().getCategories().add(category_cell)
leaf.getGroupingLevels().setGroupingItem(1, "Stem2")
category_cell = workbook.getCell(0, "C4", "Leaf4")
chart.getChartData().getCategories().add(category_cell)
# Nhánh 2
category_cell = workbook.getCell(0, "C5", "Leaf5")
leaf = chart.getChartData().getCategories().add(category_cell)
leaf.getGroupingLevels().setGroupingItem(1, "Stem3")
leaf.getGroupingLevels().setGroupingItem(2, "Branch2")
category_cell = workbook.getCell(0, "C6", "Leaf6")
chart.getChartData().getCategories().add(category_cell)
category_cell = workbook.getCell(0, "C7", "Leaf7")
leaf = chart.getChartData().getCategories().add(category_cell)
leaf.getGroupingLevels().setGroupingItem(1, "Stem4")
category_cell = workbook.getCell(0, "C8", "Leaf8")
chart.getChartData().getCategories().add(category_cell)
series = chart.getChartData().getSeries().add(ChartType.Sunburst)
series.getLabels().getDefaultDataLabelFormat().setShowCategoryName(True)
value_cell = workbook.getCell(0, "D1", 4)
series.getDataPoints().addDataPointForSunburstSeries(value_cell)
value_cell = workbook.getCell(0, "D2", 5)
series.getDataPoints().addDataPointForSunburstSeries(value_cell)
value_cell = workbook.getCell(0, "D3", 3)
series.getDataPoints().addDataPointForSunburstSeries(value_cell)
value_cell = workbook.getCell(0, "D4", 6)
series.getDataPoints().addDataPointForSunburstSeries(value_cell)
value_cell = workbook.getCell(0, "D5", 9)
series.getDataPoints().addDataPointForSunburstSeries(value_cell)
value_cell = workbook.getCell(0, "D6", 9)
series.getDataPoints().addDataPointForSunburstSeries(value_cell)
value_cell = workbook.getCell(0, "D7", 4)
series.getDataPoints().addDataPointForSunburstSeries(value_cell)
value_cell = workbook.getCell(0, "D8", 3)
series.getDataPoints().addDataPointForSunburstSeries(value_cell)
presentation.save("Sunburst.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
Kết quả:

Tạo biểu đồ Histogram
Biểu đồ histogram cho thấy cách các giá trị số được phân bố qua các khoảng (bins). Đối với các báo cáo hàng tháng về thời gian xử lý đơn hàng, hãy sử dụng cùng một độ rộng bin và các ngưỡng để giữ cho các khoảng nhất quán và dễ so sánh phân bố.
Gọi setAggregationType trên trục ngang với một chế độ từ AxisAggregationType. setBinWidth chỉ áp dụng cho ByBinWidth; setNumberOfBins chỉ áp dụng cho ByNumberOfBins. Sử dụng Automatic để cho biểu đồ tự chọn các bin.
Sử dụng Độ rộng Bin cố định
Ví dụ này nhóm thời gian xử lý thành các khoảng 10 phút, với các bin riêng cho thời gian ≤ 10 phút và > 40 phút. Kích hoạt các bin này bằng setUnderflowBin và setOverflowBin. Khi cung cấp ngưỡng rõ ràng qua setUnderflowBin và setOverflowBin, gọi setAutomaticUnderflowBin và setAutomaticOverflowBin với False.
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import AxisAggregationType, ChartType, Presentation, SaveFormat
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
chart = slide.getShapes().addChart(ChartType.Histogram, 20, 20, 680, 200)
chart.setLegend(False)
chart.setTitle(False)
chart.getChartData().getCategories().clear()
chart.getChartData().getSeries().clear()
workbook = chart.getChartData().getChartDataWorkbook()
workbook.clear(0)
series = chart.getChartData().getSeries().add(ChartType.Histogram)
values = [5, 10, 10, 11, 15, 20, 20, 21, 25, 30, 30, 31, 35, 40, 40, 41, 50]
for i, value in enumerate(values):
value_cell = workbook.getCell(0, i, 0, float(value))
series.getDataPoints().addDataPointForHistogramSeries(value_cell)
axis = chart.getAxes().getHorizontalAxis()
axis.setAggregationType(AxisAggregationType.ByBinWidth)
axis.setBinWidth(10.0)
axis.setUnderflowBin(True)
axis.setAutomaticUnderflowBin(False)
axis.setUnderflowBin(10.0)
axis.setOverflowBin(True)
axis.setAutomaticOverflowBin(False)
axis.setOverflowBin(40.0)
presentation.save("Histogram-bin-width.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
Bản trình bày hiển thị trong PowerPoint cho thấy số lượng bin là 3, 4, 4, 4, và 2. Bin dưới ngưỡng bao gồm 5, 10, 10, vì vậy ngưỡng của nó là bao hàm. Bin trên ngưỡng chỉ bao gồm 41, 50, vì vậy ngưỡng của nó là loại trừ; cả hai giá trị bằng 40 vẫn thuộc khoảng trước. Các bin nội chứa các giá trị lớn hơn giới hạn dưới và nhỏ hơn hoặc bằng giới hạn trên.

Xác định Số lượng Bin
Đối với mức chi tiết cố định thay vì các khoảng cố định, yêu cầu năm bin cho cùng dữ liệu. Ví dụ này để các bin underflow và overflow bị tắt. Biểu đồ tính độ rộng dựa trên phạm vi dữ liệu, vì vậy các giới hạn có thể thay đổi giữa các báo cáo.
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import AxisAggregationType, ChartType, Presentation, SaveFormat
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
chart = slide.getShapes().addChart(ChartType.Histogram, 20, 20, 680, 200)
chart.setLegend(False)
chart.setTitle(False)
chart.getChartData().getCategories().clear()
chart.getChartData().getSeries().clear()
workbook = chart.getChartData().getChartDataWorkbook()
workbook.clear(0)
series = chart.getChartData().getSeries().add(ChartType.Histogram)
values = [5, 10, 10, 11, 15, 20, 20, 21, 25, 30, 30, 31, 35, 40, 40, 41, 50]
for i, value in enumerate(values):
value_cell = workbook.getCell(0, i, 0, float(value))
series.getDataPoints().addDataPointForHistogramSeries(value_cell)
axis = chart.getAxes().getHorizontalAxis()
axis.setAggregationType(AxisAggregationType.ByNumberOfBins)
axis.setNumberOfBins(5)
presentation.save("Histogram-bin-count.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
Năm bin có số lượng 4, 4, 4, 4, và 1. So sánh phân bố này với kết quả độ rộng cố định ở trên: các giá trị đầu vào giống nhau, nhưng các giới hạn khác nhau.

Tạo biểu đồ Radar
Biểu đồ radar được dùng để hiển thị dữ liệu đa biến trong một định dạng hai chiều, cho phép so sánh dễ dàng nhiều biến cùng lúc. Chúng đặc biệt hữu ích để xác định các mẫu, điểm mạnh và điểm yếu trên nhiều chỉ số hiệu năng hoặc thuộc tính.
Ví dụ này sử dụng addChart để tạo một biểu đồ radar với chuỗi và danh mục mặc định, sau đó lưu bản trình bày dưới dạng PPTX.
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import ChartType, Presentation, SaveFormat
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
slide.getShapes().addChart(ChartType.Radar, 20, 20, 500, 300)
presentation.save("Radar-chart.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
Kết quả:

Tạo biểu đồ Đa‑Danh mục
Biểu đồ đa‑danh mục được dùng để hiển thị dữ liệu liên quan tới nhiều nhóm phân loại, cho phép bạn so sánh các giá trị qua nhiều chiều đồng thời. Chúng đặc biệt hữu ích khi cần phân tích xu hướng và mối quan hệ trong các bộ dữ liệu phức tạp, đa lớp.
Ví dụ này tạo một biểu đồ cột nhóm với tám danh mục được sắp xếp thành bốn cặp. Phương thức setGroupingItem cấp cho mỗi cặp một nhãn chung ở mức nhóm 1, tạo ra trục danh mục có cả nhãn riêng lẻ và nhãn nhóm.
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import ChartType, Presentation, SaveFormat
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
chart = slide.getShapes().addChart(ChartType.ClusteredColumn, 20, 20, 500, 300)
chart.getChartData().getSeries().clear()
chart.getChartData().getCategories().clear()
workbook = chart.getChartData().getChartDataWorkbook()
workbook.clear(0)
worksheet_index = 0
category_cell = workbook.getCell(0, "c2", "A")
category = chart.getChartData().getCategories().add(category_cell)
category.getGroupingLevels().setGroupingItem(1, "Group1")
category_cell = workbook.getCell(0, "c3", "B")
category = chart.getChartData().getCategories().add(category_cell)
category_cell = workbook.getCell(0, "c4", "C")
category = chart.getChartData().getCategories().add(category_cell)
category.getGroupingLevels().setGroupingItem(1, "Group2")
category_cell = workbook.getCell(0, "c5", "D")
category = chart.getChartData().getCategories().add(category_cell)
category_cell = workbook.getCell(0, "c6", "E")
category = chart.getChartData().getCategories().add(category_cell)
category.getGroupingLevels().setGroupingItem(1, "Group3")
category_cell = workbook.getCell(0, "c7", "F")
category = chart.getChartData().getCategories().add(category_cell)
category_cell = workbook.getCell(0, "c8", "G")
category = chart.getChartData().getCategories().add(category_cell)
category.getGroupingLevels().setGroupingItem(1, "Group4")
category_cell = workbook.getCell(0, "c9", "H")
category = chart.getChartData().getCategories().add(category_cell)
series_name_cell = workbook.getCell(0, "D1", "Series 1")
series = chart.getChartData().getSeries().add(series_name_cell, ChartType.ClusteredColumn)
value_cell = workbook.getCell(worksheet_index, "D2", 10)
series.getDataPoints().addDataPointForBarSeries(value_cell)
value_cell = workbook.getCell(worksheet_index, "D3", 20)
series.getDataPoints().addDataPointForBarSeries(value_cell)
value_cell = workbook.getCell(worksheet_index, "D4", 30)
series.getDataPoints().addDataPointForBarSeries(value_cell)
value_cell = workbook.getCell(worksheet_index, "D5", 40)
series.getDataPoints().addDataPointForBarSeries(value_cell)
value_cell = workbook.getCell(worksheet_index, "D6", 50)
series.getDataPoints().addDataPointForBarSeries(value_cell)
value_cell = workbook.getCell(worksheet_index, "D7", 60)
series.getDataPoints().addDataPointForBarSeries(value_cell)
value_cell = workbook.getCell(worksheet_index, "D8", 70)
series.getDataPoints().addDataPointForBarSeries(value_cell)
value_cell = workbook.getCell(worksheet_index, "D9", 80)
series.getDataPoints().addDataPointForBarSeries(value_cell)
presentation.save("AsposeChart.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
Kết quả:

Xóa Nhãn Quý
Tải về quarters.pptx và đặt nó trong thư mục làm việc. Biểu đồ trong file này hiển thị chín tháng và một chuỗi bán hàng với các giá trị từ 10 tới 18. Trên trục danh mục, Q1 bao phủ từ tháng 1 tới tháng 3, Q2 từ tháng 4 tới tháng 6, và Q3 từ tháng 7 tới tháng 9.
Tháng 4 là danh mục thứ tư (chỉ số 3) và chứa nhãn Q2. Sử dụng getGroupingLevels để truy cập các mức nhóm của tháng 4, sau đó gọi deleteGroupingItem với mức 1 để xóa nhãn quý. Mức 0 đại diện cho tháng và không thể xóa bằng phương pháp này.
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import Presentation, SaveFormat
presentation = Presentation("quarters.pptx")
try:
chart = presentation.getSlides().get_Item(0).getShapes().get_Item(0)
april = chart.getChartData().getCategories().get_Item(3)
april.getGroupingLevels().deleteGroupingItem(1)
presentation.save("quarters-updated.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
reopened = Presentation("quarters-updated.pptx")
try:
chart = reopened.getSlides().get_Item(0).getShapes().get_Item(0)
data = chart.getChartData()
points = data.getSeries().get_Item(0).getDataPoints()
print(", ".join(str(month.getAsCell().getValue()) for month in data.getCategories()))
print(", ".join(str(point.getValue().getData()) for point in points))
finally:
reopened.dispose()
Sau khi mở lại biểu đồ, ví dụ in ra các tháng gốc (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep) và các giá trị bán hàng (10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0) theo cùng thứ tự. Mở quarters-updated.pptx trong PowerPoint để so sánh các nhãn trục. Nhãn Q2 và ranh giới nhóm trước tháng 4 biến mất, vì vậy Q1 bao phủ từ tháng 1 tới tháng 6. Q3 vẫn bắt đầu từ tháng 7.

Tạo biểu đồ Bản đồ
Biểu đồ bản đồ được dùng để trực quan hoá dữ liệu địa lý bằng cách gắn thông tin vào các vị trí cụ thể như quốc gia, tiểu bang hoặc thành phố. Chúng đặc biệt hữu ích cho việc phân tích xu hướng khu vực, dữ liệu dân số và phân bố không gian một cách rõ ràng, sinh động.
Ví dụ này sử dụng addChart để tạo một biểu đồ bản đồ với dữ liệu địa lý mặc định và lưu nó để xem trong PowerPoint.
import jpexe
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import ChartType, Presentation, SaveFormat
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
chart = slide.getShapes().addChart(ChartType.Map, 20, 20, 500, 300)
presentation.save("mapChart.pptx", SaveFormat.Pptx)
finally:
presentation.save
Kết quả:

Note
Hình trên hiển thị bản trình bày đã lưu và mở trong PowerPoint. Aspose.Slides ghi đúng biểu đồ bản đồ và dữ liệu của nó, nhưng không tự vẽ biểu đồ bản đồ: khi một slide chứa biểu đồ này được render thành hình ảnh hoặc chuyển đổi sang PDF hay SVG, vùng biểu đồ sẽ xuất hiện trống. Các hình dạng khác trên cùng slide không bị ảnh hưởng.Tạo biểu đồ Kết hợp
Biểu đồ kết hợp (hoặc combo chart) kết hợp hai hoặc nhiều loại biểu đồ trong một đồ thị. Biểu đồ này cho phép bạn làm nổi bật, so sánh hoặc kiểm tra sự khác nhau giữa hai hoặc nhiều bộ dữ liệu, giúp nhận dạng mối quan hệ giữa chúng.

Ví dụ này kết hợp hai chuỗi cột nhóm với một chuỗi đường qua bốn danh mục. Các cột sử dụng trục dọc chính, trong khi setPlotOnSecondAxis đặt đường trên trục dọc phụ ở bên phải. Các hàm trợ giúp cấu hình tiêu đề, chú giải, nhãn trục và các đường lưới.
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import AxisPositionType, ChartType, CrossesType, FillType, LegendPositionType, NullableBool, Presentation, SaveFormat
Color = jpype.JClass("java.awt.Color")
def create_combo_chart():
presentation = Presentation()
slide = presentation.getSlides().get_Item(0)
try:
chart = create_chart_with_first_series(slide)
add_second_series_to_chart(chart)
add_third_series_to_chart(chart)
set_primary_axes_format(chart)
set_secondary_axes_format(chart)
presentation.save("combo-chart.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
def create_chart_with_first_series(slide):
chart = slide.getShapes().addChart(ChartType.ClusteredColumn, 50, 50, 600, 400)
# Đặt tiêu đề biểu đồ.
chart.setTitle(True)
chart.getChartTitle().addTextFrameForOverriding("Chart Title")
chart.getChartTitle().setOverlay(False)
title_paragraph = chart.getChartTitle().getTextFrameForOverriding().getParagraphs().get_Item(0)
title_format = title_paragraph.getParagraphFormat().getDefaultPortionFormat()
title_format.setFontBold(NullableBool.False_)
title_format.setFontHeight(18.0)
# Đặt chú giải biểu đồ.
chart.getLegend().setPosition(LegendPositionType.Bottom)
chart.getLegend().getTextFormat().getPortionFormat().setFontHeight(12.0)
# Xóa các chuỗi và danh mục được tạo mặc định.
chart.getChartData().getSeries().clear()
chart.getChartData().getCategories().clear()
worksheet_index = 0
workbook = chart.getChartData().getChartDataWorkbook()
# Thêm danh mục mới.
cell = workbook.getCell(worksheet_index, 1, 0, "Category 1")
chart.getChartData().getCategories().add(cell)
cell = workbook.getCell(worksheet_index, 2, 0, "Category 2")
chart.getChartData().getCategories().add(cell)
cell = workbook.getCell(worksheet_index, 3, 0, "Category 3")
chart.getChartData().getCategories().add(cell)
cell = workbook.getCell(worksheet_index, 4, 0, "Category 4")
chart.getChartData().getCategories().add(cell)
# Thêm chuỗi đầu tiên.
series_name_cell = workbook.getCell(worksheet_index, 0, 1, "Series 1")
series = chart.getChartData().getSeries().add(series_name_cell, chart.getType())
series.getParentSeriesGroup().setOverlap(jpype.JByte(-25))
series.getParentSeriesGroup().setGapWidth(220)
cell = workbook.getCell(worksheet_index, 1, 1, 4.3)
series.getDataPoints().addDataPointForBarSeries(cell)
cell = workbook.getCell(worksheet_index, 2, 1, 2.5)
series.getDataPoints().addDataPointForBarSeries(cell)
cell = workbook.getCell(worksheet_index, 3, 1, 3.5)
series.getDataPoints().addDataPointForBarSeries(cell)
cell = workbook.getCell(worksheet_index, 4, 1, 4.5)
series.getDataPoints().addDataPointForBarSeries(cell)
return chart
def add_second_series_to_chart(chart):
workbook = chart.getChartData().getChartDataWorkbook()
worksheet_index = 0
series_name_cell = workbook.getCell(worksheet_index, 0, 2, "Series 2")
series = chart.getChartData().getSeries().add(series_name_cell, ChartType.ClusteredColumn)
series.getParentSeriesGroup().setOverlap(jpype.JByte(-25))
series.getParentSeriesGroup().setGapWidth(220)
cell = workbook.getCell(worksheet_index, 1, 2, 2.4)
series.getDataPoints().addDataPointForBarSeries(cell)
cell = workbook.getCell(worksheet_index, 2, 2, 4.4)
series.getDataPoints().addDataPointForBarSeries(cell)
cell = workbook.getCell(worksheet_index, 3, 2, 1.8)
series.getDataPoints().addDataPointForBarSeries(cell)
cell = workbook.getCell(worksheet_index, 4, 2, 2.8)
series.getDataPoints().addDataPointForBarSeries(cell)
def add_third_series_to_chart(chart):
workbook = chart.getChartData().getChartDataWorkbook()
worksheet_index = 0
series_name_cell = workbook.getCell(worksheet_index, 0, 3, "Series 3")
series = chart.getChartData().getSeries().add(series_name_cell, ChartType.Line)
cell = workbook.getCell(worksheet_index, 1, 3, 2.0)
series.getDataPoints().addDataPointForLineSeries(cell)
cell = workbook.getCell(worksheet_index, 2, 3, 2.0)
series.getDataPoints().addDataPointForLineSeries(cell)
cell = workbook.getCell(worksheet_index, 3, 3, 3.0)
series.getDataPoints().addDataPointForLineSeries(cell)
cell = workbook.getCell(worksheet_index, 4, 3, 5.0)
series.getDataPoints().addDataPointForLineSeries(cell)
series.setPlotOnSecondAxis(True)
def set_primary_axes_format(chart):
# Đặt trục ngang.
horizontal_axis = chart.getAxes().getHorizontalAxis()
horizontal_axis.getTextFormat().getPortionFormat().setFontHeight(12.0)
horizontal_axis.getFormat().getLine().getFillFormat().setFillType(FillType.NoFill)
set_axis_title(horizontal_axis, "X Axis")
# Đặt trục dọc.
vertical_axis = chart.getAxes().getVerticalAxis()
vertical_axis.getTextFormat().getPortionFormat().setFontHeight(12.0)
vertical_axis.getFormat().getLine().getFillFormat().setFillType(FillType.NoFill)
set_axis_title(vertical_axis, "Y Axis 1")
# Đặt màu cho các đường lưới chính dọc.
major_grid_lines_format = vertical_axis.getMajorGridLinesFormat().getLine().getFillFormat()
major_grid_lines_format.setFillType(FillType.Solid)
color = Color(217, 217, 217)
major_grid_lines_format.getSolidFillColor().setColor(color)
def set_secondary_axes_format(chart):
# Đặt trục ngang phụ.
secondary_horizontal_axis = chart.getAxes().getSecondaryHorizontalAxis()
secondary_horizontal_axis.setPosition(AxisPositionType.Bottom)
secondary_horizontal_axis.setCrossType(CrossesType.Maximum)
secondary_horizontal_axis.setVisible(False)
secondary_horizontal_axis.getMajorGridLinesFormat().getLine().getFillFormat().setFillType(FillType.NoFill)
secondary_horizontal_axis.getMinorGridLinesFormat().getLine().getFillFormat().setFillType(FillType.NoFill)
# Đặt trục dọc phụ.
secondary_vertical_axis = chart.getAxes().getSecondaryVerticalAxis()
secondary_vertical_axis.setPosition(AxisPositionType.Right)
secondary_vertical_axis.getTextFormat().getPortionFormat().setFontHeight(12.0)
secondary_vertical_axis.getFormat().getLine().getFillFormat().setFillType(FillType.NoFill)
secondary_vertical_axis.getMajorGridLinesFormat().getLine().getFillFormat().setFillType(FillType.NoFill)
secondary_vertical_axis.getMinorGridLinesFormat().getLine().getFillFormat().setFillType(FillType.NoFill)
set_axis_title(secondary_vertical_axis, "Y Axis 2")
def set_axis_title(axis, axis_title):
axis.setTitle(True)
axis.getTitle().setOverlay(False)
title_paragraph = axis.getTitle().addTextFrameForOverriding(axis_title).getParagraphs().get_Item(0)
title_format = title_paragraph.getParagraphFormat().getDefaultPortionFormat()
title_format.setFontBold(NullableBool.False_)
title_format.setFontHeight(12.0)
create_combo_chart()
Cập nhật biểu đồ
Aspose.Slides cho Python thông qua Java cho phép bạn cập nhật các biểu đồ PowerPoint bằng cách sửa đổi dữ liệu, định dạng và kiểu dáng biểu đồ. Tính năng này đơn giản hoá quá trình duy trì bản trình bày luôn cập nhật với nội dung động và đảm bảo biểu đồ phản ánh chính xác dữ liệu và tiêu chuẩn hình ảnh hiện tại.
Ví dụ này cập nhật biểu đồ có tên “My chart” trên slide đầu tiên của một bản trình bày hiện có. Nó yêu cầu hai chuỗi cột, mỗi chuỗi có ba điểm dữ liệu, với các nhãn danh mục ở cột A và tên chuỗi ở hàng đầu tiên của worksheet đầu tiên. Ví dụ sử dụng getCell để thay đổi các ô workbook, cập nhật giá trị chuỗi, thêm một chuỗi thứ ba và thay đổi loại biểu đồ thành cilindr nhóm qua setType.
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import Chart, ChartType, Presentation, SaveFormat
chart_name = "My chart"
presentation = Presentation("ExistingChart.pptx")
try:
slide = presentation.getSlides().get_Item(0)
for shape in slide.getShapes():
if isinstance(shape, Chart) and chart_name == shape.getName():
chart = shape
worksheet_index = 0
workbook = chart.getChartData().getChartDataWorkbook()
workbook.getCell(worksheet_index, 1, 0, "Modified Category 1")
workbook.getCell(worksheet_index, 2, 0, "Modified Category 2")
series = chart.getChartData().getSeries().get_Item(0)
workbook.getCell(worksheet_index, 0, 1, "New_Series 1")
series.getDataPoints().get_Item(0).getValue().setData(90)
series.getDataPoints().get_Item(1).getValue().setData(123)
series.getDataPoints().get_Item(2).getValue().setData(44)
series = chart.getChartData().getSeries().get_Item(1)
workbook.getCell(worksheet_index, 0, 2, "New_Series 2")
series.getDataPoints().get_Item(0).getValue().setData(23)
series.getDataPoints().get_Item(1).getValue().setData(67)
series.getDataPoints().get_Item(2).getValue().setData(99)
series_name_cell = workbook.getCell(worksheet_index, 0, 3, "Series 3")
series = chart.getChartData().getSeries().add(series_name_cell, chart.getType())
value_cell = workbook.getCell(worksheet_index, 1, 3, 20)
series.getDataPoints().addDataPointForBarSeries(value_cell)
value_cell = workbook.getCell(worksheet_index, 2, 3, 50)
series.getDataPoints().addDataPointForBarSeries(value_cell)
value_cell = workbook.getCell(worksheet_index, 3, 3, 30)
series.getDataPoints().addDataPointForBarSeries(value_cell)
chart.setType(ChartType.ClusteredCylinder)
presentation.save("AsposeChartModified.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
Đặt phạm vi dữ liệu cho biểu đồ
Để kiểm tra phạm vi đã được biểu đồ hiện có sử dụng, xem Retrieve a Chart’s Data Range.
Aspose.Slides cho Python thông qua Java cung cấp khả năng linh hoạt để xác định một phạm vi dữ liệu cụ thể từ worksheet làm nguồn dữ liệu cho biểu đồ của bạn. Điều này có nghĩa là bạn có thể ánh xạ trực tiếp một phần của worksheet tới biểu đồ, cho phép kiểm soát các ô nào đóng góp vào chuỗi và danh mục của biểu đồ. Kết quả là, bạn có thể dễ dàng cập nhật và đồng bộ hóa các biểu đồ với các thay đổi dữ liệu mới nhất trong worksheet, đảm bảo bản trình bày PowerPoint của bạn phản ánh thông tin hiện tại và chính xác.
Ví dụ này sử dụng setRange để đặt nguồn dữ liệu của biểu đồ có tên “My chart” trên slide đầu tiên của một bản trình bày hiện có thành Sheet1!A1:B4. Workbook của biểu đồ phải chứa worksheet đó và một phạm vi đã được điền với tiêu đề chuỗi ở hàng đầu, các nhãn danh mục ở cột A và ba giá trị ở cột B.
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import Chart, Presentation, SaveFormat
chart_name = "My chart"
presentation = Presentation("ExistingChart.pptx")
try:
slide = presentation.getSlides().get_Item(0)
for shape in slide.getShapes():
if isinstance(shape, Chart) and chart_name == shape.getName():
chart = shape
chart.getChartData().setRange("Sheet1!A1:B4")
presentation.save("SetDataRange.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
Sử dụng Dấu Mặc định trong Biểu đồ
Khi bạn sử dụng dấu mặc định trong biểu đồ, mỗi chuỗi biểu đồ sẽ tự động nhận một ký hiệu dấu khác nhau.
Ví dụ này sử dụng addChart để tạo một biểu đồ với hai chuỗi đường và một chú giải. Cài đặt dấu được truy cập qua getMarker giữ lại các ký hiệu dấu được gán tự động. Chuỗi đầu tiên bao gồm một giá trị âm và một giá trị thiếu, được biểu thị bằng None, trong khi chuỗi thứ hai có giá trị cho mọi danh mục.
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import ChartType, Presentation, SaveFormat
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
chart = slide.getShapes().addChart(ChartType.LineWithMarkers, 10, 10, 400, 400)
chart.getChartData().getSeries().clear()
chart.getChartData().getCategories().clear()
workbook = chart.getChartData().getChartDataWorkbook()
series_name_cell = workbook.getCell(0, 0, 1, "Series 1")
series = chart.getChartData().getSeries().add(series_name_cell, chart.getType())
category_cell = workbook.getCell(0, 1, 0, "C1")
chart.getChartData().getCategories().add(category_cell)
value_cell = workbook.getCell(0, 1, 1, 24)
series.getDataPoints().addDataPointForLineSeries(value_cell)
category_cell = workbook.getCell(0, 2, 0, "C2")
chart.getChartData().getCategories().add(category_cell)
value_cell = workbook.getCell(0, 2, 1, 23)
series.getDataPoints().addDataPointForLineSeries(value_cell)
category_cell = workbook.getCell(0, 3, 0, "C3")
chart.getChartData().getCategories().add(category_cell)
value_cell = workbook.getCell(0, 3, 1, -10)
series.getDataPoints().addDataPointForLineSeries(value_cell)
category_cell = workbook.getCell(0, 4, 0, "C4")
chart.getChartData().getCategories().add(category_cell)
value_cell = workbook.getCell(0, 4, 1, None)
series.getDataPoints().addDataPointForLineSeries(value_cell)
series_name_cell = workbook.getCell(0, 0, 2, "Series 2")
second_series = chart.getChartData().getSeries().add(series_name_cell, chart.getType())
value_cell = workbook.getCell(0, 1, 2, 30)
second_series.getDataPoints().addDataPointForLineSeries(value_cell)
value_cell = workbook.getCell(0, 2, 2, 10)
second_series.getDataPoints().addDataPointForLineSeries(value_cell)
value_cell = workbook.getCell(0, 3, 2, 60)
second_series.getDataPoints().addDataPointForLineSeries(value_cell)
value_cell = workbook.getCell(0, 4, 2, 40)
second_series.getDataPoints().addDataPointForLineSeries(value_cell)
chart.setLegend(True)
chart.getLegend().setOverlay(False)
presentation.save("DefaultMarkersInChart.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
FAQ
Các loại biểu đồ nào được Aspose.Slides cho Python thông qua Java hỗ trợ?
Aspose.Slides cho Python thông qua Java hỗ trợ nhiều loại biểu đồ, bao gồm bar, line, pie, area, scatter, histogram, radar và nhiều hơn nữa. Sự linh hoạt này cho phép bạn lựa chọn loại biểu đồ phù hợp nhất cho nhu cầu trực quan hoá dữ liệu của mình.
Làm sao để thêm một biểu đồ mới vào slide?
Để thêm biểu đồ, bạn đầu tiên tạo một thể hiện của lớp Presentation, lấy slide mong muốn bằng chỉ số của nó, sau đó gọi phương thức để thêm biểu đồ, chỉ định loại biểu đồ và dữ liệu ban đầu. Quá trình này tích hợp biểu đồ trực tiếp vào bản trình bày của bạn.
Làm sao tôi có thể cập nhật dữ liệu hiển thị trong biểu đồ?
Bạn có thể cập nhật dữ liệu của biểu đồ bằng cách truy cập workbook dữ liệu của nó (ChartDataWorkbook), xóa bất kỳ chuỗi và danh mục mặc định nào, rồi thêm dữ liệu tùy chỉnh của bạn. Điều này cho phép bạn làm mới biểu đồ một cách lập trình để phản ánh dữ liệu mới nhất.
Có thể tùy chỉnh giao diện của biểu đồ không?
Có, Aspose.Slides cho Python thông qua Java cung cấp nhiều tùy chọn tùy chỉnh. Bạn có thể thay đổi màu sắc, phông chữ, nhãn, chú giải và các yếu tố định dạng khác để điều chỉnh giao diện biểu đồ sao cho phù hợp với yêu cầu thiết kế cụ thể của mình.