Skip to content

Commit c98f448

Browse files
SLIDESDOC-658 Update articles to improve time to read (batch 6) (#811)
Updated 9 articles.
1 parent 66b3b62 commit c98f448

File tree

14 files changed

+777
-794
lines changed

14 files changed

+777
-794
lines changed

en/python-net/developer-guide/presentation-content/powerpoint-charts/chart-entities/chart-data-label/_index.md

Lines changed: 109 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -18,108 +18,120 @@ keywords:
1818
description: "Learn to add and format chart data labels in PowerPoint and OpenDocument presentations using Aspose.Slides for Python via .NET for more engaging slides."
1919
---
2020

21-
Data labels on a chart show details about the chart data series or individual data points. They allow readers to quickly identify data series and they also make charts easier to understand.
21+
## **Overview**
2222

23-
## **Set Precision of Data in Chart Data Labels**
23+
Data labels on a chart show details about the chart data series or individual data points. They allow readers to quickly identify data series and they also make charts easier to understand. In Aspose.Slides for Python, you can enable, customize, and format data labels for any chart—choosing what to display (values, percentages, series or category names), where to position labels, and how they look (font, number format, separators, leader lines, and more). This article outlines the essential APIs and examples you need to add clear, informative labels to your charts.
2424

25-
This Python code shows you how to set the data precision in a chart data label:
25+
## **Set Data Label Precision**
26+
27+
Chart data labels often display numeric values that require consistent precision. This section shows how to control the number of decimal places for data labels in Aspose.Slides by applying an appropriate number format.
28+
29+
The following Python example shows how to set the numeric precision for chart data labels:
2630

2731
```py
28-
import aspose.slides.charts as charts
2932
import aspose.slides as slides
33+
import aspose.slides.charts as charts
34+
35+
with slides.Presentation() as presentation:
36+
slide = presentation.slides[0]
37+
38+
chart = slide.shapes.add_chart(charts.ChartType.LINE, 50, 50, 500, 300)
3039

31-
with slides.Presentation() as pres:
32-
chart = pres.slides[0].shapes.add_chart(charts.ChartType.LINE, 50, 50, 450, 300)
33-
chart.has_data_table = True
34-
chart.chart_data.series[0].number_format_of_values = "#,##0.00"
40+
series = chart.chart_data.series[0]
41+
series.labels.default_data_label_format.show_value = True
42+
series.number_format_of_values = "#,##0.00"
3543

36-
pres.save("PrecisionOfDatalabels_out.pptx", slides.export.SaveFormat.PPTX)
44+
presentation.save("data_label_precision.pptx", slides.export.SaveFormat.PPTX)
3745
```
3846

39-
## **Display Percentage as Labels**
40-
Aspose.Slides for Python via .NET allows you to set percentage labels on displayed charts. This Python code demonstrates the operation:
47+
## **Display Percentages as Labels**
48+
49+
With Aspose.Slides, you can display percentages as data labels on charts. The example below calculates each point’s share within its category and formats the label to show the percentage.
4150

4251
```py
43-
import aspose.slides.charts as charts
4452
import aspose.slides as slides
53+
import aspose.slides.charts as charts
4554

46-
# Creates an instance of the Presentation class
55+
# Create an instance of the Presentation class.
4756
with slides.Presentation() as presentation:
4857
slide = presentation.slides[0]
49-
chart = slide.shapes.add_chart(charts.ChartType.STACKED_COLUMN, 20, 20, 400, 400)
58+
59+
chart = slide.shapes.add_chart(charts.ChartType.STACKED_COLUMN, 20, 20, 600, 400)
5060
series = chart.chart_data.series[0]
51-
total_for_Cat = [0]*len(chart.chart_data.categories)
61+
62+
total_for_categories = [0]*len(chart.chart_data.categories)
5263
for k in range(len(chart.chart_data.categories)):
53-
cat = chart.chart_data.categories[k]
5464
for i in range(len(chart.chart_data.series)):
55-
total_for_Cat[k] += chart.chart_data.series[i].data_points[k].value.data
65+
total_for_categories[k] += chart.chart_data.series[i].data_points[k].value.data
66+
67+
for i in range(len(chart.chart_data.series)):
68+
series = chart.chart_data.series[i]
69+
series.labels.default_data_label_format.show_legend_key = False
5670

57-
dataPontPercent = 0
71+
for j in range(len(series.data_points)):
72+
data_point_percent = series.data_points[j].value.data / total_for_categories[j] * 100
5873

59-
for x in range(len(chart.chart_data.series)):
60-
series = chart.chart_data.series[x]
61-
series.labels.default_data_label_format.show_legend_key = False
74+
text_portion = slides.Portion()
75+
text_portion.text = "{0:.2f} %".format(data_point_percent)
76+
text_portion.portion_format.font_height = 8
6277

63-
for j in range(len(series.data_points)):
64-
lbl = series.data_points[j].label
65-
dataPontPercent = series.data_points[j].value.data / total_for_Cat[j] * 100
78+
label = series.data_points[j].label
79+
label.text_frame_for_overriding.text = ""
6680

67-
port = slides.Portion()
68-
port.text = "{0:.2f} %".format(dataPontPercent)
69-
port.portion_format.font_height = 8
70-
lbl.text_frame_for_overriding.text = ""
71-
para = lbl.text_frame_for_overriding.paragraphs[0]
72-
para.portions.add(port)
81+
paragraph = label.text_frame_for_overriding.paragraphs[0]
82+
paragraph.portions.add(text_portion)
7383

74-
lbl.data_label_format.show_series_name = False
75-
lbl.data_label_format.show_percentage = False
76-
lbl.data_label_format.show_legend_key = False
77-
lbl.data_label_format.show_category_name = False
78-
lbl.data_label_format.show_bubble_size = False
84+
label.data_label_format.show_series_name = False
85+
label.data_label_format.show_percentage = False
86+
label.data_label_format.show_legend_key = False
87+
label.data_label_format.show_category_name = False
88+
label.data_label_format.show_bubble_size = False
7989

80-
# Saves the presentation containing the chart
81-
presentation.save("DisplayPercentageAsLabels_out.pptx", slides.export.SaveFormat.PPTX)
90+
# Save the presentation containing the chart.
91+
presentation.save("percentage_as_label.pptx", slides.export.SaveFormat.PPTX)
8292
```
8393

84-
## **Set Percentage Sign with Chart Data Labels**
85-
This Python code shows you to set the percentage sign for a chart data label:
94+
## **Show Percent Signs with Chart Data Labels**
95+
96+
This section shows how to display percentages in chart data labels and include the percent sign using Aspose.Slides. You’ll learn how to enable percentage values for entire series or specific points (ideal for pie, doughnut, and 100% stacked charts) and how to control formatting through label options or a custom number format.
97+
98+
The following Python example shows how to add a percent sign to a chart’s data label:
8699

87100
```py
88-
import aspose.slides.charts as charts
89101
import aspose.slides as slides
102+
import aspose.slides.charts as charts
90103
import aspose.pydrawing as draw
91104

92-
# Creates an instance of Presentation class
105+
# Create an instance of the Presentation class.
93106
with slides.Presentation() as presentation:
94107

95-
# Gets a slide's reference through its index
108+
# Get a slide reference by index.
96109
slide = presentation.slides[0]
97110

98-
# Creates the PercentsStackedColumn chart on a slide
99-
chart = slide.shapes.add_chart(charts.ChartType.PERCENTS_STACKED_COLUMN, 20, 20, 500, 400)
111+
# Create a PercentsStackedColumn chart on the slide.
112+
chart = slide.shapes.add_chart(charts.ChartType.PERCENTS_STACKED_COLUMN, 20, 20, 600, 400)
100113

101-
# Sets the NumberFormatLinkedToSource to false
102114
chart.axes.vertical_axis.is_number_format_linked_to_source = False
103115
chart.axes.vertical_axis.number_format = "0.00%"
104116

105117
chart.chart_data.series.clear()
106-
defaultWorksheetIndex = 0
107118

108-
# Gets the chart data worksheet
119+
# Get the chart data workbook.
109120
workbook = chart.chart_data.chart_data_workbook
121+
worksheet_index = 0
110122

111-
# Adds new series
112-
series = chart.chart_data.series.add(workbook.get_cell(defaultWorksheetIndex, 0, 1, "Reds"), chart.type)
113-
series.data_points.add_data_point_for_bar_series(workbook.get_cell(defaultWorksheetIndex, 1, 1, 0.30))
114-
series.data_points.add_data_point_for_bar_series(workbook.get_cell(defaultWorksheetIndex, 2, 1, 0.50))
115-
series.data_points.add_data_point_for_bar_series(workbook.get_cell(defaultWorksheetIndex, 3, 1, 0.80))
116-
series.data_points.add_data_point_for_bar_series(workbook.get_cell(defaultWorksheetIndex, 4, 1, 0.65))
123+
# Add a new series.
124+
series = chart.chart_data.series.add(workbook.get_cell(worksheet_index, 0, 1, "Reds"), chart.type)
125+
series.data_points.add_data_point_for_bar_series(workbook.get_cell(worksheet_index, 1, 1, 0.30))
126+
series.data_points.add_data_point_for_bar_series(workbook.get_cell(worksheet_index, 2, 1, 0.50))
127+
series.data_points.add_data_point_for_bar_series(workbook.get_cell(worksheet_index, 3, 1, 0.80))
128+
series.data_points.add_data_point_for_bar_series(workbook.get_cell(worksheet_index, 4, 1, 0.65))
117129

118-
# Sets the fill color of series
130+
# Set the series fill color.
119131
series.format.fill.fill_type = slides.FillType.SOLID
120132
series.format.fill.solid_fill_color.color = draw.Color.red
121133

122-
# Sets the LabelFormat properties
134+
# Set label format properties.
123135
series.labels.default_data_label_format.show_value = True
124136
series.labels.default_data_label_format.is_number_format_linked_to_source = False
125137
series.labels.default_data_label_format.number_format = "0.0%"
@@ -128,14 +140,14 @@ with slides.Presentation() as presentation:
128140
series.labels.default_data_label_format.text_format.portion_format.fill_format.solid_fill_color.color = draw.Color.white
129141
series.labels.default_data_label_format.show_value = True
130142

131-
# Adds new series
132-
series2 = chart.chart_data.series.add(workbook.get_cell(defaultWorksheetIndex, 0, 2, "Blues"), chart.type)
133-
series2.data_points.add_data_point_for_bar_series(workbook.get_cell(defaultWorksheetIndex, 1, 2, 0.70))
134-
series2.data_points.add_data_point_for_bar_series(workbook.get_cell(defaultWorksheetIndex, 2, 2, 0.50))
135-
series2.data_points.add_data_point_for_bar_series(workbook.get_cell(defaultWorksheetIndex, 3, 2, 0.20))
136-
series2.data_points.add_data_point_for_bar_series(workbook.get_cell(defaultWorksheetIndex, 4, 2, 0.35))
143+
# Add a new series.
144+
series2 = chart.chart_data.series.add(workbook.get_cell(worksheet_index, 0, 2, "Blues"), chart.type)
145+
series2.data_points.add_data_point_for_bar_series(workbook.get_cell(worksheet_index, 1, 2, 0.70))
146+
series2.data_points.add_data_point_for_bar_series(workbook.get_cell(worksheet_index, 2, 2, 0.50))
147+
series2.data_points.add_data_point_for_bar_series(workbook.get_cell(worksheet_index, 3, 2, 0.20))
148+
series2.data_points.add_data_point_for_bar_series(workbook.get_cell(worksheet_index, 4, 2, 0.35))
137149

138-
# Sets Fill type and color
150+
# Set the fill type and color.
139151
series2.format.fill.fill_type = slides.FillType.SOLID
140152
series2.format.fill.solid_fill_color.color = draw.Color.blue
141153
series2.labels.default_data_label_format.show_value = True
@@ -145,54 +157,61 @@ with slides.Presentation() as presentation:
145157
series2.labels.default_data_label_format.text_format.portion_format.fill_format.fill_type = slides.FillType.SOLID
146158
series2.labels.default_data_label_format.text_format.portion_format.fill_format.solid_fill_color.color = draw.Color.white
147159

148-
# Writes the presentation to disk
149-
presentation.save("SetDatalabelsPercentageSign_out.pptx", slides.export.SaveFormat.PPTX)
160+
# Save the presentation.
161+
presentation.save("percentage_sign.pptx", slides.export.SaveFormat.PPTX)
150162
```
151163

152-
## **Set Label Distance From Axis**
153-
This Python code shows you how to set the label distance from a category axis when you are dealing with a chart plotted from axes:
164+
## **Set Label Distance from Axis**
165+
166+
This section shows how to control the distance between data labels and the chart axis in Aspose.Slides. Adjusting this offset helps prevent overlaps and improves readability in dense visuals.
167+
168+
The following Python code shows how to set the label distance from the category axis when working with an axes-based chart:
154169

155170
```py
156-
import aspose.slides.charts as charts
157171
import aspose.slides as slides
172+
import aspose.slides.charts as charts
158173

159-
# Creates an instance of the Presentation class
174+
# Create an instance of the Presentation class.
160175
with slides.Presentation() as presentation:
161-
# Gets a slide's reference
162-
sld = presentation.slides[0]
163-
164-
# Creates a chart on the slide
165-
ch = sld.shapes.add_chart(charts.ChartType.CLUSTERED_COLUMN, 20, 20, 500, 300)
176+
# Get a slide reference.
177+
slide = presentation.slides[0]
178+
179+
# Create a clustered column chart on the slide.
180+
chart = slide.shapes.add_chart(charts.ChartType.CLUSTERED_COLUMN, 20, 20, 500, 300)
166181

167-
# Sets the label distance from an axis
168-
ch.axes.horizontal_axis.label_offset = 500
182+
# Set the label distance from the category (horizontal) axis.
183+
chart.axes.horizontal_axis.label_offset = 500
169184

170-
# Writes the presentation to disk
171-
presentation.save("SetCategoryAxisLabelDistance_out.pptx", slides.export.SaveFormat.PPTX)
185+
# Save the presentation.
186+
presentation.save("axis_label_distance.pptx", slides.export.SaveFormat.PPTX)
172187
```
173188

174-
## **Adjust Label Location**
189+
## **Adjust Label Position**
175190

176-
When you create a chart that does not rely on any axis such as a pie chart, the chart's data labels may end up being too close to its edge. In such a case, you have to adjust the location of the data label so that the leader lines get displayed clearly.
191+
When you create a chart that does not use axes, such as a pie chart, the data labels may be too close to the edge. In that case, adjust the label position so leader lines display clearly.
177192

178-
This Python code shows you how to adjust the label location on a pie chart:
193+
The following Python code shows how to adjust the label position on a pie chart:
179194

180195
```python
181196
import aspose.slides as slides
197+
import aspose.slides.charts as charts
198+
199+
with slides.Presentation() as presentation:
200+
slide = presentation.slides[0]
182201

202+
chart = slide.shapes.add_chart(charts.ChartType.PIE, 50, 50, 600, 300)
183203

184-
with slides.Presentation() as pres:
185-
chart = pres.slides[0].shapes.add_chart(slides.charts.ChartType.PIE, 50, 50, 200, 200)
204+
series = chart.chart_data.series[0]
205+
series.labels.default_data_label_format.show_value = True
206+
series.labels.default_data_label_format.show_leader_lines = True
186207

187-
series = chart.chart_data.series
188-
label = series[0].labels[0]
208+
label = series.labels[0]
209+
label.data_label_format.position = charts.LegendDataLabelPosition.OUTSIDE_END
189210

190-
label.data_label_format.show_value = True
191-
label.data_label_format.position = slides.charts.LegendDataLabelPosition.OUTSIDE_END
192-
label.x = 0.71
193-
label.y = 0.04
211+
label.x = 0.05
212+
label.y = 0.1
194213

195-
pres.save("pres.pptx", slides.export.SaveFormat.PPTX)
214+
presentation.save("presentation.pptx", slides.export.SaveFormat.PPTX)
196215
```
197216

198-
![pie-chart-adjusted-label](pie-chart-adjusted-label.png)
217+
![Changed label position](changed_label_position.png)
24.4 KB
Loading
-19.1 KB
Binary file not shown.

0 commit comments

Comments
 (0)