Định dạng biểu đồ trong bản trình bày bằng Python
Tổng quan
Bài viết này giải thích cách định dạng biểu đồ trong bản trình bày PowerPoint bằng cách sử dụng Aspose.Slides. Nó cho thấy cách tùy chỉnh các thành phần quan trọng của biểu đồ như các trục, đường lưới, tiêu đề, chú giải, vùng vẽ và màu nền tường để cải thiện ngoại hình và khả năng đọc dữ liệu biểu đồ.
Nó cũng trình bày cách thiết lập thuộc tính phông chữ cho văn bản biểu đồ, áp dụng các định dạng số đã định sẵn cho dữ liệu biểu đồ và bật góc bo tròn cho khu vực biểu đồ. Những ví dụ này cho thấy cách kiểm soát cả phong cách hiển thị và cách trình bày dữ liệu của biểu đồ trong một bản trình bày.
Định dạng các thực thể biểu đồ
Sử dụng Biểu đồ để định dạng tiêu đề, trục, đường lưới, chú giải và vùng vẽ của biểu đồ. Ví dụ này tạo một biểu đồ đường có dấu đánh dấu và dữ liệu mặc định, sau đó áp dụng các kiểu văn bản và đường riêng biệt cho trục giá trị và trục danh mục. Kích thước phông chữ và độ rộng đường được chỉ định bằng điểm; nhãn danh mục được xoay 45 độ.
Trục giá trị sử dụng number_format độc lập với các ô nguồn. Thang đo chạy từ -2 đến 15, với khoảng cách lớn là 2 và khoảng cách nhỏ là 0,5. Ví dụ cũng kết hợp định dạng phần trăm với display_unit được đặt thành hàng nghìn, vì vậy hãy điều chỉnh các thiết lập này cho ý nghĩa và quy mô dữ liệu của bạn. Chú giải ở ngoài vùng vẽ vì overlay bị tắt. Màu nền phía sau và sàn được cấu hình qua back_wall và floor, nhưng những bề mặt này chỉ hiện ra trong biểu đồ 3D.
import aspose.slides.charts as charts
import aspose.slides as slides
with slides.Presentation() as presentation:
slide = presentation.slides[0]
chart = slide.shapes.add_chart(charts.ChartType.LINE_WITH_MARKERS, 50, 50, 500, 400)
chart.has_title = True
chart.chart_title.add_text_frame_for_overriding("")
chart_title = chart.chart_title.text_frame_for_overriding.paragraphs[0].portions[0]
chart_title.text = "Sample Chart"
chart_title.portion_format.fill_format.fill_type = slides.FillType.SOLID
chart_title.portion_format.fill_format.solid_fill_color.color = slides.Color.gray
chart_title.portion_format.font_height = 20
chart_title.portion_format.font_bold = slides.NullableBool.TRUE
chart_title.portion_format.font_italic = slides.NullableBool.TRUE
chart.axes.vertical_axis.major_grid_lines_format.line.fill_format.fill_type = slides.FillType.SOLID
chart.axes.vertical_axis.major_grid_lines_format.line.fill_format.solid_fill_color.color = slides.Color.blue
chart.axes.vertical_axis.major_grid_lines_format.line.width = 5
chart.axes.vertical_axis.major_grid_lines_format.line.dash_style = slides.LineDashStyle.DASH_DOT
chart.axes.vertical_axis.minor_grid_lines_format.line.fill_format.fill_type = slides.FillType.SOLID
chart.axes.vertical_axis.minor_grid_lines_format.line.fill_format.solid_fill_color.color = slides.Color.red
chart.axes.vertical_axis.minor_grid_lines_format.line.width = 3
chart.axes.vertical_axis.is_number_format_linked_to_source = False
chart.axes.vertical_axis.display_unit = charts.DisplayUnitType.THOUSANDS
chart.axes.vertical_axis.number_format = "0.0%"
chart.axes.vertical_axis.is_automatic_major_unit = False
chart.axes.vertical_axis.is_automatic_max_value = False
chart.axes.vertical_axis.is_automatic_minor_unit = False
chart.axes.vertical_axis.is_automatic_min_value = False
chart.axes.vertical_axis.max_value = 15
chart.axes.vertical_axis.min_value = -2
chart.axes.vertical_axis.minor_unit = 0.5
chart.axes.vertical_axis.major_unit = 2.0
vertical_axis_portion_format = chart.axes.vertical_axis.text_format.portion_format
vertical_axis_portion_format.font_bold = slides.NullableBool.TRUE
vertical_axis_portion_format.font_height = 16
vertical_axis_portion_format.font_italic = slides.NullableBool.TRUE
vertical_axis_portion_format.fill_format.fill_type = slides.FillType.SOLID
vertical_axis_portion_format.fill_format.solid_fill_color.color = slides.Color.dark_green
vertical_axis_portion_format.latin_font = slides.FontData("Times New Roman")
chart.axes.vertical_axis.has_title = True
chart.axes.vertical_axis.title.add_text_frame_for_overriding("")
vertical_axis_title = chart.axes.vertical_axis.title.text_frame_for_overriding.paragraphs[0].portions[0]
vertical_axis_title.text = "Primary Axis"
vertical_axis_title.portion_format.fill_format.fill_type = slides.FillType.SOLID
vertical_axis_title.portion_format.fill_format.solid_fill_color.color = slides.Color.gray
vertical_axis_title.portion_format.font_height = 20
vertical_axis_title.portion_format.font_bold = slides.NullableBool.TRUE
vertical_axis_title.portion_format.font_italic = slides.NullableBool.TRUE
chart.axes.horizontal_axis.major_grid_lines_format.line.fill_format.fill_type = slides.FillType.SOLID
chart.axes.horizontal_axis.major_grid_lines_format.line.fill_format.solid_fill_color.color = slides.Color.green
chart.axes.horizontal_axis.major_grid_lines_format.line.width = 5
chart.axes.horizontal_axis.minor_grid_lines_format.line.fill_format.fill_type = slides.FillType.SOLID
chart.axes.horizontal_axis.minor_grid_lines_format.line.fill_format.solid_fill_color.color = slides.Color.yellow
chart.axes.horizontal_axis.minor_grid_lines_format.line.width = 3
horizontal_axis_portion_format = chart.axes.horizontal_axis.text_format.portion_format
horizontal_axis_portion_format.font_bold = slides.NullableBool.TRUE
horizontal_axis_portion_format.font_height = 16
horizontal_axis_portion_format.font_italic = slides.NullableBool.TRUE
horizontal_axis_portion_format.fill_format.fill_type = slides.FillType.SOLID
horizontal_axis_portion_format.fill_format.solid_fill_color.color = slides.Color.blue
horizontal_axis_portion_format.latin_font = slides.FontData("Arial")
chart.axes.horizontal_axis.has_title = True
chart.axes.horizontal_axis.title.add_text_frame_for_overriding("")
horizontal_axis_title = chart.axes.horizontal_axis.title.text_frame_for_overriding.paragraphs[0].portions[0]
horizontal_axis_title.text = "Sample Category"
horizontal_axis_title.portion_format.fill_format.fill_type = slides.FillType.SOLID
horizontal_axis_title.portion_format.fill_format.solid_fill_color.color = slides.Color.gray
horizontal_axis_title.portion_format.font_height = 20
horizontal_axis_title.portion_format.font_bold = slides.NullableBool.TRUE
horizontal_axis_title.portion_format.font_italic = slides.NullableBool.TRUE
chart.axes.horizontal_axis.tick_label_position = charts.TickLabelPositionType.LOW
chart.axes.horizontal_axis.tick_label_rotation_angle = 45
legend_portion_format = chart.legend.text_format.portion_format
legend_portion_format.font_bold = slides.NullableBool.TRUE
legend_portion_format.font_height = 16
legend_portion_format.font_italic = slides.NullableBool.TRUE
legend_portion_format.fill_format.fill_type = slides.FillType.SOLID
legend_portion_format.fill_format.solid_fill_color.color = slides.Color.dark_red
chart.legend.overlay = False
chart.back_wall.thickness = 1
chart.back_wall.format.fill.fill_type = slides.FillType.SOLID
chart.back_wall.format.fill.solid_fill_color.color = slides.Color.orange
chart.floor.format.fill.fill_type = slides.FillType.SOLID
chart.floor.format.fill.solid_fill_color.color = slides.Color.red
chart.plot_area.format.fill.fill_type = slides.FillType.SOLID
chart.plot_area.format.fill.solid_fill_color.color = slides.Color.light_cyan
presentation.save("FormattedChart.pptx", slides.export.SaveFormat.PPTX)
Đặt thuộc tính phông chữ cho biểu đồ
Đặt kích thước phông chữ mặc định cho văn bản biểu đồ thông qua Chart.text_format. Ví dụ này tạo một biểu đồ cột cụm với phông chữ 20 điểm và bật nhãn giá trị cho chuỗi đầu tiên thông qua show_value. Các yếu tố văn bản có cài đặt phông chữ riêng có thể ghi đè lên mặc định của biểu đồ.
import aspose.slides.charts as charts
import aspose.slides as slides
with slides.Presentation() as presentation:
slide = presentation.slides[0]
chart = slide.shapes.add_chart(charts.ChartType.CLUSTERED_COLUMN, 100, 100, 500, 400)
chart.text_format.portion_format.font_height = 20
chart.chart_data.series[0].labels.default_data_label_format.show_value = True
presentation.save("ChartFontProperties.pptx", slides.export.SaveFormat.PPTX)
Đặt định dạng số
Sử dụng ChartDataCell.preset_number_format để áp dụng một định dạng số tích hợp sẵn cho các ô bảng tính của biểu đồ. Ví dụ này tạo một biểu đồ cột cụm với dữ liệu mặc định dựa trên ô và áp dụng chỉ mục định dạng 10 (0.00%) cho mỗi ô giá trị của chuỗi. Điều này thay đổi cách hiển thị giá trị trong bảng tính mà không thay đổi các số gốc; ví dụ, 0.25 được hiển thị là 25.00%. Các định dạng trục và nhãn dữ liệu có thể được cấu hình riêng biệt.
import aspose.slides.charts as charts
import aspose.slides as slides
with slides.Presentation() as presentation:
slide = presentation.slides[0]
chart = slide.shapes.add_chart(charts.ChartType.CLUSTERED_COLUMN, 50, 50, 500, 400)
for series in chart.chart_data.series:
for data_point in series.data_points:
data_point.value.as_cell.preset_number_format = 10 # 0.00%
presentation.save("PresetNumberFormat.pptx", slides.export.SaveFormat.PPTX)
Bảng sau liệt kê các chỉ mục định dạng số tích hợp sẵn kèm theo các định dạng Excel tiêu biểu cho miền tiếng Anh (Hoa Kỳ). Các chuỗi định dạng chính xác và cách hiển thị có thể khác nhau tùy vào ứng dụng và miền, đặc biệt đối với tiền tệ và ngày tháng. Một số định dạng Excel cũng khác so với mặc định trong tiêu chuẩn OOXML, như mô tả trong Microsoft’s number format implementation notes.
| Chỉ số | Định dạng |
|---|---|
| 0 | General |
| 1 | 0 |
| 2 | 0.00 |
| 3 | #,##0 |
| 4 | #,##0.00 |
| 5 | "$"#,##0_);("$"#,##0) |
| 6 | "$"#,##0_);[Red]("$"#,##0) |
| 7 | "$"#,##0.00_);("$"#,##0.00) |
| 8 | "$"#,##0.00_);[Red]("$"#,##0.00) |
| 9 | 0% |
| 10 | 0.00% |
| 11 | 0.00E+00 |
| 12 | # ?/? |
| 13 | # ??/?? |
| 14 | m/d/yyyy |
| 15 | d-mmm-yy |
| 16 | d-mmm |
| 17 | mmm-yy |
| 18 | h:mm AM/PM |
| 19 | h:mm:ss AM/PM |
| 20 | h:mm |
| 21 | h:mm:ss |
| 22 | m/d/yyyy h:mm |
| 37 | #,##0_);(#,##0) |
| 38 | #,##0_);[Red](#,##0) |
| 39 | #,##0.00_);(#,##0.00) |
| 40 | #,##0.00_);[Red](#,##0.00) |
| 41 | _(* #,##0_);_(* (#,##0);_(* "-"_);_(@_) |
| 42 | _("$"* #,##0_);_("$"* (#,##0);_("$"* "-"_);_(@_) |
| 43 | _(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(@_) |
| 44 | _("$"* #,##0.00_);_("$"* (#,##0.00);_("$"* "-"??_);_(@_) |
| 45 | mm:ss |
| 46 | [h]:mm:ss |
| 47 | mm:ss.0 |
| 48 | ##0.0E+0 |
| 49 | @ |
Đặt viền bo tròn cho khu vực biểu đồ
Bật Chart.has_rounded_corners để bo tròn các góc của khu vực biểu đồ. Ví dụ này tạo một biểu đồ cột cụm và sử dụng line_format để đặt viền ngoài màu xám đặc và kiểu đường đơn trước khi lưu bản trình bày.
import aspose.slides.charts as charts
import aspose.slides as slides
with slides.Presentation() as presentation:
slide = presentation.slides[0]
chart = slide.shapes.add_chart(charts.ChartType.CLUSTERED_COLUMN, 20, 100, 600, 400)
chart.line_format.fill_format.fill_type = slides.FillType.SOLID
chart.line_format.fill_format.solid_fill_color.color = slides.Color.gray
chart.line_format.style = slides.LineStyle.SINGLE
chart.has_rounded_corners = True
presentation.save("RoundedBorders.pptx", slides.export.SaveFormat.PPTX)
Chỉnh sửa chú thích biểu đồ hiện có
Một biểu đồ có thể chứa các hình dạng tùy chỉnh, chẳng hạn như chú thích văn bản giải thích trạng thái của báo cáo. Truy cập các hình dạng này qua Chart.user_shapes. Bộ sưu tập shapes của nó chứa các hình dạng thuộc sở hữu của biểu đồ.
Một hộp văn bản thông thường đặt lên trên biểu đồ thuộc về slide, ngay cả khi nó trông giống như một phần của biểu đồ. Duyệt qua Slide.shapes sẽ tìm thấy biểu đồ và các hình dạng slide thông thường, nhưng không liệt kê các hình dạng riêng lẻ bên trong biểu đồ. Tiêu đề biểu đồ và nhãn dữ liệu là các thành phần riêng biệt của biểu đồ, không phải là chú thích tùy chỉnh trong bộ sưu tập này.
Tải xuống chart-annotations.pptx và đặt nó trong thư mục làm việc của ứng dụng. Slide đầu tiên của tệp chứa một biểu đồ với một chú thích chưa nhóm có nội dung “Preliminary results”, một đường thẳng thuộc sở hữu của biểu đồ, và một hộp văn bản slide bình thường có nội dung “Slide overlay”. Ví dụ tìm biểu đồ đầu tiên trên slide đó và thay đổi chú thích thành “Final results” khi báo cáo đã sẵn sàng để công bố. Nó kiểm tra xem bộ sưu tập có rỗng hay không và chỉ chỉnh sửa một AutoShape có text_frame không phải None và chứa văn bản mong đợi.
import aspose.slides as slides
import aspose.slides.charts as charts
with slides.Presentation("chart-annotations.pptx") as presentation:
slide = presentation.slides[0]
chart = next((shape for shape in slide.shapes if isinstance(shape, charts.Chart)), None)
if chart is None or len(chart.user_shapes.shapes) == 0:
print("No chart or chart-owned shapes found.")
else:
updated = False
for shape in chart.user_shapes.shapes:
if isinstance(shape, slides.AutoShape) and shape.text_frame is not None:
if shape.text_frame.text == "Preliminary results":
shape.text_frame.text = "Final results"
updated = True
break
if updated:
presentation.save("chart-annotations-updated.pptx", slides.export.SaveFormat.PPTX)
else:
print("No matching text annotation found.")
Đường thẳng và hộp văn bản slide vẫn không thay đổi. Các hình ảnh sau đây hiển thị đầu vào và kết quả đã lưu, mở lại:
| Trước khi chỉnh sửa | Sau khi chỉnh sửa |
|---|---|
![]() |
![]() |
Note
Ví dụ chỉ kiểm tra các hình dạng thuộc sở hữu cấp đầu của biểu đồ. Nếu một chú thích nằm trong một GroupShape, hãy kiểm tra bộ sưu tập shapes của nhóm đó nữa. Lặp lại quá trình này một cách đệ quy cho các nhóm lồng nhau, áp dụng cùng các kiểm tra văn bản cho các hình dạng con của chúng. Một nhóm tự nó là một container; không coi nó như một hộp văn bản.Câu hỏi thường gặp
Tôi có thể đặt màu nền bán trong suốt cho cột/khu vực trong khi giữ viền không trong suốt không?
Có. Độ trong suốt màu nền và viền được cấu hình riêng biệt. Điều này hữu ích để cải thiện khả năng đọc lưới và dữ liệu trong các biểu đồ dày đặc.
Làm thế nào tôi có thể xử lý nhãn dữ liệu khi chúng chồng lên nhau?
Giảm kích thước phông chữ, tắt các thành phần nhãn không cần thiết (ví dụ, danh mục), điều chỉnh vị trí nhãn, chỉ hiển thị nhãn cho các điểm được chọn nếu cần, hoặc chuyển định dạng sang “giá trị + chú giải”.
Tôi có thể áp dụng màu nền gradient hoặc họa tiết cho các chuỗi không?
Có. Các chuỗi hỗ trợ màu nền đặc, gradient và họa tiết. Trong thực tế, sử dụng gradient một cách hạn chế và tránh các kết hợp làm giảm độ tương phản với lưới và văn bản.

