SciChart WPF 2D Charts > Tutorials > MVVM > Tutorial 09b - Linking Multiple Charts with MVVM
Tutorial 09b - Linking Multiple Charts with MVVM

In our series of tutorials, up until now we have added a chart with two YAxis, one XAxis, two series, added tooltips, legends and zooming, panning behavior, and added some annotations.

Source code for this tutorial can be found at our SciChart.WPF.Examples Github Repository under Tutorials section.

Next, we are going to show you how to create multiple charts and link them together.


If you haven't already you will need to review the following tutorials, as we're working straight from these:

Or, if you're in a hurry, skim through the tutorials and copy paste the code into a new solution. Once you've done that, we are ready to get started.


Refactoring our Code to go Multi Chart

At the moment our code consists of a MainWindow.xaml with a single SciChartSurface declared, and a MainViewModel with properties exposed for that chart surface. We are going to refactor this code a little bit to allow us to go multi chart.

First, create a new class called ChartViewModel and move the following code to it.

Copy Code
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Media;
using SciChart.Charting.Model.ChartSeries;
using SciChart.Charting.Model.DataSeries;
using SciChart.Charting.Visuals.Axes;
using SciChart.Data.Model;
namespace SciChart.Mvvm.Tutorial
    public class ChartViewModel : BindableObject
        private readonly IDataProvider _dataProvider;
        private string _chartTitle;
        private readonly ObservableCollection<IRenderableSeriesViewModel> _renderableSeries = new ObservableCollection<IRenderableSeriesViewModel>();
        private readonly ObservableCollection<IAxisViewModel> _yAxes = new ObservableCollection<IAxisViewModel>();
        private readonly ObservableCollection<IAxisViewModel> _xAxes = new ObservableCollection<IAxisViewModel>();
        private readonly ObservableCollection<IAnnotationViewModel> _annotations = new ObservableCollection<IAnnotationViewModel>();
        private XyDataSeries<double, double> _lineData;       
        public ChartViewModel(IDataProvider dataProvider, string chartTitle, string mouseEventGroupId)
            _dataProvider = dataProvider;
            _chartTitle = chartTitle;
            MouseEventGroup = mouseEventGroupId;
            // Subscribe to future updates
            int i = 0;
            _dataProvider.SubscribeUpdates((newValues) =>
                // Append when new values arrive
                _lineData.Append(newValues.XValues, newValues.YValues);
                // Zoom the chart to fit
                // Every 100th datapoint, add an annotation
                if (i % 100 == 0)
                    Annotations.Add(new InfoAnnotationViewModel() { X1 = _lineData.XValues.Last(), Y1 = 0.0 });
        public string MouseEventGroup { get; set; }
        public ObservableCollection<IAxisViewModel> YAxes { get { return _yAxes; } }
        public ObservableCollection<IAxisViewModel> XAxes { get { return _xAxes; } }
        public ObservableCollection<IAnnotationViewModel> Annotations { get { return _annotations; } }
        public ObservableCollection<IRenderableSeriesViewModel> RenderableSeries { get { return _renderableSeries; } }
        private void CreateChartAxis()
            YAxes.Add(new NumericAxisViewModel()
                AutoRange = AutoRange.Always,
                AxisTitle = "Left YAxis",
                Id = "LeftYAxis",
                AxisAlignment = AxisAlignment.Left,
            YAxes.Add(new NumericAxisViewModel()
                AutoRange = AutoRange.Always,
                AxisTitle = "Right YAxis",
                AxisAlignment = AxisAlignment.Right,
            XAxes.Add(new NumericAxisViewModel()
                AutoRange = AutoRange.Always,
                AxisTitle = "XAxis",
                AxisAlignment = AxisAlignment.Bottom,
        private void CreateChartData()
            var initialDataValues = _dataProvider.GetHistoricalData();
            // Create a DataSeries. We later apply this to a RenderableSeries
            _lineData = new XyDataSeries<double, double>() { SeriesName = _chartTitle + " Line Series" };
            // Append some data to the chart                                
            _lineData.Append(initialDataValues.XValues, initialDataValues.YValues);
        private void CreateChartSeries()
            // Create a RenderableSeries. Apply the DataSeries created before
            RenderableSeries.Add(new LineRenderableSeriesViewModel()
                StrokeThickness = 2,
                Stroke = Colors.SteelBlue,
                DataSeries = _lineData,
                StyleKey = "LineSeriesStyle"
        public string ChartTitle
            get { return _chartTitle; }
                _chartTitle = value;

Now we need to adjust our MainViewModel to expose a collection of charts.

Copy Code
using System.Collections.ObjectModel;
using SciChart.Data.Model;
namespace SciChart.Mvvm.Tutorial
    public class MainViewModel : BindableObject
        private bool _enablePan;
        private bool _enableZoom = true;
        private readonly ObservableCollection<ChartViewModel> _chartViewModels = new ObservableCollection<ChartViewModel>();
        public MainViewModel()
            ChartPanes.Add(new ChartViewModel(new DummyDataProvider(), "Primary Chart", "MouseGroup1"));
            ChartPanes.Add(new ChartViewModel(new DummyDataProvider(), "Secondary Chart", "MouseGroup1"));
        public ObservableCollection<ChartViewModel> ChartPanes {  get { return _chartViewModels; } }
        public bool EnableZoom
            get { return _enableZoom; }
                if (_enableZoom != value)
                    _enableZoom = value;
                    if (_enableZoom) EnablePan = false;
        public bool EnablePan
            get { return _enablePan; }
                if (_enablePan != value)
                    _enablePan = value;
                    if (_enablePan) EnableZoom = false;

That's fantastic. We've refactored our code to enable multiple chart panes. The last thing we need to do is to update our MainWindow.xaml to bind to the new ChartPanes property:

Copy Code
<Window x:Class="SciChart.Mvvm.Tutorial.MainWindow"
        Title="MainWindow" Height="550" Width="800">
                <ResourceDictionary Source="/SciChart.Charting;component/Themes/Default.xaml"/>
            <local:MainViewModel x:Key="MainViewModel"/>
            <!-- The TooltipControl template is defined below -->
            <!-- Change this if you want to have a non-default tooltip container -->
            <!-- The ContentPresenter is bound to the DataContext (a SeriesInfo type) -->
            <!-- and the ContentTemplate is the DataTemplate for the SeriesInfo -->
            <!-- Finally, the TooltipContainerStyle is set on the RenderableSeries via Style -->
            <Style x:Key="TooltipContainer" TargetType="s:TooltipControl">
                <Setter Property="Template">
                        <ControlTemplate TargetType="s:TooltipControl">
                            <Border Background="#ff6495ed"
                                <ContentPresenter Content="{TemplateBinding DataContext}"
                                              ContentTemplate="{TemplateBinding ContentTemplate}" />

            <!-- Tooltip Template for an XyDataSeries binds to XySeriesInfo -->
            <!-- Check out the properties on XySeriesInfo to see what you can bind to -->
            <DataTemplate x:Key="TooltipTemplate" DataType="s:XySeriesInfo">
                <StackPanel Orientation="Vertical">
                    <TextBlock Foreground="White">
                    <Run Text="Series: "/>
                    <Run Text="{Binding SeriesName, StringFormat='{}{0}:'}"/>
                    <TextBlock Foreground="White">
                    <Run Text="X-Value: "/>
                    <Run Text="{Binding FormattedXValue, Mode=OneWay}"/>
                    <TextBlock Foreground="White">
                    <Run Text="Y-Value: "/>
                    <Run Text="{Binding FormattedYValue, Mode=OneWay}"/>
            <!-- Style applied via x:Key name in ViewModel. See StyleKey property of LineRenderableSeriesViewModel -->
            <Style TargetType="s:BaseRenderableSeries"  x:Key="LineSeriesStyle">
                <Setter Property="s:RolloverModifier.TooltipContainerStyle" Value="{StaticResource TooltipContainer}"/>
                <Setter Property="s:RolloverModifier.TooltipTemplate" Value="{StaticResource TooltipTemplate}"/>
                <Setter Property="s:RolloverModifier.IncludeSeries" Value="True"/>
                <Setter Property="StrokeThickness" Value="2"/>
            <Style TargetType="local:InfoAnnotation" BasedOn="{StaticResource MvvmBaseAnnotationStyle}"/>
    <Grid DataContext="{StaticResource MainViewModel}">
            <RowDefinition Height="32"/>
            <RowDefinition Height="*"/>

        <StackPanel Orientation="Horizontal" Background="#333">
            <CheckBox Margin="5" Foreground="#FFF" Content="Enable Zoom?" IsChecked="{Binding EnableZoom, Mode=TwoWay}"/>
            <CheckBox Margin="5" Foreground="#FFF" Content="Enable Pan?" IsChecked="{Binding EnablePan, Mode=TwoWay}" />
        <!-- New code here. Add ItemsControl bound to ChartPanes -->
        <ItemsControl ItemsSource="{Binding ChartPanes}" Grid.Row="1">
                    <UniformGrid Rows="2">
                    <!-- Don't forget to add Grid.Row=1 -->
                                       RenderableSeries="{s:SeriesBinding RenderableSeries}" 
                                       Annotations="{s:AnnotationsBinding Annotations}"
                                       YAxes="{s:AxesBinding YAxes}"
                                       XAxes="{s:AxesBinding XAxes}"
                                       ChartTitle="{Binding ChartTitle}">

                            <!-- Bind ModifierGroup using MouseManager.MouseEventGroup attached property -->
                            <s:ModifierGroup s:MouseManager.MouseEventGroup="{Binding MouseEventGroup}">

                                <s:RubberBandXyZoomModifier IsEnabled="{Binding Path=DataContext.EnableZoom, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}}" />
                                <s:ZoomPanModifier IsEnabled="{Binding Path=DataContext.EnablePan, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}}" ClipModeX="None" />
                                <s:LegendModifier ShowLegend="True" Orientation="Horizontal" VerticalAlignment="Top" HorizontalAlignment="Center"/>
                                <s:RolloverModifier ShowTooltipOn="MouseHover"/>


In the above code, we declare an ItemsControl in the MainWindow.xaml. this is bound to a collection of ChartViewModel exposed by MainViewModel.

Each ChartViewModel has a collection of RenderableSeries, Annotations and provides the data and parameters for a chart. We have two of them arranged vertically on the view.

Finally, we link mouse events between the two charts by using the MouseManager.MouseEventGroup attached property. This is bound to a MouseEventGroup property in ChartViewModel. When two or more charts share the same string ID, they will share mouse events such as cursor, zooming and panning.