Learn how to implement custom zooming in SciChart using the mouse wheel, trackpad, or keyboard + / - keys on macOS or Catalyst.
Overview
In addition to built-in zoom modifiers like SCIPinchZoomModifier and SCIZoomPanModifier, you can implement custom zooming behavior using mouse wheel, trackpad gestures, or keyboard keys. This is especially useful in macOS Catalyst or full macOS (AppKit) applications where fine-grained zoom control is desired.
This article demonstrates how to override both scrollWheel(with:) and keyDown(with:) in an NSView-based subclass to apply a centered zoom effect on the chart surface. Both methods use the same zoom calculation logic, keeping the zoom centered around the cursor location.
NOTE: This approach assumes a 2D chart with SCINumericAxis on both X and Y axes.
For iOS or non-macOS platforms, use built-in modifiers like SCIPinchZoomModifier.
Zooming with Mouse Wheel or Trackpad
This code overrides scrollWheel(with:) to handle zooming via scroll gestures.
override func scrollWheel(with event: NSEvent) {
guard
let xAxis = self.surface.xAxes.firstObject as? SCINumericAxis,
let yAxis = self.surface.yAxes.firstObject as? SCINumericAxis,
let xVisible = xAxis.visibleRange as? SCIDoubleRange,
let yVisible = yAxis.visibleRange as? SCIDoubleRange else {
return
}
// Convert mouse location to data values
let location = view.convert(event.locationInWindow, from: nil)
let xData = xAxis.getDataValue(Float(location.x))
let yData = yAxis.getDataValue(Float(location.y))
// Zoom factor
let zoomStep = 0.1
let scale = event.scrollingDeltaY > 0 ? (1 - zoomStep) : (1 + zoomStep)
// Calculate new visible ranges keeping mouse point centered
let newXMin = xData.toDouble() - (xData.toDouble() - xVisible.minAsDouble) * scale
let newXMax = xData.toDouble() + (xVisible.maxAsDouble - xData.toDouble()) * scale
let newYMin = yData.toDouble() - (yData.toDouble() - yVisible.minAsDouble) * scale
let newYMax = yData.toDouble() + (yVisible.maxAsDouble - yData.toDouble()) * scale
//Change visible range
xAxis.visibleRange = SCIDoubleRange(min: newXMin, max: newXMax)
yAxis.visibleRange = SCIDoubleRange(min: newYMin, max: newYMax)
}
TIP:: If you want to zoom from the center of the chart instead of the cursor, use the chart center point instead of the cursor location for xData and yData.
Zooming with Keyboard (+ / - Keys)
The same logic can be applied in keyDown(with:) to allow zooming using keyboard input.