I'm using a simple ChartPlotter in my C# WPF application. When I zoom in/out by mouse scrolling, both Axis are changed. How can I control the zooming by mouse scrolling so it will affect only the X-Axis?
Asked
Active
Viewed 1,407 times
2 Answers
1
This feature is already built into D3, if you hover your mouse over one of the axes, and do a mouse wheel scroll, the zoom is only pertained to the axis you were hovered over. If you want to replicate this in your code, you can see examples of it in the source code.

Jason Higgins
- 1,516
- 1
- 17
- 37
-
I did not notice this post before answering for another question. So if you need to restrict zooming at all then maybe my answer will be helpful. http://stackoverflow.com/questions/19927086/dynamicdatadisplay-zoom-and-tooltips-on-realtime-charts-with-syncronous-x-axis/21962113#21962113 – MPękalski Feb 22 '14 at 23:38
0
The zoom feature is implemented in "MouseNavigation.cs". The MouseWheel handler will call underneath function:
Viewport.Visible = Viewport.Visible.Zoom(zoomTo, zoomSpeed);
And fortunately, there is a ZoomX function for your needs. Thus just remove MouseNavigation from your plotter, then re-implement your own as below:
// Remove mouse navigation
plotter.Children.Remove(plotter.MouseNavigation);
// ZoomX when wheeling mouse
private void plotter_MouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs e)
{
if (!e.Handled)
{
Point mousePos = e.GetPosition(this);
Point zoomTo = mousePos.ScreenToViewport(plotter.Viewport.Transform);
double zoomSpeed = Math.Abs(e.Delta / Mouse.MouseWheelDeltaForOneLine);
zoomSpeed *= 1.2;
if (e.Delta < 0)
{
zoomSpeed = 1 / zoomSpeed;
}
plotter.Viewport.SetChangeType(ChangeType.Zoom);
plotter.Viewport.Visible = plotter.Viewport.Visible.ZoomX(zoomTo, zoomSpeed);
plotter.Viewport.SetChangeType();
e.Handled = true;
}
}

Khai Nguyen
- 103
- 1
- 7