Skip to content

Commit 6b8885a

Browse files
committed
split parts of the scatter index:
- colorbar and handleXYDefaults get their own files.
1 parent 1e923c9 commit 6b8885a

File tree

3 files changed

+97
-65
lines changed

3 files changed

+97
-65
lines changed

src/traces/scatter/colorbar.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 d3 = require('d3');
13+
var isNumeric = require('fast-isnumeric');
14+
15+
var Plotly = require('../../plotly');
16+
var Lib = require('../../lib');
17+
18+
19+
module.exports = function colorbar(gd, cd) {
20+
var trace = cd[0].trace,
21+
marker = trace.marker,
22+
cbId = 'cb' + trace.uid;
23+
24+
gd._fullLayout._infolayer.selectAll('.' + cbId).remove();
25+
26+
// TODO unify Scatter.colorbar and Heatmap.colorbar
27+
// TODO make Plotly[module].colorbar support multiple colorbar per trace
28+
29+
if(marker===undefined || !marker.showscale){
30+
Plotly.Plots.autoMargin(gd, cbId);
31+
return;
32+
}
33+
34+
var scl = Plotly.Colorscale.getScale(marker.colorscale),
35+
vals = marker.color,
36+
cmin = marker.cmin,
37+
cmax = marker.cmax;
38+
39+
if(!isNumeric(cmin)) cmin = Plotly.Lib.aggNums(Math.min, null, vals);
40+
if(!isNumeric(cmax)) cmax = Plotly.Lib.aggNums(Math.max, null, vals);
41+
42+
var cb = cd[0].t.cb = Plotly.Colorbar(gd, cbId);
43+
44+
cb.fillcolor(d3.scale.linear()
45+
.domain(scl.map(function(v){ return cmin + v[0] * (cmax - cmin); }))
46+
.range(scl.map(function(v){ return v[1]; })))
47+
.filllevels({start: cmin, end: cmax, size: (cmax - cmin) / 254})
48+
.options(marker.colorbar)();
49+
50+
Lib.markTime('done colorbar');
51+
};

src/traces/scatter/index.js

Lines changed: 3 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -32,44 +32,14 @@ scatter.PTS_LINESONLY = 20;
3232

3333
scatter.attributes = require('./attributes');
3434

35-
scatter.handleXYDefaults = function(traceIn, traceOut, coerce) {
36-
var len,
37-
x = coerce('x'),
38-
y = coerce('y');
39-
40-
if(x) {
41-
if(y) {
42-
len = Math.min(x.length, y.length);
43-
// TODO: not sure we should do this here... but I think
44-
// the way it works in calc is wrong, because it'll delete data
45-
// which could be a problem eg in streaming / editing if x and y
46-
// come in at different times
47-
// so we need to revisit calc before taking this out
48-
if(len<x.length) traceOut.x = x.slice(0, len);
49-
if(len<y.length) traceOut.y = y.slice(0, len);
50-
}
51-
else {
52-
len = x.length;
53-
coerce('y0');
54-
coerce('dy');
55-
}
56-
}
57-
else {
58-
if(!y) return 0;
59-
60-
len = traceOut.y.length;
61-
coerce('x0');
62-
coerce('dx');
63-
}
64-
return len;
65-
};
35+
var handleXYDefaults = require('./xy_defaults');
6636

6737
scatter.supplyDefaults = function(traceIn, traceOut, defaultColor, layout) {
6838
function coerce(attr, dflt) {
6939
return Plotly.Lib.coerce(traceIn, traceOut, scatter.attributes, attr, dflt);
7040
}
7141

72-
var len = scatter.handleXYDefaults(traceIn, traceOut, coerce),
42+
var len = handleXYDefaults(traceIn, traceOut, coerce),
7343
// TODO: default mode by orphan points...
7444
defaultMode = len < scatter.PTS_LINESONLY ? 'lines+markers' : 'lines';
7545
if(!len) {
@@ -246,39 +216,7 @@ scatter.isBubble = function(trace) {
246216
Array.isArray(trace.marker.size));
247217
};
248218

249-
scatter.colorbar = function(gd, cd) {
250-
var trace = cd[0].trace,
251-
marker = trace.marker,
252-
cbId = 'cb' + trace.uid;
253-
254-
gd._fullLayout._infolayer.selectAll('.' + cbId).remove();
255-
256-
// TODO unify Scatter.colorbar and Heatmap.colorbar
257-
// TODO make Plotly[module].colorbar support multiple colorbar per trace
258-
259-
if(marker===undefined || !marker.showscale){
260-
Plotly.Plots.autoMargin(gd, cbId);
261-
return;
262-
}
263-
264-
var scl = Plotly.Colorscale.getScale(marker.colorscale),
265-
vals = marker.color,
266-
cmin = marker.cmin,
267-
cmax = marker.cmax;
268-
269-
if(!isNumeric(cmin)) cmin = Plotly.Lib.aggNums(Math.min, null, vals);
270-
if(!isNumeric(cmax)) cmax = Plotly.Lib.aggNums(Math.max, null, vals);
271-
272-
var cb = cd[0].t.cb = Plotly.Colorbar(gd, cbId);
273-
274-
cb.fillcolor(d3.scale.linear()
275-
.domain(scl.map(function(v){ return cmin + v[0] * (cmax - cmin); }))
276-
.range(scl.map(function(v){ return v[1]; })))
277-
.filllevels({start: cmin, end: cmax, size: (cmax - cmin) / 254})
278-
.options(marker.colorbar)();
279-
280-
Plotly.Lib.markTime('done colorbar');
281-
};
219+
scatter.colorbar = require('./colorbar');
282220

283221
// used in the drawing step for 'scatter' and 'scattegeo' and
284222
// in the convert step for 'scatter3d'

src/traces/scatter/xy_defaults.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
13+
module.exports = function handleXYDefaults(traceIn, traceOut, coerce) {
14+
var len,
15+
x = coerce('x'),
16+
y = coerce('y');
17+
18+
if(x) {
19+
if(y) {
20+
len = Math.min(x.length, y.length);
21+
// TODO: not sure we should do this here... but I think
22+
// the way it works in calc is wrong, because it'll delete data
23+
// which could be a problem eg in streaming / editing if x and y
24+
// come in at different times
25+
// so we need to revisit calc before taking this out
26+
if(len<x.length) traceOut.x = x.slice(0, len);
27+
if(len<y.length) traceOut.y = y.slice(0, len);
28+
}
29+
else {
30+
len = x.length;
31+
coerce('y0');
32+
coerce('dy');
33+
}
34+
}
35+
else {
36+
if(!y) return 0;
37+
38+
len = traceOut.y.length;
39+
coerce('x0');
40+
coerce('dx');
41+
}
42+
return len;
43+
};

0 commit comments

Comments
 (0)