11import { each , callback as call } from 'chart.js/helpers' ;
2- import { panFunctions , zoomFunctions } from './scale.types' ;
2+ import { panFunctions , updateRange , zoomFunctions } from './scale.types' ;
33import { getState } from './state' ;
44import { directionEnabled , getEnabledScalesByPoint } from './utils' ;
55
@@ -18,9 +18,9 @@ function storeOriginalScaleLimits(chart) {
1818 return originalScaleLimits ;
1919}
2020
21- function zoomScale ( scale , zoom , center , limits ) {
21+ function doZoom ( scale , amount , center , limits ) {
2222 const fn = zoomFunctions [ scale . type ] || zoomFunctions . default ;
23- call ( fn , [ scale , zoom , center , limits ] ) ;
23+ call ( fn , [ scale , amount , center , limits ] ) ;
2424}
2525
2626function getCenter ( chart ) {
@@ -33,11 +33,11 @@ function getCenter(chart) {
3333
3434/**
3535 * @param chart The chart instance
36- * @param {number | {x?: number, y?: number, focalPoint?: {x: number, y: number}} } zoom The zoom percentage or percentages and focal point
37- * @param {boolean } [useTransition] Whether to use `zoom` transition
36+ * @param {number | {x?: number, y?: number, focalPoint?: {x: number, y: number}} } amount The zoom percentage or percentages and focal point
37+ * @param {string } [transition] Which transiton mode to use. Defaults to 'none'
3838 */
39- export function doZoom ( chart , zoom , useTransition ) {
40- const { x = 1 , y = 1 , focalPoint = getCenter ( chart ) } = typeof zoom === 'number' ? { x : zoom , y : zoom } : zoom ;
39+ export function zoom ( chart , amount , transition = 'none' ) {
40+ const { x = 1 , y = 1 , focalPoint = getCenter ( chart ) } = typeof amount === 'number' ? { x : amount , y : amount } : amount ;
4141 const { options : { limits, zoom : zoomOptions } } = getState ( chart ) ;
4242 const { mode = 'xy' , overScaleMode} = zoomOptions || { } ;
4343
@@ -49,18 +49,27 @@ export function doZoom(chart, zoom, useTransition) {
4949
5050 each ( enabledScales || chart . scales , function ( scale ) {
5151 if ( scale . isHorizontal ( ) && xEnabled ) {
52- zoomScale ( scale , x , focalPoint , limits ) ;
52+ doZoom ( scale , x , focalPoint , limits ) ;
5353 } else if ( ! scale . isHorizontal ( ) && yEnabled ) {
54- zoomScale ( scale , y , focalPoint , limits ) ;
54+ doZoom ( scale , y , focalPoint , limits ) ;
5555 }
5656 } ) ;
5757
58- chart . update ( useTransition ? 'zoom' : 'none' ) ;
58+ chart . update ( transition ) ;
5959
6060 call ( zoomOptions . onZoom , [ { chart} ] ) ;
6161}
6262
63- export function resetZoom ( chart ) {
63+
64+ export function zoomScale ( chart , scaleId , range , transition = 'none' ) {
65+ storeOriginalScaleLimits ( chart ) ;
66+ const scale = chart . scales [ scaleId ] ;
67+ updateRange ( scale , range , undefined , true ) ;
68+ chart . update ( transition ) ;
69+ }
70+
71+
72+ export function resetZoom ( chart , transition = 'default' ) {
6473 const originalScaleLimits = storeOriginalScaleLimits ( chart ) ;
6574
6675 each ( chart . scales , function ( scale ) {
@@ -73,7 +82,7 @@ export function resetZoom(chart) {
7382 delete scaleOptions . max ;
7483 }
7584 } ) ;
76- chart . update ( ) ;
85+ chart . update ( transition ) ;
7786}
7887
7988function panScale ( scale , delta , limits ) {
@@ -90,8 +99,8 @@ function panScale(scale, delta, limits) {
9099 }
91100}
92101
93- export function doPan ( chart , pan , enabledScales ) {
94- const { x = 0 , y = 0 } = typeof pan === 'number' ? { x : pan , y : pan } : pan ;
102+ export function pan ( chart , delta , enabledScales , transition = 'none' ) {
103+ const { x = 0 , y = 0 } = typeof delta === 'number' ? { x : delta , y : delta } : delta ;
95104 const { options : { pan : panOptions , limits} } = getState ( chart ) ;
96105 const { mode = 'xy' , onPan} = panOptions || { } ;
97106
@@ -108,7 +117,7 @@ export function doPan(chart, pan, enabledScales) {
108117 }
109118 } ) ;
110119
111- chart . update ( 'none' ) ;
120+ chart . update ( transition ) ;
112121
113122 call ( onPan , [ { chart} ] ) ;
114123}
0 commit comments