Skip to content
Philipp Jahoda edited this page Mar 27, 2015 · 14 revisions

By default, all chart types support legends and will automatically generate and draw a legend after setting data for the chart. The Legend usually consists of multiple entries each represented by a label an a form/shape.

The number of entries the automatically generated legend contains depends on the number of different colors (across all DataSet objects) as well as on the DataSet labels. The labels of the Legend depend on the labels set for the used DataSet objects in the chart. If no labels for the DataSet objects have been specified, the chart will automatically generate them. If multiple colors are used for one DataSet, those colors are grouped and only described by one label.

For customizing the Legend, use you can retreive the Legend object from the chart using the getLegend() method:

Legend legend = chart.getLegend();

Control if the legend should be drawn

  • setEnabled(boolean enabled): Sets the Legend enabled or disabled. If disabled, the Legend will not be drawn.

Styling / modifying the legend

  • setTextColor(int color): Sets the color of the legend labels.
  • setTextSize(float size): Sets the text-size of the legend labels in dp.
  • setTypeface(Typeface tf): Sets a custom Typeface for the legend labels.

Customizing the legend

  • setPosition(LegendPosition pos): Sets the LegendPosition which defines where the Legend should appear. Choose between RIGHT_OF_CHART, RIGHT_OF_CHART_CENTER, RIGHT_OF_CHART_INSIDE, BELOW_CHART_LEFT, BELOW_CHART_RIGHT, BELOW_CHART_CENTER or PIECHART_CENTER (PieChart only).
  • setForm(LegendForm shape): Sets the LegendForm that should be used. This is the shape that is drawn next to the legend-labels with the color of the DataSet the legend-entry represents. Choose between SQUARE, CIRCLE or LINE.
  • setFormSize(float size): Sets the size of the legend-forms in dp.
  • setXEntrySpace(float space): Sets the space between the legend-entries on the horizontal axis.
  • setYEntrySpace(float space): Sets the space between the legend-entries on the vertical axis.
  • setFprmToTextSpacefloat space): Sets the space between the legend-label and the corresponding legend-form.

Example:

    // setting data...
    chart.setData(....);
    
    Legend l = chart.getLegend();
    l.setFormSize(10f); // set the size of the legend forms/shapes
    l.setForm(LegendForm.CIRCLE); // set what type of form/shape should be used
    l.setPosition(LegendPosition.BELOW_CHART_LEFT);
    l.setTypeface(...);
    l.setXEntrySpace(5f); // set the space between the legend entries on the x-axis
    l.setYEntrySpace(5f); // set the space between the legend entries on the y-axis
    // and many more...

The documentation has moved.

Clone this wiki locally