Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: DataLabels.show_category_name #242

Closed
ngnikhilgoyal opened this issue Nov 17, 2016 · 13 comments
Closed

feature: DataLabels.show_category_name #242

ngnikhilgoyal opened this issue Nov 17, 2016 · 13 comments
Labels

Comments

@ngnikhilgoyal
Copy link

When creating a pie chart, is it possible to add the category names as labels to the chart eliminating the need for the legend ?

chart.plots[0].has_data_labels = True
data_labels = chart.plots[0].data_labels
data_labels.number_format = '0%'

This puts the series values as labels. It would be nice if we can put the category values also.

@scanny
Copy link
Owner

scanny commented Nov 17, 2016

Can you do this with the PowerPoint UI?

@ngnikhilgoyal
Copy link
Author

ngnikhilgoyal commented Nov 18, 2016

Yep !

  1. Select a chart by clicking
  2. Click "Chart Design" in the menu
  3. Click "Add Chart Element"
  4. Click "Data Labels"
  5. Click "More Data Label Options . . ."
  6. On the right hand side, a menu opens called "Format Data Labels" which has a "Label Contains" option where you can check mark "Category Name" .

It makes the category name appear along side the series values in data labels.

Hope that helps. screen shot for reference: https://drive.google.com/file/d/0B_FxAHf_x3I4ZUc5RGNoV285LXM/view?usp=sharing

@scanny
Copy link
Owner

scanny commented Nov 18, 2016

Ah, ok, so I think what you're looking for is DataLabel.text_frame. That's already in place:
http://python-pptx.readthedocs.io/en/latest/api/chart.html#pptx.chart.datalabel.DataLabel

@scanny
Copy link
Owner

scanny commented Nov 18, 2016

Although, now that you mention it, there does seem to be a ShowCategoryName property on DataLabels in the MS API. So this is definitely a legitimate feature request.

@scanny scanny changed the title Feature: Category names as Data Labels feature: DataLabels.show_category_name Nov 18, 2016
@ngnikhilgoyal
Copy link
Author

ngnikhilgoyal commented Nov 18, 2016

Thanks. Is there an example of how I can use text_frame to label the pie with the categories?
I did this :

`chart.plots[0].has_data_labels = True
data_labels = chart.plots[0].data_labels
data_labels.number_format = '0%'

chart.plots[0].has_text_frame = True
text_frame = chart.plots[0].text_frame`

and got:
`---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
in ()
29
30 chart.plots[0].has_text_frame = True
---> 31 text_frame = chart.plots[0].text_frame
32
33 ## add a shape to the slide

AttributeError: 'PiePlot' object has no attribute 'text_frame'`

Without the text_frame piece it was working . . .

@scanny
Copy link
Owner

scanny commented Nov 18, 2016

I think you're looking for something like this:

data_labels = chart.plots[0].data_labels
for data_label in data_labels:
    data_label.text_frame.text = 'foobar'

@ngnikhilgoyal
Copy link
Author

ngnikhilgoyal commented Nov 18, 2016

Is 'foobar' a list of values corresponding to category names in this case?

Also I get an error:


TypeError Traceback (most recent call last)
in ()
25 chart.plots[0].has_data_labels = True
26 data_labels = chart.plots[0].data_labels
---> 27 for data_label in data_labels:
28 data_label.text_frame.text = 'foobar'
29 # data_labels.number_format = '0%'

TypeError: 'DataLabels' object is not iterable

@scanny
Copy link
Owner

scanny commented Nov 18, 2016

You might be on a prior version. I believe that was added in the latest release.

I think the categories are on the Plot object. You should refer to the API documentation for details.

@camlee
Copy link

camlee commented Dec 13, 2016

@ngnikhilgoyal: I think you're getting a TypeError because it should be something like this instead:

for series in chart.series:
     for point in series.points:
         point.data_label.text_frame.text = 'foobar'

From the docs, a "DataLabels" object is not a collection of "DataLabel" objects but rather properties for the data labels: http://python-pptx.readthedocs.io/en/latest/api/chart.html#datalabels-objects

@scanny: This feature would be useful for me too. Specifically, control over the "Label Contains" setting available in the PowerPoint UI. Changing the "Label Position" setting right below it is already supported through the DataLabels.position property (aka chart.plots[0].data_labels.position).

2016-12-13_12h16_26

I'd imaging adding a contains property to DataLabels makes sense. Unlike the position property which just uses an enum, you can select multiple values and there's the "Separator" text. So it'll have to be a new class or something.

@ygbr
Copy link

ygbr commented Jan 25, 2017

+1

Having the Label Contains mapped is a huge help specially when dealing with Pie Charts in order to show percentages together with category names.

It would be awesome to have something like:

chart.plots[0].data_labels.contains = [
    pptx.enum.chart.XL_LABEL_CONTAINS.SERIES_NAME,
    pptx.enum.chart.XL_LABEL_CONTAINS.CATEGORY_NAME,
    pptx.enum.chart.XL_LABEL_CONTAINS.VALUE,
    pptx.enum.chart.XL_LABEL_CONTAINS.PERCENTAGE,
    pptx.enum.chart.XL_LABEL_CONTAINS.SHOW_LEADER_LINES,
    pptx.enum.chart.XL_LABEL_CONTAINS.LEGEND_KEYS
]

chart.plots[0].data_labels.contains_separator = pptx.enum.chart.XL_LABEL_CONTAINS_SEPARATOR.NEW_LINE

@scanny scanny added the chart label Mar 21, 2017
@ruby1990
Copy link

Hi,

It would be great if I can use alignment on label.

ppttextalignment

@scanny
Copy link
Owner

scanny commented Nov 10, 2018

Added in release v0.6.16 circa November 9, 2018.

No access to bubble-size or leader-lines, but the other five are in there as e.g. DataLabels.show_percentage (read/write boolean).

@scanny scanny closed this as completed Nov 10, 2018
@ChithambaramB
Copy link

Hi Scanny,
First of all, I would like to thank you for the wonderful effort you have made. You never know how much amount of work this has saved in our organization using this library. Such a phenomenon effort, Scanny.

I could see someone has looking for the below input in the library, and it would be very appreciate if you could include this in your next update. I will be grateful for you all the time. Thanks again for your excellent effort, I'm out of words to express. Thanks.
chart.plots[0].data_labels.contains = [
pptx.enum.chart.XL_LABEL_CONTAINS.SERIES_NAME,
pptx.enum.chart.XL_LABEL_CONTAINS.CATEGORY_NAME,
pptx.enum.chart.XL_LABEL_CONTAINS.VALUE,
pptx.enum.chart.XL_LABEL_CONTAINS.PERCENTAGE,
pptx.enum.chart.XL_LABEL_CONTAINS.SHOW_LEADER_LINES,
pptx.enum.chart.XL_LABEL_CONTAINS.LEGEND_KEYS
]

chart.plots[0].data_labels.contains_separator = pptx.enum.chart.XL_LABEL_CONTAINS_SEPARATOR.NEW_LINE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants