Quản lý Nhãn Dữ liệu Biểu đồ trong Bản Trình chiếu bằng Python
Giới thiệu
Nhãn dữ liệu hiển thị thông tin về các chuỗi biểu đồ và các điểm dữ liệu riêng lẻ, giúp người đọc xác định giá trị và hiểu biểu đồ. Bài viết này giải thích cách định dạng giá trị, hiển thị phần trăm, đọc văn bản nhãn, kiểm soát nhãn vượt quá giá trị tối đa của trục, điều chỉnh khoảng cách nhãn trục danh mục, và định vị nhãn biểu đồ tròn.
Đặt Độ Chính Xác Dữ Liệu trong Nhãn Dữ Liệu Biểu Đồ
Sử dụng setNumberFormatOfValues để định dạng giá trị chuỗi. Ví dụ này tạo biểu đồ đường với dữ liệu mặc định, hiển thị bảng dữ liệu của nó, và bật nhãn giá trị cho chuỗi đầu tiên. Định dạng #,##0.00 hiển thị dấu phân cách hàng ngàn và hai chữ số thập phân mà không thay đổi giá trị gố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.Line, 50, 50, 450, 300)
chart.setDataTable(True)
series = chart.getChartData().getSeries().get_Item(0)
series.setNumberFormatOfValues("#,##0.00")
series.getLabels().getDefaultDataLabelFormat().setShowValue(True)
presentation.save("PrecisionOfDatalabels_out.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
Để định dạng các ô số trong workbook nhúng của biểu đồ, xem Đặt Định Dạng Số Tùy Chỉnh cho Các Ô Workbook. Ví dụ đó cũng cho thấy nhãn giá trị tuân theo định dạng ô nguồn trong khi giữ nguyên giá trị số đã lưu.
Hiển Thị Phần Trăm dưới dạng Nhãn
Đối với biểu đồ cột chồng, tính mỗi giá trị dưới dạng phần trăm của tổng danh mục và gán văn bản cho khung văn bản được trả về bởi getTextFrameForOverriding. Ví dụ này sử dụng dữ liệu biểu đồ mặc định và hiển thị phần trăm với hai chữ số thập phân trong phông chữ 8 điểm. Các danh mục có tổng bằng không sẽ bị bỏ qua để tránh chia cho zero. Tính lại văn bản nhãn tùy chỉnh nếu dữ liệu biểu đồ thay đổi.
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import ChartType, Portion, Presentation, SaveFormat
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
chart = slide.getShapes().addChart(ChartType.StackedColumn, 20, 20, 400, 400)
chart_series = chart.getChartData().getSeries()
category_totals = [0.0] * chart.getChartData().getCategories().size()
for category_index in range(len(category_totals)):
for series_index in range(chart_series.size()):
data_point = chart_series.get_Item(series_index).getDataPoints().get_Item(category_index)
category_totals[category_index] += float(data_point.getValue().getData())
for series_index in range(chart_series.size()):
series = chart_series.get_Item(series_index)
series.getLabels().getDefaultDataLabelFormat().setShowLegendKey(False)
for point_index in range(series.getDataPoints().size()):
data_point = series.getDataPoints().get_Item(point_index)
label = data_point.getLabel()
if category_totals[point_index] == 0:
print(f"Cannot calculate a percentage for category {point_index}: the total is zero.")
continue
point_percentage = float(data_point.getValue().getData()) / category_totals[point_index] * 100
portion = Portion()
portion.setText(f"{point_percentage:.2f} %")
portion.getPortionFormat().setFontHeight(8)
label.getTextFrameForOverriding().setText("")
paragraph = label.getTextFrameForOverriding().getParagraphs().get_Item(0)
paragraph.getPortions().add(portion)
label_format = label.getDataLabelFormat()
label_format.setShowValue(True)
label_format.setShowSeriesName(False)
label_format.setShowPercentage(False)
label_format.setShowLegendKey(False)
label_format.setShowCategoryName(False)
label_format.setShowBubbleSize(False)
presentation.save("DisplayPercentageAsLabels_out.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
Đặt Dấu Phần Trăm cho Nhãn Dữ Liệu Biểu Đồ
Khi giá trị được lưu dưới dạng phân số, sử dụng setNumberFormat để hiển thị phần trăm. Truyền False cho setNumberFormatLinkedToSource để áp dụng định dạng nhãn một cách độc lập với các ô nguồn.
Ví dụ này tạo biểu đồ cột chồng 100% với hai chuỗi màu đỏ và xanh lam qua bốn danh mục. Mỗi cặp giá trị cộng lại thành 1. Định dạng nhãn 0.0% hiển thị 0.30 thành 30.0%, trong khi trục dọc sử dụng hai chữ số thập phân. Cả hai chuỗi đều sử dụng văn bản nhãn màu trắng, kích thước 10 điểm.
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.PercentsStackedColumn, 20, 20, 500, 400)
chart.getAxes().getVerticalAxis().setNumberFormatLinkedToSource(False)
chart.getAxes().getVerticalAxis().setNumberFormat("0.00%")
chart.getChartData().getSeries().clear()
chart.getChartData().getCategories().clear()
workbook = chart.getChartData().getChartDataWorkbook()
worksheet_index = 0
for i in range(4):
category_cell = workbook.getCell(worksheet_index, i + 1, 0, f"Category {i + 1}")
chart.getChartData().getCategories().add(category_cell)
series_names = ["Reds", "Blues"]
series_colors = [Color.RED, Color.BLUE]
values = [[0.30, 0.50, 0.80, 0.65], [0.70, 0.50, 0.20, 0.35]]
for i, series_name in enumerate(series_names):
series_cell = workbook.getCell(worksheet_index, 0, i + 1, series_name)
series = chart.getChartData().getSeries().add(series_cell, chart.getType())
for j, value in enumerate(values[i]):
value_cell = workbook.getCell(worksheet_index, j + 1, i + 1, jpype.JDouble(value))
series.getDataPoints().addDataPointForBarSeries(value_cell)
series.getFormat().getFill().setFillType(FillType.Solid)
series.getFormat().getFill().getSolidFillColor().setColor(series_colors[i])
label_format = series.getLabels().getDefaultDataLabelFormat()
label_format.setShowValue(True)
label_format.setNumberFormatLinkedToSource(False)
label_format.setNumberFormat("0.0%")
portion_format = label_format.getTextFormat().getPortionFormat()
portion_format.setFontHeight(10)
portion_format.getFillFormat().setFillType(FillType.Solid)
portion_format.getFillFormat().getSolidFillColor().setColor(Color.WHITE)
presentation.save("SetDataLabelsPercentageSign_out.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
Đọc Văn Bản Thực tế của Nhãn Dữ Liệu
Sử dụng getActualLabelText để truy xuất văn bản được tạo ra bởi cài đặt của nhãn dữ liệu. Điều này hữu ích khi trích xuất nhãn cho báo cáo, tìm kiếm nội dung bản trình bày, hoặc xác thực các biểu đồ được tạo. Trong ví dụ dưới đây, định dạng nhãn dữ liệu mặc định kết hợp mỗi tên danh mục, tên chuỗi và giá trị. Một điểm định dạng giá trị của nó dưới dạng phần trăm, và một điểm khác sử dụng văn bản tùy chỉnh từ getTextFrameForOverriding.
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import ChartType, Presentation
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()
first_category_cell = workbook.getCell(0, 1, 0, "Q1")
chart.getChartData().getCategories().add(first_category_cell)
second_category_cell = workbook.getCell(0, 2, 0, "Q2")
chart.getChartData().getCategories().add(second_category_cell)
north_series_cell = workbook.getCell(0, 0, 1, "North")
north = chart.getChartData().getSeries().add(north_series_cell, chart.getType())
north_first_value_cell = workbook.getCell(0, 1, 1, jpype.JDouble(0.25))
north.getDataPoints().addDataPointForBarSeries(north_first_value_cell)
north_second_value_cell = workbook.getCell(0, 2, 1, jpype.JDouble(0.75))
north.getDataPoints().addDataPointForBarSeries(north_second_value_cell)
south_series_cell = workbook.getCell(0, 0, 2, "South")
south = chart.getChartData().getSeries().add(south_series_cell, chart.getType())
south_first_value_cell = workbook.getCell(0, 1, 2, jpype.JDouble(0.40))
south.getDataPoints().addDataPointForBarSeries(south_first_value_cell)
south_second_value_cell = workbook.getCell(0, 2, 2, jpype.JDouble(0.60))
south.getDataPoints().addDataPointForBarSeries(south_second_value_cell)
for series in chart.getChartData().getSeries():
label_format = series.getLabels().getDefaultDataLabelFormat()
label_format.setShowCategoryName(True)
label_format.setShowSeriesName(True)
label_format.setShowValue(True)
north.getLabels().get_Item(1).getDataLabelFormat().setNumberFormatLinkedToSource(False)
north.getLabels().get_Item(1).getDataLabelFormat().setNumberFormat("0%")
south.getLabels().get_Item(0).getTextFrameForOverriding().setText("Reviewed")
for series in chart.getChartData().getSeries():
for point in series.getDataPoints():
label = point.getLabel()
if not label.isVisible():
continue
print(f"Value: {point.getValue().getData()}; label: {label.getActualLabelText()}")
finally:
presentation.dispose()
Số được lưu trong một điểm dữ liệu vẫn là 0.75, ngay cả khi nhãn của nó hiển thị 75% cùng với tên danh mục và chuỗi. Văn bản tùy chỉnh thay thế văn bản nhãn được tạo ra. getActualLabelText trả về chuỗi nhãn kết quả trong cả hai trường hợp. Kiểm tra isVisible riêng biệt, như đã hiển thị ở trên, khi bạn muốn trích xuất chỉ những nhãn hiển thị.
Kiểm soát Nhãn Dữ Liệu vượt quá Giá trị Tối đa của Trục
Khi bạn giới hạn phạm vi trục thủ công, một số điểm dữ liệu có thể vượt quá giá trị tối đa của nó. Sử dụng setShowDataLabelsOverMaximum để kiểm soát việc hiển thị nhãn dữ liệu của chúng. Cài đặt này thay đổi khả năng hiển thị nhãn; nó không thay đổi phạm vi trục hoặc giá trị dữ liệu gốc.
Ví dụ dưới đây tạo biểu đồ cột nhóm 2D với các giá trị 60 và 120. Nó truyền False cho setAutomaticMaxValue và đặt giá trị tối đa thành 100 bằng setMaxValue trên trục dọc. Bản trình chiếu đầu tiên cho phép nhãn vượt quá tối đa; một bản sao của bản trình chiếu đó tắt chúng. Cả hai bản trình chiếu đều được lưu trong DataLabelsOverMaximum.pptx.
Kích hoạt nhãn giá trị với setShowValue. Cài đặt ở mức biểu đồ không tự động bật hiển thị giá trị hoặc ghi đè lên cài đặt tắt hiển thị giá trị của nhãn cá nhân. Ví dụ này bật giá trị cho toàn bộ chuỗi và sử dụng setPosition để đặt nhãn ở đầu ngoài của mỗi cột.
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import ChartType, LegendDataLabelPosition, Presentation, SaveFormat
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
chart = slide.getShapes().addChart(ChartType.ClusteredColumn, 50, 50, 600, 400)
chart.setLegend(False)
chart.getChartData().getSeries().clear()
chart.getChartData().getCategories().clear()
workbook = chart.getChartData().getChartDataWorkbook()
first_category = workbook.getCell(0, 1, 0, "Within range")
second_category = workbook.getCell(0, 2, 0, "Above maximum")
chart.getChartData().getCategories().add(first_category)
chart.getChartData().getCategories().add(second_category)
series_name = workbook.getCell(0, 0, 1, "Values")
series = chart.getChartData().getSeries().add(series_name, chart.getType())
first_value = workbook.getCell(0, 1, 1, jpype.JDouble(60))
second_value = workbook.getCell(0, 2, 1, jpype.JDouble(120))
series.getDataPoints().addDataPointForBarSeries(first_value)
series.getDataPoints().addDataPointForBarSeries(second_value)
series.getLabels().getDefaultDataLabelFormat().setShowValue(True)
series.getLabels().getDefaultDataLabelFormat().setPosition(LegendDataLabelPosition.OutsideEnd)
chart.getAxes().getVerticalAxis().setAutomaticMaxValue(False)
chart.getAxes().getVerticalAxis().setMaxValue(100)
chart.setShowDataLabelsOverMaximum(True)
second_slide = presentation.getSlides().addClone(slide)
second_chart = second_slide.getShapes().get_Item(0)
second_chart.setShowDataLabelsOverMaximum(False)
presentation.save("DataLabelsOverMaximum.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
Các hình ảnh sau cho thấy các slide đã lưu được render bởi Microsoft PowerPoint. Với True, nhãn 120 hiển thị ở biên trên; với False, nhãn này bị ẩn. Nhãn 60 vẫn hiển thị, giá trị tối đa của trục vẫn ở 100, và điểm dữ liệu thứ hai vẫn là 120 trong cả hai trường hợp.
| setShowDataLabelsOverMaximum(True) | setShowDataLabelsOverMaximum(False) |
|---|---|
![]() |
![]() |
Chart Type
Ví dụ này sử dụng biểu đồ cột 2D có trục giá trị. Các biểu đồ không có trục giá trị, chẳng hạn biểu đồ tròn và bánh donut, không có giá trị tối đa của trục để giới hạn theo cách này.Đặt Khoảng Cách Nhãn so với Trục
Sử dụng setLabelOffset để kiểm soát khoảng cách giữa nhãn trục danh mục và trục. Giá trị là phần trăm của kích thước phông chữ tối đa của các nhãn trục. Ví dụ này tạo biểu đồ cột nhóm và đặt độ lệch nhãn trục ngang thành 500. Cài đặt này ảnh hưởng đến nhãn trục danh mục hơn là nhãn gắn vào các điểm dữ liệu riêng lẻ.
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.getAxes().getHorizontalAxis().setLabelOffset(500)
presentation.save("SetCategoryAxisLabelDistance_out.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
Điều Chỉnh Vị Trí Nhãn
Trên biểu đồ tròn, điều chỉnh vị trí nhãn dữ liệu để cải thiện khoảng cách và tạo không gian cho các đường dẫn.
Ví dụ này hiển thị giá trị của điểm dữ liệu đầu tiên, đặt nhãn của nó ra ngoài phần lát, và điều chỉnh độ lệch ngang và dọc bằng setX và setY. Các độ lệch này tính tương đối so với chiều rộng và chiều cao của biểu đồ.
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import ChartType, LegendDataLabelPosition, Presentation, SaveFormat
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
chart = slide.getShapes().addChart(ChartType.Pie, 50, 50, 200, 200)
series = chart.getChartData().getSeries()
label = series.get_Item(0).getLabels().get_Item(0)
label.getDataLabelFormat().setShowValue(True)
label.getDataLabelFormat().setPosition(LegendDataLabelPosition.OutsideEnd)
label.setX(0.71)
label.setY(0.04)
presentation.save("presentation.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()

Định Dạng Đường Dẫn
Khi nhãn biểu đồ tròn nằm ngoài các lát cắt, các đường dẫn giúp người đọc liên kết mỗi giá trị với lát cắt tương ứng. Màu tương phản và đường hơi dày hơn có thể làm cho các kết nối này dễ theo dõi hơn, trong khi độ trong suốt và kiểu nét đứt giữ cho các đường không gây mất tập trung.
Sử dụng getLeaderLinesFormat để định dạng các đường dẫn ở mức bộ sưu tập nhãn dữ liệu của chuỗi. Các cài đặt trong series.getLabels().getLeaderLinesFormat().getLine() áp dụng cho toàn bộ bộ sưu tập, chứ không phải cho từng nhãn riêng lẻ.
Ví dụ này tạo biểu đồ tròn với dữ liệu mặc định, hiển thị giá trị ra ngoài các lát cắt, và bật setShowLeaderLines. Nó di chuyển hai nhãn xa hơn các lát cắt bằng các độ lệch setX và setY, biểu diễn dưới dạng phần của chiều rộng và chiều cao biểu đồ. Không cần bản trình chiếu đầu vào.
Định dạng getFillFormat của đường đặt màu xanh xanh đậm đặc với giá trị alpha 191 trong số 255, tạo ra khoảng 25% độ trong suốt. setWidth đặt độ dày thành 2 điểm, và setDashStyle làm cho đường nét đứt.
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import ChartType, FillType, LegendDataLabelPosition, LineDashStyle, Presentation, SaveFormat
Color = jpype.JClass("java.awt.Color")
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
chart = slide.getShapes().addChart(ChartType.Pie, 80, 50, 560, 400)
chart.setLegend(False)
series = chart.getChartData().getSeries().get_Item(0)
label_format = series.getLabels().getDefaultDataLabelFormat()
label_format.setShowValue(True)
label_format.setPosition(LegendDataLabelPosition.OutsideEnd)
label_format.setShowLeaderLines(True)
# Di chuyển hai nhãn ra xa hơn khỏi các lát cắt.
series.getLabels().get_Item(0).setX(0.10)
series.getLabels().get_Item(0).setY(-0.05)
series.getLabels().get_Item(1).setX(-0.10)
series.getLabels().get_Item(1).setY(0.05)
line = series.getLabels().getLeaderLinesFormat().getLine()
line.getFillFormat().setFillType(FillType.Solid)
color = Color(30, 90, 160, 191)
line.getFillFormat().getSolidFillColor().setColor(color)
line.setWidth(2)
line.setDashStyle(LineDashStyle.Dash)
presentation.save("FormattedLeaderLines.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
Kích hoạt các đường dẫn không đảm bảo rằng mỗi nhãn sẽ có một đường. Tính khả thấy phụ thuộc vào vị trí nhãn và hành vi bố cục của biểu đồ. Trong ví dụ này, hai nhãn được di chuyển có đường dẫn hiển thị; các nhãn còn gần lát cắt không cần chúng.
Hình ảnh sau cho thấy file FormattedLeaderLines.pptx đã được render bằng Aspose.Slides:

Thêm Nhiều Hàng Nhãn Dữ Liệu phía Trên Biểu Đồ Cột
Ví dụ này tạo biểu đồ cột với hai hàng nhãn dữ liệu ở phía trên vùng vẽ. Chuỗi A hiển thị các cột có thể nhìn thấy, trong khi Chuỗi B và Chuỗi C cung cấp các nhãn bổ sung. Các cột của chúng được ẩn bằng cách loại bỏ màu nền và viền. Phương thức ChartSeriesGroup.setOverlap căn chỉnh cả ba chuỗi với cùng một trung tâm danh mục.
Cài đặt ChartPlotArea dành chỗ cho các hàng nhãn. Sau khi Chart.validateChartLayout tính toán vị trí mặc định, DataLabel.setX và DataLabel.setY giữ nguyên căn chỉnh ngang và áp dụng độ lệch dọc để sắp xếp nhãn trong hai hàng. Các số vẫn là nhãn dữ liệu liên kết với giá trị chuỗi; chỉ tiêu đề hàng là các hình dạng văn bản riêng.
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import ChartType, FillType, LayoutTargetType, LegendDataLabelPosition, Presentation, SaveFormat, ShapeType
Color = jpype.JClass("java.awt.Color")
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
chart = slide.getShapes().addChart(ChartType.ClusteredColumn, 40, 40, 640, 200)
chart.setTitle(False)
chart.setLegend(False)
chart.getTextFormat().getPortionFormat().setFontHeight(12)
chart.getChartData().getSeries().clear()
chart.getChartData().getCategories().clear()
workbook = chart.getChartData().getChartDataWorkbook()
workbook.clear(0)
categories = ["North", "South", "East", "West"]
series_names = ["Series A", "Series B", "Series C"]
series_values = [[35, 42, 28, 47], [22, 31, 19, 26], [12, 16, 14, 18]]
for category_index, category in enumerate(categories):
category_cell = workbook.getCell(0, category_index + 1, 0, category)
chart.getChartData().getCategories().add(category_cell)
for series_index, series_name in enumerate(series_names):
series_cell = workbook.getCell(0, 0, series_index + 1, series_name)
series = chart.getChartData().getSeries().add(series_cell, ChartType.ClusteredColumn)
for category_index, value in enumerate(series_values[series_index]):
value_cell = workbook.getCell(0, category_index + 1, series_index + 1, jpype.JDouble(value))
series.getDataPoints().addDataPointForBarSeries(value_cell)
if series_index > 0:
# Ẩn các cột của B và C, nhưng giữ lại nhãn dữ liệu của chúng.
series.getFormat().getFill().setFillType(FillType.NoFill)
series.getFormat().getLine().getFillFormat().setFillType(FillType.NoFill)
label_format = series.getLabels().getDefaultDataLabelFormat()
label_format.setShowValue(True)
label_format.getTextFormat().getPortionFormat().setFontHeight(12)
label_fill = label_format.getTextFormat().getPortionFormat().getFillFormat()
label_fill.setFillType(FillType.Solid)
label_fill.getSolidFillColor().setColor(Color.BLACK)
label_format.setPosition(LegendDataLabelPosition.InsideBase)
# Căn chỉnh cả ba chuỗi sao cho chúng có cùng trung tâm danh mục.
chart.getChartData().getSeries().get_Item(0).getParentSeriesGroup().setOverlap(jpype.JByte(100))
# Sử dụng ít đường lưới hơn cho ví dụ gọn này.
chart.getAxes().getVerticalAxis().setAutomaticMajorUnit(False)
chart.getAxes().getVerticalAxis().setMajorUnit(10)
# Dành không gian phía trên khu vực vẽ cho hai hàng nhãn dữ liệu.
chart.getPlotArea().setLayoutTargetType(LayoutTargetType.Inner)
chart.getPlotArea().setX(0.15)
chart.getPlotArea().setY(0.32)
chart.getPlotArea().setWidth(0.80)
chart.getPlotArea().setHeight(0.48)
chart.validateChartLayout()
for series_index in range(1, len(series_names)):
series = chart.getChartData().getSeries().get_Item(series_index)
row_top = 0.15 if series_index == 1 else 0.03
for category_index in range(len(categories)):
data_label = series.getDataPoints().get_Item(category_index).getLabel()
# Giữ vị trí ngang mặc định. Y là độ lệch từ
# vị trí nhãn mặc định, biểu diễn dưới dạng phần của chiều cao biểu đồ.
data_label.setX(0)
data_label.setY(row_top - data_label.getActualY() / chart.getHeight())
# Chỉ tiêu đề hàng là một hình dạng văn bản riêng biệt.
heading_y = chart.getY() + row_top * chart.getHeight()
row_heading = slide.getShapes().addAutoShape(ShapeType.Rectangle, chart.getX(), heading_y, 85, 18)
row_heading.getFillFormat().setFillType(FillType.NoFill)
row_heading.getLineFormat().getFillFormat().setFillType(FillType.NoFill)
row_heading.addTextFrame(series_names[series_index])
row_heading.getTextFrame().getTextFrameFormat().setMarginTop(0)
row_heading.getTextFrame().getTextFrameFormat().setMarginBottom(0)
heading_format = row_heading.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getPortionFormat()
heading_format.setFontHeight(12)
heading_format.getFillFormat().setFillType(FillType.Solid)
heading_format.getFillFormat().getSolidFillColor().setColor(Color.BLACK)
presentation.save("multiple-rows-of-labels.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()

Câu hỏi thường gặp
Làm thế nào để ngăn nhãn dữ liệu bị chồng lấp trên các biểu đồ dày đặc?
Kết hợp đặt nhãn tự động, các đường dẫn, và giảm kích thước phông chữ; nếu cần, ẩn một số trường (ví dụ, danh mục) hoặc chỉ hiển thị nhãn cho các giá trị cực đoan hoặc các điểm quan trọng.
Làm thế nào để tắt nhãn chỉ cho các giá trị bằng 0, âm hoặc rỗng?
Lọc các điểm dữ liệu trước khi bật nhãn và tắt hiển thị cho các giá trị 0, giá trị âm, hoặc giá trị thiếu theo quy tắc đã định.
Làm thế nào để đảm bảo kiểu nhãn nhất quán khi xuất ra PDF/hình ảnh?
Thiết lập rõ ràng họ phông chữ và kích thước và xác minh phông chữ có sẵn trong môi trường render để tránh fallback.

