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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
'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,
    CrookedLine = Annotation.types.crookedLine,
    ControlPoint = Annotation.ControlPoint,
    MockPoint = Annotation.MockPoint;
 
function getSecondCoordinate(p1, p2, x) {
    return (p2.y - p1.y) / (p2.x - p1.x) * (x - p1.x) + p1.y;
}
 
/**
 * @class
 * @extends Annotation.CrookedLine
 * @memberOf Annotation
 **/
function Tunnel() {
    CrookedLine.apply(this, arguments);
}
 
_Globals2.default.extendAnnotation(Tunnel, CrookedLine,
/** @lends Annotation.Tunnel# */
{
    getPointsOptions: function getPointsOptions() {
        var pointsOptions = CrookedLine.prototype.getPointsOptions.call(this);
 
        pointsOptions[2] = this.heightPointOptions(pointsOptions[1]);
        pointsOptions[3] = this.heightPointOptions(pointsOptions[0]);
 
        return pointsOptions;
    },
 
    getControlPointsOptions: function getControlPointsOptions() {
        return this.getPointsOptions().slice(0, 2);
    },
 
    heightPointOptions: function heightPointOptions(pointOptions) {
        var heightPointOptions = _Globals2.default.merge(pointOptions);
 
        heightPointOptions.y += this.options.typeOptions.height;
 
        return heightPointOptions;
    },
 
    addControlPoints: function addControlPoints() {
        CrookedLine.prototype.addControlPoints.call(this);
 
        var options = this.options,
            controlPoint = new ControlPoint(this.chart, this, _Globals2.default.merge(options.controlPointOptions, options.typeOptions.heightControlPoint), 2);
 
        this.controlPoints.push(controlPoint);
 
        options.typeOptions.heightControlPoint = controlPoint.options;
    },
 
    addShapes: function addShapes() {
        this.addLine();
        this.addBackground();
    },
 
    addLine: function addLine() {
        var line = this.initShape(_Globals2.default.merge(this.options.typeOptions.line, {
            type: 'path',
            points: [this.points[0], this.points[1], function (target) {
                var pointOptions = MockPoint.pointToOptions(target.annotation.points[2]);
 
                pointOptions.command = 'M';
 
                return pointOptions;
            }, this.points[3]]
        }), false);
 
        this.options.typeOptions.line = line.options;
    },
 
    addBackground: function addBackground() {
        var background = this.initShape(_Globals2.default.merge(this.options.typeOptions.background, {
            type: 'path',
            points: this.points.slice()
        }));
 
        this.options.typeOptions.background = background.options;
    },
 
    /**
     * Translate start or end ("left" or "right") side of the tunnel.
     *
     * @param {number} dx - the amount of x translation
     * @param {number} dy - the amount of y translation
     * @param {boolean} [end] - whether to translate start or end side
     */
    translateSide: function translateSide(dx, dy, end) {
        var topIndex = Number(end),
            bottomIndex = topIndex === 0 ? 3 : 2;
 
        this.translatePoint(dx, dy, topIndex);
        this.translatePoint(dx, dy, bottomIndex);
    },
 
    /**
     * Translate height of the tunnel.
     *
     * @param {number} dh - the amount of height translation
     */
    translateHeight: function translateHeight(dh) {
        this.translatePoint(0, dh, 2);
        this.translatePoint(0, dh, 3);
 
        this.options.typeOptions.height = this.points[3].y - this.points[0].y;
    }
},
 
/**
 * A tunnel annotation.
 *
 * @extends annotations.crookedLine
 * @sample highcharts/annotations-advanced/tunnel/
 *         Tunnel
 * @product highstock
 * @optionparent annotations.tunnel
 */
{
    typeOptions: {
        xAxis: 0,
        yAxis: 0,
        /**
         * Background options.
         *
         * @type {Object}
         * @excluding height, point, points, r, type, width, markerEnd,
         *            markerStart
         */
        background: {
            fill: 'rgba(130, 170, 255, 0.4)',
            strokeWidth: 0
        },
        line: {
            strokeWidth: 1
        },
        /**
         * The height of the annotation in terms of yAxis.
         */
        height: -2,
 
        /**
         * Options for the control point which controls
         * the annotation's height.
         *
         * @extends annotations.crookedLine.controlPointOptions
         * @excluding positioner, events
         */
        heightControlPoint: {
            positioner: function positioner(target) {
                var startXY = MockPoint.pointToPixels(target.points[2]),
                    endXY = MockPoint.pointToPixels(target.points[3]),
                    x = (startXY.x + endXY.x) / 2;
 
                return {
                    x: x - this.graphic.width / 2,
                    y: getSecondCoordinate(startXY, endXY, x) - this.graphic.height / 2
                };
            },
            events: {
                drag: function drag(e, target) {
                    if (target.chart.isInsidePlot(e.chartX - target.chart.plotLeft, e.chartY - target.chart.plotTop)) {
                        target.translateHeight(this.mouseMoveToTranslation(e).y);
 
                        target.redraw(false);
                    }
                }
            }
        }
    },
 
    /**
     * @extends annotations.crookedLine.controlPointOptions
     * @excluding positioner, events
     */
    controlPointOptions: {
        events: {
            drag: function drag(e, target) {
                if (target.chart.isInsidePlot(e.chartX - target.chart.plotLeft, e.chartY - target.chart.plotTop)) {
                    var translation = this.mouseMoveToTranslation(e);
 
                    target.translateSide(translation.x, translation.y, this.index);
 
                    target.redraw(false);
                }
            }
        }
    }
});
 
Annotation.types.tunnel = Tunnel;
 
exports.default = Tunnel;