Skip to content

Commit 231883f

Browse files
committed
lint and factor out constants
1 parent 3130d1c commit 231883f

File tree

3 files changed

+57
-45
lines changed

3 files changed

+57
-45
lines changed

src/traces/scatter/constants.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,16 @@
1010
'use strict';
1111

1212
module.exports = {
13-
PTS_LINESONLY: 20
13+
PTS_LINESONLY: 20,
14+
15+
// fixed parameters of clustering and clipping algorithms
16+
17+
// fraction of clustering tolerance "so close we don't even consider it a new point"
18+
minTolerance: 0.2,
19+
// how fast does clustering tolerance increase as you get away from the visible region
20+
toleranceGrowth: 10,
21+
22+
// number of viewport sizes away from the visible region
23+
// at which we clip all lines to the perimeter
24+
maxScreensAway: 20
1425
};

src/traces/scatter/line_points.js

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,52 +11,54 @@
1111

1212
var BADNUM = require('../../constants/numerical').BADNUM;
1313
var segmentsIntersect = require('../../lib/geometry2d').segmentsIntersect;
14+
var constants = require('./constants');
1415

1516

1617
module.exports = function linePoints(d, opts) {
17-
var xa = opts.xaxis,
18-
ya = opts.yaxis,
19-
simplify = opts.simplify,
20-
connectGaps = opts.connectGaps,
21-
baseTolerance = opts.baseTolerance,
22-
linear = opts.linear,
23-
segments = [],
24-
minTolerance = 0.2, // fraction of tolerance "so close we don't even consider it a new point"
25-
pts = new Array(d.length),
26-
pti = 0,
27-
i,
28-
29-
// pt variables are pixel coordinates [x,y] of one point
30-
clusterStartPt, // these four are the outputs of clustering on a line
31-
clusterEndPt,
32-
clusterHighPt,
33-
clusterLowPt,
34-
thisPt, // "this" is the next point we're considering adding to the cluster
35-
36-
clusterRefDist,
37-
clusterHighFirst, // did we encounter the high point first, then a low point, or vice versa?
38-
clusterUnitVector, // the first two points in the cluster determine its unit vector
39-
// so the second is always in the "High" direction
40-
thisVector, // the pixel delta from clusterStartPt
41-
42-
// val variables are (signed) pixel distances along the cluster vector
43-
clusterHighVal,
44-
clusterLowVal,
45-
thisVal,
46-
47-
// deviation variables are (signed) pixel distances normal to the cluster vector
48-
clusterMinDeviation,
49-
clusterMaxDeviation,
50-
thisDeviation;
18+
var xa = opts.xaxis;
19+
var ya = opts.yaxis;
20+
var simplify = opts.simplify;
21+
var connectGaps = opts.connectGaps;
22+
var baseTolerance = opts.baseTolerance;
23+
var linear = opts.linear;
24+
var segments = [];
25+
var minTolerance = constants.minTolerance;
26+
var pts = new Array(d.length);
27+
var pti = 0;
28+
29+
var i;
30+
31+
// pt variables are pixel coordinates [x,y] of one point
32+
// these four are the outputs of clustering on a line
33+
var clusterStartPt, clusterEndPt, clusterHighPt, clusterLowPt;
34+
35+
// "this" is the next point we're considering adding to the cluster
36+
var thisPt;
37+
38+
// did we encounter the high point first, then a low point, or vice versa?
39+
var clusterHighFirst;
40+
41+
// the first two points in the cluster determine its unit vector
42+
// so the second is always in the "High" direction
43+
var clusterUnitVector;
44+
45+
// the pixel delta from clusterStartPt
46+
var thisVector;
47+
48+
// val variables are (signed) pixel distances along the cluster vector
49+
var clusterRefDist, clusterHighVal, clusterLowVal, thisVal;
50+
51+
// deviation variables are (signed) pixel distances normal to the cluster vector
52+
var clusterMinDeviation, clusterMaxDeviation, thisDeviation;
5153

5254
if(!simplify) {
5355
baseTolerance = minTolerance = -1;
5456
}
5557

5658
// turn one calcdata point into pixel coordinates
5759
function getPt(index) {
58-
var x = xa.c2p(d[index].x),
59-
y = ya.c2p(d[index].y);
60+
var x = xa.c2p(d[index].x);
61+
var y = ya.c2p(d[index].y);
6062
if(x === BADNUM || y === BADNUM) return false;
6163
return [x, y];
6264
}
@@ -65,20 +67,19 @@ module.exports = function linePoints(d, opts) {
6567
function getTolerance(pt) {
6668
var xFrac = pt[0] / xa._length;
6769
var yFrac = pt[1] / ya._length;
68-
return (1 + 10 * Math.max(0, -xFrac, xFrac - 1, -yFrac, yFrac - 1)) * baseTolerance;
70+
return (1 + constants.toleranceGrowth * Math.max(0, -xFrac, xFrac - 1, -yFrac, yFrac - 1)) * baseTolerance;
6971
}
7072

7173
function ptDist(pt1, pt2) {
72-
var dx = pt1[0] - pt2[0],
73-
dy = pt1[1] - pt2[1];
74+
var dx = pt1[0] - pt2[0];
75+
var dy = pt1[1] - pt2[1];
7476
return Math.sqrt(dx * dx + dy * dy);
7577
}
7678

7779
// last bit of filtering: clip paths that are VERY far off-screen
7880
// so we don't get near the browser's hard limit (+/- 2^29 px in Chrome and FF)
7981

80-
// maximum multiple of the screen size to use
81-
var maxScreensAway = 20;
82+
var maxScreensAway = constants.maxScreensAway;
8283

8384
// find the intersections between the segment from pt1 to pt2
8485
// and the large rectangle maxScreensAway around the viewport

test/jasmine/tests/scatter_test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -391,13 +391,13 @@ describe('Test scatter', function() {
391391
function reverseXY(v) { return [v[1], v[0]]; }
392392

393393
ptsIn.forEach(function(ptsIni, i) {
394-
// baseTolerance: disable clustering for this test
395-
var ptsOut = callLinePoints(ptsIni, {baseTolerance: 0});
394+
// disable clustering for these tests
395+
var ptsOut = callLinePoints(ptsIni, {simplify: false});
396396
expect(ptsOut.length).toBe(1, i);
397397
expect(ptsOut[0]).toBeCloseTo2DArray(ptsExpected[i], 1, i);
398398

399399
// swap X and Y and all should work identically
400-
var ptsOut2 = callLinePoints(ptsIni.map(reverseXY), {baseTolerance: 0});
400+
var ptsOut2 = callLinePoints(ptsIni.map(reverseXY), {simplify: false});
401401
expect(ptsOut2.length).toBe(1, i);
402402
expect(ptsOut2[0]).toBeCloseTo2DArray(ptsExpected[i].map(reverseXY), 1, i);
403403
});

0 commit comments

Comments
 (0)