I'm looking to build a Scatter plot with a best fit line using Live Charts.
Now the way I've been doing this is by having a SeriesCollection
on the main view model like so, within the class of this interface I manually add the BubbleSeries
and the LineSeries
for the chart:
public interface IPointAnalysisViewModel : IAnalysisViewModel
{
SeriesCollection Series
{
get;
}
}
PointAnalysisViewModel:
foreach (var pointSliceViewModel in this.slices)
{
this.series.Add(pointSliceViewModel.Series);
}
this.bestFitLineSeries = this.BuildBestFitLine(pointAnalysisModel.BestFitValues);
this.series.Add(this.bestFitLineSeries);
This series is then bound within xaml to the Series
of the CartesianChart
as follows:
<wpf:CartesianChart Grid.Column="1"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Series="{Binding Path=Series}" >
... UI Fluff
</wpf:CartesianChart>
Pretty basic as you'd expect, but this means that in my ViewModels for the series I then have know that the view will display a bubble series for me as follow:
public interface IPointSliceViewModel : IViewModel
{
IBubbleSeriesView Series
{
get;
}
bool Show
{
get;
set;
}
}
To me this looks badly like some code 'smell' in that I need to know view specific objects within my View Model.
The only way that I can think that would allow me to separate the concerns of the View and the ViewModel is if I were able to provide the CartesianChart
with the data templates for the Series
and bind the Series
to a collection of IPointSliceViewModels
instead as follows:
<wpf:CartesianChart Grid.Column="1"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
SeriesSource="{Binding Path=Series}">
<wpf:CartesianChart.SeriesTemplates>
<DataTemplate DataType={x:Type local:PointSliceViewModel}>
<wpf:BubbleSeries .../>
</DataTemplate>
</wpf:CartesianChart.SeriesTemplates>
... UI Fluff
</wpf:CartesianChart>
Which would simply allow me to have:
public interface IPointAnalysisViewModel : IAnalysisViewModel
{
ObservableCollection<IPointSliceViewModel> Series
{
get;
}
}
and:
public interface IPointSliceViewModel : IViewModel
{
ChartValues<ObservablePoint> Values
{
get;
}
bool Show
{
get;
set;
}
}
Is it currently possible to provide the DataTemplates
for the Series
or do I have to manually do this in the code behind at the moment?
Or is there a different way of having potentially limitless numbers of series in a chart without having to define each of them in the View and without having to keep track of the view specific details in the VM?