The Chart Control can generate its child elements from their ViewModels. This is useful when the application uses the MVVM architecture and the Chart should contain several chart elements that are bound to data.
You can bind the element’s ItemsSource to the View Model’s property to generate a chart element collection from a chart’s View Model. Then specify the element’s ItemTemplate or ItemTemplateSelector to bind the element’s properties to the element view model’s properties. For example, you can bind a chart’s legend collection to the view model’s legend collection using the ChartControlBase.LegendItemsSource
and configure the generated legends using the ChartControlBase.LegendItemTemplate.
Imports System
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports System.Globalization
Imports System.IO
Imports System.Xml.Linq
Namespace MvvmSample.Model
Friend Class WeatherInfo
Private privateTimestamp As Date
Public Property Timestamp() As Date
Get
Return privateTimestamp
End Get
Private Set(ByVal value As Date)
privateTimestamp = value
End Set
End Property
Private privateTemperature As Double
Public Property Temperature() As Double
Get
Return privateTemperature
End Get
Private Set(ByVal value As Double)
privateTemperature = value
End Set
End Property
Private privatePressure As Integer
Public Property Pressure() As Integer
Get
Return privatePressure
End Get
Private Set(ByVal value As Integer)
privatePressure = value
End Set
End Property
Private privateRelativeHumidity As Integer
Public Property RelativeHumidity() As Integer
Get
Return privateRelativeHumidity
End Get
Private Set(ByVal value As Integer)
privateRelativeHumidity = value
End Set
End Property
Public Sub New(ByVal timestamp As Date, ByVal temperature As Double, ByVal pressure As Integer, ByVal relativeHumidity As Integer)
Me.Timestamp = timestamp
Me.Temperature = temperature
Me.Pressure = pressure
Me.RelativeHumidity = relativeHumidity
End Sub
End Class
Friend Interface WeatherProvider
ReadOnly Property WeatherInfos() As IEnumerable(Of WeatherInfo)
End Interface
Friend Class XmlWeatherProvider
Implements WeatherProvider
Private filename As String
Public Sub New(ByVal filename As String)
If File.Exists(filename) Then
Me.filename = filename
Else
Throw New Exception(String.Format("The '{0}' file does not exist.", filename))
End If
End Sub
Private infos As Collection(Of WeatherInfo)
Public ReadOnly Property WeatherInfos() As IEnumerable(Of WeatherInfo) Implements WeatherProvider.WeatherInfos
Get
If infos Is Nothing Then
Dim doc As XDocument = XDocument.Load(filename)
infos = New Collection(Of WeatherInfo)()
For Each element As XElement In doc.Element("WeatherData").Elements("WeatherInfo")
infos.Add(New WeatherInfo(timestamp:= Date.Parse(element.Element("Timestamp").Value, CultureInfo.InvariantCulture), temperature:= Double.Parse(element.Element("Temperature").Value, CultureInfo.InvariantCulture), pressure:= Integer.Parse(element.Element("Pressure").Value, CultureInfo.InvariantCulture), relativeHumidity:= Integer.Parse(element.Element("RelativeHumidity").Value, CultureInfo.InvariantCulture)))
Next element
End If
Return infos
End Get
End Property
End Class
End Namespace
Imports MvvmSample.ViewModel
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Threading.Tasks
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Documents
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Imaging
Imports System.Windows.Navigation
Imports System.Windows.Shapes
Namespace MvvmSample
''' <summary>
''' Interaction logic for MainWindow.xaml
''' </summary>
Partial Public Class MainWindow
Inherits Window
Public Sub New()
InitializeComponent()
End Sub
End Class
Public Class SeriesTypeTemplateSelector
Inherits DataTemplateSelector
Public Property AreaTemplate() As DataTemplate
Public Property BarTemplate() As DataTemplate
Public Property LineTemplate() As DataTemplate
Public Overrides Function SelectTemplate(ByVal item As Object, ByVal container As DependencyObject) As DataTemplate
Dim seriesVM As SeriesViewModel = TryCast(item, SeriesViewModel)
If seriesVM Is Nothing Then
Return MyBase.SelectTemplate(item, container)
End If
Select Case seriesVM.Type
Case SeriesType.Area
Return AreaTemplate
Case SeriesType.Bar
Return BarTemplate
Case SeriesType.Line
Return LineTemplate
Case Else
Return MyBase.SelectTemplate(item, container)
End Select
End Function
End Class
End Namespace
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Threading.Tasks
Namespace MvvmSample.ViewModel
Friend Class ChartViewModel
Private privatePanes As IEnumerable(Of PaneViewModel)
Public Property Panes() As IEnumerable(Of PaneViewModel)
Get
Return privatePanes
End Get
Private Set(ByVal value As IEnumerable(Of PaneViewModel))
privatePanes = value
End Set
End Property
Private privateXAxis As XAxisViewModel
Public Property XAxis() As XAxisViewModel
Get
Return privateXAxis
End Get
Private Set(ByVal value As XAxisViewModel)
privateXAxis = value
End Set
End Property
Private privateYAxes As IEnumerable(Of YAxisViewModel)
Public Property YAxes() As IEnumerable(Of YAxisViewModel)
Get
Return privateYAxes
End Get
Private Set(ByVal value As IEnumerable(Of YAxisViewModel))
privateYAxes = value
End Set
End Property
Private privateSeries As IEnumerable(Of SeriesViewModel)
Public Property Series() As IEnumerable(Of SeriesViewModel)
Get
Return privateSeries
End Get
Private Set(ByVal value As IEnumerable(Of SeriesViewModel))
privateSeries = value
End Set
End Property
Private privateLegends As IEnumerable(Of LegendViewModel)
Public Property Legends() As IEnumerable(Of LegendViewModel)
Get
Return privateLegends
End Get
Private Set(ByVal value As IEnumerable(Of LegendViewModel))
privateLegends = value
End Set
End Property
Public Sub New(ByVal series As IEnumerable(Of SeriesViewModel), ByVal legends As IEnumerable(Of LegendViewModel), ByVal panes As IEnumerable(Of PaneViewModel), ByVal xAxis As XAxisViewModel, ByVal yAxes As IEnumerable(Of YAxisViewModel))
Me.Series = series
Me.XAxis = xAxis
Me.YAxes = yAxes
Me.Panes = panes
Me.Legends = legends
End Sub
End Class
Friend Class LegendViewModel
Private privateDockTarget As PaneViewModel
Public Property DockTarget() As PaneViewModel
Get
Return privateDockTarget
End Get
Private Set(ByVal value As PaneViewModel)
privateDockTarget = value
End Set
End Property
Public Sub New(ByVal dockTarget As PaneViewModel)
Me.DockTarget = dockTarget
End Sub
End Class
Friend Class PaneViewModel
Private privateShowXAxis As Boolean
Public Property ShowXAxis() As Boolean
Get
Return privateShowXAxis
End Get
Private Set(ByVal value As Boolean)
privateShowXAxis = value
End Set
End Property
Public Sub New(ByVal showXAxis As Boolean)
Me.ShowXAxis = showXAxis
End Sub
End Class
Friend Class XAxisViewModel
Public Property MinValue() As Date
Public Property MaxValue() As Date
End Class
Friend Class YAxisViewModel
Private privateTitle As String
Public Property Title() As String
Get
Return privateTitle
End Get
Private Set(ByVal value As String)
privateTitle = value
End Set
End Property
Private privateConstantLines As IEnumerable(Of ConstantLineViewModel)
Public Property ConstantLines() As IEnumerable(Of ConstantLineViewModel)
Get
Return privateConstantLines
End Get
Private Set(ByVal value As IEnumerable(Of ConstantLineViewModel))
privateConstantLines = value
End Set
End Property
Public Sub New(ByVal title As String, ByVal constantLines As IEnumerable(Of ConstantLineViewModel))
Me.Title = title
Me.ConstantLines = constantLines
End Sub
End Class
Friend Class SeriesViewModel
Public Property Name() As String
Private privateArgumentName As String
Public Property ArgumentName() As String
Get
Return privateArgumentName
End Get
Private Set(ByVal value As String)
privateArgumentName = value
End Set
End Property
Private privateValueName As String
Public Property ValueName() As String
Get
Return privateValueName
End Get
Private Set(ByVal value As String)
privateValueName = value
End Set
End Property
Private privateLegend As LegendViewModel
Public Property Legend() As LegendViewModel
Get
Return privateLegend
End Get
Private Set(ByVal value As LegendViewModel)
privateLegend = value
End Set
End Property
Private privatePane As PaneViewModel
Public Property Pane() As PaneViewModel
Get
Return privatePane
End Get
Private Set(ByVal value As PaneViewModel)
privatePane = value
End Set
End Property
Private privateYAxis As YAxisViewModel
Public Property YAxis() As YAxisViewModel
Get
Return privateYAxis
End Get
Private Set(ByVal value As YAxisViewModel)
privateYAxis = value
End Set
End Property
Private privateType As SeriesType
Public Property Type() As SeriesType
Get
Return privateType
End Get
Private Set(ByVal value As SeriesType)
privateType = value
End Set
End Property
Public Sub New(ByVal name As String, ByVal type As SeriesType, ByVal argumentName As String, ByVal valueName As String, ByVal legend As LegendViewModel, ByVal pane As PaneViewModel, ByVal yAxis As YAxisViewModel)
Me.Name = name
Me.Type = type
Me.ArgumentName = argumentName
Me.ValueName = valueName
Me.Legend = legend
Me.Pane = pane
Me.YAxis = yAxis
End Sub
End Class
Friend Class ConstantLineViewModel
Private privateTitle As String
Public Property Title() As String
Get
Return privateTitle
End Get
Private Set(ByVal value As String)
privateTitle = value
End Set
End Property
Private privateValue As Double
Public Property Value() As Double
Get
Return privateValue
End Get
Private Set(ByVal value As Double)
privateValue = value
End Set
End Property
Public Sub New(ByVal title As String, ByVal value As Double)
Me.Title = title
Me.Value = value
End Sub
End Class
Public Enum SeriesType
Bar
Area
Line
End Enum
End Namespace
Imports MvvmSample.Model
Imports System
Imports System.Linq
Namespace MvvmSample.ViewModel
Friend Class MainViewModel
Private privateChart As ChartViewModel
Public Property Chart() As ChartViewModel
Get
Return privateChart
End Get
Private Set(ByVal value As ChartViewModel)
privateChart = value
End Set
End Property
Private privateDataProvider As WeatherProvider
Public Property DataProvider() As WeatherProvider
Get
Return privateDataProvider
End Get
Private Set(ByVal value As WeatherProvider)
privateDataProvider = value
End Set
End Property
Public Sub New()
DataProvider = New XmlWeatherProvider("Data/WeatherData.xml")
Dim temperaturePane As New PaneViewModel(showXAxis:= False)
Dim pressurePane As New PaneViewModel(showXAxis:= False)
Dim humidityPane As New PaneViewModel(showXAxis:= True)
Dim temperatureLegend As New LegendViewModel(dockTarget:= temperaturePane)
Dim pressureLegend As New LegendViewModel(dockTarget:= pressurePane)
Dim humidityLegend As New LegendViewModel(dockTarget:= humidityPane)
Dim xAxis As XAxisViewModel = New XAxisViewModel With { _
.MinValue = DataProvider.WeatherInfos.First().Timestamp, _
.MaxValue = DataProvider.WeatherInfos.ElementAt(10).Timestamp _
}
Dim temperatureYAxis As New YAxisViewModel(title:= "Temperature, F", constantLines:= Nothing)
Dim pressureYAxis As New YAxisViewModel(title:= "Pressure, mbar", constantLines:= Nothing)
Dim humidityYAxis As New YAxisViewModel(title:= "Humidity, %", constantLines:= New ConstantLineViewModel() { New ConstantLineViewModel(title:= String.Empty, value:= 60.0) })
Dim temperatureSeries As New SeriesViewModel(name:= "Temperature", type:= SeriesType.Line, argumentName:= "Timestamp", valueName:= "Temperature", legend:= temperatureLegend, pane:= temperaturePane, yAxis:= temperatureYAxis)
Dim pressureSeries As New SeriesViewModel(name:= "Pressure", type:= SeriesType.Area, argumentName:= "Timestamp", valueName:= "Pressure", legend:= pressureLegend, pane:= pressurePane, yAxis:= pressureYAxis)
Dim humiditySeries As New SeriesViewModel(name:= "Humidity", type:= SeriesType.Bar, argumentName:= "Timestamp", valueName:= "RelativeHumidity", legend:= humidityLegend, pane:= humidityPane, yAxis:= humidityYAxis)
Chart = New ChartViewModel(series:= New SeriesViewModel() { temperatureSeries, pressureSeries, humiditySeries }, panes:= New PaneViewModel() { temperaturePane, pressurePane, humidityPane }, xAxis:= xAxis, yAxes:= New YAxisViewModel() { temperatureYAxis, pressureYAxis, humidityYAxis }, legends:= New LegendViewModel() { temperatureLegend, pressureLegend, humidityLegend })
End Sub
End Class
End Namespace
<Window
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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MvvmSample"
xmlns:viewModel="clr-namespace:MvvmSample.ViewModel"
xmlns:dxc="http://schemas.devexpress.com/winfx/2008/xaml/charts" x:Class="MvvmSample.MainWindow"
mc:Ignorable="d"
Title="MainWindow" Height="405" Width="720">
<Window.DataContext>
<viewModel:MainViewModel/>
</Window.DataContext>
<Grid>
<dxc:ChartControl DataSource="{Binding DataProvider.WeatherInfos}" LegendItemsSource="{Binding Chart.Legends}">
<dxc:ChartControl.CrosshairOptions>
<dxc:CrosshairOptions ShowOnlyInFocusedPane="False" GroupHeaderPattern="{}{A:d MMMM, hh:mm}"/>
</dxc:ChartControl.CrosshairOptions>
<dxc:ChartControl.Titles>
<dxc:Title Content="Weather in London" HorizontalAlignment="Center"/>
</dxc:ChartControl.Titles>
<dxc:XYDiagram2D SeriesItemsSource="{Binding Chart.Series}" SecondaryAxisYItemsSource="{Binding Chart.YAxes}" PaneItemsSource="{Binding Chart.Panes}"
EnableAxisXNavigation="True">
<dxc:XYDiagram2D.DefaultPane>
<dxc:Pane Visibility="Collapsed"/>
</dxc:XYDiagram2D.DefaultPane>
<dxc:XYDiagram2D.AxisX>
<dxc:AxisX2D VisibilityInPaneItemsSource="{Binding Chart.Panes}" GridLinesMinorVisible="True" GridLinesVisible="True" Interlaced="True">
<dxc:AxisX2D.VisualRange>
<dxc:Range MinValue="{Binding Chart.XAxis.MinValue}" MaxValue="{Binding Chart.XAxis.MaxValue}"/>
</dxc:AxisX2D.VisualRange>
<dxc:AxisX2D.DateTimeScaleOptions>
<dxc:ManualDateTimeScaleOptions MeasureUnit="Hour" GridAlignment="Hour"/>
</dxc:AxisX2D.DateTimeScaleOptions>
<dxc:AxisX2D.VisibilityInPaneItemTemplate>
<DataTemplate>
<ContentControl>
<dxc:VisibilityInPane Pane="{Binding}" Visible="{Binding ShowXAxis}"/>
</ContentControl>
</DataTemplate>
</dxc:AxisX2D.VisibilityInPaneItemTemplate>
</dxc:AxisX2D>
</dxc:XYDiagram2D.AxisX>
<dxc:XYDiagram2D.AxisY>
<dxc:AxisY2D Visible="False" GridLinesVisible="False" GridLinesMinorVisible="False" Interlaced="False"/>
</dxc:XYDiagram2D.AxisY>
<dxc:XYDiagram2D.SeriesItemTemplateSelector>
<local:SeriesTypeTemplateSelector>
<local:SeriesTypeTemplateSelector.AreaTemplate>
<DataTemplate>
<dxc:AreaSeries2D DisplayName="{Binding Name}"
ArgumentDataMember="{Binding ArgumentName}" ValueDataMember="{Binding ValueName}"
Legend="{Binding Legend}" Pane="{Binding Pane}" AxisY="{Binding YAxis}"
CrosshairLabelPattern="{}{V}"/>
</DataTemplate>
</local:SeriesTypeTemplateSelector.AreaTemplate>
<local:SeriesTypeTemplateSelector.BarTemplate>
<DataTemplate>
<dxc:BarSideBySideSeries2D DisplayName="{Binding Name}"
ArgumentDataMember="{Binding ArgumentName}" ValueDataMember="{Binding ValueName}"
Legend="{Binding Legend}" Pane="{Binding Pane}" AxisY="{Binding YAxis}"
CrosshairLabelPattern="{}{V}" BarWidth="2"/>
</DataTemplate>
</local:SeriesTypeTemplateSelector.BarTemplate>
<local:SeriesTypeTemplateSelector.LineTemplate>
<DataTemplate>
<dxc:LineSeries2D DisplayName="{Binding Name}"
ArgumentDataMember="{Binding ArgumentName}" ValueDataMember="{Binding ValueName}"
Legend="{Binding Legend}" Pane="{Binding Pane}" AxisY="{Binding YAxis}"
CrosshairLabelPattern="{}{V}"/>
</DataTemplate>
</local:SeriesTypeTemplateSelector.LineTemplate>
</local:SeriesTypeTemplateSelector>
</dxc:XYDiagram2D.SeriesItemTemplateSelector>
<dxc:XYDiagram2D.PaneItemTemplate>
<DataTemplate>
<dxc:Pane>
<dxc:Pane.AxisXScrollBarOptions>
<dxc:ScrollBarOptions Visible="{Binding ShowXAxis}"/>
</dxc:Pane.AxisXScrollBarOptions>
</dxc:Pane>
</DataTemplate>
</dxc:XYDiagram2D.PaneItemTemplate>
<dxc:XYDiagram2D.SecondaryAxisYItemTemplate>
<DataTemplate>
<dxc:SecondaryAxisY2D Alignment="Near" GridLinesMinorVisible="True" GridLinesVisible="True"
ConstantLineInFrontItemsSource="{Binding ConstantLines}">
<dxc:SecondaryAxisY2D.WholeRange>
<dxc:Range dxc:AxisY2D.AlwaysShowZeroLevel="False"/>
</dxc:SecondaryAxisY2D.WholeRange>
<dxc:SecondaryAxisY2D.Title>
<dxc:AxisTitle Content="{Binding Title}"/>
</dxc:SecondaryAxisY2D.Title>
<dxc:SecondaryAxisY2D.ConstantLineInFrontItemTemplate>
<DataTemplate>
<dxc:ConstantLine Title="{Binding Title}" Value="{Binding Value}"/>
</DataTemplate>
</dxc:SecondaryAxisY2D.ConstantLineInFrontItemTemplate>
</dxc:SecondaryAxisY2D>
</DataTemplate>
</dxc:XYDiagram2D.SecondaryAxisYItemTemplate>
</dxc:XYDiagram2D>
<dxc:ChartControl.LegendItemTemplate>
<DataTemplate>
<dxc:Legend DockTarget="{Binding DockTarget}" HorizontalPosition="Left" VerticalPosition="Top"/>
</DataTemplate>
</dxc:ChartControl.LegendItemTemplate>
</dxc:ChartControl>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MvvmSample.ViewModel {
class ChartViewModel {
public IEnumerable<PaneViewModel> Panes { get; private set; }
public XAxisViewModel XAxis { get; private set; }
public IEnumerable<YAxisViewModel> YAxes { get; private set; }
public IEnumerable<SeriesViewModel> Series { get; private set; }
public IEnumerable<LegendViewModel> Legends { get; private set; }
public ChartViewModel (IEnumerable<SeriesViewModel> series, IEnumerable<LegendViewModel> legends, IEnumerable<PaneViewModel> panes, XAxisViewModel xAxis, IEnumerable<YAxisViewModel> yAxes) {
this.Series = series;
this.XAxis = xAxis;
this.YAxes = yAxes;
this.Panes = panes;
this.Legends = legends;
}
}
class LegendViewModel {
public PaneViewModel DockTarget { get; private set; }
public LegendViewModel(PaneViewModel dockTarget) {
this.DockTarget = dockTarget;
}
}
class PaneViewModel {
public bool ShowXAxis { get; private set; }
public PaneViewModel(bool showXAxis) {
this.ShowXAxis = showXAxis;
}
}
class XAxisViewModel {
public DateTime MinValue { get; set; }
public DateTime MaxValue { get; set; }
}
class YAxisViewModel {
public string Title { get; private set; }
public IEnumerable<ConstantLineViewModel> ConstantLines { get; private set; }
public YAxisViewModel(string title, IEnumerable<ConstantLineViewModel> constantLines) {
this.Title = title;
this.ConstantLines = constantLines;
}
}
class SeriesViewModel {
public string Name { get; set; }
public string ArgumentName { get; private set; }
public string ValueName { get; private set; }
public LegendViewModel Legend { get; private set; }
public PaneViewModel Pane { get; private set; }
public YAxisViewModel YAxis { get; private set; }
public SeriesType Type { get; private set; }
public SeriesViewModel(string name, SeriesType type, string argumentName, string valueName, LegendViewModel legend, PaneViewModel pane, YAxisViewModel yAxis) {
this.Name = name;
this.Type = type;
this.ArgumentName = argumentName;
this.ValueName = valueName;
this.Legend = legend;
this.Pane = pane;
this.YAxis = yAxis;
}
}
class ConstantLineViewModel {
public string Title { get; private set; }
public double Value { get; private set; }
public ConstantLineViewModel(string title, double value) {
this.Title = title;
this.Value = value;
}
}
public enum SeriesType {
Bar, Area, Line
}
}
using MvvmSample.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace MvvmSample {
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
}
}
public class SeriesTypeTemplateSelector: DataTemplateSelector {
public DataTemplate AreaTemplate { get; set; }
public DataTemplate BarTemplate { get; set; }
public DataTemplate LineTemplate { get; set; }
public override DataTemplate SelectTemplate(object item, DependencyObject container) {
SeriesViewModel seriesVM = item as SeriesViewModel;
if(seriesVM == null) return base.SelectTemplate(item, container);
switch (seriesVM.Type) {
case SeriesType.Area: return AreaTemplate;
case SeriesType.Bar: return BarTemplate;
case SeriesType.Line: return LineTemplate;
default: return base.SelectTemplate(item, container);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.IO;
using System.Xml.Linq;
namespace MvvmSample.Model {
class WeatherInfo {
public DateTime Timestamp { get; private set; }
public double Temperature { get; private set; }
public int Pressure { get; private set; }
public int RelativeHumidity { get; private set; }
public WeatherInfo(
DateTime timestamp,
double temperature,
int pressure,
int relativeHumidity
) {
this.Timestamp = timestamp;
this.Temperature = temperature;
this.Pressure = pressure;
this.RelativeHumidity = relativeHumidity;
}
}
interface WeatherProvider {
IEnumerable<WeatherInfo> WeatherInfos { get; }
}
class XmlWeatherProvider : WeatherProvider {
string filename;
public XmlWeatherProvider(string filename) {
if (File.Exists(filename)) {
this.filename = filename;
}
else {
throw new Exception(String.Format("The \'{0}\' file does not exist.", filename));
}
}
Collection<WeatherInfo> infos;
public IEnumerable<WeatherInfo> WeatherInfos { get {
if(infos == null) {
XDocument doc = XDocument.Load(filename);
infos = new Collection<WeatherInfo>();
foreach (XElement element in doc.Element("WeatherData").Elements("WeatherInfo")) {
infos.Add(new WeatherInfo(
timestamp: DateTime.Parse(element.Element("Timestamp").Value, CultureInfo.InvariantCulture),
temperature: double.Parse(element.Element("Temperature").Value, CultureInfo.InvariantCulture),
pressure: int.Parse(element.Element("Pressure").Value, CultureInfo.InvariantCulture),
relativeHumidity: int.Parse(element.Element("RelativeHumidity").Value, CultureInfo.InvariantCulture)
));
}
}
return infos;
}}
}
}
using MvvmSample.Model;
using System;
using System.Linq;
namespace MvvmSample.ViewModel {
class MainViewModel {
public ChartViewModel Chart { get; private set; }
public WeatherProvider DataProvider { get; private set; }
public MainViewModel() {
DataProvider = new XmlWeatherProvider("Data/WeatherData.xml");
PaneViewModel temperaturePane = new PaneViewModel(
showXAxis: false
);
PaneViewModel pressurePane = new PaneViewModel(
showXAxis: false
);
PaneViewModel humidityPane = new PaneViewModel(
showXAxis: true
);
LegendViewModel temperatureLegend = new LegendViewModel(dockTarget: temperaturePane);
LegendViewModel pressureLegend = new LegendViewModel(dockTarget: pressurePane);
LegendViewModel humidityLegend = new LegendViewModel(dockTarget: humidityPane);
XAxisViewModel xAxis = new XAxisViewModel {
MinValue = DataProvider.WeatherInfos.First().Timestamp,
MaxValue = DataProvider.WeatherInfos.ElementAt(10).Timestamp
};
YAxisViewModel temperatureYAxis = new YAxisViewModel(
title: "Temperature, F",
constantLines: null
);
YAxisViewModel pressureYAxis = new YAxisViewModel(
title: "Pressure, mbar",
constantLines: null
);
YAxisViewModel humidityYAxis = new YAxisViewModel(
title: "Humidity, %",
constantLines: new ConstantLineViewModel[] {
new ConstantLineViewModel(
title: String.Empty,
value: 60.0)
}
);
SeriesViewModel temperatureSeries = new SeriesViewModel(
name: "Temperature",
type: SeriesType.Line,
argumentName: "Timestamp",
valueName: "Temperature",
legend: temperatureLegend,
pane: temperaturePane,
yAxis: temperatureYAxis
);
SeriesViewModel pressureSeries = new SeriesViewModel(
name: "Pressure",
type: SeriesType.Area,
argumentName: "Timestamp",
valueName: "Pressure",
legend: pressureLegend,
pane: pressurePane,
yAxis: pressureYAxis
);
SeriesViewModel humiditySeries = new SeriesViewModel(
name: "Humidity",
type: SeriesType.Bar,
argumentName: "Timestamp",
valueName: "RelativeHumidity",
legend: humidityLegend,
pane: humidityPane,
yAxis: humidityYAxis);
Chart = new ChartViewModel(
series: new SeriesViewModel[] { temperatureSeries, pressureSeries, humiditySeries },
panes: new PaneViewModel[] { temperaturePane, pressurePane, humidityPane },
xAxis: xAxis,
yAxes: new YAxisViewModel[] { temperatureYAxis, pressureYAxis, humidityYAxis },
legends: new LegendViewModel[] { temperatureLegend, pressureLegend, humidityLegend }
);
}
}
}