-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
Legend
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 theLegendenabled or disabled. If disabled, theLegendwill 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 customTypefacefor the legend labels.
Customizing the legend
-
setPosition(LegendPosition pos): Sets theLegendPositionwhich defines where theLegendshould 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 (PieChartonly), ... and more. -
setForm(LegendForm shape): Sets theLegendFormthat should be used. This is the shape that is drawn next to the legend-labels with the color of theDataSetthe 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. -
setFormToTextSpacefloat space): Sets the space between the legend-label and the corresponding legend-form.
Setting custom labels & colors
-
setCustom(int[] colors, String[] labels): Sets a custom legend's labels and colors arrays. The colors count should match the labels count. Each color is for the form drawn at the same index. A null label will start a group. A (-2) color will avoid drawing a form This will disable the feature that automatically calculates the legend labels and colors from the datasets. CallresetCustom()to re-enable automatic calculation (and thennotifyDataSetChanged()is needed to auto-calculate the legend again) -
resetCustom(): Calling this will disable the custom legend labels (set bysetCustom(...)). Instead, the labels will again be calculated automatically (afternotifyDataSetChanged()is called). -
setExtraLabels(String[] labels): Sets labels that will be appended to the end of the labels array after calculating the legend. A null label will start a group. (if the legend has already been calculated, you will need to callnotifyDataSetChanged()) -
setExtraColors(List colors): Sets colors that will be appended to the end of the colors array after calculating the legend. (if the legend has already been calculated, you will need to callnotifyDataSetChanged())
Example
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.setTextSize(12f);
l.setTextColor(Color.BLACK);
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...