Pre loader

Android Chart Legends

Android Chart - Examples

SciChart Android ships with ~90 Android Chart Examples which you can browse, play with, view the source-code and even export each SciChart Android Chart Example to a stand-alone Android Studio project. All of this is possible with the new and improved SciChart Android Examples Suite, which ships as part of our Android Charts SDK.

Download Scichart Android

Generates a simple line chart with a Chart Legend. To create a Legend, add a LegendModifier to the ChartModifierCollection of SciChartSurface. It aggregates and exposes series data which the Legend is bound to.

Tip!

There are many different configurations for the legend, including auto-creation of legend, or fine grained control over the legend rows. Please review the API documentation for further information.

To learn more about Legends in SciChart Android Charts, see the LegendModifier documentation.

The full source code for the Android Chart Legends example is included below (Scroll down!).

Did you know you can also view the source code from one of the following sources as well?

  1. Clone the SciChart.Android.Examples from Github.
  2. Or, view source and export each example to an Android Studio project from the Java version of the SciChart Android Examples app.
  3. Also the SciChart Android Trial contains the full source for the examples (link below).

DOWNLOAD THE ANDROID CHART EXAMPLES

Kotlin: LegendFragment.kt
View source code
//******************************************************************************
// SCICHART® Copyright SciChart Ltd. 2011-2021. All rights reserved.
//
// Web: http://www.scichart.com
// Support: support@scichart.com
// Sales:   sales@scichart.com
//
// LegendFragment.kt is part of SCICHART®, High Performance Scientific Charts
// For full terms and conditions of the license, see http://www.scichart.com/scichart-eula/
//
// This source code is protected by international copyright law. Unauthorized
// reproduction, reverse-engineering, or distribution of all or any portion of
// this source code is strictly prohibited.
//
// This source code contains confidential and proprietary trade secrets of
// SciChart Ltd., and should at no time be copied, transferred, sold,
// distributed or made available without express written permission.
//******************************************************************************

package com.scichart.examples.fragments.examples2d.legends.kt

import android.view.Gravity
import android.view.View
import android.view.animation.DecelerateInterpolator
import android.widget.AdapterView
import android.widget.CompoundButton
import android.widget.Spinner
import com.scichart.charting.modifiers.LegendModifier
import com.scichart.charting.modifiers.SourceMode
import com.scichart.charting.visuals.SciChartSurface
import com.scichart.charting.visuals.renderableSeries.IRenderableSeries
import com.scichart.charting.visuals.renderableSeries.StyleBase
import com.scichart.core.annotations.Orientation
import com.scichart.drawing.common.PenStyle
import com.scichart.drawing.utility.ColorUtil
import com.scichart.examples.R
import com.scichart.examples.components.SpinnerStringAdapter
import com.scichart.examples.data.DataManager
import com.scichart.examples.fragments.base.ExampleSingleChartBaseFragment
import com.scichart.examples.utils.Constant
import com.scichart.examples.utils.EnumUtils
import com.scichart.examples.utils.ItemSelectedListenerBase
import com.scichart.examples.utils.ViewSettingsUtil
import com.scichart.examples.utils.interpolator.DefaultInterpolator
import com.scichart.examples.utils.scichartExtensions.*
import com.scichart.examples.utils.widgetgeneration.ImageViewWidget
import com.scichart.examples.utils.widgetgeneration.Widget
import java.util.*

class LegendFragment : ExampleSingleChartBaseFragment() {
    private val sourceModeValues = listOf(*SourceMode.values())
    private lateinit var legendModifier: LegendModifier

    private var selectedOrientation = Orientation.VERTICAL
    private var selectedSourceMode = 0
    private var showLegend = true
    private var showCheckBoxes = true
    private var showSeriesMarkers = true

