Implementing Mouse-Wheel Zoom in CanvasJS Charts

Vishwas R
3 min readJul 8, 2024

--

In the world of data visualization, interactivity is key to providing users with a deeper understanding of complex datasets. CanvasJS, a JavaScript charting library, offers a variety of features to create dynamic and interactive charts. Mouse-wheel zoom functionality can significantly enhance user experience by allowing users to zoom in and out of charts effortlessly to explore data in greater detail.

Setting Up Your CanvasJS Environment

Before implementing zoom functionality, ensure you have set up CanvasJS in your project. You can include CanvasJS via CDN or by downloading the library.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CanvasJS Chart with Zoom</title>
<script src="https://cdn.canvasjs.com/canvasjs.min.js"></script>
</head>
<body>
<div id="chartContainer" style="height: 370px; width: 100%;"></div>
<script>
// JavaScript code will go here
</script>
</body>
</html>

Implementing Mouse-Wheel Zooming

To implement mouse-wheel zooming, you need to bind the mouse-wheel event and adjust the viewport of the chart based on the scroll direction. Here’s how you can do it:

function bindMouseWheelZoom(chart) {
chart.overlaidCanvas.addEventListener("wheel", function (e) {
e.preventDefault();

var zoomFactor = 0.1; // Change Zoom-Factor

if (e.offsetX < chart.plotArea.x1 || e.offsetX > chart.plotArea.x2 || e.offsetY < chart.plotArea.y1 || e.offsetY > chart.plotArea.y2)
return;

var xValue = Math.round(chart.axisX[0].convertPixelToValue(e.offsetX));
var yValue = Math.round(chart.axisY[0].convertPixelToValue(e.offsetY));

var axisXViewportMin = chart.axisX[0].get("viewportMinimum"),
axisXViewportMax = chart.axisX[0].get("viewportMaximum"),
axisXMin = chart.axisX[0].get("minimum"),
axisXMax = chart.axisX[0].get("maximum");

var newAxisXViewportMin, newAxisXViewportMax;

if (e.deltaY < 0) {
newAxisXViewportMin = axisXViewportMin + (xValue - axisXViewportMin) * zoomFactor;
newAxisXViewportMax = axisXViewportMax - (axisXViewportMax - xValue) * zoomFactor;
} else if (e.deltaY > 0) {
newAxisXViewportMin = (axisXViewportMin - (xValue - axisXViewportMin) * zoomFactor) >= axisXMin ? (axisXViewportMin - (xValue - axisXViewportMin) * zoomFactor) : axisXMin;
newAxisXViewportMax = (axisXViewportMax + (axisXViewportMax - xValue) * zoomFactor) <= axisXMax ? (axisXViewportMax + (axisXViewportMax - xValue) * zoomFactor) : axisXMax;
}

if (newAxisXViewportMin >= axisXMin && newAxisXViewportMax <= axisXMax && (newAxisXViewportMax - newAxisXViewportMin) > (2 * zoomFactor)) {
chart.axisX[0].set("viewportMinimum", newAxisXViewportMin, false);
chart.axisX[0].set("viewportMaximum", newAxisXViewportMax);

if (chart.stockChart && chart.stockChart._syncCharts) // For StockChart
chart.stockChart._syncCharts(newAxisXViewportMin, newAxisXViewportMax);
}
});

if (!chart.options.rangeChanged) {
chart.set("rangeChanged", function (e) {
if (e.trigger === "reset") {
e.chart.axisX[0].set("viewportMinimum", e.axisX[0].minimum, false);
e.chart.axisX[0].set("viewportMaximum", e.axisX[0].maximum);
}
});
}
}

Customizing Zooming Options

You can customize the zoom factor and other properties to fine-tune the zooming experience:

  • zoomFactor: Adjust this value to change the zoom speed.
  • viewportMinimum and viewportMaximum: Set these properties to control the initial zoom.

JSFiddle: https://jsfiddle.net/vishwas_r/yase8rt6/

Best Practices for Zoomable Charts

  • Maintain Readability: Ensure the chart remains readable when zoomed in. Avoid overcrowding datapoints.
  • Provide Reset Option: Allow users to reset the zoom to the default view. CanvasJS provides a built-in reset button.
  • Optimize Performance: For large datasets, optimize performance by enabling only necessary features and reducing the number of datapoints if possible.

Implementing mouse-wheel zooming in CanvasJS charts significantly enhances the user experience by allowing detailed data exploration. By following the steps outlined in this article, you can easily add this functionality to your charts and customize it to suit your needs.

--

--

Vishwas R
Vishwas R

Written by Vishwas R

<< Engineer || Software Developer || Techie || Cricketer || Traveler || Blogger >>

No responses yet