Skip to content

Commit bc4069c

Browse files
committed
cleanup: break shapes into files
- rename Shapes.drawAll -> Shapes.draw - make old Shapes.draw a private method in shapes/draw.js - make old Shapes.convertPath a private method in shapes/draw.js - rm Shapes.add (wasn't used anywhere)
1 parent 27bb36d commit bc4069c

File tree

8 files changed

+878
-775
lines changed

8 files changed

+878
-775
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* Copyright 2012-2016, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
10+
'use strict';
11+
12+
var Axes = require('../../plots/cartesian/axes');
13+
14+
var constants = require('./constants');
15+
var helpers = require('./helpers');
16+
17+
18+
module.exports = function calcAutorange(gd) {
19+
var fullLayout = gd._fullLayout,
20+
shapeList = fullLayout.shapes;
21+
22+
if(!shapeList.length || !gd._fullData.length) return;
23+
24+
for(var i = 0; i < shapeList.length; i++) {
25+
var shape = shapeList[i],
26+
ppad = shape.line.width / 2;
27+
28+
var ax, bounds;
29+
30+
if(shape.xref !== 'paper') {
31+
ax = Axes.getFromId(gd, shape.xref);
32+
bounds = shapeBounds(ax, shape.x0, shape.x1, shape.path, constants.paramIsX);
33+
if(bounds) Axes.expand(ax, bounds, {ppad: ppad});
34+
}
35+
36+
if(shape.yref !== 'paper') {
37+
ax = Axes.getFromId(gd, shape.yref);
38+
bounds = shapeBounds(ax, shape.y0, shape.y1, shape.path, constants.paramIsY);
39+
if(bounds) Axes.expand(ax, bounds, {ppad: ppad});
40+
}
41+
}
42+
};
43+
44+
function shapeBounds(ax, v0, v1, path, paramsToUse) {
45+
var convertVal = (ax.type === 'category') ? Number : ax.d2c;
46+
47+
if(v0 !== undefined) return [convertVal(v0), convertVal(v1)];
48+
if(!path) return;
49+
50+
var min = Infinity,
51+
max = -Infinity,
52+
segments = path.match(constants.segmentRE),
53+
i,
54+
segment,
55+
drawnParam,
56+
params,
57+
val;
58+
59+
if(ax.type === 'date') convertVal = helpers.decodeDate(convertVal);
60+
61+
for(i = 0; i < segments.length; i++) {
62+
segment = segments[i];
63+
drawnParam = paramsToUse[segment.charAt(0)].drawn;
64+
if(drawnParam === undefined) continue;
65+
66+
params = segments[i].substr(1).match(constants.paramRE);
67+
if(!params || params.length < drawnParam) continue;
68+
69+
val = convertVal(params[drawnParam]);
70+
if(val < min) min = val;
71+
if(val > max) max = val;
72+
}
73+
if(max >= min) return [min, max];
74+
}

src/components/shapes/constants.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* Copyright 2012-2016, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
10+
'use strict';
11+
12+
13+
module.exports = {
14+
segmentRE: /[MLHVQCTSZ][^MLHVQCTSZ]*/g,
15+
paramRE: /[^\s,]+/g,
16+
17+
// which numbers in each path segment are x (or y) values
18+
// drawn is which param is a drawn point, as opposed to a
19+
// control point (which doesn't count toward autorange.
20+
// TODO: this means curved paths could extend beyond the
21+
// autorange bounds. This is a bit tricky to get right
22+
// unless we revert to bounding boxes, but perhaps there's
23+
// a calculation we could do...)
24+
paramIsX: {
25+
M: {0: true, drawn: 0},
26+
L: {0: true, drawn: 0},
27+
H: {0: true, drawn: 0},
28+
V: {},
29+
Q: {0: true, 2: true, drawn: 2},
30+
C: {0: true, 2: true, 4: true, drawn: 4},
31+
T: {0: true, drawn: 0},
32+
S: {0: true, 2: true, drawn: 2},
33+
// A: {0: true, 5: true},
34+
Z: {}
35+
},
36+
37+
paramIsY: {
38+
M: {1: true, drawn: 1},
39+
L: {1: true, drawn: 1},
40+
H: {},
41+
V: {0: true, drawn: 0},
42+
Q: {1: true, 3: true, drawn: 3},
43+
C: {1: true, 3: true, 5: true, drawn: 5},
44+
T: {1: true, drawn: 1},
45+
S: {1: true, 3: true, drawn: 5},
46+
// A: {1: true, 6: true},
47+
Z: {}
48+
},
49+
50+
numParams: {
51+
M: 2,
52+
L: 2,
53+
H: 1,
54+
V: 1,
55+
Q: 4,
56+
C: 6,
57+
T: 2,
58+
S: 4,
59+
// A: 7,
60+
Z: 0
61+
}
62+
};

src/components/shapes/defaults.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Copyright 2012-2016, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
10+
'use strict';
11+
12+
var handleShapeDefaults = require('./shape_defaults');
13+
14+
15+
module.exports = function supplyLayoutDefaults(layoutIn, layoutOut) {
16+
var containerIn = layoutIn.shapes || [],
17+
containerOut = layoutOut.shapes = [];
18+
19+
for(var i = 0; i < containerIn.length; i++) {
20+
var shapeIn = containerIn[i] || {},
21+
shapeOut = handleShapeDefaults(shapeIn, layoutOut);
22+
23+
containerOut.push(shapeOut);
24+
}
25+
};

0 commit comments

Comments
 (0)