Tạo hoặc Cập nhật Biểu đồ Bài thuyết trình PowerPoint trong 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 for Python via 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, và áp dụng các tùy chọn định dạng khác nhau để phù hợp với yêu cầu thiết kế cụ thể của bạn. 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 đối tượng Presentation và Chart cho tới cấu hình series, trục và legend. Bằng cách 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 của mình, giúp đơn giản hoá quy trình tạo các bản thuyết trình dựa trên dữ liệu.
Tạo biểu đồ
Biểu đồ giúp mọi người nhanh chóng trực quan hoá dữ liệu và rút ra các hiểu biết mà có thể không ngay lập tức rõ ràng từ bảng hoặc bảng tính.
Tại sao tạo biểu đồ?
Sử dụng biểu đồ, bạn có thể:
- tổng hợp, cô đọng hoặc tóm tắt lượng lớn dữ liệu trên một slide trong bản thuyết trình;
- tiết lộ 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 theo 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 không hợp lý;
- truyền đạt hoặc trình bày dữ liệu phức tạp.
Trong PowerPoint, bạn có thể tạo biểu đồ thông 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ả biểu đồ thông thường (dựa trên các loại biểu đồ phổ biến) và biểu đồ tùy chỉnh.
Note
Sử dụng enumeration ChartType trong package 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 series qua ba danh mục. Các series sử dụng màu đỏ và xanh lá, và các nhãn riêng lẻ hiển thị tên danh mục, tên series và giá trị. Chỉ mục worksheet 0 đề cập đến 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 đồ phân tán
Biểu đồ phân tán (còn gọi là scatter plot 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 đồ phân tán khi:
- Bạn có dữ liệu số cặp đôi.
- Bạn có hai biến có quan hệ chặt chẽ với nhau.
- Bạn muốn xác định xem 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 bộ giá trị x‑y. Series đầu tiên sử dụng đường thẳng với dấu sao, trong khi series thứ hai dùng đường mượt với dấu tròn; cả hai đều có kích thước marker là 10, được cấu hình bằng setSize. Phương thức setSymbol điều khiển hình dạng marker.
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 đồ tròn
Biểu đồ tròn thích hợp để hiển thị mối quan hệ phần‑toàn trong dữ liệu, đặc biệt khi dữ liệu có 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ó quá 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 phần 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 series. Phương thức setFirstSliceAngle đặt góc bắt đầu của phần đầu tiên ở 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ủa các nhãn nằm ngoài biểu đồ tròn, xem Format Leader Lines để có ví dụ về cách đặt vị trí 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à line graph) thích hợp trong các trường hợp 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 series dữ liệu, và hơn thế nữa.
Ví dụ này sử dụng addChart để tạo một biểu đồ đường với series và danh mục mặc định, sau đó lưu bản thuyết trình 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 đường thẳng liên tục. Nếu bạn muốn các điểm được nối bằng các đoạn gạch, hãy gọi setDashStyle với một 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 đồ cây
Biểu đồ cây (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ú ý tới các mục đóng góp lớn trong mỗi danh mục.
Ví dụ này tạo một biểu đồ cây với tám lá được nhóm thành bốn thân và hai nhánh. Phương thức setGroupingItem gán các thân ở mức nhóm 1 và các nhánh ở 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 hierarchy.
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 đồ thác nước
Biểu đồ thác nước 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 bảng phân tích lợi nhuận có thể bắt đầu bằng doanh thu, trừ chi phí, và cộng thu nhập khác. Các cột thay đổi nổi giữa giá trị chạy trước và mới, trong khi các cột tổng mở rộng từ zero tới giá trị tích lũy.
Ví dụ dưới đây sử dụng các số tiền tính bằng nghìn đô la Mỹ:
| Danh mục | Giá trị workbook | Loại cột |
|---|---|---|
| Doanh thu | 1000 | Tăng |
| Giá vốn bán hàng | -400 | Giảm |
| Lợi nhuận gộp | 600 | Tổng trung gian |
| Chi phí hoạt động | -200 | Giảm |
| Thu nhập khác | 50 | Tăng |
| Lợi nhuận ròng | 450 | Tổng cuối cùng |
Để đá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 hay điền giá trị ô. Không sử dụng zero làm placeholder hoặc cung cấp thay đổi khác cho một điểm tổng. Cột tổng hiển thị giá trị tích lũy mà không cộng lại vào giá trị đang chạy.
Ví dụ Python đầy đủ này tạo một biểu đồ sử dụng thành viên Waterfall của ChartType, xóa dữ liệu mặc định, và điền workbook được nhúng. Chỉ mục worksheet 0 chọn worksheet đầu tiên; cột A chứa tên danh mục và cột B chứa các giá trị. Mỗi ô giá trị được thêm bằng addDataPointForWaterfallSeries. Các chỉ mục điểm không có số 0 là 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 yêu cầu bản thuyết trình hoặc workbook đầu vào.
import jpage
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)
# Đ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 series setShowConnectorLines với True để hiển thị các đường nối các cột liên tiếp và làm cho 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 khả năng hiển thị đường, không ảnh hưởng tới dữ liệu hoặc tổng.
Bản thuyết trình đã 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 đều bắt đầu từ zero:

