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

0
0

Now I am trying to add MVVM bindings for a Chart Legend

I have

        <SciChart:SciChartSurface x:Name="sciChart" DataSet="{Binding DataSeriesSet}" 
            SciChart:ThemeManager.Theme="Oscilloscope" 
            Grid.Row="1" 
            RenderableSeries="{Binding RenderableSeries}"
            ChartModifier="{Binding ChartModifier}"
            XAxis="{Binding XAxis}" 
            YAxes="{Binding YAxes}">
                   </SciChart:SciChartSurface>
        <SciChart:SciChartLegend x:Name="legendControl" Grid.Row="1" Margin="23,23" ShowVisibilityCheckboxes="True" LegendData="{ Binding LegendDataSource,  Mode=OneWay}"/>

and in My ViewModel

        public AnalogueChartViewModel()
        {
            XAxis = new DateTimeAxis
            {
                VisibleRange = RangeFactory.NewRange(DateTime.Now.AddDays(-2), DateTime.Now),
                DrawMinorGridLines = false,
                AxisTitle = "X-Axis"
            };
            RenderableSeries = new ObservableCollection<IRenderableSeries>();
            YAxes = new AxisCollection();
            DataSeriesSet = new DataSeriesSet<DateTime, double>();
            var xAxisDrag = new XAxisDragModifier();
            var zoomExtents = new ZoomExtentsModifier();
            var rubberBandZoom = new RubberBandXyZoomModifier();
            var panModifier = new ZoomPanModifier { ExecuteOn = ExecuteOn.MouseRightButton };
            _legendDataSource = new LegendModifier { GetLegendDataFor = 0 };

            ChartModifier = new ModifierGroup(xAxisDrag, zoomExtents, rubberBandZoom, panModifier, _legendDataSource);

        }

        private LegendModifier _legendDataSource;

        public LegendModifier LegendDataSource
        {
            get { return _legendDataSource; }
            set 
            { 
                _legendDataSource = value;
                OnPropertyChanged("LegendDataSource");
            }
        }


But not sure how to bind to the LegendData

Sorry I keep troubling you, but after all this there should be some good MVVM code for others to follow too.

  • You must to post comments
0
0

I’ve half answered my own question above by using:-

 <SciChart:SciChartLegend x:Name="legendControl" Grid.Row="1" Margin="23,23" ShowVisibilityCheckboxes="True" LegendData="{ Binding LegendData,  Mode=OneWay}" />

and

        public ChartDataObject LegendData
        {
            get { return _legendDataSource.LegendData; }
            set 
            { 
                _legendDataSource.LegendData = value;
                OnPropertyChanged("LegendData");
            }
        }

But I do not get any axis Name Text on the Legend.How do I add this?

  • Yuriy
    Hi there,Don't worry about troubling - feel yourself free to ask, we are glad to help! :) OK, you need to change a Binding for the legend:
    <SciChart:SciChartLegend x:Name="legendControl" Grid.Row="1" Margin="23,23" ShowVisibilityCheckboxes="True" LegendData="{Binding LegendDataSource.LegendData,  Mode=OneWay}"/>
    
    This should help, please, try out and let us know,Best regards, Yuriy
  • Yuriy
    Hi,Your solution is working one too - glad you've sorted it out. Text on the legend is bound to your data series name. So you need to have something like that:
                // SeriesName is what will be shown in the legend
                var dataSeries0 = new XyDataSeries<double, double> {SeriesName = "Curve A"};
                var dataSeries1 = new XyDataSeries<double, double> {SeriesName = "Curve B"};
                var dataSeries2 = new XyDataSeries<double, double> {SeriesName = "Curve C"};
    
    Hope this helps!Regards, Yuriy
  • wilx
    That's Great thanks. I have added it to the AddRemoveAxis example above.
  • You must to post comments
Showing 1 result
Your Answer

Please first to submit.