-
Notifications
You must be signed in to change notification settings - Fork 11.9k
/
element.line.js
359 lines (311 loc) · 9.37 KB
/
element.line.js
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
import defaults from '../core/core.defaults';
import Element from '../core/core.element';
import {_bezierInterpolation, _pointInLine, _steppedInterpolation} from '../helpers/helpers.interpolation';
import {_computeSegments, _boundSegments} from '../helpers/helpers.segment';
import {_steppedLineTo, _bezierCurveTo} from '../helpers/helpers.canvas';
import {_updateBezierControlPoints} from '../helpers/helpers.curve';
/**
* @typedef { import("./element.point").default } Point
*/
const defaultColor = defaults.color;
defaults.set('elements', {
line: {
backgroundColor: defaultColor,
borderCapStyle: 'butt',
borderColor: defaultColor,
borderDash: [],
borderDashOffset: 0,
borderJoinStyle: 'miter',
borderWidth: 3,
capBezierPoints: true,
fill: true,
tension: 0.4
}
});
function setStyle(ctx, vm) {
ctx.lineCap = vm.borderCapStyle;
ctx.setLineDash(vm.borderDash);
ctx.lineDashOffset = vm.borderDashOffset;
ctx.lineJoin = vm.borderJoinStyle;
ctx.lineWidth = vm.borderWidth;
ctx.strokeStyle = vm.borderColor;
}
function lineTo(ctx, previous, target) {
ctx.lineTo(target.x, target.y);
}
function getLineMethod(options) {
if (options.stepped) {
return _steppedLineTo;
}
if (options.tension) {
return _bezierCurveTo;
}
return lineTo;
}
/**
* Create path from points, grouping by truncated x-coordinate
* Points need to be in order by x-coordinate for this to work efficiently
* @param {CanvasRenderingContext2D} ctx - Context
* @param {Line} line
* @param {object} segment
* @param {number} segment.start - start index of the segment, referring the points array
* @param {number} segment.end - end index of the segment, referring the points array
* @param {boolean} segment.loop - indicates that the segment is a loop
* @param {object} params
* @param {object} params.move - move to starting point (vs line to it)
* @param {object} params.reverse - path the segment from end to start
*/
function pathSegment(ctx, line, segment, params) {
const {start, end, loop} = segment;
const {points, options} = line;
const lineMethod = getLineMethod(options);
const count = points.length;
// eslint-disable-next-line prefer-const
let {move = true, reverse} = params || {};
const ilen = end < start ? count + end - start : end - start;
let i, point, prev;
for (i = 0; i <= ilen; ++i) {
point = points[(start + (reverse ? ilen - i : i)) % count];
if (point.skip) {
// If there is a skipped point inside a segment, spanGaps must be true
continue;
} else if (move) {
ctx.moveTo(point.x, point.y);
move = false;
} else {
lineMethod(ctx, prev, point, reverse, options.stepped);
}
prev = point;
}
if (loop) {
point = points[(start + (reverse ? ilen : 0)) % count];
lineMethod(ctx, prev, point, reverse, options.stepped);
}
return !!loop;
}
/**
* Create path from points, grouping by truncated x-coordinate
* Points need to be in order by x-coordinate for this to work efficiently
* @param {CanvasRenderingContext2D} ctx - Context
* @param {Line} line
* @param {object} segment
* @param {number} segment.start - start index of the segment, referring the points array
* @param {number} segment.end - end index of the segment, referring the points array
* @param {boolean} segment.loop - indicates that the segment is a loop
* @param {object} params
* @param {object} params.move - move to starting point (vs line to it)
* @param {object} params.reverse - path the segment from end to start
*/
function fastPathSegment(ctx, line, segment, params) {
const points = line.points;
const count = points.length;
const {start, end} = segment;
const {move = true, reverse} = params || {};
const ilen = end < start ? count + end - start : end - start;
let avgX = 0;
let countX = 0;
let i, point, prevX, minY, maxY, lastY;
if (move) {
point = points[(start + (reverse ? ilen : 0)) % count];
ctx.moveTo(point.x, point.y);
}
for (i = 0; i <= ilen; ++i) {
point = points[(start + (reverse ? ilen - i : i)) % count];
if (point.skip) {
// If there is a skipped point inside a segment, spanGaps must be true
continue;
}
const x = point.x;
const y = point.y;
const truncX = x | 0; // truncated x-coordinate
if (truncX === prevX) {
// Determine `minY` / `maxY` and `avgX` while we stay within same x-position
if (y < minY) {
minY = y;
} else if (y > maxY) {
maxY = y;
}
// For first point in group, countX is `0`, so average will be `x` / 1.
avgX = (countX * avgX + x) / ++countX;
} else {
if (minY !== maxY) {
// Draw line to maxY and minY, using the average x-coordinate
ctx.lineTo(avgX, maxY);
ctx.lineTo(avgX, minY);
// Line to y-value of last point in group. So the line continues
// from correct position. Not using move, to have solid path.
ctx.lineTo(avgX, lastY);
}
// Draw line to next x-position, using the first (or only)
// y-value in that group
ctx.lineTo(x, y);
prevX = truncX;
countX = 0;
minY = maxY = y;
}
// Keep track of the last y-value in group
lastY = y;
}
}
/**
* @param {Line} line - the line
* @returns {function}
* @private
*/
function _getSegmentMethod(line) {
const opts = line.options;
const borderDash = opts.borderDash && opts.borderDash.length;
const useFastPath = !line._loop && !opts.tension && !opts.stepped && !borderDash;
return useFastPath ? fastPathSegment : pathSegment;
}
/**
* @private
*/
function _getInterpolationMethod(options) {
if (options.stepped) {
return _steppedInterpolation;
}
if (options.tension) {
return _bezierInterpolation;
}
return _pointInLine;
}
export default class Line extends Element {
static _type = 'line';
constructor(cfg) {
super();
this.options = undefined;
this._loop = undefined;
this._fullLoop = undefined;
this._controlPointsUpdated = undefined;
this._points = undefined;
this._segments = undefined;
if (cfg) {
Object.assign(this, cfg);
}
}
updateControlPoints(chartArea) {
const me = this;
if (me._controlPointsUpdated) {
return;
}
const options = me.options;
if (options.tension && !options.stepped) {
const loop = options.spanGaps ? me._loop : me._fullLoop;
_updateBezierControlPoints(me._points, options, chartArea, loop);
}
}
set points(points) {
this._points = points;
delete this._segments;
}
get points() {
return this._points;
}
get segments() {
return this._segments || (this._segments = _computeSegments(this));
}
/**
* First non-skipped point on this line
* @returns {Point|undefined}
*/
first() {
const segments = this.segments;
const points = this.points;
return segments.length && points[segments[0].start];
}
/**
* Last non-skipped point on this line
* @returns {Point|undefined}
*/
last() {
const segments = this.segments;
const points = this.points;
const count = segments.length;
return count && points[segments[count - 1].end];
}
/**
* Interpolate a point in this line at the same value on `property` as
* the reference `point` provided
* @param {Point} point - the reference point
* @param {string} property - the property to match on
* @returns {Point|undefined}
*/
interpolate(point, property) {
const me = this;
const options = me.options;
const value = point[property];
const points = me.points;
const segments = _boundSegments(me, {property, start: value, end: value});
if (!segments.length) {
return;
}
const result = [];
const _interpolate = _getInterpolationMethod(options);
let i, ilen;
for (i = 0, ilen = segments.length; i < ilen; ++i) {
const {start, end} = segments[i];
const p1 = points[start];
const p2 = points[end];
if (p1 === p2) {
result.push(p1);
continue;
}
const t = Math.abs((value - p1[property]) / (p2[property] - p1[property]));
const interpolated = _interpolate(p1, p2, t, options.stepped);
interpolated[property] = point[property];
result.push(interpolated);
}
return result.length === 1 ? result[0] : result;
}
/**
* Append a segment of this line to current path.
* @param {CanvasRenderingContext2D} ctx
* @param {object} segment
* @param {number} segment.start - start index of the segment, referring the points array
* @param {number} segment.end - end index of the segment, referring the points array
* @param {boolean} segment.loop - indicates that the segment is a loop
* @param {object} params
* @param {object} params.move - move to starting point (vs line to it)
* @param {object} params.reverse - path the segment from end to start
* @returns {undefined|boolean} - true if the segment is a full loop (path should be closed)
*/
pathSegment(ctx, segment, params) {
const segmentMethod = _getSegmentMethod(this);
return segmentMethod(ctx, this, segment, params);
}
/**
* Append all segments of this line to current path.
* @param {CanvasRenderingContext2D} ctx
* @returns {undefined|boolean} - true if line is a full loop (path should be closed)
*/
path(ctx) {
const me = this;
const segments = me.segments;
const ilen = segments.length;
const segmentMethod = _getSegmentMethod(me);
let loop = me._loop;
for (let i = 0; i < ilen; ++i) {
loop &= segmentMethod(ctx, me, segments[i]);
}
return !!loop;
}
/**
* Draw
* @param {CanvasRenderingContext2D} ctx
*/
draw(ctx) {
const me = this;
if (!me.points.length) {
return;
}
ctx.save();
setStyle(ctx, me.options);
ctx.beginPath();
if (me.path(ctx)) {
ctx.closePath();
}
ctx.stroke();
ctx.restore();
}
}