From 933e193c255da0142fbd3b3f367d26b3d3231a7e Mon Sep 17 00:00:00 2001 From: Martin Prusse Date: Tue, 9 Aug 2016 15:56:49 -0300 Subject: [PATCH 1/2] Support for `chart.inverted=true` Properly position annotations on inverted charts when: - linked to points; - positioned using `yValue` and/or `xValue` ; - drag/drop annotations along the correct axis when `allowDragX` or `allowDragY` are disabled (also correctly update the `xValue` and/or `yValue`); --- js/annotations.js | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/js/annotations.js b/js/annotations.js index 582358c..fb57aae 100644 --- a/js/annotations.js +++ b/js/annotations.js @@ -579,8 +579,8 @@ linkType = (linkedTo instanceof H.Point) ? 'point' : (linkedTo instanceof H.Series) ? 'series' : null; if (linkType === 'point') { - options.x = linkedTo.plotX + chart.plotLeft; - options.y = linkedTo.plotY + chart.plotTop; + options.x = xAxis.toPixels(linkedTo.x); + options.y = yAxis.toPixels(linkedTo.y); series = linkedTo.series; } else if (linkType === 'series') { series = linkedTo; @@ -601,6 +601,12 @@ x = (defined(options.xValue) ? xAxis.toPixels(options.xValue /* + xAxis.minPointOffset */) : options.x); y = defined(options.yValue) ? yAxis.toPixels(options.yValue) : options.y; + if (chart.inverted) { + var temp_x = x; + x = y; + y = temp_x; + } + if (isNaN(x) || isNaN(y) || !isNumber(x) || !isNumber(y)) { return; } @@ -841,10 +847,17 @@ return; } var note = chart.activeAnnotation; - - var x = note.options.allowDragX ? event.clientX - note.startX + note.group.translateX : note.group.translateX, - y = note.options.allowDragY ? event.clientY - note.startY + note.group.translateY : note.group.translateY; - + + var allow_axis_x_drag = note.options.allowDragX, + allow_axis_y_drag = note.options.allowDragY; + if (chart.inverted) { + var temp_allow = allow_axis_x_drag; + allow_axis_x_drag = allow_axis_y_drag; + allow_axis_y_drag = temp_allow; + } + var x = allow_axis_x_drag ? event.clientX - note.startX + note.group.translateX : note.group.translateX, + y = allow_axis_y_drag ? event.clientY - note.startY + note.group.translateY : note.group.translateY; + note.transX = x; note.transY = y; note.group.attr({ @@ -889,9 +902,17 @@ allowDragY = options.allowDragY, xAxis = note.chart.xAxis[note.options.xAxis], yAxis = note.chart.yAxis[note.options.yAxis], - newX = xAxis.toValue(x), - newY = yAxis.toValue(y); - + newX, + newY; + + if (chart.inverted) { + var temp_x = x; + x = y; + y = temp_x; + } + + newX = xAxis.toValue(x); + newY = yAxis.toValue(y); if (x !== 0 || y !== 0) { if (allowDragX && allowDragY) { note.update({ From b8005f3f113f7634d77863a3e41f1a87ad20e3e3 Mon Sep 17 00:00:00 2001 From: Martin Prusse Date: Wed, 10 Aug 2016 11:01:13 -0300 Subject: [PATCH 2/2] Lint js code --- js/annotations.js | 41 +++++++++++++++++++---------------------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/js/annotations.js b/js/annotations.js index fb57aae..5ca12ee 100644 --- a/js/annotations.js +++ b/js/annotations.js @@ -602,9 +602,9 @@ y = defined(options.yValue) ? yAxis.toPixels(options.yValue) : options.y; if (chart.inverted) { - var temp_x = x; + var tempX = x; x = y; - y = temp_x; + y = tempX; } if (isNaN(x) || isNaN(y) || !isNumber(x) || !isNumber(y)) { @@ -846,20 +846,24 @@ if (!chart.isInsidePlot(clickX - chart.plotLeft, clickY - chart.plotTop)) { return; } - var note = chart.activeAnnotation; + var note = chart.activeAnnotation, + x, + y; - var allow_axis_x_drag = note.options.allowDragX, - allow_axis_y_drag = note.options.allowDragY; if (chart.inverted) { - var temp_allow = allow_axis_x_drag; - allow_axis_x_drag = allow_axis_y_drag; - allow_axis_y_drag = temp_allow; + // Screen X is Y axis and screen Y is X axis. + x = note.options.allowDragY ? event.clientX - note.startX + note.group.translateX : note.group.translateX; + y = note.options.allowDragX ? event.clientY - note.startY + note.group.translateY : note.group.translateY; + note.transX = y; + note.transY = x; + } else { + // Screen X is X axis and screen X is X axis. + x = note.options.allowDragX ? event.clientX - note.startX + note.group.translateX : note.group.translateX; + y = note.options.allowDragY ? event.clientY - note.startY + note.group.translateY : note.group.translateY; + note.transX = x; + note.transY = y; } - var x = allow_axis_x_drag ? event.clientX - note.startX + note.group.translateX : note.group.translateX, - y = allow_axis_y_drag ? event.clientY - note.startY + note.group.translateY : note.group.translateY; - note.transX = x; - note.transY = y; note.group.attr({ transform: 'translate(' + x + ',' + y + ')' }); @@ -890,6 +894,7 @@ event.stopPropagation(); event.preventDefault(); if (chart.activeAnnotation && (chart.activeAnnotation.transX !== 0 || chart.activeAnnotation.transY !== 0)) { + // `transX` and `transY` are set taking in acount `chart.inverted`. var note = chart.activeAnnotation, x = note.transX, y = note.transY, @@ -902,17 +907,9 @@ allowDragY = options.allowDragY, xAxis = note.chart.xAxis[note.options.xAxis], yAxis = note.chart.yAxis[note.options.yAxis], - newX, - newY; - - if (chart.inverted) { - var temp_x = x; - x = y; - y = temp_x; - } + newX = xAxis.toValue(x), + newY = yAxis.toValue(y); - newX = xAxis.toValue(x); - newY = yAxis.toValue(y); if (x !== 0 || y !== 0) { if (allowDragX && allowDragY) { note.update({