@@ -708,87 +708,22 @@ export const DygraphChart = ({
708708 } ,
709709
710710 wheel ( event : WheelEvent , dygraph : Dygraph ) {
711- // Take the offset of a mouse event on the dygraph canvas and
712- // convert it to a pair of percentages from the bottom left.
713- // (Not top left, bottom is where the lower value is.)
714- function offsetToPercentage ( g : Dygraph , offsetX : number , offsetY : number ) {
715- // This is calculating the pixel offset of the leftmost date.
716- const xOffset = g . toDomXCoord ( g . xAxisRange ( ) [ 0 ] )
717- const yar0 = g . yAxisRange ( 0 )
718-
719- // This is calculating the pixel of the highest value. (Top pixel)
720- const yOffset = g . toDomYCoord ( yar0 [ 1 ] )
721-
722- // x y w and h are relative to the corner of the drawing area,
723- // so that the upper corner of the drawing area is (0, 0).
724- const x = offsetX - xOffset
725- const y = offsetY - yOffset
726-
727- // This is computing the rightmost pixel, effectively defining the
728- // width.
729- // const w = g.toDomCoords(g.xAxisRange()[1], null)[0] - xOffset
730- const w = g . toDomXCoord ( g . xAxisRange ( ) [ 1 ] ) - xOffset
731-
732- // This is computing the lowest pixel, effectively defining the height.
733- // const h = g.toDomCoords(null, yar0[0])[1] - yOffset
734- const h = g . toDomYCoord ( yar0 [ 0 ] ) - yOffset
735-
736- // Percentage from the left.
737- const xPct = w === 0 ? 0 : ( x / w )
738- // Percentage from the top.
739- const yPct = h === 0 ? 0 : ( y / h )
711+ if ( ! event . shiftKey && ! event . altKey ) return
740712
741- // The (1-) part below changes it from "% distance down from the top"
742- // to "% distance up from the bottom".
743- return [ xPct , ( 1 - yPct ) ]
744- }
713+ latestIsUserAction . current = true
714+ event . preventDefault ( )
715+ event . stopPropagation ( )
745716
746- function adjustAxis ( axis : [ number , number ] , zoomInPercentage : number , bias : number ) {
747- const delta = axis [ 1 ] - axis [ 0 ]
717+ // https://dygraphs.com/gallery/interaction-api.js
718+ const zoom = ( g , zoomInPercentage , bias ) => {
719+ bias = bias || 0.5
720+ const [ afterAxis , beforeAxis ] = g . xAxisRange ( )
721+ const delta = afterAxis - beforeAxis
748722 const increment = delta * zoomInPercentage
749- const foo = [ increment * bias , increment * ( 1 - bias ) ]
750-
751- return [ axis [ 0 ] + foo [ 0 ] , axis [ 1 ] - foo [ 1 ] ]
752- }
753-
754- // Adjusts [x, y] toward each other by zoomInPercentage%
755- // Split it so the left/bottom axis gets xBias/yBias of that change and
756- // tight/top gets (1-xBias)/(1-yBias) of that change.
757- //
758- // If a bias is missing it splits it down the middle.
759- function zoomRange ( g : Dygraph , zoomInPercentage : number , xBias : number , yBias : number ) {
760- const yAxes = g . yAxisRanges ( )
761- const newYAxes = [ ]
762- for ( let i = 0 ; i < yAxes . length ; i += 1 ) {
763- newYAxes [ i ] = adjustAxis ( yAxes [ i ] , zoomInPercentage , ( yBias || 0.5 ) )
764- }
765-
766- return adjustAxis ( g . xAxisRange ( ) , zoomInPercentage , ( xBias || 0.5 ) )
767- }
768-
769- if ( event . altKey || event . shiftKey ) {
770- latestIsUserAction . current = true
771-
772- // http://dygraphs.com/gallery/interaction-api.js
773- let normalDef
774- // @ts -ignore
775- if ( typeof event . wheelDelta === "number" && ! Number . isNaN ( event . wheelDelta ) ) {
776- // chrome
777- // @ts -ignore
778- normalDef = event . wheelDelta / 40
779- } else {
780- // firefox
781- normalDef = event . deltaY * - 1.2
782- }
783-
784- const normal = ( event . detail ) ? event . detail * - 1 : normalDef
785- const percentage = normal / 50
786-
787- const percentages = offsetToPercentage ( dygraph , event . offsetX , event . offsetY )
788- const xPct = percentages [ 0 ]
789- const yPct = percentages [ 1 ]
723+ const [ afterIncrement , beforeIncrement ] = [ increment * bias , increment * ( 1 - bias ) ]
790724
791- const [ after , before ] = zoomRange ( dygraph , percentage , xPct , yPct )
725+ const after = afterAxis + afterIncrement
726+ const before = beforeAxis - beforeIncrement
792727
793728 propsRef . current . updateChartPanOrZoom ( {
794729 after,
@@ -800,9 +735,34 @@ export const DygraphChart = ({
800735 } )
801736 } ,
802737 } )
738+ }
803739
804- event . preventDefault ( )
740+ const offsetToPercentage = ( g , offsetX ) => {
741+ // This is calculating the pixel offset of the leftmost date.
742+ const [ axisAfterOffset ] = g . toDomCoords ( g . xAxisRange ( ) [ 0 ] , null )
743+ // x and w are relative to the corner of the drawing area,
744+ // so that the upper corner of the drawing area is (0, 0).
745+ const x = offsetX - axisAfterOffset
746+ // This is computing the rightmost pixel, effectively defining the
747+ // width.
748+ const w = g . toDomCoords ( g . xAxisRange ( ) [ 1 ] , null ) [ 0 ] - axisAfterOffset
749+
750+ // Percentage from the left.
751+ return w == 0 ? 0 : x / w
805752 }
753+
754+ const normalDef =
755+ typeof event . wheelDelta === "number" && ! Number . isNaN ( event . wheelDelta )
756+ ? event . wheelDelta / 40
757+ : event . deltaY * - 1.2
758+
759+ const normal = event . detail ? event . detail * - 1 : normalDef
760+ const percentage = normal / 50
761+
762+ if ( ! event . offsetX ) event . offsetX = event . layerX - event . target . offsetLeft
763+ const xPct = offsetToPercentage ( dygraph , event . offsetX )
764+
765+ zoom ( dygraph , percentage , xPct )
806766 } ,
807767
808768 click ( event : MouseEvent ) {
0 commit comments