    override fun initExample(surface: SciChartSurface) {
        val ds1Points = DataManager.getInstance().getStraightLine(4000.0, 1.0, 10)
        val ds2Points = DataManager.getInstance().getStraightLine(3000.0, 1.0, 10)
        val ds3Points = DataManager.getInstance().getStraightLine(2000.0, 1.0, 10)
        val ds4Points = DataManager.getInstance().getStraightLine(1000.0, 1.0, 10)

        surface.suspendUpdates {
            xAxes { numericAxis() }
            yAxes { numericAxis() }
            renderableSeries {
                fastLineRenderableSeries {
                    xyDataSeries<Double, Double>("Curve A") { append(ds1Points.xValues, ds1Points.yValues) }
                    strokeStyle = SolidPenStyle(ColorUtil.argb(0xFF, 0x47, 0xBD, 0xE6))
                    sweepAnimation {
                        duration = Constant.ANIMATION_DURATION
                        startDelay = Constant.ANIMATION_START_DELAY
                        interpolator = DefaultInterpolator.getInterpolator()
                    }
                }
                fastLineRenderableSeries {
                    xyDataSeries<Double, Double>("Curve B") { append(ds2Points.xValues, ds2Points.yValues) }
                    strokeStyle = SolidPenStyle(ColorUtil.argb(0xFF, 0xAE, 0x41, 0x8D))
                    sweepAnimation {
                        duration = Constant.ANIMATION_DURATION
                        startDelay = Constant.ANIMATION_START_DELAY
                        interpolator = DefaultInterpolator.getInterpolator()
                    }
                }
                fastLineRenderableSeries {
                    xyDataSeries<Double, Double>("Curve C") { append(ds3Points.xValues, ds3Points.yValues) }
                    strokeStyle = SolidPenStyle(ColorUtil.argb(0xFF, 0x68, 0xBC, 0xA8))
                    sweepAnimation {
                        duration = Constant.ANIMATION_DURATION
                        startDelay = Constant.ANIMATION_START_DELAY
                        interpolator = DefaultInterpolator.getInterpolator()
                    }
                }
                fastLineRenderableSeries {
                    xyDataSeries<Double, Double>("Curve D") { append(ds4Points.xValues, ds4Points.yValues) }
                    strokeStyle = SolidPenStyle(ColorUtil.argb(0xFF, 0xE9, 0x70, 0x64))
                    sweepAnimation {
                        duration = Constant.ANIMATION_DURATION
                        startDelay = Constant.ANIMATION_START_DELAY
                        interpolator = DefaultInterpolator.getInterpolator()
                    }
                }
            }

            legendModifier = LegendModifier(context).apply {
                setLegendPosition(Gravity.TOP or Gravity.START, 16)
                setSourceMode(sourceModeValues[selectedSourceMode])
                setOrientation(selectedOrientation)
            }
            chartModifiers {
                modifier(legendModifier)
                seriesSelectionModifier {
                    selectedSeriesStyle = object : StyleBase<IRenderableSeries>(IRenderableSeries::class.java) {
                        override fun applyStyleInternal(renderableSeriesToStyle: IRenderableSeries) {
                            val currentStrokeStyle = renderableSeriesToStyle.strokeStyle
                            putPropertyValue(renderableSeriesToStyle, "Stroke", currentStrokeStyle)

                            val newStrokeStyle = SolidPenStyle(currentStrokeStyle.color, 3f)
                            renderableSeriesToStyle.strokeStyle = newStrokeStyle
                        }

                        override fun discardStyleInternal(renderableSeriesToStyle: IRenderableSeries) {
                            val stroke = getPropertyValue(renderableSeriesToStyle, "Stroke", PenStyle::class.java)
                            renderableSeriesToStyle.strokeStyle = stroke
                        }
                    }
                }
            }
        }
    }

    override fun getToolbarItems(): List<Widget> = ArrayList<Widget>().apply {
        add(ImageViewWidget.Builder().setId(R.drawable.example_toolbar_settings).setListener { openSettingsDialog() }.build())
    }

