我正在使用 oxyplot 库来创建时间序列图表。我正在尝试创建一个两侧具有相同 y 轴的绘图,只要我不与绘图交互进行缩放或平移,它就可以正常工作,这是我的代码
model.Axes.Add(new LinearAxis()
{
MajorStep = 0.1,
Minimum = 0,
Maximum = 1,
Position = AxisPosition.Left,
Title = "Title Axis",
AxisTitleDistance = 10
});
model.Axes.Add(new LinearAxis()
{
Key = "RightYAxis"
MajorStep = 0.1,
Minimum = 0,
Maximum = 1,
Position = AxisPosition.Right
});
DateTimeAxis dta = new DateTimeAxis()
{
Key = "DateTimeAxis",
IntervalType = SetDateTimeIntervalType(minDate, maxDate),
StringFormat = "dd/MM/yyyy",
Position = AxisPosition.Bottom,
Minimum = DateTimeAxis.ToDouble(minDate),
Maximum = DateTimeAxis.ToDouble(maxDate),
Title = "Date",
AxisTitleDistance = 10
};
dta.AxisChanged += new EventHandler<AxisChangedEventArgs>(AxisChanged);
model.Axes.Add(dta);
当我与绘图交互时,只有左侧 y 轴和底部轴适当缩放。当我在左 y 轴之前添加右 y 轴时,底部和右侧轴正在缩放。
我需要设置什么设置,才能相应地缩放两个 y 轴而不只是一个?
我认为没有内置的设置。我过去也遇到过类似的问题,也曾寻找过这样的功能,但没有找到。 OxyPlot 演示浏览器中也没有这样的示例。直接但不是很优雅的方法是将事件处理程序附加到左轴,将最小值和最大值设置为右轴。您的示例将如下所示:
Axis leftAxis = new LinearAxis()
{
MajorStep = 0.1,
Minimum = 0,
Maximum = 1,
Position = AxisPosition.Left,
Title = "Title Axis",
AxisTitleDistance = 10
});
leftAxis.AxisChanged += LeftAxis_AxisChanged;
model.Axes.Add(leftAxis);
model.Axes.Add(new LinearAxis()
{
Key = "RightYAxis"
MajorStep = 0.1,
Minimum = 0,
Maximum = 1,
Position = AxisPosition.Right
});
DateTimeAxis dta = new DateTimeAxis()
{
Key = "DateTimeAxis",
IntervalType = SetDateTimeIntervalType(minDate, maxDate),
StringFormat = "dd/MM/yyyy",
Position = AxisPosition.Bottom,
Minimum = DateTimeAxis.ToDouble(minDate),
Maximum = DateTimeAxis.ToDouble(maxDate),
Title = "Date",
AxisTitleDistance = 10
};
dta.AxisChanged += new EventHandler<AxisChangedEventArgs>(AxisChanged);
model.Axes.Add(dta);
事件处理程序看起来像
private void LeftAxis_AxisChanged(object sender, AxisChangedEventArgs e)
{
Axis leftAxis = sender as Axis;
Axis rightAxis = leftAxis.PlotModel.Axes.First(a => a.Position == AxisPosition.Right);
rightAxis.Maximum = leftAxis.ActualMaximum;
rightAxis.Minimum = leftAxis.ActualMinimum;
}
为了补充上述讨论,我刚刚在应用程序中向 OxyPlot 添加了一个辅助轴,并且缩放和平移“正常工作”,无需任何额外的代码或事件处理程序。我正在使用 OxyPlot.Core 和 OxyPlot.Wpf 2.1.2 版本。