@@ -363,7 +363,6 @@ export class Chart extends EventEmitter.EventEmitter {
363363 // (so that the functions don't overflow on zoom or drag)
364364 const id = this . id
365365 const defs = this . canvas . enter . append ( 'defs' )
366-
367366 defs
368367 . append ( 'clipPath' )
369368 . attr ( 'id' , 'function-plot-clip-' + id )
@@ -406,6 +405,7 @@ export class Chart extends EventEmitter.EventEmitter {
406405 . select ( '.x.axis' )
407406 . attr ( 'transform' , 'translate(0,' + this . meta . height + ')' )
408407 . call ( this . meta . xAxis )
408+
409409 this . canvas . merge ( this . canvas . enter ) . select ( '.y.axis' ) . call ( this . meta . yAxis )
410410 }
411411
@@ -632,18 +632,55 @@ export class Chart extends EventEmitter.EventEmitter {
632632 updateAxes ( ) {
633633 const instance = this
634634 const canvas = instance . canvas . merge ( instance . canvas . enter )
635+
636+ // center the axes
635637 canvas . select ( '.x.axis' ) . call ( instance . meta . xAxis )
638+
639+ if ( this . options . xAxis . position === 'sticky' ) {
640+ const yMin = this . meta . yScale . domain ( ) [ 0 ]
641+ const yMax = this . meta . yScale . domain ( ) [ 1 ]
642+
643+ const yMid = ( yMax + yMin ) / 2
644+
645+ const yScaleFactor = this . meta . height / ( yMax - yMin )
646+
647+ let yTranslation = yScaleFactor * yMid + this . meta . height / 2
648+ yTranslation = yTranslation < 0 ? 0 : yTranslation
649+ yTranslation = yTranslation > this . meta . height ? this . meta . height : yTranslation
650+
651+ canvas . select ( '.x.axis' ) . attr ( 'transform' , 'translate(0,' + yTranslation + ')' )
652+
653+ canvas
654+ . selectAll ( '.x.axis path, .x.axis line' )
655+ . attr ( 'transform' , 'translate(0,' + ( this . meta . height / 2 - yTranslation + this . meta . height / 2 ) + ')' )
656+ }
657+
636658 canvas . select ( '.y.axis' ) . call ( instance . meta . yAxis )
637659
638- // updates the style of the axes
660+ if ( this . options . yAxis . position === 'sticky' ) {
661+ const xMin = this . meta . xScale . domain ( ) [ 0 ]
662+ const xMax = this . meta . xScale . domain ( ) [ 1 ]
663+
664+ const xMid = ( xMax + xMin ) / 2
665+
666+ const xScaleFactor = this . meta . width / ( xMin - xMax )
667+
668+ let xTranslation = xScaleFactor * xMid + this . meta . width / 2
669+
670+ xTranslation = xTranslation < 0 ? 0 : xTranslation
671+ xTranslation = xTranslation > this . meta . width ? this . meta . width : xTranslation
672+ canvas . select ( '.y.axis' ) . attr ( 'transform' , 'translate(' + xTranslation + ',0)' )
673+
674+ canvas . selectAll ( '.y.axis path, .y.axis line' ) . attr ( 'transform' , 'translate(' + - xTranslation + ',0)' )
675+ }
676+
639677 canvas
640678 . selectAll ( '.axis path, .axis line' )
641679 . attr ( 'fill' , 'none' )
642680 . attr ( 'stroke' , 'black' )
643681 . attr ( 'shape-rendering' , 'crispedges' )
644682 . attr ( 'opacity' , 0.1 )
645683 }
646-
647684 syncOptions ( ) {
648685 // update the original options yDomain and xDomain, this is done so that next calls to functionPlot()
649686 // with the same object preserve some of the computed state
0 commit comments