    private fun openSettingsDialog() {
        val dialog = ViewSettingsUtil.createSettingsPopup(activity, R.layout.example_performance_demo_popup_layout)
        val context = dialog.context

        dialog.findViewById<Spinner>(R.id.legend_orientation_spinner).run {
            adapter = SpinnerStringAdapter(context, R.array.legend_orientation)

            setSelection(selectedOrientation)
            onItemSelectedListener = object : ItemSelectedListenerBase() {
                override fun onItemSelected(parent: AdapterView<*>?, view: View, position: Int, id: Long) {
                    selectedOrientation = if (position == 0) Orientation.HORIZONTAL else Orientation.VERTICAL
                    legendModifier.setOrientation(selectedOrientation)
                }
            }
        }

        ViewSettingsUtil.setUpSwitchCompat(dialog, R.id.show_legend_checkbox, showLegend) { _: CompoundButton?, isChecked: Boolean ->
            showLegend = isChecked
            legendModifier.setShowLegend(showLegend)
        }
        ViewSettingsUtil.setUpSwitchCompat(dialog, R.id.show_checkboxes_checkbox, showCheckBoxes) { _: CompoundButton?, isChecked: Boolean ->
            showCheckBoxes = isChecked
            legendModifier.setShowCheckboxes(showCheckBoxes)
        }
        ViewSettingsUtil.setUpSwitchCompat(dialog, R.id.show_series_markers_checkbox, showSeriesMarkers) { _: CompoundButton?, isChecked: Boolean ->
            showSeriesMarkers = isChecked
            legendModifier.setShowSeriesMarkers(showSeriesMarkers)
        }

        dialog.findViewById<Spinner>(R.id.legend_source_spinner).run {
            adapter = SpinnerStringAdapter(context, EnumUtils.getEnumValuesArray(SourceMode::class.java))

            setSelection(selectedSourceMode)
            onItemSelectedListener = object : ItemSelectedListenerBase() {
                override fun onItemSelected(parent: AdapterView<*>?, view: View, position: Int, id: Long) {
                    selectedSourceMode = position
                    legendModifier.setSourceMode(sourceModeValues[selectedSourceMode])
                }
            }
        }

        dialog.show()
    }
}
Java: LegendFragment.java
View source code
//******************************************************************************
// SCICHART® Copyright SciChart Ltd. 2011-2021. All rights reserved.
//
// Web: http://www.scichart.com
// Support: support@scichart.com
// Sales:   sales@scichart.com
//
// LegendFragment.java is part of SCICHART®, High Performance Scientific Charts
// For full terms and conditions of the license, see http://www.scichart.com/scichart-eula/
//
// This source code is protected by international copyright law. Unauthorized
// reproduction, reverse-engineering, or distribution of all or any portion of
// this source code is strictly prohibited.
//
// This source code contains confidential and proprietary trade secrets of
// SciChart Ltd., and should at no time be copied, transferred, sold,
// distributed or made available without express written permission.
//******************************************************************************

package com.scichart.examples.fragments.examples2d.legends;

import static java.util.Arrays.asList;
import static java.util.Collections.unmodifiableList;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.widget.AdapterView;
import android.widget.Spinner;

import androidx.annotation.NonNull;

