c# - OxyPlot 自动缩放

标签 c# wpf oxyplot

AutoScale OxyPlot 图表。 例如我有这样的东西。

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 代码看起来像

<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>

如何实现拖动后Y轴的自动缩放?例如,当我在窗口中拖动图表时,只出现一条线 ((1,2),(4,4))。 Y 轴将显示从 2 到 4。谢谢。

最佳答案

如果您只想重置 Y 轴,则需要先为 Y 轴设置一个键。

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

然后您可以使用 LINQ 获取对它的引用。在拖动事件后调用以下命令。

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

如果你想重置 X 轴和 Y 轴,你可以调用

PlotModel.ResetAllAxes();

关于c# - OxyPlot 自动缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29608087/

相关文章:

wpf - 覆盖切换按钮样式

.net - 如何使用 DataTrigger 覆盖绑定(bind)属性

c# - 如何安装 OxyPlot NuGet 包?

c# - 隐藏轴,同时仍然能够缩放和平移

c# - Oxyplot:为 Oxyplot 的热图设置边距/填充

c# - 在构造函数中使用 'this' 关键字

c# - WPF 将第三方 ResourceDictionary 添加到 App.xaml?

wpf - 在 WPF 中设置数据网格的可见性

c# - 如何使用 XNA 加载文件夹中的所有文件?

c# - WSHttpBinding TransportWithMessageCredential SecurityMode 改变请求