Multiple LineSeries Binding in OxyPlot

Sturm picture Sturm · Jun 19, 2013 · Viewed 10.8k times · Source

Is it possible to bind a plot to a collection of LineSeries instead of a single LineSeries in OxyPlot? (and not through the Model).

I'm looking for something like this:

<oxy:Plot>        
    <oxy:Plot.Series>     
        <oxy:LineSeries ItemsSource="{Binding myCollectionOfLineSeries}" />              
    </oxy:Plot.Series>
</oxy:Plot>

Where myCollectionOfLineSeries is:

private ObservableCollection<LineSeries> _myCollectionOfLineSeries ;
        public ObservableCollection<LineSeries> myCollectionOfLineSeries 
        {
            get
            {
                return _myCollectionOfLineSeries ;
            }
            set
            {
                _myCollectionOfLineSeries = value;
                OnPropertyChanged("myCollectionOfLineSeries ");

            }
        }

I expect as answer: a) "No, it's impossible" or b) "Yes, just put XYZ before IJK".

Thanks for reading.

Answer

Karl Cheng picture Karl Cheng · Mar 6, 2014

It may be a bit late, but recently I have the same question: I need to plot multiple series dynamically (several yield curves based on user selected currencies) but I don't want to directly bind the Plot using PlotModel, as other properties (e.g. Title) need to be set in my View Model as code instead of XAML markup.

So I defined the PlotModel as resource, binding it to the Plot. And look up the PlotModel when the view is loaded. By this approach, I can define visual stuffs (e.g. Title, Axes, Legend, etc) by XAML markup, while putting logic to generate series in my view model code.

Not sure if it's a good way, but it solves my problem.

1) XAML - define resource

<UserControl.Resources>
    <oxyPlot:PlotModel 
        x:Key="TestPlotModel"
        Title="XXX Curves (Preview)"
        Subtitle="Scroll mousewheel to zoom; Right-drag to pan"
        LegendPlacement="Outside"
        LegendBorder="{x:Static Member=oxyPlot:OxyColors.Black}"
        >
        <oxyPlot:PlotModel.Axes>
            <axes:LinearAxis 
                Title="Rate (%)" 
                Position="Left" 
                StartPosition="0" 
                StringFormat="#.00000"
                MajorGridlineStyle="Solid"
                MajorGridlineColor="{x:Static Member=oxyPlot:OxyColors.LightGray}"
                MinorGridlineStyle="Dot"
                MinorGridlineColor="{x:Static Member=oxyPlot:OxyColors.LightGray}"
                >
            </axes:LinearAxis>
            <axes:LinearAxis 
                Title="Maturity (Days)" 
                Position="Bottom" 
                StartPosition="0"
                MajorGridlineStyle="Solid"
                MajorGridlineColor="{x:Static Member=oxyPlot:OxyColors.LightGray}"
                >
            </axes:LinearAxis>
        </oxyPlot:PlotModel.Axes>
    </oxyPlot:PlotModel>
</UserControl.Resources>

2) XAML - Plot

<oxy:Plot Grid.Row="1" Model="{Binding Source={StaticResource TestPlotModel}}">
</oxy:Plot>

3) View model - get the model from view but not binding

protected override void OnViewLoaded(object view)
{
    base.OnViewLoaded(view);
    this._model = (PlotModel)((XXXView)view).FindResource("TestPlotModel");
}

4) View model - generate multiple series

_model.Series.Clear();
foreach (var currency in distinctCurrencies)
{
    IEnumerable<DataPoint> dataPoints = ...;

    LineSeries lineSeries = new LineSeries()
    {
        Title = String.Format("*{0}", currency),
        ItemsSource = dataPoints
    };

    _model.Series.Add(lineSeries);
}

hope it helps!