SciChart Android 2D Charts API > ChartModifier API > Selection and Legends > Legend Modifier
Legend Modifier

In SciChart, the easiest way to add a Legend onto a chart is to use a LegendModifier:

 LegendModifier Features

The LegendModifier class exposes several configurational properties. Please find them explained in the table below:

Feature Description
setShowLegend() Allows to hide or show the Legend.
setLegendPosition() Allows to specify Gravity and Margins for the Legend.
setOrientation() Determines orientation of the Legend. Can be either Horizontal or Vertical.
setShowSeriesMarkers() Determines whether to show colored markers for every RenderableSeries in the Legend or not.
setShowCheckboxes() Determines whether to show visibility checkboxes for every RenderableSeries in the Legend or not. These allow hiding or showing their corresponsing RenderableSeries.
setSourceMode() Allows to specify which RenderableSeries should appear in the Legend, e.g. VisibleSeriesSelectedSeries, etc. Other will be ignored by the modifier. Expects a member of the SourceMode enumeration.

Adding the LegendModifier to a Chart

A ChartModifier can be added to a ChartModifiersCollection of a SciChartSurface via the getChartModifiers()setChartModifiers() methods. This can be done using the code below:

Copy Code
// Assume the surface has been created and configured earlier
ISciChartSurface surface;

// Create a LegendModifier
LegendModifier legendModifier = new LegendModifier(getActivity());
legendModifier.setShowLegend(true);
legendModifier.setSourceMode(SourceMode.AllVisibleSeries);
legendModifier.setOrientation(Orientation.HORIZONTAL);

 // Add the modifier to the surface
 surface.getChartModifiers().add(legendModifier);

Also a LegendModifier can be created using Chart Builders. To learn more about features available, Please refer to the Common ChartModifiers Features article.

 

Placing Legend outside Chart

Starting from v2.1 and above you can place Legend outside chart. To do so first of all you need to declare SciChartLegend somewhere in your layout:

XML layout
Copy Code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <com.scichart.charting.visuals.SciChartSurface
        android:id="@+id/chart"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"/>
    <com.scichart.charting.visuals.legend.SciChartLegend
        android:id="@+id/legend"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_margin="10dp"/>
</LinearLayout>

Then you need to find legend using findViewById() in code and use it to create LegendModifier:

Creation LegendModifier with legend outside Chart
Copy Code
// locate legend in layout
final SciChartLegend legend = getActivity().findViewById(R.id.legend);
// create LegendModifier using legend and passing useAutoPlacement = false to disable auto placement of legend within chart
final LegendModifier legendModifier = new LegendModifier(legend, false);
// Add the modifier to the surface
surface.getChartModifiers().add(legendModifier);

 This should produce next output:

 

 

See Also