Skip to content

Commit a7ab01b

Browse files
committed
Merge pull request #124 from plotly/trace-module-1-to-1
Trace type <-> trace module 1-to-1 correspondance
2 parents b2f07f7 + 2247b7c commit a7ab01b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+3926
-3045
lines changed

src/components/colorbar/index.js

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -575,29 +575,3 @@ colorbar.supplyDefaults = function(containerIn, containerOut, layout) {
575575
Plotly.Lib.coerceFont(coerce, 'titlefont', layout.font);
576576
coerce('titleside');
577577
};
578-
579-
colorbar.traceColorbar = function(gd, cd) {
580-
var trace = cd[0].trace,
581-
cbId = 'cb' + trace.uid,
582-
scl = Plotly.Colorscale.getScale(trace.colorscale),
583-
zmin = trace.zmin,
584-
zmax = trace.zmax;
585-
586-
if(!isNumeric(zmin)) zmin = Plotly.Lib.aggNums(Math.min, null, trace.z);
587-
if(!isNumeric(zmax)) zmax = Plotly.Lib.aggNums(Math.max, null, trace.z);
588-
589-
gd._fullLayout._infolayer.selectAll('.'+cbId).remove();
590-
if(!trace.showscale){
591-
Plotly.Plots.autoMargin(gd, cbId);
592-
return;
593-
}
594-
595-
var cb = cd[0].t.cb = colorbar(gd, cbId);
596-
cb.fillcolor(d3.scale.linear()
597-
.domain(scl.map(function(v){ return zmin + v[0]*(zmax-zmin); }))
598-
.range(scl.map(function(v){ return v[1]; })))
599-
.filllevels({start: zmin, end: zmax, size: (zmax-zmin)/254})
600-
.options(trace.colorbar)();
601-
602-
Plotly.Lib.markTime('done colorbar');
603-
};

src/plot_api/plot_schema.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,12 +259,13 @@ function assignPolarLayoutAttrs(layoutAttributes) {
259259
}
260260

