timeline.src.js
24.4 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
/* *
*
* Timeline Series.
*
* (c) 2010-2019 Highsoft AS
*
* Author: Daniel Studencki
*
* License: www.highcharts.com/license
*
* */
'use strict';
import H from '../parts/Globals.js';
import U from '../parts/Utilities.js';
var defined = U.defined,
isNumber = U.isNumber,
objectEach = U.objectEach;
var addEvent = H.addEvent,
LegendSymbolMixin = H.LegendSymbolMixin,
TrackerMixin = H.TrackerMixin,
merge = H.merge,
pick = H.pick,
Point = H.Point,
Series = H.Series,
seriesType = H.seriesType,
seriesTypes = H.seriesTypes;
/**
* The timeline series type.
*
* @private
* @class
* @name Highcharts.seriesTypes.timeline
*
* @augments Highcharts.Series
*/
seriesType('timeline', 'line',
/**
* The timeline series presents given events along a drawn line.
*
* @sample highcharts/series-timeline/alternate-labels
* Timeline series
* @sample highcharts/series-timeline/inverted
* Inverted timeline
* @sample highcharts/series-timeline/datetime-axis
* With true datetime axis
*
* @extends plotOptions.line
* @since 7.0.0
* @product highcharts
* @excluding animationLimit, boostThreshold, connectEnds, connectNulls,
* cropThreshold, dashStyle, findNearestPointBy,
* getExtremesFromAll, lineWidth, negativeColor,
* pointInterval, pointIntervalUnit, pointPlacement,
* pointStart, softThreshold, stacking, step, threshold,
* turboThreshold, zoneAxis, zones
* @optionparent plotOptions.timeline
*/
{
colorByPoint: true,
stickyTracking: false,
ignoreHiddenPoint: true,
legendType: 'point',
lineWidth: 4,
tooltip: {
headerFormat: '<span style="color:{point.color}">\u25CF</span> ' +
'<span style="font-size: 10px"> {point.key}</span><br/>',
pointFormat: '{point.description}'
},
states: {
hover: {
lineWidthPlus: 0
}
},
dataLabels: {
enabled: true,
allowOverlap: true,
/**
* The width of the line connecting the data label to the point.
*
*
* In styled mode, the connector stroke width is given in the
* `.highcharts-data-label-connector` class.
*
* @sample {highcharts} highcharts/series-timeline/connector-styles
* Custom connector width and color
*/
connectorWidth: 1,
/**
* The color of the line connecting the data label to the point.
* The default color is the same as the point's color.
*
* In styled mode, the connector stroke is given in the
* `.highcharts-data-label-connector` class.
*
* @type {String}
* @apioption plotOptions.timeline.dataLabels.connectorColor
* @sample {highcharts} highcharts/series-timeline/connector-styles
* Custom connector width and color
*/
backgroundColor: '#ffffff',
/**
* @type {Highcharts.FormatterCallbackFunction<object>}
* @default function () {
*
* var format;
*
* if (!this.series.chart.styledMode) {
* format = '<span style="color:' + this.point.color +
* '">● </span>';
* } else {
* format = '<span>● </span>';
* }
* format += '<span>' + (this.key || '') + '</span><br/>' +
* (this.point.label || '');
* return format;
* }
* @apioption plotOptions.timeline.dataLabels.formatter
*/
formatter: function () {
var format;
if (!this.series.chart.styledMode) {
format = '<span style="color:' + this.point.color +
'">● </span>';
} else {
format = '<span>● </span>';
}
format += '<span class="highcharts-strong">' +
(this.key || '') + '</span><br/>' +
(this.point.label || '');
return format;
},
borderWidth: 1,
borderColor: '#999999',
borderRadius: 3,
/**
* A pixel value defining the distance between the data label
* and the point. Negative numbers puts the label on top
* of the point.
*/
distance: 100,
/**
* Whether to position data labels alternately. For example, if
* [distance](#plotOptions.timeline.dataLabels.distance) is set
* equal to `100`, then data labels will be positioned alternately
* (on both sides of the point) at a distance of 100px.
*
* @sample {highcharts} highcharts/series-timeline/alternate-disabled
* Alternate disabled
*/
alternate: true,
verticalAlign: 'middle',
color: '#333333',
style: {
textOutline: 'none',
fontWeight: 'normal',
fontSize: '12px'
},
/**
* Shadow options for the data label.
* @type {boolean|Highcharts.CSSObject}
*/
shadow: false
},
marker: {
enabledThreshold: 0,
symbol: 'square',
radius: 6,
lineWidth: 2,
height: 15
},
showInLegend: false,
colorKey: 'x'
},
/**
* @lends Highcharts.Series#
*/
{
trackerGroups: ['markerGroup', 'dataLabelsGroup'],
// Use a simple symbol from LegendSymbolMixin
drawLegendSymbol: LegendSymbolMixin.drawRectangle,
// Use a group of trackers from TrackerMixin
drawTracker: TrackerMixin.drawTrackerPoint,
init: function () {
var series = this;
Series.prototype.init.apply(series, arguments);
addEvent(series, 'afterTranslate', function () {
var lastPlotX,
closestPointRangePx = Number.MAX_VALUE;
series.points.forEach(function (point) {
// Set the isInside parameter basing also on the real point
// visibility, in order to avoid showing hidden points
// in drawPoints method.
point.isInside = point.isInside && point.visible;
// New way of calculating closestPointRangePx value, which
// respects the real point visibility is needed.
if (point.visible && !point.isNull) {
if (defined(lastPlotX)) {
closestPointRangePx = Math.min(
closestPointRangePx,
Math.abs(point.plotX - lastPlotX)
);
}
lastPlotX = point.plotX;
}
});
series.closestPointRangePx = closestPointRangePx;
});
// Distribute data labels before rendering them. Distribution is
// based on the 'dataLabels.distance' and 'dataLabels.alternate'
// property.
addEvent(series, 'drawDataLabels', function () {
// Distribute data labels basing on defined algorithm.
series.distributeDL();
});
addEvent(series, 'afterDrawDataLabels', function () {
var dataLabel;
// Draw or align connector for each point.
series.points.forEach(function (point) {
dataLabel = point.dataLabel;
if (dataLabel) {
// Within this wrap method is necessary to save the
// current animation params, because the data label
// target position (after animation) is needed to align
// connectors.
dataLabel.animate = function (params) {
if (this.targetPosition) {
this.targetPosition = params;
}
return H.SVGElement.prototype.animate.apply(
this,
arguments
);
};
// Initialize the targetPosition field within data label
// object. It's necessary because there is need to know
// expected position of specific data label, when
// aligning connectors. This field is overrided inside
// of SVGElement.animate() wrapped method.
if (!dataLabel.targetPosition) {
dataLabel.targetPosition = {};
}
return point.drawConnector();
}
});
});
addEvent(series.chart, 'afterHideOverlappingLabels', function () {
series.points.forEach(function (p) {
if (
p.connector &&
p.dataLabel &&
p.dataLabel.oldOpacity !== p.dataLabel.newOpacity
) {
p.alignConnector();
}
});
});
},
alignDataLabel: function (point, dataLabel) {
var series = this,
isInverted = series.chart.inverted,
visiblePoints = series.visibilityMap.filter(function (point) {
return point;
}),
visiblePointsCount = series.visiblePointsCount,
pointIndex = visiblePoints.indexOf(point),
isFirstOrLast = !pointIndex ||
pointIndex === visiblePointsCount - 1,
dataLabelsOptions = series.options.dataLabels,
userDLOptions = point.userDLOptions || {},
// Define multiplier which is used to calculate data label
// width. If data labels are alternate, they have two times more
// space to adapt (excepting first and last ones, which has only
// one and half), than in case of placing all data labels side
// by side.
multiplier = dataLabelsOptions.alternate ?
(isFirstOrLast ? 1.5 : 2) :
1,
distance,
availableSpace = Math.floor(
series.xAxis.len / visiblePointsCount
),
pad = dataLabel.padding,
targetDLWidth,
styles;
// Adjust data label width to the currently available space.
if (point.visible) {
distance = Math.abs(
userDLOptions.x || point.options.dataLabels.x
);
if (isInverted) {
targetDLWidth = (
(distance - pad) * 2 - (point.itemHeight / 2)
);
styles = {
width: targetDLWidth,
// Apply ellipsis when data label height is exceeded.
textOverflow: dataLabel.width / targetDLWidth *
dataLabel.height / 2 > availableSpace * multiplier ?
'ellipsis' : 'none'
};
} else {
styles = {
width: userDLOptions.width ||
dataLabelsOptions.width ||
availableSpace * multiplier - (pad * 2)
};
}
dataLabel.css(styles);
if (!series.chart.styledMode) {
dataLabel.shadow(dataLabelsOptions.shadow);
}
}
Series.prototype.alignDataLabel.apply(series, arguments);
},
processData: function () {
var series = this,
visiblePoints = 0,
i;
series.visibilityMap = series.getVisibilityMap();
// Calculate currently visible points.
series.visibilityMap.forEach(function (point) {
if (point) {
visiblePoints++;
}
});
series.visiblePointsCount = visiblePoints;
for (i = 0; i < series.xData.length; i++) {
series.yData[i] = 1;
}
Series.prototype.processData.call(this, arguments);
},
getXExtremes: function (xData) {
var series = this,
filteredData = xData.filter(function (x, i) {
return series.points[i].isValid() &&
series.points[i].visible;
});
return {
min: H.arrayMin(filteredData),
max: H.arrayMax(filteredData)
};
},
generatePoints: function () {
var series = this;
Series.prototype.generatePoints.apply(series);
series.points.forEach(function (point, i) {
point.applyOptions({
x: series.xData[i]
}, series.xData[i]);
});
},
getVisibilityMap: function () {
var series = this,
map = (series.data.length ?
series.data : series.userOptions.data
).map(function (point) {
return (
point &&
point.visible !== false &&
!point.isNull
) ? point : false;
});
return map;
},
distributeDL: function () {
var series = this,
dataLabelsOptions = series.options.dataLabels,
options,
pointDLOptions,
newOptions = {},
visibilityIndex = 1,
distance = dataLabelsOptions.distance;
series.points.forEach(function (point) {
if (point.visible && !point.isNull) {
options = point.options;
pointDLOptions = point.options.dataLabels;
if (!series.hasRendered) {
point.userDLOptions = merge({}, pointDLOptions);
}
newOptions[series.chart.inverted ? 'x' : 'y'] =
dataLabelsOptions.alternate && visibilityIndex % 2 ?
-distance : distance;
options.dataLabels = merge(newOptions, point.userDLOptions);
visibilityIndex++;
}
});
},
markerAttribs: function (point, state) {
var series = this,
seriesMarkerOptions = series.options.marker,
seriesStateOptions,
pointMarkerOptions = point.marker || {},
symbol = (
pointMarkerOptions.symbol || seriesMarkerOptions.symbol
),
pointStateOptions,
width = pick(
pointMarkerOptions.width,
seriesMarkerOptions.width,
series.closestPointRangePx
),
height = pick(
pointMarkerOptions.height,
seriesMarkerOptions.height
),
radius = 0,
attribs;
// Call default markerAttribs method, when the xAxis type
// is set to datetime.
if (series.xAxis.isDatetimeAxis) {
return seriesTypes.line.prototype.markerAttribs
.call(this, point, state);
}
// Handle hover and select states
if (state) {
seriesStateOptions = seriesMarkerOptions.states[state] || {};
pointStateOptions = pointMarkerOptions.states &&
pointMarkerOptions.states[state] || {};
radius = pick(
pointStateOptions.radius,
seriesStateOptions.radius,
radius + (
seriesStateOptions.radiusPlus || 0
)
);
}
point.hasImage = symbol && symbol.indexOf('url') === 0;
attribs = {
x: Math.floor(point.plotX) - (width / 2) - (radius / 2),
y: point.plotY - (height / 2) - (radius / 2),
width: width + radius,
height: height + radius
};
return attribs;
},
bindAxes: function () {
var series = this;
Series.prototype.bindAxes.call(series);
['xAxis', 'yAxis'].forEach(function (axis) {
// Initially set the linked xAxis type to category.
if (axis === 'xAxis' && !series[axis].userOptions.type) {
series[axis].categories = series[axis].hasNames = true;
}
});
}
},
/**
* @lends Highcharts.Point#
*/
{
init: function () {
var point = Point.prototype.init.apply(this, arguments);
point.name = pick(point.name, 'Event');
point.y = 1;
return point;
},
isValid: function () {
return this.options.y !== null;
},
setVisible: function (vis, redraw) {
var point = this,
series = point.series;
redraw = pick(redraw, series.options.ignoreHiddenPoint);
seriesTypes.pie.prototype.pointClass.prototype
.setVisible.call(point, vis, false);
// Process new data
series.processData();
if (redraw) {
series.chart.redraw();
}
},
setState: function () {
var proceed = Series.prototype.pointClass.prototype.setState;
// Prevent triggering the setState method on null points.
if (!this.isNull) {
proceed.apply(this, arguments);
}
},
getConnectorPath: function () {
var point = this,
chart = point.series.chart,
xAxisLen = point.series.xAxis.len,
inverted = chart.inverted,
direction = inverted ? 'x2' : 'y2',
dl = point.dataLabel,
targetDLPos = dl.targetPosition,
coords = {
x1: point.plotX,
y1: point.plotY,
x2: point.plotX,
y2: isNumber(targetDLPos.y) ? targetDLPos.y : dl.y
},
negativeDistance = (
(dl.alignAttr || dl)[direction[0]] <
point.series.yAxis.len / 2
),
path;
// Recalculate coords when the chart is inverted.
if (inverted) {
coords = {
x1: point.plotY,
y1: xAxisLen - point.plotX,
x2: targetDLPos.x || dl.x,
y2: xAxisLen - point.plotX
};
}
// Subtract data label width or height from expected coordinate so
// that the connector would start from the appropriate edge.
if (negativeDistance) {
coords[direction] += dl[inverted ? 'width' : 'height'];
}
// Change coordinates so that they will be relative to data label.
objectEach(coords, function (_coord, i) {
coords[i] -= (dl.alignAttr || dl)[i[0]];
});
path = chart.renderer.crispLine([
'M',
coords.x1,
coords.y1,
'L',
coords.x2,
coords.y2
], dl.options.connectorWidth);
return path;
},
drawConnector: function () {
var point = this,
series = point.series;
if (!point.connector) {
point.connector = series.chart.renderer
.path(point.getConnectorPath())
.attr({
zIndex: -1
})
.add(point.dataLabel);
}
if (point.series.chart.isInsidePlot( // #10507
point.dataLabel.x, point.dataLabel.y
)) {
point.alignConnector();
}
},
alignConnector: function () {
var point = this,
series = point.series,
connector = point.connector,
dl = point.dataLabel,
dlOptions = point.dataLabel.options = merge(
series.options.dataLabels,
point.options.dataLabels
),
chart = point.series.chart,
bBox = connector.getBBox(),
plotPos = {
x: bBox.x + dl.translateX,
y: bBox.y + dl.translateY
},
isVisible;
// Include a half of connector width in order to run animation,
// when connectors are aligned to the plot area edge.
if (chart.inverted) {
plotPos.y -= dl.options.connectorWidth / 2;
} else {
plotPos.x += dl.options.connectorWidth / 2;
}
isVisible = chart.isInsidePlot(
plotPos.x, plotPos.y
);
connector[isVisible ? 'animate' : 'attr']({
d: point.getConnectorPath()
});
if (!series.chart.styledMode) {
connector.attr({
stroke: dlOptions.connectorColor || point.color,
'stroke-width': dlOptions.connectorWidth,
opacity: dl[
defined(dl.newOpacity) ? 'newOpacity' : 'opacity'
]
});
}
}
});
/**
* The `timeline` series. If the [type](#series.timeline.type) option is
* not specified, it is inherited from [chart.type](#chart.type).
*
* @extends series,plotOptions.timeline
* @excluding animationLimit, boostThreshold, connectEnds, connectNulls,
* cropThreshold, dashStyle, dataParser, dataURL, findNearestPointBy,
* getExtremesFromAll, lineWidth, negativeColor,
* pointInterval, pointIntervalUnit, pointPlacement, pointStart,
* softThreshold, stacking, stack, step, threshold, turboThreshold,
* zoneAxis, zones
* @product highcharts
* @apioption series.timeline
*/
/**
* An array of data points for the series. For the `timeline` series type,
* points can be given with three general parameters, `name`, `label`,
* and `description`:
*
* Example:
*
* ```js
* series: [{
* type: 'timeline',
* data: [{
* name: 'Jan 2018',
* label: 'Some event label',
* description: 'Description to show in tooltip'
* }]
* }]
* ```
* If all points additionally have the `x` values, and xAxis type is set to
* `datetime`, then events are laid out on a true time axis, where their
* placement reflects the actual time between them.
*
* @sample {highcharts} highcharts/series-timeline/alternate-labels
* Alternate labels
* @sample {highcharts} highcharts/series-timeline/datetime-axis
* Real time intervals
*
* @type {Array<*>}
* @extends series.line.data
* @excluding marker, y
* @product highcharts
* @apioption series.timeline.data
*/
/**
* The name of event.
*
* @type {string}
* @product highcharts
* @apioption series.timeline.data.name
*/
/**
* The label of event.
*
* @type {string}
* @product highcharts
* @apioption series.timeline.data.label
*/
/**
* The description of event. This description will be shown in tooltip.
*
* @type {string}
* @product highcharts
* @apioption series.timeline.data.description
*/