whychdw
2019-09-09 c0b9ce437d409b89ca0e5388344ca9c5a56d70a4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
'use strict';
 
Object.defineProperty(exports, "__esModule", {
    value: true
});
 
var _Globals = require('../../parts/Globals.js');
 
var _Globals2 = _interopRequireDefault(_Globals);
 
require('../../parts/Utilities.js');
 
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 
var Annotation = _Globals2.default.Annotation,
    MockPoint = Annotation.MockPoint,
    CrookedLine = Annotation.types.crookedLine;
 
/**
 * @class
 * @extends Annotation.CrookedLine
 * @memberOf Annotation
 */
function InfinityLine() {
    CrookedLine.apply(this, arguments);
}
 
InfinityLine.findEdgeCoordinate = function (firstPoint, secondPoint, xOrY, edgePointFirstCoordinate) {
    var xOrYOpposite = xOrY === 'x' ? 'y' : 'x';
 
    // solves equation for x or y
    // y - y1 = (y2 - y1) / (x2 - x1) * (x - x1)
    return (secondPoint[xOrY] - firstPoint[xOrY]) * (edgePointFirstCoordinate - firstPoint[xOrYOpposite]) / (secondPoint[xOrYOpposite] - firstPoint[xOrYOpposite]) + firstPoint[xOrY];
};
 
InfinityLine.findEdgePoint = function (firstPoint, secondPoint) {
    var xAxis = firstPoint.series.xAxis,
        yAxis = secondPoint.series.yAxis,
        firstPointPixels = MockPoint.pointToPixels(firstPoint),
        secondPointPixels = MockPoint.pointToPixels(secondPoint),
        deltaX = secondPointPixels.x - firstPointPixels.x,
        deltaY = secondPointPixels.y - firstPointPixels.y,
        xAxisMin = xAxis.left,
        xAxisMax = xAxisMin + xAxis.width,
        yAxisMin = yAxis.top,
        yAxisMax = yAxisMin + yAxis.height,
        xLimit = deltaX < 0 ? xAxisMin : xAxisMax,
        yLimit = deltaY < 0 ? yAxisMin : yAxisMax,
        edgePoint = {
        x: deltaX === 0 ? firstPointPixels.x : xLimit,
        y: deltaY === 0 ? firstPointPixels.y : yLimit
    },
        edgePointX,
        edgePointY,
        swap;
 
    if (deltaX !== 0 && deltaY !== 0) {
        edgePointY = InfinityLine.findEdgeCoordinate(firstPointPixels, secondPointPixels, 'y', xLimit);
 
        edgePointX = InfinityLine.findEdgeCoordinate(firstPointPixels, secondPointPixels, 'x', yLimit);
 
        if (edgePointY >= yAxisMin && edgePointY <= yAxisMax) {
            edgePoint.x = xLimit;
            edgePoint.y = edgePointY;
        } else {
            edgePoint.x = edgePointX;
            edgePoint.y = yLimit;
        }
    }
 
    edgePoint.x -= xAxisMin;
    edgePoint.y -= yAxisMin;
 
    if (firstPoint.series.chart.inverted) {
        swap = edgePoint.x;
        edgePoint.x = edgePoint.y;
        edgePoint.y = swap;
    }
 
    return edgePoint;
};
 
var edgePoint = function edgePoint(startIndex, endIndex) {
    return function (target) {
        var annotation = target.annotation,
            points = annotation.points,
            type = annotation.options.typeOptions.type;
 
        if (type === 'horizontalLine') {
            // Horizontal line has only one point,
            // make a copy of it:
            points = [points[0], new MockPoint(annotation.chart, points[0].target, {
                x: points[0].x + 1,
                y: points[0].y,
                xAxis: points[0].options.xAxis,
                yAxis: points[0].options.yAxis
            })];
        } else if (type === 'verticalLine') {
            // The same for verticalLine type:
            points = [points[0], new MockPoint(annotation.chart, points[0].target, {
                x: points[0].x,
                y: points[0].y + 1,
                xAxis: points[0].options.xAxis,
                yAxis: points[0].options.yAxis
            })];
        }
 
        return InfinityLine.findEdgePoint(points[startIndex], points[endIndex]);
    };
};
 
InfinityLine.endEdgePoint = edgePoint(0, 1);
InfinityLine.startEdgePoint = edgePoint(1, 0);
 
_Globals2.default.extendAnnotation(InfinityLine, CrookedLine,
/** @lends Annotation.InfinityLine# */
{
    addShapes: function addShapes() {
        var typeOptions = this.options.typeOptions,
            points = [this.points[0], InfinityLine.endEdgePoint];
 
        if (typeOptions.type.match(/Line/g)) {
            points[0] = InfinityLine.startEdgePoint;
        }
 
        var line = this.initShape(_Globals2.default.merge(typeOptions.line, {
            type: 'path',
            points: points
        }), false);
 
        typeOptions.line = line.options;
    }
 
});
 
/**
 * An infinity line annotation.
 *
 * @sample highcharts/annotations-advanced/infinity-line/
 *         Infinity Line
 *
 * @extends   annotations.crookedLine
 * @product   highstock
 * @apioption annotations.infinityLine
 */
 
Annotation.types.infinityLine = InfinityLine;
 
exports.default = InfinityLine;