OxyPlot AutoScale

A191919 picture A191919 · Apr 13, 2015 · Viewed 18.8k times · Source

AutoScale OxyPlot Chart. For example i have something like this.

using OxyPlot;
using OxyPlot.Axes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using OxyPlot.Wpf;
using PlotControllerTest.Properties;
using System.Diagnostics;
namespace PlotControllerTest
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
/// 
public class Chart
{
    public PlotController myController { get; set; }
    private OxyPlot.Series.LineSeries LS;
    OxyPlot.Axes.LinearAxis LAY;
    OxyPlot.Axes.LinearAxis LAX;
    private int i = 0;
    public PlotModel PlotModel {get;set;}
    public Chart()
    {

        PlotModel = new PlotModel();
        myController = new PlotController();
        myController.UnbindAll();
        myController.BindMouseDown(OxyMouseButton.Left, OxyPlot.PlotCommands.PanAt);
        LS = new OxyPlot.Series.LineSeries();
        LAY = new OxyPlot.Axes.LinearAxis()
        {
            Position = OxyPlot.Axes.AxisPosition.Left,
            AbsoluteMaximum = 100,
            AbsoluteMinimum = 1,
        };
        LAX = new OxyPlot.Axes.LinearAxis()
        {
            Position = OxyPlot.Axes.AxisPosition.Bottom,
            AbsoluteMaximum = 200,
            AbsoluteMinimum = 1,
            MinorStep=5,
        };
        PlotModel.Series.Add(LS);
        PlotModel.Axes.Add(LAY);
        PlotModel.Axes.Add(LAX);
    }
    public void BeginAddPoints()
    {
        Random rnd = new Random();
        do
        {
            int temp=rnd.Next(1, 100);
            LS.Points.Add(new DataPoint( ++i,temp));
            System.Threading.Thread.Sleep(100);

            Update();
        } while (i<30);
        Update();
    }
    public void Update()
    {
        PlotModel.InvalidatePlot(true);
    }
}
public partial class MainWindow : Window
{
    Chart TChart;
    delegate void BeginUpdate();
    private Stopwatch stopwatch = new Stopwatch();
    private long lastUpdateMilliSeconds;
    public MainWindow()
    {

        TChart = new Chart();
        BeginUpdate BU = new BeginUpdate(TChart.BeginAddPoints);
        IAsyncResult result = BU.BeginInvoke(null,null);
        DataContext = TChart;
        CompositionTarget.Rendering += CompositionTargetRendering;
        InitializeComponent();
    }
    private void CompositionTargetRendering(object sender, EventArgs e)
    {
        if (stopwatch.ElapsedMilliseconds > lastUpdateMilliSeconds + 300)
        {
            TChart.Update();
        }
    }
}

}

Xaml code look's like

<Window x:Class="PlotControllerTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:oxy="http://oxyplot.org/wpf"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <oxy:PlotView Model="{Binding PlotModel}" DefaultTrackerTemplate="{x:Null}" Controller="{Binding myController}"></oxy:PlotView>
</Grid>

How to implement autoscale of Y Axes after dragging? For example when i drag chart in window, and there appears only one Line ((1,2),(4,4)). Y axis will show from 2 to 4.Thanks.

Answer

Kurtis Aung picture Kurtis Aung · Apr 16, 2015

If you would like to reset just the Y Axis, you would need to set a Key to your Y-Axis first.

 LAX = new OxyPlot.Axes.LinearAxis()
    {
        Key = "YAxis",
        Position = OxyPlot.Axes.AxisPosition.Bottom,
        AbsoluteMaximum = 200,
        AbsoluteMinimum = 1,
        MinorStep=5,
    };

Then you could get a reference to it by using LINQ. Call the following after your dragging event.

 Axis yAxis = PlotModel.Axes.FirstOrDefault(s => s.Key == "YAxis");
 yAxis.Reset();

If you would like to reset both the X and Y Axis then, you could just call

PlotModel.ResetAllAxes();