使用 Python 在演示文稿中管理图表数据标签
介绍
数据标签显示有关图表系列和单个数据点的信息,帮助读者识别数值并理解图表。本文说明如何格式化数值、显示百分比、读取标签文本、控制超出轴最大值的标签、调整类别轴标签间距以及定位饼图标签。
在图表数据标签中设置数据精度
使用 number_format_of_values 来格式化系列值。此示例创建一个带默认数据的折线图,显示其数据表,并为第一系列启用值标签。格式 #,##0.00 显示千位分隔符和两位小数,而不更改底层值。
import aspose.slides as slides
import aspose.slides.charts as charts
with slides.Presentation() as presentation:
slide = presentation.slides[0]
chart = slide.shapes.add_chart(charts.ChartType.LINE, 50, 50, 450, 300)
chart.has_data_table = True
series = chart.chart_data.series[0]
series.number_format_of_values = "#,##0.00"
series.labels.default_data_label_format.show_value = True
presentation.save("PrecisionOfDatalabels_out.pptx", slides.export.SaveFormat.PPTX)
显示百分比为标签
对于堆积柱形图,计算每个值占其类别总计的百分比,并将文本分配给 text_frame_for_overriding。此示例使用默认图表数据,并以 8 磅字体显示两位小数的百分比。总计为零的类别会被跳过,以避免除以零。如果图表数据更改,需要重新计算自定义标签文本。
import aspose.slides as slides
import aspose.slides.charts as charts
with slides.Presentation() as presentation:
slide = presentation.slides[0]
chart = slide.shapes.add_chart(charts.ChartType.STACKED_COLUMN, 20, 20, 400, 400)
category_totals = [0.0] * len(chart.chart_data.categories)
for k in range(len(chart.chart_data.categories)):
for series in chart.chart_data.series:
point_value = float(series.data_points[k].value.data)
category_totals[k] += point_value
for series in chart.chart_data.series:
series.labels.default_data_label_format.show_legend_key = False
for j in range(len(series.data_points)):
label = series.data_points[j].label
if category_totals[j] == 0:
continue
point_value = float(series.data_points[j].value.data)
data_point_percent = point_value / category_totals[j] * 100
portion = slides.Portion()
portion.text = f"{data_point_percent:.2f} %"
portion.portion_format.font_height = 8
label.text_frame_for_overriding.text = ""
paragraph = label.text_frame_for_overriding.paragraphs[0]
paragraph.portions.add(portion)
label.data_label_format.show_value = True
label.data_label_format.show_series_name = False
label.data_label_format.show_percentage = False
label.data_label_format.show_legend_key = False
label.data_label_format.show_category_name = False
label.data_label_format.show_bubble_size = False
presentation.save("DisplayPercentageAsLabels_out.pptx", slides.export.SaveFormat.PPTX)
在图表数据标签中设置百分号
当值以分数形式存储时,使用 number_format 来显示百分比。将 is_number_format_linked_to_source 设置为 False,即可使标签格式独立于源单元格。
此示例创建一个 100% 堆积柱形图,包含红色和蓝色系列,跨越四个类别。每对值的和为 1。标签格式 0.0% 将 0.30 显示为 30.0%,而垂直轴使用两位小数。两个系列的标签文本均为白色、10 磅。
import aspose.slides as slides
import aspose.slides.charts as charts
import aspose.pydrawing as drawing
with slides.Presentation() as presentation:
slide = presentation.slides[0]
chart = slide.shapes.add_chart(charts.ChartType.PERCENTS_STACKED_COLUMN, 20, 20, 500, 400)
chart.axes.vertical_axis.is_number_format_linked_to_source = False
chart.axes.vertical_axis.number_format = "0.00%"
chart.chart_data.series.clear()
chart.chart_data.categories.clear()
workbook = chart.chart_data.chart_data_workbook
worksheet_index = 0
for i in range(4):
category_cell = workbook.get_cell(worksheet_index, i + 1, 0, f"Category {i + 1}")
chart.chart_data.categories.add(category_cell)
series_names = ["Reds", "Blues"]
series_colors = [drawing.Color.red, drawing.Color.blue]
values = [[0.30, 0.50, 0.80, 0.65], [0.70, 0.50, 0.20, 0.35]]
for i in range(len(series_names)):
series_cell = workbook.get_cell(worksheet_index, 0, i + 1, series_names[i])
series = chart.chart_data.series.add(series_cell, chart.type)
for j in range(4):
value_cell = workbook.get_cell(worksheet_index, j + 1, i + 1, values[i][j])
series.data_points.add_data_point_for_bar_series(value_cell)
series.format.fill.fill_type = slides.FillType.SOLID
series.format.fill.solid_fill_color.color = series_colors[i]
label_format = series.labels.default_data_label_format
label_format.show_value = True
label_format.is_number_format_linked_to_source = False
label_format.number_format = "0.0%"
label_format.text_format.portion_format.font_height = 10
label_format.text_format.portion_format.fill_format.fill_type = slides.FillType.SOLID
label_format.text_format.portion_format.fill_format.solid_fill_color.color = drawing.Color.white
presentation.save("SetDataLabelsPercentageSign_out.pptx", slides.export.SaveFormat.PPTX)
读取数据标签的实际文本
使用 get_actual_label_text 来获取数据标签设置生成的文本。此功能在提取报告标签、搜索演示文稿内容或验证生成的图表时非常有用。在下面的示例中,默认的 data label format 将每个类别名称、系列名称和数值组合在一起。一个数据点将其值格式化为百分比,另一个则使用来自 text_frame_for_overriding 的自定义文本。
import aspose.slides as slides
import aspose.slides.charts as charts
with slides.Presentation() as presentation:
slide = presentation.slides[0]
chart = slide.shapes.add_chart(charts.ChartType.CLUSTERED_COLUMN, 20, 20, 500, 300)
chart.chart_data.series.clear()
chart.chart_data.categories.clear()
workbook = chart.chart_data.chart_data_workbook
for i, category_name in enumerate(["Q1", "Q2"]):
category_cell = workbook.get_cell(0, i + 1, 0, category_name)
chart.chart_data.categories.add(category_cell)
north_cell = workbook.get_cell(0, 0, 1, "North")
north = chart.chart_data.series.add(north_cell, chart.type)
for i, value in enumerate([0.25, 0.75]):
value_cell = workbook.get_cell(0, i + 1, 1, value)
north.data_points.add_data_point_for_bar_series(value_cell)
south_cell = workbook.get_cell(0, 0, 2, "South")
south = chart.chart_data.series.add(south_cell, chart.type)
for i, value in enumerate([0.40, 0.60]):
value_cell = workbook.get_cell(0, i + 1, 2, value)
south.data_points.add_data_point_for_bar_series(value_cell)
for series in chart.chart_data.series:
label_format = series.labels.default_data_label_format
label_format.show_category_name = True
label_format.show_series_name = True
label_format.show_value = True
north.labels[1].data_label_format.is_number_format_linked_to_source = False
north.labels[1].data_label_format.number_format = "0%"
south.labels[0].text_frame_for_overriding.text = "Reviewed"
for series in chart.chart_data.series:
for point in series.data_points:
label = point.label
if not label.is_visible:
continue
label_text = label.get_actual_label_text()
print(f"Value: {point.value.data}; label: {label_text}")
数据点中存储的数值仍为 0.75,即使其标签显示为 75% 并附带类别和系列名称。自定义文本会替代生成的标签文本。 get_actual_label_text 在两种情况下都返回生成的标签字符串。若只想提取可见标签,请单独检查 is_visible ,如上所示。
控制超出轴最大值的数据标签
当手动限制坐标轴范围时,某些数据点可能会超出其最大值。使用 show_data_labels_over_maximum 来控制是否显示这些数据标签。此设置只改变标签的可见性,不会更改坐标轴范围或底层数据值。
下面的示例创建一个值为 60 和 120 的 2D 集合柱形图。它将垂直轴的 is_automatic_max_value 设置为 False,并将 max_value 设置为 100。第一张幻灯片允许标签超出最大值;其副本则禁用该功能。两张幻灯片均保存为 DataLabelsOverMaximum.pptx。
使用 show_value 启用数值标签。图表级别的设置本身并不会启用数值显示,也不会覆盖单个标签已禁用的数值显示。此示例为整个系列启用数值,并使用 position 将标签放置在每根柱子的外端。
import aspose.slides as slides
import aspose.slides.charts as charts
with slides.Presentation() as presentation:
slide = presentation.slides[0]
chart = slide.shapes.add_chart(charts.ChartType.CLUSTERED_COLUMN, 50, 50, 600, 400)
chart.has_legend = False
chart.chart_data.series.clear()
chart.chart_data.categories.clear()
workbook = chart.chart_data.chart_data_workbook
first_category = workbook.get_cell(0, 1, 0, "Within range")
second_category = workbook.get_cell(0, 2, 0, "Above maximum")
chart.chart_data.categories.add(first_category)
chart.chart_data.categories.add(second_category)
series_name = workbook.get_cell(0, 0, 1, "Values")
series = chart.chart_data.series.add(series_name, chart.type)
first_value = workbook.get_cell(0, 1, 1, 60)
second_value = workbook.get_cell(0, 2, 1, 120)
series.data_points.add_data_point_for_bar_series(first_value)
series.data_points.add_data_point_for_bar_series(second_value)
series.labels.default_data_label_format.show_value = True
series.labels.default_data_label_format.position = charts.LegendDataLabelPosition.OUTSIDE_END
chart.axes.vertical_axis.is_automatic_max_value = False
chart.axes.vertical_axis.max_value = 100
chart.show_data_labels_over_maximum = True
second_slide = presentation.slides.add_clone(slide)
second_chart = second_slide.shapes[0]
second_chart.show_data_labels_over_maximum = False
presentation.save("DataLabelsOverMaximum.pptx", slides.export.SaveFormat.PPTX)
下图显示了 Microsoft PowerPoint 渲染的已保存幻灯片。True 时,标签 120 在上边界可见;False 时,该标签被隐藏。标签 60 保持可见,轴最大值仍为 100,第二个数据点在两种情况下均为 120。
| show_data_labels_over_maximum = True | show_data_labels_over_maximum = False |
|---|---|
![]() |
![]() |
Chart Type
此示例使用带值轴的 2D 柱形图。没有值轴的图表,如饼图和环形图,无法以此方式设置轴最大值限制。设置标签与坐标轴的距离
使用 label_offset 来控制类别轴标签与坐标轴之间的距离。该值是轴标签最大字体大小的百分比。此示例创建一个集合柱形图,并将水平轴标签偏移设置为 500。此设置影响类别轴标签,而不是附加到单个数据点的标签。
import aspose.slides as slides
import aspose.slides.charts as charts
with slides.Presentation() as presentation:
slide = presentation.slides[0]
chart = slide.shapes.add_chart(charts.ChartType.CLUSTERED_COLUMN, 20, 20, 500, 300)
chart.axes.horizontal_axis.label_offset = 500
presentation.save("SetCategoryAxisLabelDistance_out.pptx", slides.export.SaveFormat.PPTX)
调整标签位置
在饼图中,调整数据标签位置以优化间距并为引导线留出空间。
此示例显示第一个数据点的数值,将其标签放置在扇形外部,并调整其 x 和 y 偏移。这些偏移分别相对于图表的宽度和高度。
import aspose.slides as slides
import aspose.slides.charts as charts
with slides.Presentation() as presentation:
slide = presentation.slides[0]
chart = slide.shapes.add_chart(charts.ChartType.PIE, 50, 50, 200, 200)
series = chart.chart_data.series
label = series[0].labels[0]
label.data_label_format.show_value = True
label.data_label_format.position = charts.LegendDataLabelPosition.OUTSIDE_END
label.x = 0.71
label.y = 0.04
presentation.save("presentation.pptx", slides.export.SaveFormat.PPTX)

