highcharts-for-python / highcharts-core

Python wrapper for the Highcharts Core JavaScript library
https://www.highcharts.com/integrations/python
Other
58 stars 13 forks source link

BUG: Silently ignoring distance property for data Labels in pie charts #209

Open secastro opened 1 week ago

secastro commented 1 week ago

Describe the bug While trying to reproduce the example in https://www.highcharts.com/demo/highcharts/pie-chart using Python Code, setting the distance property in dataLabels is ignored.

To Reproduce This is a minimal piece of Python code to reproduce:

from highcharts_core.chart import Chart

chart0 = Chart.from_options(
    {'title': {'text': 'Pie chart'},
     'chart': {'type': 'pie'},
     'plotOptions': {'pie': {'dataLabels': {'enabled': True}},
                     'series': {
                         'dataLabels': [{'enabled': True, 'distance': 20},
                                        {'enabled': True, 'distance': -40, 'format': '{point.percentage:.1f}%' }]
                     }
                     },
     'series': [{'name': 'Random Data', 'colorByPoint': True, 'data': [{'name': 'X', 'y': 10000},
                                                                       {'name': 'Y', 'y': 20000},
                                                                       {'name': 'Z', 'y': 40000}] }]}
)

print([_ for _ in chart0.options.plot_options.series.data_labels])

[DataLabel(enabled = True),
 DataLabel(enabled = True, format = '{point.percentage:.1f}%')]

Also the exported chart in JSON format doesn't contain the 'distance' values

Expected behavior Ideally, the 'distance' property is preserved in the chart, and included when exported. Otherwise, a warning about the property being excluded would be useful.

Your Environment:

hcpchris commented 1 week ago

Sorry you're running into this issue, @secastro ! This looks to be a bug that we should have addressed in the next release (hopefully later today).

hcpchris commented 1 week ago

So after taking a closer look at this, it's a bit of an interesting case. What's happening here is two things:

  1. Because you are setting the data_labels property at the series level, Highcharts for Python is converting that into a generic DataLabel instance (because the plot_options.series property does not know that you are applying the data label to a Pie Series).
  2. The generic DataLabel class does not support the chart-type specific configuration distance property, which is specific to the series type-specific PieDataLabel type.

This leaves us with two options for how to proceed:

  1. With the current version of Highcharts Core for Python, you can simply move that data label configuration out of plot_options.series and into plot_options.pie. Placing it there will yield the result you want, because then Highcharts for Python knows that you are attempting to use the PieDataLabel type.
  2. I have to give some thought for whether and how to dynamically determine if plot_options.series.data_labels should coerce to a series-specific data label type. Highcharts for Python supports many different series type-specific data label structures, so it's a bit of a non-trivial question as to how to handle this use case in Highcharts for Python. Highcharts JS is a little more flexible here by nature of JavaScript, but the same effect can be achieved in Python. I just need to consider what the optimum implementation will look like.