Skip to content

Commit 41c8aaf

Browse files
committed
comments
1 parent 371f10b commit 41c8aaf

File tree

2 files changed

+72
-14
lines changed

2 files changed

+72
-14
lines changed

src/controllers/controller.radar.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ module.exports = DatasetController.extend({
150150
point._model = {
151151
x,
152152
y,
153-
r: pointPosition.r,
153+
r: pointPosition.r, // `r` refers to radial axis, value is in radians
154154
skip: isNaN(x) || isNaN(y),
155155
// Appearance
156156
radius: options.radius,

src/elements/element.line.js

Lines changed: 71 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,27 @@ function lineTo(ctx, previous, target) {
3838
}
3939

4040
function getLineMethod(line) {
41-
return line.steppedLine ? _steppedLineTo
42-
: !line.tension ? lineTo
43-
: _bezierCurveTo;
41+
if (line.steppedLine) {
42+
return _steppedLineTo;
43+
}
44+
45+
if (line.tension) {
46+
return _bezierCurveTo;
47+
}
48+
49+
return lineTo;
4450
}
4551

4652
function getInterpolationMethod(line) {
47-
return line.steppedLine ? steppedInterpolation
48-
: !line.tension ? pointInLine
49-
: bezierInterpolation;
53+
if (line.steppedLine) {
54+
return steppedInterpolation;
55+
}
56+
57+
if (line.tension) {
58+
return bezierInterpolation;
59+
}
60+
61+
return pointInLine;
5062
}
5163

5264
function pathSegment(ctx, line, segment, options) {
@@ -104,11 +116,11 @@ function fastPath(ctx, points, line, options) {
104116
let {move = true, startIndex = 0, reverse = false} = options;
105117
let count = 0;
106118
let avgX = 0;
107-
let i, index, point, truncX, prevX, minY, maxY, lastY;
119+
let i, prevX, minY, maxY, lastY;
108120

109121
for (i = 0; i < ilen; ++i) {
110-
index = (startIndex + i) % ilen;
111-
point = points[reverse ? ilen - index - 1 : index]._view;
122+
const index = (startIndex + i) % ilen;
123+
const point = points[reverse ? ilen - index - 1 : index]._view;
112124

113125
// If point is skipped, we either move to next (not skipped) point
114126
// or line to it if spanGaps is true. `move` can already be true.
@@ -117,8 +129,8 @@ function fastPath(ctx, points, line, options) {
117129
continue;
118130
}
119131

120-
let {x, y} = point;
121-
truncX = x | 0; // truncated x-coordinate
132+
const {x, y} = point;
133+
const truncX = x | 0; // truncated x-coordinate
122134

123135
if (move) {
124136
ctx.moveTo(x, y);
@@ -210,6 +222,18 @@ function getSegment(segment, points, bounds) {
210222
return {start, end, loop};
211223
}
212224

225+
/**
226+
* Returns the sub-segment(s) of a line segment that fall in the given bounds
227+
* @param {object} segment
228+
* @param {number} segment.start - start index of the segment, referring the points array
229+
* @param {number} segment.end - end index of the segment, referring the points array
230+
* @param {boolean} segment.loop - indicates that the segment is a loop
231+
* @param {Point[]} points - the points that this segment refers to
232+
* @param {object} bounds
233+
* @param {string} bounds.axis - the axis we are bounding. `x`, `y` or `r`.
234+
* @param {number} bounds.start - start value on the axis
235+
* @param {number} bounds.end - end value on the axis
236+
**/
213237
export function boundSegment(segment, points, bounds) {
214238
if (!bounds) {
215239
return [segment];
@@ -252,6 +276,14 @@ export function boundSegment(segment, points, bounds) {
252276
return result;
253277
}
254278

279+
/**
280+
* Returns the segments of then line that are inside given bounds
281+
* @param {Line} line
282+
* @param {object} bounds
283+
* @param {string} bounds.axis - the axis we are bounding. `x`, `y` or `r`.
284+
* @param {number} bounds.start - start value on the axis
285+
* @param {number} bounds.end - end value on the axis
286+
*/
255287
export function boundSegments(line, bounds) {
256288
const result = [];
257289

@@ -278,6 +310,11 @@ function findStart(points, count, loop) {
278310
return start % count;
279311
}
280312

313+
/**
314+
* Compute the continuous segments that define the whole line
315+
* There can be skipped points within a segment, if spanGaps is true.
316+
* @param {Line} line
317+
*/
281318
function computeSegments(line) {
282319
const points = line.points;
283320
const spanGaps = line._view.spanGaps;
@@ -339,19 +376,34 @@ class Line extends Element {
339376
return this._segments || (this._segments = computeSegments(this));
340377
}
341378

379+
/**
380+
* First non-skipped point on this line
381+
* @returns {Point|undefined}
382+
*/
342383
first() {
343384
const segments = this.segments;
344385
const points = this.points;
345386
return segments.length && points[segments[0].start]._view;
346387
}
347388

389+
/**
390+
* Last non-skipped point on this line
391+
* @returns {Point|undefined}
392+
*/
348393
last() {
349394
const segments = this.segments;
350395
const points = this.points;
351396
const count = segments.length;
352397
return count && points[segments[count - 1].end]._view;
353398
}
354399

400+
/**
401+
* Interpolate a point in this line at the same value on `axis` as
402+
* the reference `point` provided
403+
* @param {Point} point - the reference point
404+
* @param {string} axis - the axis to match on
405+
* @returns {Point|undefined}
406+
*/
355407
interpolate(point, axis) {
356408
const me = this;
357409
const vm = me._view;
@@ -364,7 +416,7 @@ class Line extends Element {
364416
}
365417

366418
const result = [];
367-
const method = getInterpolationMethod(vm);
419+
const _interpolate = getInterpolationMethod(vm);
368420
let i, ilen;
369421
for (i = 0, ilen = segments.length; i < ilen; ++i) {
370422
const {start, end} = segments[i];
@@ -375,13 +427,19 @@ class Line extends Element {
375427
continue;
376428
}
377429
const t = Math.abs((value - p1[axis]) / (p2[axis] - p1[axis]));
378-
let interpolated = method(p1, p2, t, vm.steppedLine);
430+
let interpolated = _interpolate(p1, p2, t, vm.steppedLine);
379431
interpolated[axis] = point[axis];
380432
result.push(interpolated);
381433
}
382434
return result.lenght === 1 ? result[0] : result;
383435
}
384436

437+
/**
438+
* Append a segment of this line to current path.
439+
* @param {CanvasRenderingContext2D} ctx
440+
* @param {object} segment
441+
* @param {object} options
442+
*/
385443
pathSegment(ctx, segment, options) {
386444
return pathSegment(ctx, this, segment, options);
387445
}

0 commit comments

Comments
 (0)