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

4 posts, 0 answers
  1. Raga
    Raga avatar
    4 posts
    Member since:
    Jan 2016

    Posted 01 Feb Link to this post

    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.
  2. Todor
    Admin
    Todor avatar
    778 posts

    Posted 02 Feb Link to this post

    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
  3. DevCraft banner
  4. Raga
    Raga avatar
    4 posts
    Member since:
    Jan 2016

    Posted 03 Feb in reply to Todor Link to this post

    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. 
  5. Todor
    Admin
    Todor avatar
    778 posts

    Posted 03 Feb Link to this post

    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
Back to Top
DevCraft banner