Hello,
I am trying to make a custom user control for the legend. I’d like to have a file, like MyLegend.xaml, along with a MyLegendViewModel.cs, which can be referenced in a larger view containing the associated surfaces, MyBigChartView.xaml / MyBigChartViewModel.cs. I can’t find any examples of this. Everything I’ve seen is about declaring an ItemTemplate or DataTemplate…
Can anyone point me towards a very simple example of using a custom xaml control to define the legend and then using that in another large xaml view?
- Kurt Madland asked 1 year ago
- You must login to post comments
Hi Kurt
This should be possible in SciChart WPF. Take a look at the LegendModifier documentation, we have a number of techniques on how to consume legend data.
Specifically this method looks closest to what you want to achieve:
Binding LegendModifier.LegendData to SciChartLegend
You can alternatively bind LegendModifier.LegendData to a
SciChartLegend and place anywhere in your application. You can also
bind to an LegendModifier.LegendData to an ItemsControl.ItemsSource
and template it as you wish.
<s:SciChartSurface x:Name="sciChart" s:ThemeManager.Theme="Chrome">
<!-- XAxis, YAxis, Omitted for brevity -->
<!-- Declare ChartModifiers -->
<s:SciChartSurface.ChartModifier>
<s:ModifierGroup>
<s:LegendModifier x:Name="legendModifier" GetLegendDataFor="AllSeries" ShowLegend="False"/>
</s:ModifierGroup>
</s:SciChartSurface.ChartModifier>
</s:SciChartSurface>
<!-- Somewhere else in your application, you can declare a SciChartLegend and bind to LegendModifier.LegendData -->
<s:SciChartLegend x:Name="legendControl"
s:ThemeManager.Theme="Chrome"
Margin="23,23"
ScrollViewer.HorizontalScrollBarVisibility="Auto"
ScrollViewer.VerticalScrollBarVisibility="Auto"
LegendData="{Binding LegendData, ElementName=legendModifier, Mode=OneWay}"
ShowVisibilityCheckboxes="True" />
the SciChartLegend is just a templated ItemsControl. You don’t need to use it. legendModifier.LegendData is an ObservableCollection of SeriesInfo. We discuss more about the SeriesInfo type here: SeriesInfo – the ViewModels for Tooltips and Legends.
In terms of actual examples? No-one has ever asked this exact requirement before so we don’t have an example.
There’s a couple of nice examples though in our Github repo showing some legend customisations. Check out this one on a custom legend row with color picker and more and this one which adds a Checkbox to show/hide the axis for each series in legend. These both use DataTemplate for legend items which is why I recommend the above technique on creating a custom control and binding to legendModifier.LegendData.
Hope this helps
Best regards,
Andrew
- Andrew Burnett-Thompson answered 1 year ago
- You must login to post comments
Please login first to submit.