Tạo biểu đồ chứng khoán
Biểu đồ chứng khoán được dùng để hiển thị dữ liệu tài chính như giá mở, cao, thấp và đóng, hỗ trợ 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, giúp nhà đầu tư và nhà phân tích đưa ra quyết định sáng suốt.
Ví dụ này tạo một biểu đồ mở‑cao‑thấp‑đóng với bốn series theo thứ tự đó và ba danh mục. Phương thức setUpDownBars bật thanh lên/xuống, và getHiLowLinesFormat điều khiển các đường cao‑thấp hiển thị dải giá, trong khi các đường series riêng lẻ và lưới ngang phụ bị ẩn.
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import ChartType, FillType, Presentation, SaveFormat
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
chart = slide.getShapes().addChart(ChartType.OpenHighLowClose, 20, 20, 500, 300, 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)
chart.getChartData().getSeriesGroups().get_Item(0).getUpDownBars().setUpDownBars(True)
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ả:

Tạo biểu đồ hộp và râu
Biểu đồ hộp và râu được dùng để hiển thị sự phân bố dữ liệu bằng cách tóm tắt các thước đo 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 biến động dữ liệu và xác định bất thường.
Ví dụ này tạo một biểu đồ hộp và râu 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 hiệu trung bình, điểm nội và đ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 đồ phễu
Biểu đồ phễu được dùng để trực quan hoá 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 để 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 đồ phễu vào slide đầu tiên của một bản thuyết trình 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 thuyết trình đã cập nhật. Bản thuyết trình đầu vào phải chứa ít nhất một slide.
import jpype
import asposeslides
if not jpime.isJVMStarted():
jpime.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 đồ pháo hoa
Biểu đồ pháo hoa (Sunburst) được dùng để trực quan hoá dữ liệu phân cấp, hiển thị các mức độ dưới dạng các vòng đồng tâm. Chúng giúp minh họa các mối quan hệ phần‑toàn và là lựa chọn lý tưởng để biểu diễn các danh mục và tiểu danh mục lồng nhau một cách rõ ràng, gọn gàng.
Ví dụ này tạo một biểu đồ pháo hoa với tám lá được nhóm thành bốn thân và hai nhánh. Phương thức setGroupingItem gán các thân ở mức nhóm 1 và các nhánh ở mức 2. Các nhãn danh mục xác định các phân đ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 phối qua các khoảng (bins). Đối với các báo cáo tháng về thời gian xử lý đơn hàng, hãy sử dụng cùng một độ rộng bin và ngưỡng để giữ các khoảng nhất quán và giúp việc so sánh phân bố dễ dàng hơn.
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 phép biểu đồ tự chọn các bins.
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 thuyết trình hiển thị trong PowerPoint cho thấy số lượng bin là 3, 4, 4, 4, và 2. Bin underflow bao gồm 5, 10, 10, vì vậy ngưỡng của nó là bao gồm. Bin overflow 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 vào khoảng trước. Các bin nội chứa các giá trị lớn hơn ranh giới dưới và nhỏ hơn hoặc bằng ranh giới 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 một dữ liệu. Ví dụ này để bin underflow và overflow bị tắt. Biểu đồ xác định độ rộng dựa trên phạm vi dữ liệu, vì vậy các ranh giới 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 là 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 ranh giới 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 nhiều biến cùng lúc một cách dễ dàng. Chúng đặc biệt hữu ích để xác định các mẫu, điểm mạnh và điểm yếu qua nhiều chỉ số hoặc thuộc tính hiệu suất.
Ví dụ này sử dụng addChart để tạo một biểu đồ radar với series và danh mục mặc định, sau đó lưu bản thuyết trình 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 có hơn một nhóm danh mục, 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, nhiều 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 cung cấp cho mỗi cặp một nhãn chung ở mức nhóm 1, tạo ra một 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ả:

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 để phân tích xu hướng khu vực, dữ liệu nhân khẩu và sự phân bố không gian một cách rõ ràng, hấp dẫn.
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 để xem trong PowerPoint.
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.Map, 20, 20, 500, 300)
presentation.save("mapChart.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
Kết quả:

Note
Hình trên cho thấy bản thuyết trình đã lưu được 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 ảnh hoặc chuyển đổi sang PDF hoặc SVG, khu vực biểu đồ sẽ trống. Các hình 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 biệt 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 series cột nhóm với một series đường qua bốn danh mục. Các cột 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 đề, legend, nhãn trục và 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 đề cho 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 cho biểu đồ.
chart.getLegend().setPosition(LegendPositionType.Bottom)
chart.getLegend().getTextFormat().getPortionFormat().setFontHeight(12.0)
# Xóa các series 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 các 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 series đầ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 for Python via 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 biểu đồ, định dạng và kiểu dáng. Tính năng này đơn giản hoá quá trình giữ cho các bản thuyết trình luôn cập nhật với nội dung động và đảm bảo các 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 thuyết trình hiện có. Nó yêu cầu hai series cột với ba điểm dữ liệu mỗi series, với nhãn danh mục trong cột A và tên series trong 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ị series, thêm một series thứ ba, và thay đổi loại biểu đồ thành hình trụ 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 for Python via Java cung cấp tính linh hoạt để định nghĩa một phạm vi dữ liệu cụ thể từ worksheet làm nguồn cho dữ liệu 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 worksheet tới biểu đồ, cho phép kiểm soát các ô nào đóng góp vào series 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ộ các biểu đồ với các thay đổi dữ liệu mới nhất trong worksheet, đảm bảo các bản thuyết trình 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 thuyết trình 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 đề series ở hàng đầu, 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 marker mặc định trong biểu đồ
Khi bạn sử dụng marker mặc định trong biểu đồ, mỗi series sẽ tự động nhận một ký hiệu marker mặc định khác nhau.
Ví dụ này sử dụng addChart để tạo một biểu đồ với hai series đường và một legend. Cài đặt marker truy cập qua getMarker giữ lại các ký hiệu marker được gán tự động. Series đầ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 series 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 for Python via Java hỗ trợ?
Aspose.Slides for Python via Java hỗ trợ một loạt các loại biểu đồ, bao gồm bar, line, pie, area, scatter, histogram, radar và nhiều loại khác. Sự linh hoạt này cho phép bạn 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 một biểu đồ, trước tiên bạn tạo một thể hiện của lớp Presentation, lấy slide mong muốn bằng chỉ mục 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. Quy trình này tích hợp biểu đồ trực tiếp vào bản thuyết trình của bạn.
Làm sao tôi có thể cập nhật dữ liệu hiển thị trong một 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ỳ series và danh mục mặc định nào, và sau đó thêm dữ liệu tùy chỉnh của bạn. Điều này cho phép bạn lập trình làm mới biểu đồ để 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 for Python via Java cung cấp các tùy chọn tùy chỉnh phong phú. Bạn có thể thay đổi màu sắc, phông chữ, nhãn, legend và các yếu tố định dạng khác để điều chỉnh giao diện biểu đồ phù hợp với yêu cầu thiết kế cụ thể của mình.