Multiple LineSeries Binding in OxyPlot
Asked Answered
F

2

9

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.

Fancy answered 19/6, 2013 at 18:43 Comment(0)
O
8

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!

Orbiculate answered 6/3, 2014 at 11:17 Comment(1)
This is not the solution. You should move PlotModel into your ViewModel and add multiple series in your viewmodel. PlotModel must be bound from ViewModel. This is no way to go : this._model = (PlotModel)((XXXView)view).FindResource("TestPlotModel");Sleepless
B
1

Yes, have a look at their examples, you need to bind to a collection of DataPoint

public ObservableCollection<DataPoint> MyCollection { ... }

and

<oxy:Plot.Series>
    <oxy:LineSeries ItemsSource="{Binding MyCollection}" DataFieldX="X" DataFieldY="Y"/>
</oxy:Plot.Series>

The Series property on the Plot type has no setter:

public Collection<Series> Series
{
    get
    {
        return this.series;
    }
}

You can though bind to the Model property, which is a PlotModel type which has a Series collection property with a getter and setter. Have a look at the SimpleDemo for more details.

<oxy:Plot Model="{Binding MyModel}" ...
Bocage answered 19/6, 2013 at 18:50 Comment(4)
I'm not sure if I understand you! What you are suggesting in your answer is the same as binding a single LineSeries, from my point of view. I wonder if it possible to bind to a collection of LineSeries or, in this case, a collection of collections of DataPoint. Thank youFancy
"The property "Series" does not have an accessible setter." it says,I've tried several way of binding "Series" but no success so far. <oxy:Plot.Series ItemsSource="{Binding ...}"/> doesn't work either.Fancy
objo (creator of OxyPlot): "No, this is not possible. You need one LineSeries for each line you want to show. Create a PlotModel and bind the Model property or use code-behind to manipulate the Series collection more dynamically. I am sure there are also other ways to do it in XAML only (maybe using an attached property??)". Thanks, finally I'm doing it with the Model as you suggested.Fancy
19.3.2017: Model is no longer property of <oxy:Plot /> but <oxy:PlotView />Pasteurism

© 2022 - 2025 — McMap. All rights reserved.