Skip to content

Commit 3213e49

Browse files
committed
fixes #4895; brush chart bugfix
1 parent f0c2008 commit 3213e49

File tree

1 file changed

+34
-35
lines changed

1 file changed

+34
-35
lines changed

src/modules/ZoomPanSelection.js

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -355,15 +355,11 @@ export default class ZoomPanSelection extends Toolbar {
355355
})
356356
.resize()
357357
.on('resize', () => {
358-
// clearTimeout(this.w.globals.selectionResizeTimer)
359-
// this.w.globals.selectionResizeTimer = window.setTimeout(() => {
360-
// timer commented for now as performance drop is negligible
361358
let zoomtype = w.globals.zoomEnabled
362359
? w.config.chart.zoom.type
363360
: w.config.chart.selection.type
364361

365362
this.handleMouseUp({ zoomtype, isResized: true })
366-
//}, 50)
367363
})
368364
}
369365
}
@@ -397,7 +393,7 @@ export default class ZoomPanSelection extends Toolbar {
397393
x
398394
if (w.globals.isRangeBar) {
399395
// rangebars put datetime data in y axis
400-
x = // calculation: (selection left time - chart left time) / milliseconds per pixel = selection X value in pixels
396+
x =
401397
(w.config.chart.selection.xaxis.min -
402398
w.globals.yAxisScale[0].niceMin) /
403399
xyRatios.invertedYRatio
@@ -630,7 +626,7 @@ export default class ZoomPanSelection extends Toolbar {
630626
let minX, maxX, minY, maxY
631627

632628
if (!w.globals.isRangeBar) {
633-
// original code is in the IF. rangeBar exception is in the ELSE.
629+
// normal XY charts
634630
minX =
635631
w.globals.xAxisScale.niceMin +
636632
(selectionRect.left - gridRectDim.left) * xyRatios.xRatio
@@ -645,15 +641,15 @@ export default class ZoomPanSelection extends Toolbar {
645641
w.globals.yAxisScale[0].niceMax -
646642
(selectionRect.top - gridRectDim.top) * xyRatios.yRatio[0]
647643
} else {
648-
// rangeBars use x as the category, and y as the datetime data. // find data in y axis and use Y ratio
644+
// rangeBars use y for datetime
649645
minX =
650646
w.globals.yAxisScale[0].niceMin +
651647
(selectionRect.left - gridRectDim.left) * xyRatios.invertedYRatio
652648
maxX =
653649
w.globals.yAxisScale[0].niceMin +
654650
(selectionRect.right - gridRectDim.left) * xyRatios.invertedYRatio
655651

656-
minY = 0 // there is no y min/max with rangebars (it uses categories, not numeric data), so use dummy values
652+
minY = 0
657653
maxY = 1
658654
}
659655

@@ -685,48 +681,51 @@ export default class ZoomPanSelection extends Toolbar {
685681
const xyRatios = this.xyRatios
686682
const toolbar = this.ctx.toolbar
687683

688-
if (me.startX > me.endX) {
689-
let tempX = me.startX
690-
me.startX = me.endX
691-
me.endX = tempX
692-
}
693-
if (me.startY > me.endY) {
694-
let tempY = me.startY
695-
me.startY = me.endY
696-
me.endY = tempY
697-
}
684+
// Use boundingRect for final selection area
685+
const selRect = me.selectionRect.node.getBoundingClientRect()
686+
const gridRectDim = me.gridRect.getBoundingClientRect()
698687

699-
let xLowestValue = undefined
700-
let xHighestValue = undefined
688+
// Local coords in the chart's grid
689+
const localStartX =
690+
selRect.left - gridRectDim.left - w.globals.barPadForNumericAxis
691+
const localEndX =
692+
selRect.right - gridRectDim.left - w.globals.barPadForNumericAxis
693+
const localStartY = selRect.top - gridRectDim.top
694+
const localEndY = selRect.bottom - gridRectDim.top
695+
696+
// Convert those local coords to actual data values
697+
let xLowestValue, xHighestValue
701698

702699
if (!w.globals.isRangeBar) {
703-
xLowestValue = w.globals.xAxisScale.niceMin + me.startX * xyRatios.xRatio
704-
xHighestValue = w.globals.xAxisScale.niceMin + me.endX * xyRatios.xRatio
700+
xLowestValue =
701+
w.globals.xAxisScale.niceMin + localStartX * xyRatios.xRatio
702+
xHighestValue = w.globals.xAxisScale.niceMin + localEndX * xyRatios.xRatio
705703
} else {
706704
xLowestValue =
707-
w.globals.yAxisScale[0].niceMin + me.startX * xyRatios.invertedYRatio
705+
w.globals.yAxisScale[0].niceMin + localStartX * xyRatios.invertedYRatio
708706
xHighestValue =
709-
w.globals.yAxisScale[0].niceMin + me.endX * xyRatios.invertedYRatio
707+
w.globals.yAxisScale[0].niceMin + localEndX * xyRatios.invertedYRatio
710708
}
711709

712-
// TODO: we will consider the 1st y axis values here for getting highest and lowest y
710+
// For Y values, pick from the first y-axis, but handle multi-axis
713711
let yHighestValue = []
714712
let yLowestValue = []
715713

716714
w.config.yaxis.forEach((yaxe, index) => {
717-
// We can use the index of any series referenced by the Yaxis
718-
// because they will all return the same value, so we choose the first.
715+
// pick whichever series is mapped to this y-axis
719716
let seriesIndex = w.globals.seriesYAxisMap[index][0]
720-
yHighestValue.push(
717+
let highestVal =
721718
w.globals.yAxisScale[index].niceMax -
722-
xyRatios.yRatio[seriesIndex] * me.startY
723-
)
724-
yLowestValue.push(
719+
xyRatios.yRatio[seriesIndex] * localStartY
720+
let lowestVal =
725721
w.globals.yAxisScale[index].niceMax -
726-
xyRatios.yRatio[seriesIndex] * me.endY
727-
)
722+
xyRatios.yRatio[seriesIndex] * localEndY
723+
724+
yHighestValue.push(highestVal)
725+
yLowestValue.push(lowestVal)
728726
})
729727

728+
// Only apply if user actually dragged far enough to consider it a selection
730729
if (
731730
me.dragged &&
732731
(me.dragX > 10 || me.dragY > 10) &&
@@ -828,7 +827,7 @@ export default class ZoomPanSelection extends Toolbar {
828827
const deltaX = w.globals.lastClientPosition.x - me.clientX
829828
const deltaY = w.globals.lastClientPosition.y - me.clientY
830829

831-
// check which direction had the highest amplitude and then figure out direction by checking if the value is greater or less than zero
830+
// check which direction had the highest amplitude
832831
if (Math.abs(deltaX) > Math.abs(deltaY) && deltaX > 0) {
833832
this.moveDirection = 'left'
834833
} else if (Math.abs(deltaX) > Math.abs(deltaY) && deltaX < 0) {
@@ -850,7 +849,7 @@ export default class ZoomPanSelection extends Toolbar {
850849

851850
let xHighestValue = w.globals.isRangeBar ? w.globals.maxY : w.globals.maxX
852851

853-
// on a category, we don't pan continuosly as it causes bugs
852+
// on a category, we don't pan continuously as it causes bugs
854853
if (!w.config.xaxis.convertedCatToNumeric) {
855854
me.panScrolled(xLowestValue, xHighestValue)
856855
}

0 commit comments

Comments
 (0)