格式化引导线
当饼图标签位于扇形外部时,引导线帮助读者将每个数值与正确的扇形对应。使用对比色和稍粗的线条可以使连接更易辨认,而透明度和虚线样式则使线条在视觉上不显突兀。
使用 leader_lines_format 在系列的数据标签集合层面格式化引导线。series.labels.leader_lines_format.line 中的设置作用于整个集合,而非单个标签。
此示例创建一个默认数据的饼图,在扇形外显示数值,并启用 show_leader_lines。它使用 x 和 y 偏移将两个标签从扇形进一步移开,偏移以图表宽高的比例表示。无需输入演示文稿。
该线条的 fill_format 设置为实心蓝色,透明度为 191/255(约 25% 透明)。width 将粗细设为 2 磅,且 dash_style 将线条设为虚线。
import aspose.slides as slides
import aspose.slides.charts as charts
import aspose.pydrawing as drawing
with slides.Presentation() as presentation:
slide = presentation.slides[0]
chart = slide.shapes.add_chart(charts.ChartType.PIE, 80, 50, 560, 400)
chart.has_legend = False
series = chart.chart_data.series[0]
label_format = series.labels.default_data_label_format
label_format.show_value = True
label_format.position = charts.LegendDataLabelPosition.OUTSIDE_END
label_format.show_leader_lines = True
# 将两个标签远离它们的切片
series.labels[0].x = 0.10
series.labels[0].y = -0.05
series.labels[1].x = -0.10
series.labels[1].y = 0.05
line = series.labels.leader_lines_format.line
line.fill_format.fill_type = slides.FillType.SOLID
line.fill_format.solid_fill_color.color = drawing.Color.from_argb(191, 30, 90, 160)
line.width = 2
line.dash_style = slides.LineDashStyle.DASH
presentation.save("FormattedLeaderLines.pptx", slides.export.SaveFormat.PPTX)
启用引导线并不保证每个标签都会出现连线。可见性取决于标签位置和图表的布局行为。在本例中,两个偏离的标签拥有可见的引导线;靠近扇形的标签则不需要。
下图显示了使用 Aspose.Slides 渲染的已保存 FormattedLeaderLines.pptx:

