SciChart® the market leader in Fast WPF Charts, WPF 3D Charts, iOS Chart, Android Chart and JavaScript Chart Components
SciChart WPF ships with hundreds of WPF Chart Examples which you can browse, play with, view the source-code and even export each WPF Chart Example to a stand-alone Visual Studio solution. All of this is possible with the new and improved SciChart WPF Examples Suite, which ships as part of the SciChart WPF SDK.
Demonstrates how to use MVVM to databind IRednerableSeriesViewModel to RenderableSeries on SciChartSurface using the new SeriesBinding Markup Extension, which forms part of the MVVM API in SciChart.
By binding SciChartSurface.RenderableSeries to a collection of BaseRenderableSeriesViewModel derived Types via the SeriesBinding Markup Extension, you can have full control over the series type, style, properties and data displayed on the chart from your ViewModel.
Check the source-code of this example for more details on how we achieve this with SciChart.
Documentation Links
– SeriesBinding Markup Extension
– List of BaseRenderableSeriesViewModel derived Types
The C#/WPF source code for the WPF Chart Series Binding with MVVM example is included below (Scroll down!).
Did you know you can also view the source code from one of the following sources as well?
<UserControl x:Class="SciChart.Examples.Examples.UseSciChartWithMvvm.SeriesBinding.SeriesBindingView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:ex="http://schemas.abtsoftware.co.uk/scichart/exampleExternals"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:s="http://schemas.abtsoftware.co.uk/scichart"
d:DesignHeight="400"
d:DesignWidth="600"
mc:Ignorable="d">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<!-- The SciChartInteractionToolbar adds zoom, pan, zoom extents and rotate functionality -->
<!-- to the chart and is included for example purposes. -->
<!-- If you wish to know how to zoom and pan a chart then do a search for Zoom Pan in the Examples suite! -->
<ex:SciChartInteractionToolbar TargetSurface="{Binding Source={x:Reference Name=SciChart}}" />
<!-- Shows the SciChartSurface and binds ZoomHistoryManager to the instance in the viewmodel -->
<!-- This is what we use to control zoom history as well as Undo Redo -->
<s:SciChartSurface x:Name="SciChart"
Grid.Column="1"
RenderableSeries="{s:SeriesBinding RenderableSeriesViewModels}">
<s:SciChartSurface.Resources>
<Style x:Key="LineSeriesStyle" TargetType="s:FastLineRenderableSeries">
<Setter Property="StrokeThickness" Value="2" />
<Setter Property="Opacity" Value="0.6" />
<Setter Property="Stroke" Value="#ff99ed3f" />
</Style>
</s:SciChartSurface.Resources>
<!-- Create an X Axis with Growby -->
<s:SciChartSurface.XAxis>
<s:NumericAxis />
</s:SciChartSurface.XAxis>
<s:SciChartSurface.YAxis>
<s:NumericAxis />
</s:SciChartSurface.YAxis>
</s:SciChartSurface>
</Grid>
</UserControl>
// *************************************************************************************
// SCICHART® Copyright SciChart Ltd. 2011-2022. All rights reserved.
//
// Web: http://www.scichart.com
// Support: support@scichart.com
// Sales: sales@scichart.com
//
// SeriesBindingView.xaml.cs is part of the SCICHART® Examples. Permission is hereby granted
// to modify, create derivative works, distribute and publish any part of this source
// code whether for commercial, private or personal use.
//
// The SCICHART® examples are distributed in the hope that they will be useful, but
// without any warranty. It is provided "AS IS" without warranty of any kind, either
// expressed or implied.
// *************************************************************************************
using System.Windows.Controls;
namespace SciChart.Examples.Examples.UseSciChartWithMvvm.SeriesBinding
{
/// <summary>
/// Interaction logic for SeriesBindingView.xaml
/// </summary>
public partial class SeriesBindingView : UserControl
{
public SeriesBindingView()
{
InitializeComponent();
}
}
}
// *************************************************************************************
// SCICHART® Copyright SciChart Ltd. 2011-2022. All rights reserved.
//
// Web: http://www.scichart.com
// Support: support@scichart.com
// Sales: sales@scichart.com
//
// SeriesBindingViewModel.cs is part of the SCICHART® Examples. Permission is hereby granted
// to modify, create derivative works, distribute and publish any part of this source
// code whether for commercial, private or personal use.
//
// The SCICHART® examples are distributed in the hope that they will be useful, but
// without any warranty. It is provided "AS IS" without warranty of any kind, either
// expressed or implied.
// *************************************************************************************
using System;
using System.Collections.Generic;
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.PointMarkers;
using SciChart.Examples.ExternalDependencies.Common;
using SciChart.Examples.ExternalDependencies.Data;
namespace SciChart.Examples.Examples.UseSciChartWithMvvm.SeriesBinding
{
public class SeriesBindingViewModel : BaseViewModel
{
private const int PointsCount = 50;
public SeriesBindingViewModel()
{
var data = DataManager.Instance.GetSinewave(1.0, 0.5, PointsCount*10, 5);
var lineDataSeries = new XyDataSeries<double, double>();
lineDataSeries.Append(data.XData.Select(d => d * 5), data.YData);
IEnumerable<BoxPoint> boxData = GetBoxPlotData().ToArray();
var boxDataSeries = new BoxPlotDataSeries<double, double>();
boxDataSeries.Append(boxData.Select(x => x.XValue),
boxData.Select(x => x.Median),
boxData.Select(x => x.Minimum),
boxData.Select(x => x.LowerQuartile),
boxData.Select(x => x.UpperQuartile),
boxData.Select(x => x.Maximum));
EllipsePointMarker epm = new EllipsePointMarker
{
Width = 9,
Height = 9,
Fill = Colors.Transparent,
Stroke = Colors.White,
StrokeThickness = 2
};
RenderableSeriesViewModels = new ObservableCollection<IRenderableSeriesViewModel>()
{
new LineRenderableSeriesViewModel {DataSeries = lineDataSeries, StyleKey = "LineSeriesStyle"},
new XyScatterRenderableSeriesViewModel { DataSeries = lineDataSeries, PointMarker = epm},
new BoxPlotRenderableSeriesViewModel{DataSeries = boxDataSeries}
};
}
public ObservableCollection<IRenderableSeriesViewModel> RenderableSeriesViewModels { get; set; }
private IEnumerable<BoxPoint> GetBoxPlotData()
{
var dates = Enumerable.Range(0, PointsCount).Select(i => i).ToArray();
var medianValues = new RandomWalkGenerator(0).GetRandomWalkSeries(PointsCount).YData;
var random = new Random(0);
for (int i = 0; i < PointsCount; i++)
{
double med = medianValues[i];
double min = med - random.NextDouble();
double max = med + random.NextDouble();
double lower = (med - min)*random.NextDouble() + min;
double upper = (max - med)*random.NextDouble() + med;
yield return new BoxPoint(dates[i], min, lower, med, upper, max);
}
}
private struct BoxPoint
{
public readonly double XValue;
public readonly double Minimum;
public readonly double LowerQuartile;
public readonly double Median;
public readonly double UpperQuartile;
public readonly double Maximum;
public BoxPoint(double xValue, double min, double lower, double med, double upper, double max)
: this()
{
XValue = xValue;
Minimum = min;
LowerQuartile = lower;
Median = med;
UpperQuartile = upper;
Maximum = max;
}
}
}
}