SciChart® the market leader in Fast WPF Charts, WPF 3D Charts, and now iOS Charting & Android Chart Components

Answered
1
0

Hi,

I have a chart showing data sets composed of multiple renderable series. Each data set is displayed using one FastLineRenderableSeries and three XyScatterRenderableSeries. All four series show in the legend for each data set, but I would only like to show the FastLineRenderableSeries in the legend for each data set. How can I do this?

I’ve seen the legendModifier.GetLegendDataFor property, but that can only be set to show options from this enum

public enum SourceMode
{
    AllSeries = 0,
    AllVisibleSeries = 1,
    SelectedSeries = 2,
    UnselectedSeries = 3,
}

which aren’t what I need. Some kind of flag on the renderable series saying whether they should be shown in the legend, or some way to specify the items source for the legend would be good.

Thanks!

  • You must to post comments
Best Answer
1
0

Sorted it. I declare the SciChartLegend separately from the LegendModifier in the xaml and attach a behaviour to the SciChartLegend. The behaviour declares a dependency property and binds the LegendModifier.LegendData property to it. The behavour subscribes to the LegendModifier.LegendData.SeriesInfo.CollectionChanged event in order to maintain its own ObservableCollection (which is a copy of LegendModifier.LegendData.SeriesInfo with the unwanted SeriesInfo objects filtered out), which it uses to create a new ChartDataObject which is finally assigned to the behaviour’s AssociatedObject.LegendData property.

  • You must to post comments
0
0

I wanted to do the same thing and thought I would share my solution as its pretty simple and is consistent with how you exclude series from the RolloverModifier:

I created a derived legend modifier with a boolean attached property “IncludeSeries”. This has a default value of true. This derived class overrides the GetSeriesInfo() method and filters the renderable series by the value of the “IncludeSeries” attached property (code below):

public class FilteringLegendModifier : LegendModifier
{
   public static bool GetIncludeSeries(DependencyObject obj)
   {
      return (bool)obj.GetValue(IncludeSeriesProperty);
   }

   public static void SetIncludeSeries(DependencyObject obj, bool value)
   {
      obj.SetValue(IncludeSeriesProperty, value);
   }

    // Using a DependencyProperty as the backing store for IncludeSeries.  This enables animation, styling, binding, etc...
   public static readonly DependencyProperty IncludeSeriesProperty = DependencyProperty.RegisterAttached("IncludeSeries", typeof(bool), typeof(FilteringLegendModifier), new PropertyMetadata(true));

   protected override ObservableCollection<SeriesInfo> GetSeriesInfo(IEnumerable<IRenderableSeries> allSeries)  
   {
       var filteredSeries = allSeries.Where(s => s is UIElement)
                                                              .Where(s => (bool)((UIElement)s).GetValue(IncludeSeriesProperty));      
      return base.GetSeriesInfo(filteredSeries); 
   }
}

Use this modifier in your xaml exactly the same as you use the standard LegendModifier. If you want a series to be excluded from the legend add the IncludeSeries attached property set to false (since True is the default value you do not need the property on series you want to show):

...
<s:SciChartSurface.RenderableSeries>
   <s:XyScatterRenderableSeries x:Name="seriesToShowInLegend"/>
   <s:XyScatterRenderableSeries x:Name="seriesToExcluedFromInLegend" modifiers:FilteringLegendModifier.IncludeSeries="False" />
</s:SciChartSurface.RenderableSeries>
...

Hope that might help someone!

  • Andrew
    I see there’s a use-case for adding LegendModifier.IncludeSeries attached property … I will add it to the TODO list
  • You must to post comments
Showing 2 results
Your Answer

Please first to submit.