Skip to main content

Legend

  • 3 minutes to read

Legend is a chart element that displays series and series points’ designations.

Legend example

Note that a legend’s content depends on a series. In the case of a Cartesian series, a legend item’s text is the series’s display name (getDisplayName()/setDisplayName(String)). If the series is a Pie, a legend contains items for each pie point. The getLegendTextPattern()/setLegendTextPattern(String) methods configure the legend item’s text.

How to: Change a legend’s position and layout

You can place a legend all around the chart area; for example, at the top-center, either horizontally or vertically. The image below shows a legend placed at the top-center with a horizontal layout:

TopOutside-Centered horizontal legend

See this code to learn how to obtain the image:

Legend legend = new Legend();
legend.setHorizontalPosition(LegendHorizontalPosition.CENTER);
legend.setVerticalPosition(LegendVerticalPosition.TOP_OUTSIDE);
legend.setOrientation(LegendOrientation.LEFT_TO_RIGHT);
mChart.setLegend(legend);

Use the following symbols to configure a legend’s layout and position.

Symbol Description
PieChart.getLegend() Returns the chart legend‘s options.
PieChart.setLegend(Legend) Specifies the chart legend‘s options.
Legend The legend of the chart.
Legend.getHorizontalPosition() Returns the horizontal position of the legend within the chart.
Legend.setHorizontalPosition(LegendHorizontalPosition) Specifies the horizontal position of the legend within the chart.
Legend.getVerticalPosition() Returns the vertical position of the legend within the chart.
Legend.setVerticalPosition(LegendVerticalPosition) Specifies the vertical position of the legend within the chart.
Legend.getOrientation() Returns the legend orientation.
Legend.setOrientation(LegendOrientation) Specifies the legend orientation.

How to: Show or hide a series in the legend

Use the setVisibleInLegend method, to show or hide an individual series in the legend:

series.setVisibleInLegend(true);

How to: Configure a legend’s appearance

You can style a legend like any other chart element using the following symbols:

Symbol Description
LegendStyle The storage of the chart legend’s appearance settings.
Legend.getStyle() Returns the legend style.
Legend.setStyle(LegendStyle) Specifies the legend style.

Below are changeable appearance parameters:

Styleable legend elements

See the code snippet below to learn how to change this legend’s style.

// All sizes are in pixels.
LegendStyle style = new LegendStyle();
style.setBorderColor(Color.DKGRAY);
style.setBorderThickness(5f);
style.setBackgroundColor(Color.GRAY);
style.setMarkerSize(40);
style.setTextIndent(25);
style.setItemsVerticalIndent(50);

TextStyle textStyle = new TextStyle();
textStyle.setSize(40.0f);
textStyle.setTypeface(Typeface.DEFAULT_BOLD);
textStyle.setColor(Color.WHITE);
style.setTextStyle(textStyle);

legend.setStyle(style);