oxyplot icon indicating copy to clipboard operation
oxyplot copied to clipboard

Synchronize two or more `PlotView`s

Open ghost opened this issue 7 years ago • 3 comments

Hello,

Is there a way to synchronize two or more PlotViews The PlotViews have the same axis height and axis width.

All plots have a different plot model, but with the same count of points.

I mean, when the zoom or the scale of one plot has changed, so I need to change the zoom and the scale of the other plots.

best wishes

ghost avatar Jul 16 '19 12:07 ghost

The simple thing to do is to disable the default zooming behavior and synchronize the Minimum and Maximum properties of the corresponding Axis objects. (Preferably through something like data binding; you don't say what platform you're on.)

If you want to keep the default zooming behavior I think you're in for a much harder time. That's because the default zooming does not alter Axis.Minimum and Axis.Maximum, but rather uses the protected Axis.ViewMinimum and Axis.ViewMaximum properties. You can listen for the Axis.AxisChanged event to know when these change and use reflection to get their values, but I don't think it's for the feint of heart.

eaglemc avatar Jul 21 '19 19:07 eaglemc

I actually do this in my own code by subscribing to Axis.AxisChanged for both X and Y, as suggested by @eaglemc and then applying the current axis range to the other charts using the ActualMinimum and ActualMaximum values (which probably correlate to the ViewMin/ViewMax suggested above)

axis.Zoom(otherAxis.ActualMinimum, otherAxis.ActualMaximum);

danpaul88 avatar Jul 22 '19 12:07 danpaul88

Works perfect 👍

Sample code

var currentPlotModel = new PlotModel();
var otherPlotModel   = new PlotModel();
var currentAxis      = new DateTimeAxis();
var otherAxis        = new DateTimeAxis();

currentPlotModel.Axes.Add(currentAxis);
otherPlotModel.Axes.Add(otherAxis);

currentAxis.AxisChanged += AxisChanged;

void AxisChanged(object? sender, AxisChangedEventArgs e)
{
    otherAxis.Zoom(currentAxis.ActualMinimum, currentAxis.ActualMaximum);

    otherPlotModel.InvalidatePlot(false);
}

ghost avatar Jul 31 '19 08:07 ghost