261261
function getSubplotRegistry(traceType) {
262+
if(traceType === 'area') return {}; // FIXME
263+
262264
var subplotsRegistry = Plotly.Plots.subplotsRegistry,
263265
subplotType = Object.keys(subplotsRegistry).filter(function(subplotType) {
264266
return Plotly.Plots.traceIs({type: traceType}, subplotType);
265267
})[0];
266268

267-
if(traceType === 'area') return {}; // FIXME
268269
if(subplotType === undefined) return {};
269270

270271
return subplotsRegistry[subplotType];

src/plotly.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,12 @@ exports.ModeBar = require('./components/modebar');
5757

5858
// traces
5959
exports.Scatter = require('./traces/scatter');
60-
exports.Bars = require('./traces/bars');
61-
exports.Boxes = require('./traces/boxes');
60+
exports.Bar = require('./traces/bar');
61+
exports.Box = require('./traces/box');
6262
exports.Heatmap = require('./traces/heatmap');
6363
exports.Histogram = require('./traces/histogram');
64+
exports.Histogram2d = require('./traces/histogram2d');
65+
exports.Histogram2dContour = require('./traces/histogram2dcontour');
6466
exports.Pie = require('./traces/pie');
6567
exports.Contour = require('./traces/contour');
6668
exports.Scatter3D = require('./traces/scatter3d');

src/plots/plots.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ plots.supplyLayoutGlobalDefaults = function(layoutIn, layoutOut) {
653653
plots.supplyLayoutModuleDefaults = function(layoutIn, layoutOut, fullData) {
654654
var moduleLayoutDefaults = [
655655
'Axes', 'Annotations', 'Shapes', 'Fx',
656-
'Bars', 'Boxes', 'Gl3dLayout', 'GeoLayout', 'Pie', 'Legend'
656+
'Bar', 'Box', 'Gl3dLayout', 'GeoLayout', 'Pie', 'Legend'
657657
];
658658

659659
var i, module;

src/traces/bar/arrays_to_calcdata.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Copyright 2012-2015, 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 mergeArray = require('../../lib').mergeArray;
13+
14+
15+
// arrayOk attributes, merge them into calcdata array
16+
module.exports = function arraysToCalcdata(cd) {
17+
var trace = cd[0].trace,
18+
marker = trace.marker,
19+
markerLine = marker.line;
20+
21+
mergeArray(trace.text, cd, 'tx');
22+
mergeArray(marker.opacity, cd, 'mo');
23+
mergeArray(marker.color, cd, 'mc');
24+
mergeArray(markerLine.color, cd, 'mlc');
25+
mergeArray(markerLine.width, cd, 'mlw');
26+
};

src/traces/bars/attributes.js renamed to src/traces/bar/attributes.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,9 @@ module.exports = {
5151
}
5252
},
5353

54-
r: scatterAttrs.r, // FIXME this shouldn't get included in 'histogram'
54+
r: scatterAttrs.r,
5555
t: scatterAttrs.t,
5656

57-
_composedModules: { // composed module coupling
58-
'histogram': 'Histogram'
59-
},
6057
_nestedModules: { // nested module coupling
6158
'error_y': 'ErrorBars',
6259
'error_x': 'ErrorBars',

src/traces/bar/calc.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* Copyright 2012-2015, 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 isNumeric = require('fast-isnumeric');
13+
14+
var Plotly = require('../../plotly');
15+
16+
17+
module.exports = function calc(gd, trace) {
18+
// depending on bar direction, set position and size axes
19+
// and data ranges
20+
// note: this logic for choosing orientation is
21+
// duplicated in graph_obj->setstyles
22+
23+
var xa = Plotly.Axes.getFromId(gd, trace.xaxis||'x'),
24+
ya = Plotly.Axes.getFromId(gd, trace.yaxis||'y'),
25+
orientation = trace.orientation || ((trace.x && !trace.y) ? 'h' : 'v'),
26+
pos, size, i;
27+
28+
if(orientation==='h') {
29+
size = xa.makeCalcdata(trace, 'x');
30+
pos = ya.makeCalcdata(trace, 'y');
31+
}
32+
else {
33+
size = ya.makeCalcdata(trace, 'y');
34+
pos = xa.makeCalcdata(trace, 'x');
35+
}
36+
37+
// create the "calculated data" to plot
38+
var serieslen = Math.min(pos.length, size.length),
39+
cd = [];
40+
for(i=0; i<serieslen; i++) {
41+
if((isNumeric(pos[i]) && isNumeric(size[i]))) {
42+
cd.push({p: pos[i], s: size[i], b: 0});
43+
}
44+
}
45+
46+
// auto-z and autocolorscale if applicable
47+
if(Plotly.Colorscale.hasColorscale(trace, 'marker')) {
48+
Plotly.Colorscale.calc(trace, trace.marker.color, 'marker', 'c');
49+
}
50+
if(Plotly.Colorscale.hasColorscale(trace, 'marker.line')) {
51+
Plotly.Colorscale.calc(trace, trace.marker.line.color, 'marker.line', 'c');
52+
}
53+
54+
return cd;
55+
};

src/traces/bar/defaults.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Copyright 2012-2015, 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 Lib = require('../../lib');
13+
var Color = require('../../components/color');
14+
15+
var handleXYDefaults = require('../scatter/xy_defaults');
16+
var handleStyleDefaults = require('../bar/style_defaults');
17+
var errorBarsSupplyDefaults = require('../../components/errorbars/defaults');
18+
var attributes = require('./attributes');
19+
20+
21+
module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout) {
22+
function coerce(attr, dflt) {
23+
return Lib.coerce(traceIn, traceOut, attributes, attr, dflt);
24+
}
25+
26+
var len = handleXYDefaults(traceIn, traceOut, coerce);
27+
if(!len) {
28+
traceOut.visible = false;
29+
return;
30+
}
31+
32+
coerce('orientation', (traceOut.x && !traceOut.y) ? 'h' : 'v');
33+
coerce('text');
34+
35+
handleStyleDefaults(traceIn, traceOut, coerce, defaultColor, layout);
36+
37+
// override defaultColor for error bars with defaultLine
38+
errorBarsSupplyDefaults(traceIn, traceOut, Color.defaultLine, {axis: 'y'});
39+
errorBarsSupplyDefaults(traceIn, traceOut, Color.defaultLine, {axis: 'x', inherit: 'y'});
40+
};

src/traces/bar/hover.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
* Copyright 2012-2015, 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 Plotly = require('../../plotly');
13+
var Color = require('../../components/color');
14+
15+
16+
module.exports = function hoverPoints(pointData, xval, yval, hovermode) {
17+
var cd = pointData.cd,
18+
trace = cd[0].trace,
19+
t = cd[0].t,
20+
xa = pointData.xa,
21+
ya = pointData.ya,
22+
barDelta = (hovermode==='closest') ?
23+
t.barwidth/2 : t.dbar*(1-xa._td._fullLayout.bargap)/2,
24+
barPos;
25+
26+
if(hovermode!=='closest') barPos = function(di) { return di.p; };
27+
else if(trace.orientation==='h') barPos = function(di) { return di.y; };
28+
else barPos = function(di) { return di.x; };
29+
30+
var dx, dy;
31+
if(trace.orientation==='h') {
32+
dx = function(di){
33+
// add a gradient so hovering near the end of a
34+
// bar makes it a little closer match
35+
return Plotly.Fx.inbox(di.b-xval, di.x-xval) + (di.x-xval)/(di.x-di.b);
36+
};
37+
dy = function(di){
38+
var centerPos = barPos(di) - yval;
39+
return Plotly.Fx.inbox(centerPos - barDelta, centerPos + barDelta);
40+
};
41+
}
42+
else {
43+
dy = function(di){
44+
return Plotly.Fx.inbox(di.b-yval, di.y-yval) + (di.y-yval)/(di.y-di.b);
45+
};
46+
dx = function(di){
47+
var centerPos = barPos(di) - xval;
48+
return Plotly.Fx.inbox(centerPos - barDelta, centerPos + barDelta);
49+
};
50+
}
51+
52+
var distfn = Plotly.Fx.getDistanceFunction(hovermode, dx, dy);
53+
Plotly.Fx.getClosest(cd, distfn, pointData);
54+
55+
// skip the rest (for this trace) if we didn't find a close point
56+
if(pointData.index===false) return;
57+
58+
// the closest data point
59+
var di = cd[pointData.index],
60+
mc = di.mcc || trace.marker.color,
61+
mlc = di.mlcc || trace.marker.line.color,
62+
mlw = di.mlw || trace.marker.line.width;
63+
if(Color.opacity(mc)) pointData.color = mc;
64+
else if(Color.opacity(mlc) && mlw) pointData.color = mlc;
65+
66+
if(trace.orientation==='h') {
67+
pointData.x0 = pointData.x1 = xa.c2p(di.x, true);
68+
pointData.xLabelVal = di.s;
69+
70+
pointData.y0 = ya.c2p(barPos(di) - barDelta, true);
71+
pointData.y1 = ya.c2p(barPos(di) + barDelta, true);
72+
pointData.yLabelVal = di.p;
73+
}
74+
else {
75+
pointData.y0 = pointData.y1 = ya.c2p(di.y,true);
76+
pointData.yLabelVal = di.s;
77+
78+
pointData.x0 = xa.c2p(barPos(di) - barDelta, true);
79+
pointData.x1 = xa.c2p(barPos(di) + barDelta, true);
80+
pointData.xLabelVal = di.p;
81+
}
82+
83+
if(di.tx) pointData.text = di.tx;
84+
85+
Plotly.ErrorBars.hoverInfo(di, trace, pointData);
86+
87+
return [pointData];
88+
};

src/traces/bar/index.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Copyright 2012-2015, 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 Plotly = require('../../plotly');
13+
14+
Plotly.Plots.register(exports, 'bar',
15+
['cartesian', 'bar', 'oriented', 'markerColorscale', 'errorBarsOK', 'showLegend'], {
16+
description: [
17+
'The data visualized by the span of the bars is set in `y`',
18+
'if `orientation` is set th *v* (the default)',
19+
'and the labels are set in `x`.',
20+
21+
'By setting `orientation` to *h*, the roles are interchanged.'
22+
].join(' ')
23+
});
24+
25+
exports.attributes = require('./attributes');
26+
27+
exports.layoutAttributes = require('./layout_attributes');
28+
29+
exports.supplyDefaults = require('./defaults');
30+
31+
exports.supplyLayoutDefaults = require('./layout_defaults');
32+
33+
exports.calc = require('./calc');
34+
35+
exports.setPositions = require('./set_positions');
36+
37+
exports.colorbar = require('../scatter/colorbar');
38+
39+
exports.arraysToCalcdata = require('./arrays_to_calcdata');
40+
41+
exports.plot = require('./plot');
42+
43+
exports.style = require('./style');
44+
45+
exports.hoverPoints = require('./hover');

0 commit comments

Comments
 (0)