Pre loader

Reordering seriesnames in legend

Welcome to the SciChart Forums!

  • Please read our Question Asking Guidelines for how to format a good question
  • Some reputation is required to post answers. Get up-voted to avoid the spam filter!
  • We welcome community answers and upvotes. Every Q&A improves SciChart for everyone

WPF Forums | JavaScript Forums | Android Forums | iOS Forums

1
0

I am dynamically adding and removing items from an observablecollection of Irenderable series in my viewmodel, and binding it to the renderableseries on the chart surface. However, when I add and remove items that I want to be ordered, and are appropriately ordered in the collection, they are not ordered on the legend.

Is there a way to reorder the series in the legend (MVVM solution would be great)?

You can replicate the issue (or the symptom), by toggling the visibilty of the series in the below example by clicking the checkbox within the legend and then switching the getlegenddatafrom to visible series, and then switch back. (no longer in ABC order).

https://www.scichart.com/Abt.Controls.SciChart.SL.ExampleTestPage.html#/Abt.Controls.SciChart.Example;component/Examples/IWantTo/CreateMultiseriesChart/MultipleLinesView.xaml

Thanks,
Jason

  • You must to post comments
0
0

Hi Jason,

I’ve just pushed a change to our v3.1 Nightly build (r5373)

This just makes some base methods in the LegendModifier protected virtual, or public virtual. Now you can override the UpdateLegend() method as follows:

public class LegendModifierEx : LegendModifier
{
    /// <summary>
    /// Refreshes the legend with up-to-date <see cref="SeriesInfo" /> with series names, latest values
    /// </summary>
    public override void UpdateLegend()
    {
        if (IsEnabled && IsAttached)
        {
            // Get the series to check. In the base this checks Visible, Invisible, Selected series etc... 
            // against legend flag GetLegendDataFor
            // THIS IS THE LINE YOU NEED TO CHANGE IF YOU WANT TO ALTER WHICH SERIES ARE IN THE LEGEND
            var allSeries = ParentSurface.RenderableSeries.Where(IsSeriesValid);

            // Get the series info collection for the renderable series
            var newSerieesInfoCollection = GetSeriesInfo(allSeries);
            var oldSeriesInfoCollection = SeriesData.SeriesInfo;

            // We need this trick to prevent visibility checkboxes flickering in .NET4.5             
            // Do a diff on the Old and New seriesinfo objects
            var newRenderableSeries = newSerieesInfoCollection.Select(x => x.RenderableSeries).ToArray();
            oldSeriesInfoCollection.RemoveWhere(info => !newRenderableSeries.Contains(info.RenderableSeries));

            // Update old elements and add new elements
            foreach (var seriesInfo in newSerieesInfoCollection)
            {
                var newSeriesInfo = seriesInfo;
                var oldSeriesInfo =
                    oldSeriesInfoCollection.FirstOrDefault(
                        x => x.RenderableSeries.Equals(newSeriesInfo.RenderableSeries));

                if (oldSeriesInfo != null)
                {
                    // This replaces data on the oldSeriesInfo instance with data from newSeriesInfo
                    // Rather than changing the instance, this results in a less flickery, less performance intensive legend
                    UpdateSeriesInfo(oldSeriesInfo, newSeriesInfo);
                }
                else
                {
                    oldSeriesInfoCollection.Add(newSeriesInfo);
                }
            }
        }
    }
}

Altering the highlighted line to do an OrderBy() will allow you to order your legend items as you wish. Also, you can use this method to further customize the legend output such as getting LegendData for only certain series.

Try getting the nightly build and let us know if it works. This change will also be merged to v3.2 and pushed in the next v3.2 release.

Best regards,
Andrew

  • You must to post comments
Showing 1 result
Your Answer

Please first to submit.

Try SciChart Today

Start a trial and discover why we are the choice
of demanding developers worldwide

Start TrialCase Studies