scanny / python-pptx

Create Open XML PowerPoint documents in Python
MIT License
2.36k stars 511 forks source link

Doughnut chart not displaying data labels #347

Open talhasch opened 6 years ago

talhasch commented 6 years ago

"chart.plots[0].has_data_labels = True" does not activate data labels for doughnut charts.

jmm commented 6 years ago

@talhasch, thanks for reporting this! Can you please copy the text of your second post into the original post and delete the second one? You can also put your code snippet in between a pair of triple backticks. I'd suggest the title "Setting _BasePlot.has_data_labels = True doesn't work for doughnut".

Same here, with python-pptx@0.6.10. Here is a repro.

chart_data = ChartData()
chart_data.categories = ['Yes', 'No']
chart_data.add_series('Series 1', (42, 24))

for chart_type in [XL_CHART_TYPE.PIE, XL_CHART_TYPE.DOUGHNUT]:
    for has_data_labels in [True, False]:
        slide = presentation.slides.add_slide(presentation.slide_layouts[1])
        placeholder = slide.placeholders[13]
        chart = placeholder.insert_chart(chart_type, chart_data).chart

        for plot in chart.plots:
            plot.has_data_labels = has_data_labels
mohit879 commented 4 years ago

Hey @jmm @scanny , is there any fix of this issue yet ? I am still not able to add data labels in doughnut chart .

sbthunder commented 4 years ago

def apply_data_labels(chart): plot = chart.plots[0] plot.has_data_labels = True for series in plot.series: values = series.values counter = 0 for point in series.points: data_label = point.data_label data_label.has_text_frame = True data_label.text_frame.text = str(values[counter]) counter = counter + 1

This works @mohit879 , I am sorry there is problem with indentation.

mohit879 commented 4 years ago

Cool, thanks @sbthunder !

himanshu-yaduvanshi commented 2 years ago
d_plot = chart.plots[0]
d_plot.has_data_labels = True
for series in d_plot.series:
    values = series.values
    counter = 0
    for point in series.points:
        ddl = point.data_label
        ddl.has_text_frame = True
        ddl.text_frame.text = str("{:.0%}".format(values[counter]))
        ddl.text_frame.paragraphs[0].font.name = "calibri"
        ddl.text_frame.paragraphs[0].font.size = Pt(9)
        ddl.font.color.rgb = RGBColor(255, 0, 0)
        counter = counter + 1`

@scanny - I have formatted the code. Is there any way to set the font color of the data labels of the doughnut chart? I am using the above code but it's not working.

scanny commented 2 years ago

@Himanshu-CEB Google is your friend for figuring out how to format code on GitHub. I can only speak for myself, but I won't even try to read code that's not formatted.

himanshu-yaduvanshi commented 2 years ago

@scanny - can you help me with this now as I have formatted the code.

scanny commented 2 years ago

@Himanshu-CEB please open this as a question on StackOverflow using the "python-pptx" tab.

This is a support question and support Q&A is conducted there. Also, this is a distinct topic from this issue, please do not add new questions to an existing issue as it dilutes the focus and of the issue with side-tracks.