Skip to content

Commit c0f85e6

Browse files
committed
split up bars index
1 parent 6b8885a commit c0f85e6

File tree

9 files changed

+683
-538
lines changed

9 files changed

+683
-538
lines changed

src/traces/bars/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/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/bars/defaults.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 Lib = require('../../lib');
14+
var Color = require('../../components/color');
15+
16+
var histogramSupplyDefaults = require('../histogram/defaults');
17+
var handleXYDefaults = require('../scatter/xy_defaults');
18+
var errorBarsSupplyDefaults = require('../../components/errorbars/defaults');
19+
20+
var attributes = require('./attributes');
21+
22+
23+
module.exports = function(traceIn, traceOut, defaultColor, layout) {
24+
function coerce(attr, dflt) {
25+
return Lib.coerce(traceIn, traceOut, attributes, attr, dflt);
26+
}
27+
28+
if(traceOut.type === 'histogram') {
29+
// x, y, and orientation are coerced in the histogram supplyDefaults
30+
// (along with histogram-specific attributes)
31+
histogramSupplyDefaults(traceIn, traceOut);
32+
if(!traceOut.visible) return;
33+
}
34+
else {
35+
var len = handleXYDefaults(traceIn, traceOut, coerce);
36+
if(!len) {
37+
traceOut.visible = false;
38+
return;
39+
}
40+
41+
coerce('orientation', (traceOut.x && !traceOut.y) ? 'h' : 'v');
42+
}
43+
44+
coerce('marker.color', defaultColor);
45+
if(Plotly.Colorscale.hasColorscale(traceIn, 'marker')) {
46+
Plotly.Colorscale.handleDefaults(
47+
traceIn, traceOut, layout, coerce, {prefix: 'marker.', cLetter: 'c'}
48+
);
49+
}
50+
51+
coerce('marker.line.color', Plotly.Color.defaultLine);
52+
if(Plotly.Colorscale.hasColorscale(traceIn, 'marker.line')) {
53+
Plotly.Colorscale.handleDefaults(
54+
traceIn, traceOut, layout, coerce, {prefix: 'marker.line.', cLetter: 'c'}
55+
);
56+
}
57+
58+
coerce('marker.line.width', 0);
59+
coerce('text');
60+
61+
// override defaultColor for error bars with defaultLine
62+
errorBarsSupplyDefaults(traceIn, traceOut, Color.defaultLine, {axis: 'y'});
63+
errorBarsSupplyDefaults(traceIn, traceOut, Color.defaultLine, {axis: 'x', inherit: 'y'});
64+
};

src/traces/bars/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+
};

0 commit comments

Comments
 (0)