gpt4 book ai didi

c# - OxyPlot 自动缩放

转载 作者:行者123 更新时间:2023-11-30 19:39:04 27 4
gpt4 key购买 nike

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com