import com.scichart.charting.model.dataSeries.XyDataSeries;
import com.scichart.charting.modifiers.LegendModifier;
import com.scichart.charting.modifiers.ModifierGroup;
import com.scichart.charting.modifiers.SeriesSelectionModifier;
import com.scichart.charting.modifiers.SourceMode;
import com.scichart.charting.visuals.SciChartSurface;
import com.scichart.charting.visuals.renderableSeries.FastLineRenderableSeries;
import com.scichart.charting.visuals.renderableSeries.IRenderableSeries;
import com.scichart.charting.visuals.renderableSeries.StyleBase;
import com.scichart.core.annotations.Orientation;
import com.scichart.core.framework.UpdateSuspender;
import com.scichart.drawing.common.PenStyle;
import com.scichart.drawing.utility.ColorUtil;
import com.scichart.examples.R;
import com.scichart.examples.components.SpinnerStringAdapter;
import com.scichart.examples.data.DataManager;
import com.scichart.examples.data.DoubleSeries;
import com.scichart.examples.fragments.base.ExampleSingleChartBaseFragment;
import com.scichart.examples.utils.Constant;
import com.scichart.examples.utils.EnumUtils;
import com.scichart.examples.utils.ItemSelectedListenerBase;
import com.scichart.examples.utils.ViewSettingsUtil;
import com.scichart.examples.utils.interpolator.DefaultInterpolator;
import com.scichart.examples.utils.widgetgeneration.ImageViewWidget;
import com.scichart.examples.utils.widgetgeneration.Widget;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class LegendFragment extends ExampleSingleChartBaseFragment {
    private static final List<SourceMode> sourceModeValues = unmodifiableList(asList(SourceMode.values()));
    private LegendModifier legendModifier;

    private int selectedOrientation = Orientation.VERTICAL;
    private int selectedSourceMode = 0;
    private boolean showLegend = true;
    private boolean showCheckBoxes = true;
    private boolean showSeriesMarkers = true;

    @NonNull
    @Override
    public List<Widget> getToolbarItems() {
        return new ArrayList<Widget>() {
            {
                add(new ImageViewWidget.Builder().setId(R.drawable.example_toolbar_settings).setListener(v -> openSettingsDialog()).build());
            }
        };
    }

    @Override
    protected void initExample(@NonNull SciChartSurface surface) {
        final XyDataSeries<Double, Double> dataSeries1 = sciChartBuilder.newXyDataSeries(Double.class, Double.class).withSeriesName("Curve A").build();
        final XyDataSeries<Double, Double> dataSeries2 = sciChartBuilder.newXyDataSeries(Double.class, Double.class).withSeriesName("Curve B").build();
        final XyDataSeries<Double, Double> dataSeries3 = sciChartBuilder.newXyDataSeries(Double.class, Double.class).withSeriesName("Curve C").build();
        final XyDataSeries<Double, Double> dataSeries4 = sciChartBuilder.newXyDataSeries(Double.class, Double.class).withSeriesName("Curve D").build();

        final DoubleSeries ds1Points = DataManager.getInstance().getStraightLine(4000, 1.0, 10);
        final DoubleSeries ds2Points = DataManager.getInstance().getStraightLine(3000, 1.0, 10);
        final DoubleSeries ds3Points = DataManager.getInstance().getStraightLine(2000, 1.0, 10);
        final DoubleSeries ds4Points = DataManager.getInstance().getStraightLine(1000, 1.0, 10);

        dataSeries1.append(ds1Points.xValues, ds1Points.yValues);
        dataSeries2.append(ds2Points.xValues, ds2Points.yValues);
        dataSeries3.append(ds3Points.xValues, ds3Points.yValues);
        dataSeries4.append(ds4Points.xValues, ds4Points.yValues);

        final int line1Color = ColorUtil.argb(0xFF, 0x47, 0xBD, 0xE6);
        final int line2Color = ColorUtil.argb(0xFF, 0xAE, 0x41, 0x8D);
        final int line3Color = ColorUtil.argb(0xFF, 0x68, 0xBC, 0xA8);
        final int line4Color = ColorUtil.argb(0xFF, 0xE9, 0x70, 0x64);

        final FastLineRenderableSeries line1 = sciChartBuilder.newLineSeries().withStrokeStyle(line1Color, 1f, true).withDataSeries(dataSeries1).build();
        final FastLineRenderableSeries line2 = sciChartBuilder.newLineSeries().withStrokeStyle(line2Color, 1f, true).withDataSeries(dataSeries2).build();
        final FastLineRenderableSeries line3 = sciChartBuilder.newLineSeries().withStrokeStyle(line3Color, 1f, true).withDataSeries(dataSeries3).build();
        final FastLineRenderableSeries line4 = sciChartBuilder.newLineSeries().withStrokeStyle(line4Color, 1f, true).withDataSeries(dataSeries4).withIsVisible(false).build();

        UpdateSuspender.using(surface, () -> {
            final SeriesSelectionModifier seriesSelectionModifier = new SeriesSelectionModifier();
            seriesSelectionModifier.setSelectedSeriesStyle(new StyleBase<IRenderableSeries>(IRenderableSeries.class) {
                @Override
                protected void applyStyleInternal(IRenderableSeries renderableSeriesToStyle) {
                    final PenStyle currentStrokeStyle = renderableSeriesToStyle.getStrokeStyle();
                    putPropertyValue(renderableSeriesToStyle, "Stroke", currentStrokeStyle);

                    final PenStyle newStrokeStyle = sciChartBuilder.newPen().withColor(currentStrokeStyle.getColor()).withThickness(3).build();
                    renderableSeriesToStyle.setStrokeStyle(newStrokeStyle);
                }

                @Override
                protected void discardStyleInternal(IRenderableSeries renderableSeriesToStyle) {
                    final PenStyle stroke = getPropertyValue(renderableSeriesToStyle, "Stroke", PenStyle.class);
                    renderableSeriesToStyle.setStrokeStyle(stroke);
                }
            });

            ModifierGroup modifierGroup = sciChartBuilder.newModifierGroup()
                    .withLegendModifier().withPosition(Gravity.TOP | Gravity.START, 16).withSourceMode(sourceModeValues.get(selectedSourceMode)).withOrientation(selectedOrientation).build()
                    .withModifier(seriesSelectionModifier)
                    .build();
            legendModifier = (LegendModifier) modifierGroup.getChildModifiers().get(0);

            Collections.addAll(surface.getXAxes(), sciChartBuilder.newNumericAxis().build());
            Collections.addAll(surface.getYAxes(), sciChartBuilder.newNumericAxis().build());
            Collections.addAll(surface.getRenderableSeries(), line1, line2, line3, line4);
            Collections.addAll(surface.getChartModifiers(), modifierGroup);

            sciChartBuilder.newAnimator(line1).withSweepTransformation().withInterpolator(DefaultInterpolator.getInterpolator()).withDuration(Constant.ANIMATION_DURATION).withStartDelay(Constant.ANIMATION_START_DELAY).start();
            sciChartBuilder.newAnimator(line2).withSweepTransformation().withInterpolator(DefaultInterpolator.getInterpolator()).withDuration(Constant.ANIMATION_DURATION).withStartDelay(Constant.ANIMATION_START_DELAY).start();
            sciChartBuilder.newAnimator(line3).withSweepTransformation().withInterpolator(DefaultInterpolator.getInterpolator()).withDuration(Constant.ANIMATION_DURATION).withStartDelay(Constant.ANIMATION_START_DELAY).start();
            sciChartBuilder.newAnimator(line4).withSweepTransformation().withInterpolator(DefaultInterpolator.getInterpolator()).withDuration(Constant.ANIMATION_DURATION).withStartDelay(Constant.ANIMATION_START_DELAY).start();
        });
    }

    private void openSettingsDialog() {
        final Dialog dialog = ViewSettingsUtil.createSettingsPopup(getActivity(), R.layout.example_chart_legend_popup_layout);
        Context context = dialog.getContext();

        final SpinnerStringAdapter legendOrientationAdapter = new SpinnerStringAdapter(context, R.array.legend_orientation);
        final Spinner legendOrientationSpinner = dialog.findViewById(R.id.legend_orientation_spinner);
        legendOrientationSpinner.setAdapter(legendOrientationAdapter);
        legendOrientationSpinner.setSelection(selectedOrientation);
        legendOrientationSpinner.setOnItemSelectedListener(new ItemSelectedListenerBase() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                selectedOrientation = position == 0 ? Orientation.HORIZONTAL : Orientation.VERTICAL;
                legendModifier.setOrientation(selectedOrientation);
            }
        });

        ViewSettingsUtil.setUpSwitchCompat(dialog, R.id.show_legend_checkbox, showLegend, (buttonView, isChecked) -> {
            showLegend = isChecked;
            legendModifier.setShowLegend(showLegend);
        });

        ViewSettingsUtil.setUpSwitchCompat(dialog, R.id.show_checkboxes_checkbox, showCheckBoxes, (buttonView, isChecked) -> {
            showCheckBoxes = isChecked;
            legendModifier.setShowCheckboxes(showCheckBoxes);
        });

        ViewSettingsUtil.setUpSwitchCompat(dialog, R.id.show_series_markers_checkbox, showSeriesMarkers, (buttonView, isChecked) -> {
            showSeriesMarkers = isChecked;
            legendModifier.setShowSeriesMarkers(showSeriesMarkers);
        });

        final SpinnerStringAdapter legendSourceAdapter = new SpinnerStringAdapter(context, EnumUtils.getEnumValuesArray(SourceMode.class));
        final Spinner legendSourceSpinner = dialog.findViewById(R.id.legend_source_spinner);
        legendSourceSpinner.setAdapter(legendSourceAdapter);
        legendSourceSpinner.setSelection(selectedSourceMode);
        legendSourceSpinner.setOnItemSelectedListener(new ItemSelectedListenerBase() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                selectedSourceMode = position;
                legendModifier.setSourceMode(sourceModeValues.get(selectedSourceMode));
            }
        });

        dialog.show();
    }
}
Back to Android Chart Examples