Skip to content

Commit 2c6e340

Browse files
davidswinegarbenmccann
authored andcommitted
Allow using a function for mode (#258)
Allow using a function for `mode`
1 parent 7118770 commit 2c6e340

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ plugins: {
3333

3434
// Panning directions. Remove the appropriate direction to disable
3535
// Eg. 'y' would only allow panning in the y direction
36+
// A function that is called as the user is panning and returns the
37+
// available directions can also be used:
38+
// mode: function({ chart }) {
39+
// return 'xy';
40+
// },
3641
mode: 'xy',
3742

3843
rangeMin: {
@@ -69,6 +74,11 @@ plugins: {
6974

7075
// Zooming directions. Remove the appropriate direction to disable
7176
// Eg. 'y' would only allow zooming in the y direction
77+
// A function that is called as the user is zooming and returns the
78+
// available directions can also be used:
79+
// mode: function({ chart }) {
80+
// return 'xy';
81+
// },
7282
mode: 'xy',
7383

7484
rangeMin: {

src/plugin.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ function storeOriginalOptions(chart) {
7373
});
7474
}
7575

76+
/**
77+
* @param {string} mode can be 'x', 'y' or 'xy'
78+
* @param {string} dir can be 'x' or 'y'
79+
*/
7680
function directionEnabled(mode, dir) {
7781
if (mode === undefined) {
7882
return true;
@@ -195,7 +199,7 @@ function doZoom(chart, percentZoomX, percentZoomY, focalPoint, whichAxes) {
195199
if (zoomOptions.enabled) {
196200
storeOriginalOptions(chart);
197201
// Do the zoom here
198-
var zoomMode = zoomOptions.mode;
202+
var zoomMode = typeof zoomOptions.mode === 'function' ? zoomOptions.mode({chart: chart}) : zoomOptions.mode;
199203

200204
// Which axe should be modified when figers were used.
201205
var _whichAxes;
@@ -301,7 +305,7 @@ function doPan(chartInstance, deltaX, deltaY) {
301305
storeOriginalOptions(chartInstance);
302306
var panOptions = chartInstance.$zoom._options.pan;
303307
if (panOptions.enabled) {
304-
var panMode = panOptions.mode;
308+
var panMode = typeof panOptions.mode === 'function' ? panOptions.mode({chart: chartInstance}) : panOptions.mode;
305309

306310
helpers.each(chartInstance.scales, function(scale) {
307311
if (scale.isHorizontal() && directionEnabled(panMode, 'x') && deltaX !== 0) {

0 commit comments

Comments
 (0)