Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple LineSeries Binding in OxyPlot

Tags:

c#

plot

wpf

oxyplot

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.

like image 348
Sturm Avatar asked Jun 19 '13 18:06

Sturm


2 Answers

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!

like image 152
Karl Cheng Avatar answered Oct 21 '22 13:10

Karl Cheng


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}" ...
like image 38
devdigital Avatar answered Oct 21 '22 13:10

devdigital