Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
Custom zoom functionality

Hi, so I am trying to create a zoom functionality for the XamDataChart using the zoom bars like the one in Python:

  • moving the mouse right, the chart zooms in horizontally (with mouse start position as zoom focus point)
  • moving the mouse left, the chart zooms out horizontally (with mouse start position as zoom focus point)

On the mouse_down event I have saved the starting X,Y of the mouse and the on the mouse_move I have this: 

if (e.RightButton == MouseButtonState.Pressed && Keyboard.Modifiers == ModifierKeys.Shift)
long deltaMouse;
GetXMousePositionOnChart(out var xpos);
GetYMousePositionOnChart(out var ypos);
var xMaxViewPort = xmDataChart.ViewportRect.Right;
var yMaxViewPort = xmDataChart.ViewportRect.Bottom;
var xAxis = xmDataChart.Axes[0];
var yAxis = xmDataChart.Axes[1];
var winRectContext = new Rect(0, 0, 1, 1);
var xScalerParams = new ScalerParams(winRectContext, xmDataChart.ViewportRect, xAxis.IsInverted);
var yScalerParams = new ScalerParams(winRectContext, xmDataChart.ViewportRect, yAxis.IsInverted);
if (xpos.Ticks > vm.XStartPosition)
deltaMouse = xpos.Ticks - vm.XStartPosition;

deltaMouse = vm.XStartPosition - xpos.Ticks;

var maxRange = vm.MaxTicks;
var chartCenter = (xmNumericSlider.MinValue + xmNumericSlider.MaxValue) / 2;
var deltaRangeMax = (deltaMouse * 0.05 * maxRange) + (vm.XStartPosition - chartCenter);
var deltaRangeMin = (deltaMouse * 0.05 * maxRange) - (vm.XStartPosition - chartCenter);

if (vm.XStartPosition != xpos.Ticks)
if (!xMaxViewPort.Equals(0))
xmDataChart.HorizontalZoombar.Range = new Range
Minimum = xAxis.GetScaledValue(deltaRangeMin, xScalerParams) / xMaxViewPort,
Maximum = xAxis.GetScaledValue(deltaRangeMax, xScalerParams) / xMaxViewPort

But the zoombars go crazy, what am I doing wrong?

  • 34430
    Offline posted

    Hello rdbogdan,

    I have been investigating into the behavior you are seeing, and it appears that there is some information that I am missing from the code-snippet you have provided. The pieces I am missing include that I am unsure what you are doing with the GetX/Y MousePositionOnChart methods, what vm.MaxTicks is, and what the values of the xmNumericSlider is in this case.

    With the above said, if the zoom bar is going crazy here, it is probably due to the calculation not being completely correct, but I am unsure of which piece is incorrect here. I would recommend that you print out the values that you are setting to the Minimum / Maximum range to the Output window in Visual Studio in this case so that you can examine them as they are being set – they should be between 0 and 1.

    My best guess in this case is that since the range is being set on mouse move, you may be hitting a behavior where the zoom of the chart is making the scaled value fluctuate based on the mouse position versus what the current zoom is. At the same time though, I would think that the ScalerParams that you have defined should prevent this fluctuation, although that too may be messing with the calculation, since it is always assuming a fully zoomed-out chart in this case, rather than taking the current window rect of the chart. Perhaps taking the current ActualWindowRect of the chart may help there?

    Please let me know if you have any other questions or concerns on this matter.

Reply Children