Repository: chartjs/chartjs-plugin-zoom
Pull request #547 (2021-06-21 05:56:54)

Fix click to zoom sample
Couple of places were still checking for moved options, so the title and border did not reflect the state.


Patch:
diff --git a/docs/samples/wheel/click-zoom.md b/docs/samples/wheel/click-zoom.md
index 512e3de..20e0ba1 100644
--- a/docs/samples/wheel/click-zoom.md
+++ b/docs/samples/wheel/click-zoom.md
@@ -73,7 +73,7 @@ const borderPlugin = {
   id: 'chartAreaBorder',
   beforeDraw(chart, args, options) {
     const {ctx, chartArea: {left, top, width, height}} = chart;
-    if (chart.options.plugins.zoom.zoom.enabled) {
+    if (chart.options.plugins.zoom.zoom.wheel.enabled) {
       ctx.save();
       ctx.strokeStyle = 'red';
       ctx.lineWidth = 1;
@@ -84,7 +84,7 @@ const borderPlugin = {
 };
 // </block:border>
 
-const zoomStatus = () => 'Zoom: ' + (zoomOptions.zoom.enabled ? 'enabled' : 'disabled');
+const zoomStatus = () => 'Zoom: ' + (zoomOptions.zoom.wheel.enabled ? 'enabled' : 'disabled');
 
 // <block:config:1>
 const config = {
diff --git a/src/scale.types.js b/src/scale.types.js
index 55994e1..e8b2fcc 100644
--- a/src/scale.types.js
+++ b/src/scale.types.js
@@ -134,8 +134,11 @@ function panNumericalScale(scale, delta, limits, canZoom = false) {
   const newMin = scale.getValueForPixel(scale.getPixelForValue(prevStart + offset) - delta);
   const newMax = scale.getValueForPixel(scale.getPixelForValue(prevEnd + offset) - delta);
   const {min: minLimit = -Infinity, max: maxLimit = Infinity} = canZoom && limits && limits[scale.axis] || {};
-  if ((newMin < minLimit || newMax > maxLimit)) {
-    return true; // At limit: No change but return true to indicate no need to store the delta.
+  if (isNaN(newMin) || isNaN(newMax) || newMin < minLimit || newMax > maxLimit) {
+    // At limit: No change but return true to indicate no need to store the delta.
+    // NaN can happen for 0-dimension scales (either because they were configured
+    // with min === max or because the chart has 0 plottable area).
+    return true;
   }
   return updateRange(scale, {min: newMin, max: newMax}, limits, canZoom);
 }


Test patch:
diff --git a/test/specs/pan.spec.js b/test/specs/pan.spec.js
index d5e475c..7772575 100644
--- a/test/specs/pan.spec.js
+++ b/test/specs/pan.spec.js
@@ -92,6 +92,38 @@ describe('pan', function() {
       expect(scale.min).toBeLessThan(2);
       expect(scale.max).toBe(scale.min + 2);
     });
+
+    it('should handle zero-dimension scales', function() {
+      const chart = window.acquireChart({
+        type: 'line',
+        data,
+        options: {
+          plugins: {
+            zoom: {
+              pan: {
+                enabled: true,
+                mode: 'y',
+              }
+            }
+          },
+          scales: {
+            y: {
+              type: 'linear',
+              min: 2,
+              max: 2
+            }
+          }
+        }
+      });
+      const scale = chart.scales.y;
+      expect(scale.min).toBe(2);
+      expect(scale.max).toBe(2);
+      chart.pan(50);
+      expect(scale.min).toBe(2);
+      expect(scale.max).toBe(2);
+      expect(scale.options.min).toBe(2);
+      expect(scale.options.max).toBe(2);
+    });
   });
 
   describe('events', function() {