8000 Break up annotations and shapes components by etpinard · Pull Request #833 · plotly/plotly.js · GitHub
[go: up one dir, main page]

Skip to content

Break up annotations and shapes components #833

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 10, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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)
  • Loading branch information
etpinard committed Aug 8, 2016
commit bc4069c7dffa8985922785fed284bcb727b234ac
74 changes: 74 additions & 0 deletions src/components/shapes/calc_autorange.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* Copyright 2012-2016, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/


'use strict';

var Axes = require('../../plots/cartesian/axes');

var constants = require('./constants');
var helpers = require('./helpers');


module.exports = function calcAutorange(gd) {
var fullLayout = gd._fullLayout,
shapeList = fullLayout.shapes;

if(!shapeList.length || !gd._fullData.length) return;

for(var i = 0; i < shapeList.length; i++) {
var shape = shapeList[i],
ppad = shape.line.width / 2;

var ax, bounds;

if(shape.xref !== 'paper') {
ax = Axes.getFromId(gd, shape.xref);
bounds = shapeBounds(ax, shape.x0, shape.x1, shape.path, constants.paramIsX);
if(bounds) Axes.expand(ax, bounds, {ppad: ppad});
}

if(shape.yref !== 'paper') {
ax = Axes.getFromId(gd, shape.yref);
bounds = shapeBounds(ax, shape.y0, shape.y1, shape.path, constants.paramIsY);
if(bounds) Axes.expand(ax, bounds, {ppad: ppad});
}
}
};

function shapeBounds(ax, v0, v1, path, paramsToUse) {
var convertVal = (ax.type === 'category') ? Number : ax.d2c;

if(v0 !== undefined) return [convertVal(v0), convertVal(v1)];
if(!path) return;

var min = Infinity,
max = -Infinity,
segments = path.match(constants.segmentRE),
i,
segment,
drawnParam,
params,
val;

if(ax.type === 'date') convertVal = helpers.decodeDate(convertVal);

for(i = 0; i < segments.length; i++) {
segment = segments[i];
drawnParam = paramsToUse[segment.charAt(0)].drawn;
if(drawnParam === undefined) continue;

params = segments[i].substr(1).match(constants.paramRE);
if(!params || params.length < drawnParam) continue;

val = convertVal(params[drawnParam]);
if(val < min) min = val;
if(val > max) max = val;
}
if(max >= min) return [min, max];
}
62 changes: 62 additions & 0 deletions src/components/shapes/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* Copyright 2012-2016, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/


'use strict';


module.exports = {
segmentRE: /[MLHVQCTSZ][^MLHVQCTSZ]*/g,
paramRE: /[^\s,]+/g,

// which numbers in each path segment are x (or y) values
// drawn is which param is a drawn point, as opposed to a
// control point (which doesn't count toward autorange.
// TODO: this means curved paths could extend beyond the
// autorange bounds. This is a bit tricky to get right
// unless we revert to bounding boxes, but perhaps there's
// a calculation we could do...)
paramIsX: {
M: {0: true, drawn: 0},
L: {0: true, drawn: 0},
H: {0: true, drawn: 0},
V: {},
Q: {0: true, 2: true, drawn: 2},
C: {0: true, 2: true, 4: true, drawn: 4},
T: {0: true, drawn: 0},
S: {0: true, 2: true, drawn: 2},
// A: {0: true, 5: true},
Z: {}
},

paramIsY: {
M: {1: true, drawn: 1},
L: {1: true, drawn: 1},
H: {},
V: {0: true, drawn: 0},
Q: {1: true, 3: true, drawn: 3},
C: {1: true, 3: true, 5: true, drawn: 5},
T: {1: true, drawn: 1},
S: {1: true, 3: true, drawn: 5},
// A: {1: true, 6: true},
Z: {}
},

numParams: {
M: 2,
L: 2,
H: 1,
V: 1,
Q: 4,
C: 6,
T: 2,
S: 4,
// A: 7,
Z: 0
}
};
25 changes: 25 additions & 0 deletions src/components/shapes/defaults.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright 2012-2016, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/


'use strict';

var handleShapeDefaults = require('./shape_defaults');


module.exports = function supplyLayoutDefaults(layoutIn, layoutOut) {
var containerIn = layoutIn.shapes || [],
containerOut = layoutOut.shapes = [];

for(var i = 0; i < containerIn.length; i++) {
var shapeIn = containerIn[i] || {},
shapeOut = handleShapeDefaults(shapeIn, layoutOut);

containerOut.push(shapeOut);
}
};
Loading
0