Search code examples
c#wpfoxyplot

Display multiple oxyplot charts c# wpf


I have two oxyplot charts (a lineseries and a rectanglebarseries) in my project. However, I can only display one of them at the same time. I know it is because of how I set the DataContext, but I do not know how to change my code so that both of the charts can be displayed at the same time. How can I achieve that?

The xaml-code of my mainpanel:

        <oxy:PlotView x:Name="Plot" Model="{Binding PlotModel}" Margin="171,648,407,0" Background="MistyRose"/>
        <oxy:PlotView x:Name ="Histogram" Model="{Binding HistogramModel}" Margin="445,304,78,459" Background="AliceBlue"/>

mainpanel.cs

...
    trendModel = new TrendModel("VariableName");
    DataContext = trendmodel;
    Histogram histogram = new Histogram(freq_List, axis_List);
    DateContext = histogram;

Parts of my cs-classes:

namespace ...
{
    public class Histogram : INotifyPropertyChanged
    {
    public Collection<Item> Items { get; set; }
    private PlotModel histogramModel;
    public PlotModel HistogramModel //{ get; set; }
    {
        get { return histogramModel; }
        set { histogramModel = value; OnPropertyChanged("HistogramModel"); }
    }

    public class Item
    {
        public string Label { get; set; }
        public double Value { get; set; }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    //NotifyPropertyChangedInvocator
    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public Histogram(List<double> frequency, List<double> axis)
    {
        CreateRectangleBar(frequency, axis);
    }

The lineseries cs:

namespace ...
{
    public class TrendModel : INotifyPropertyChanged
    {
        private PlotModel plotModel;
        public PlotModel PlotModel
    {
        get { return plotModel; }
        set { plotModel = value; OnPropertyChanged("PlotModel"); }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    //NotifyPropertyChangedInvocator
    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    //Constructor
    public TrendModel(string Name)
    {
        PlotModel = new PlotModel() { Title = Name };
        SetUpModel();
    }

Solution

  • Set the DataContext property of each PlotView:

    trendModel = new TrendModel("VariableName");
    Plot.DataContext = trendmodel;
    
    Histogram histogram = new Histogram(freq_List, axis_List);
    Histogram.DateContext = histogram;
    

    Or define the PlotModel and HistogramModel properties in the same view model class and set the DataContext property of the view to an instance of this class.