4

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?

Stephen Ross
  • 882
  • 6
  • 21
  • You can't set a DataTemplate to a series, are you trying to show custom shapes as points in a bubble series? what are you trying to do, that forces you to try to pass a DataTemplate to a series? – bto.rdz Aug 05 '16 at 18:25
  • I'm trying to add multiple series to the same `CartesianChart`, as I'd like to be able to show and hide each of the bubble points and then I'd also like to have a best fit line. The only way that I could do this was to control the series in each of my ViewModels but I feel it would be better to provide a data template for a `BubbleSeries` so that I can simply provide the values in the ViewModel, and bind the series in the `CartesianChart` to an array of the ViewModels. – Stephen Ross Aug 11 '16 at 17:42
  • You could also add many bubble series, even with only one point, then only change the visibility property of each series to control which points are visible – bto.rdz Aug 18 '16 at 00:12
  • 1
    Currently this is what I am doing, I store the bubble series in the `IPointSliceViewModel` and in the `IPointAnalysisViewModel` concrete I bring all the bubble series together and add them to the `SeriesCollection`. But I feel that this is more complicated than it has to be as I'll have to know specifics (and controls specifics) of the view in the view model. – Stephen Ross Aug 22 '16 at 08:15

0 Answers0