在柱形图上方添加多行数据标签
此示例创建一个柱形图,在绘图区域上方放置两行数据标签。Series A 显示可见的柱子,Series B 和 Series C 提供额外的标签。通过移除填充和轮廓将它们的柱子隐藏。 overlap 属性使三个系列的类别中心对齐。
plot_area 设置为标签行预留空间。执行 validate_chart_layout 计算默认位置后,使用 x 和 y 保持水平对齐并应用垂直偏移,将标签排列成两行。这些数字仍是与系列数值关联的数据标签;只有行标题是独立的文本形状。
import aspose.slides as slides
import aspose.slides.charts as charts
import aspose.pydrawing as drawing
with slides.Presentation() as presentation:
slide = presentation.slides[0]
chart = slide.shapes.add_chart(charts.ChartType.CLUSTERED_COLUMN, 40, 40, 640, 200)
chart.has_title = False
chart.has_legend = False
chart.text_format.portion_format.font_height = 12
chart.chart_data.series.clear()
chart.chart_data.categories.clear()
workbook = chart.chart_data.chart_data_workbook
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_name in enumerate(categories):
category_cell = workbook.get_cell(0, category_index + 1, 0, category_name)
chart.chart_data.categories.add(category_cell)
for series_index, series_name in enumerate(series_names):
series_cell = workbook.get_cell(0, 0, series_index + 1, series_name)
series = chart.chart_data.series.add(series_cell, charts.ChartType.CLUSTERED_COLUMN)
for category_index in range(len(categories)):
value_cell = workbook.get_cell(0, category_index + 1, series_index + 1, series_values[series_index][category_index])
series.data_points.add_data_point_for_bar_series(value_cell)
if series_index > 0:
# 隐藏 B 列和 C 列的柱子,但保留它们的数据标签。
series.format.fill.fill_type = slides.FillType.NO_FILL
series.format.line.fill_format.fill_type = slides.FillType.NO_FILL
label_format = series.labels.default_data_label_format
label_format.show_value = True
label_format.text_format.portion_format.font_height = 12
label_fill = label_format.text_format.portion_format.fill_format
label_fill.fill_type = slides.FillType.SOLID
label_fill.solid_fill_color.color = drawing.Color.black
label_format.position = charts.LegendDataLabelPosition.INSIDE_BASE
# 使所有三个系列的类别中心对齐。
chart.chart_data.series[0].parent_series_group.overlap = 100
# 在此紧凑示例中使用更少的网格线。
chart.axes.vertical_axis.is_automatic_major_unit = False
chart.axes.vertical_axis.major_unit = 10
# 在绘图区上方预留空间用于两行数据标签。
chart.plot_area.layout_target_type = charts.LayoutTargetType.INNER
chart.plot_area.x = 0.15
chart.plot_area.y = 0.32
chart.plot_area.width = 0.80
chart.plot_area.height = 0.48
chart.validate_chart_layout()
for series_index in range(1, len(series_names)):
series = chart.chart_data.series[series_index]
row_top = 0.15 if series_index == 1 else 0.03
for category_index in range(len(categories)):
data_label = series.data_points[category_index].label
# 保持默认的水平位置。Y 是相对于
# 默认标签位置的偏移,以图表高度的比例表示。
data_label.x = 0
data_label.y = row_top - data_label.actual_y / chart.height
# 只有行标题是单独的文本形状。
row_heading = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, chart.x, chart.y + row_top * chart.height, 85, 18)
row_heading.fill_format.fill_type = slides.FillType.NO_FILL
row_heading.line_format.fill_format.fill_type = slides.FillType.NO_FILL
row_heading.add_text_frame(series_names[series_index])
row_heading.text_frame.text_frame_format.margin_top = 0
row_heading.text_frame.text_frame_format.margin_bottom = 0
heading_format = row_heading.text_frame.paragraphs[0].portions[0].portion_format
heading_format.font_height = 12
heading_format.fill_format.fill_type = slides.FillType.SOLID
heading_format.fill_format.solid_fill_color.color = drawing.Color.black
presentation.save("multiple-rows-of-labels.pptx", slides.export.SaveFormat.PPTX)

常见问题
如何防止数据标签在密集图表中重叠?
结合自动标签布局、引导线和减小字体大小;必要时可隐藏某些字段(例如类别),或仅对极值或关键点显示标签。
如何仅对零、负数或空值禁用标签?
在启用标签之前过滤数据点,并根据定义的规则关闭对 0、负数或缺失值的显示。
在导出为 PDF/图片时,如何确保标签样式一致?
显式设置字体族和大小,并确认渲染环境中已安装该字体,以避免回退。

