Hi, so I am trying to create a zoom functionality for the XamDataChart using the zoom bars like the one in Python:
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)
GetXMousePositionOnChart(out var xpos);
GetYMousePositionOnChart(out var ypos);
var xMaxViewPort = xmDataChart.ViewportRect.Right;
var yMaxViewPort = xmDataChart.ViewportRect.Bottom;
var xAxis = xmDataChart.Axes;
var yAxis = xmDataChart.Axes;
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)
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?
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.
So regarding the vm.MaxTicks is actually NumericXAxis.MaximumValue and the GetXMousePositionOnChart actually is just=> xAxis.GetUnscaledValue(pos.X,new ScalerParams(xmDataChart.WindowRect, viewport, xAxis.IsInverted)); with some extra checks to be sure it doesn't get out of the bounds of the chart. and the MinRange=0 and MaxRange=0.4769 after a small move.
I apologize, but I am still unsure what most of the code you have provided is or what it is intended to be. You mentioned that the MinRange and MaxRange is 0 and 0.4769 after a small move, but I am unsure if you mean the minimum / maximum range of the zoombar that you are setting or something else in this case.
In any case, what I can see from your descriptions is that you are looking to have a horizontal scroll based on the mouse position after a right click and holding shift, and so I have put together my own implementation of this. It is a rough implementation at the moment, but perhaps it will help you.
If you would like me to use the code you have provided, I would request that you please modify this sample and send it back with a working version of your code so that I can see exactly what each piece is. Alternatively, if you have an isolated sample project that demonstrates your code in action, I will take a look at that.