Pie Chart legend customization

Article ID: 2220
Last updated: 29 May, 2018
Article ID: 2220
Last updated: 29 May, 2018
Revision: 3
Views: 2825
Posted: 29 Jul, 2011
by Dean J.
Updated: 29 May, 2018
by Gargani A.


How do I customize a legend item in a pie Chart?


When you associate an IlvLegend with a chart, items are automatically added to the legend. The legend invokes the createLegendItems() method of each renderer of the chart. In this case, items are by default instances of lvPieRendererLegendItem.
This class delegates the legend item drawing operation to the associated renderer, so that the legend item symbol is drawn according to the renderer type and its current rendering style.

In particular the text associated with the legend is provided by the renderer method getLegendText(). By default that method will return the label associated with the data point.

We need to override this method to change the text of the legend. The following shows the steps required to achieve a custom legend:

public class legendPieChartRenderer extends IlvPieChartRenderer

protected IlvChartRenderer createChild(IlvDataSet dataSet)

return new IlvSinglePieRenderer()


public java.lang.String getLegendText(IlvLegendItem item)





//== Add a pie chart.
IlvChart chart = new IlvChart(IlvChart.PIE);
// Create a legend pie chart renderer.
IlvPieChartRenderer disp = new legendPieChartRenderer();
// Set the data source.
// Add this renderer to the chart.

  1. To change the text displayed by default we need to create our own class of pie renderer:
  2. In this new renderer we will override the method that creates the renderer for a single dataSet:
  3. In order to return our own class of pie renderer for a single dataSet based on IlvSinglePieRenderer:
  4. In this class we will provide the customized text in the method getLegendText():
    1. String label = super.getLegendText(item);

      int idx = ((IlvPieRendererLegendItem) item).getDataIndex();

      double value = getDataSource().getDataSet(0).getYData(idx);

      return new String(label + " = " + value +")");

      1. You can retrieve the default text:
      2. You can find the index of the associated data:
      3. You can even find the value of the slice:
  5. To use this class you simply need to add this renderer to the chart as shown here:

You can a modified release of the Highlight Demo provided in the distribution in the archive file HighlightLegend.zip.

Unzip the file under jviews-directory/demos/chart/highlightlegend and use the ant script to build and run the sample.

Above the legend by default and below the custom legend.

This article was:   Helpful | Not helpful
Report an issue
Article ID: 2220
Last updated: 29 May, 2018
Revision: 3
Views: 2825
Posted: 29 Jul, 2011 by Dean J.
Updated: 29 May, 2018 by Gargani A.
Also listed in

Others in this category