Search code examples
wpfxamloxyplot

How to auto scroll/pan Oxyplot after it has more than a set maximum points?


I'm using the following LinearAxis as my X-Axis, I'd like to set the minimum as 0 and maximum as 300. But in some situation, the X-Axis can go beyond 300. In that case, I want the x-axis to auto scroll.

<oxy:LinearAxis Position="Bottom" MajorGridlineStyle="Dot" Title="seconds"
                       MinWidth="100"     Minimum="0" Maximum="300"   
IsPanEnabled="True"  ></oxy:LinearAxis>

I know that if I don't set Minimum or Maximum, the axis is actually auto-scaling, but it auto-scales from the very beginning regardless of the set maximum value, which is not what I want.

Any setting should I try on the LinearAxis?


Solution

  • You can bind the ItemsSource property of the Series to ObservableCollection<DataPoint> and handle CollectionChanged event. Something like this:

    ((ObservableCollection<DataPoint>)Points1).CollectionChanged += PlotPointsChanged;
    ((ObservableCollection<DataPoint>)Points2).CollectionChanged += PlotPointsChanged;
    //...
    

    in which

     void PlotPointsChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            double max = 300;
           foreach (OxyPlot.Wpf.Series ser in oxyp.Series) 
                foreach (DataPoint dp in ser.ItemsSource)
                    if (dp.X > max)
                        max = dp.X;
            botAxis.Maximum = max;
        }
    

    please note that oxyp is the name of the Plot and botAxis is the axis you are using.

    Hope it helps.