This is a migrated thread and some comments may be shown as answers.

pieChart.getLegendInfos() returns LegendItems with null and 0 values

3 Answers 48 Views
Chart
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Raga
Top achievements
Rank 1
Raga asked on 01 Feb 2016, 07:06 PM
When I call the getLegendInfos() method on a pie chart, it returns LegendItems with title = null, fillColor = 0, and strokeColor = 0. I added the following code to the Pie Series example:

ObservableCollection<LegendItem> origLegendItems = pieChart.getLegendInfos();

for (int i=0; i<origLegendItems.size(); i++) {
   LegendItem origItem = origLegendItems.get(i);
 
   Log.d("TEST", "item "+i+" title = " + origItem.getTitle());
   Log.d("TEST", "item "+i+" fillColor = " + origItem.getFillColor());
   Log.d("TEST", "item "+i+" strokeColor = " + origItem.getStrokeColor());
}

The resulting log: 
02-01 13:49:49.797 22465-22465/com.telerik.examples D/TEST: item 0 title = null
02-01 13:49:49.797 22465-22465/com.telerik.examples D/TEST: item 0 fillColor = 0
02-01 13:49:49.797 22465-22465/com.telerik.examples D/TEST: item 0 strokeColor = 0
02-01 13:49:49.797 22465-22465/com.telerik.examples D/TEST: item 1 title = null
02-01 13:49:49.797 22465-22465/com.telerik.examples D/TEST: item 1 fillColor = 0
02-01 13:49:49.797 22465-22465/com.telerik.examples D/TEST: item 1 strokeColor = 0

Why does this happen with pie charts? I tried the same thing on the Line Series example and was able to log the correct values for the LegendItems returned.

3 Answers, 1 is accepted

Sort by
0
Todor
Telerik team
answered on 02 Feb 2016, 03:25 PM
Hello Raga,

Thank you for contacting us.

Indeed the legend works in a different way when used with PieChart or with CartesianChart. When it is used with CartesianChart, it displays information about the series and this information is added when each series is added. When it is used with PieChart, it display information about the data points and this information is available later. If you need to get that information you can use the following approach:

pieChart.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
    @Override
    public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
        ObservableCollection<LegendItem> origLegendItems = pieChart.getLegendInfos();
 
        for (int i=0; i<origLegendItems.size(); i++) {
            LegendItem origItem = origLegendItems.get(i);
 
            Log.d("TEST", "item "+i+" title = " + origItem.getTitle());
            Log.d("TEST", "item "+i+" fillColor = " + origItem.getFillColor());
            Log.d("TEST", "item " + i + " strokeColor = " + origItem.getStrokeColor());
        }
    }
});

If you need to change the legend title for each item, you can override PieSeries and its getLegendTitle(PieDataPoint point) method. The default implementation returns the value of the data point.

I hope this information helps. Let us know if you need further assistance.

Regards,
Todor
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Raga
Top achievements
Rank 1
answered on 03 Feb 2016, 01:52 PM
Thanks. Just a thought: shouldn't the default legend title for each item be the label of the data point rather than the value? I have honestly never seen a pie chart legend that did not display the labels. 
0
Todor
Telerik team
answered on 03 Feb 2016, 04:46 PM
Hello Raga,

Thanks for the suggestion.

I have attached one of the default charts used in Excel as an example of a chart where the labels differ from the information presented in the legend. It shows a common pattern where the legend contains the category (or name) of the data items, while the labels contain the actual values. Since the PieSeries only have a Value Binding (and not Category Binding as in other series), the information we can get from the actual item is only its value. This is why the value is used for the default implementation of both labels and legend info. Depending on your data item type, you can easily get the desired information for the category of the items as explained in my previous post or use it as a label if necessary. 

I hope this information helps. Let us know if you need further assistance with the setup of the legend of the chart.

Regards,
Todor
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
Chart
Asked by
Raga
Top achievements
Rank 1
Answers by
Todor
Telerik team
Raga
Top achievements
Rank 1
Share this question
or