Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2a600d1

Browse files
committed
split colorscale methods into seperate files
1 parent 777709e commit 2a600d1

File tree

10 files changed

+345
-203
lines changed

10 files changed

+345
-203
lines changed

src/components/colorscale/calc.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
14+
var scales = require('./scales');
15+
var flipScale = require('./flip_scale');
16+
17+
18+
module.exports = function calc(trace, vals, containerStr, cLetter) {
19+
var container, inputContainer;
20+
21+
if(containerStr) {
22+
container = Lib.nestedProperty(trace, containerStr).get();
23+
inputContainer = Lib.nestedProperty(trace._input, containerStr).get();
24+
} else {
25+
container = trace;
26+
inputContainer = trace._input;
27+
}
28+
29+
var auto = container[cLetter + 'auto'],
30+
min = container[cLetter + 'min'],
31+
max = container[cLetter + 'max'],
32+
scl = container.colorscale;
33+
34+
if(auto!==false || min===undefined) {
35+
min = Lib.aggNums(Math.min, null, vals);
36+
}
37+
38+
if(auto!==false || max===undefined) {
39+
max = Lib.aggNums(Math.max, null, vals);
40+
}
41+
42+
if(min === max) {
43+
min -= 0.5;
44+
max += 0.5;
45+
}
46+
47+
container[cLetter + 'min'] = min;
48+
container[cLetter + 'max'] = max;
49+
50+
inputContainer[cLetter + 'min'] = min;
51+
inputContainer[cLetter + 'max'] = max;
52+
53+
if(container.autocolorscale) {
54+
if(min * max < 0) scl = scales.RdBu;
55+
else if(min >= 0) scl = scales.Reds;
56+
else scl = scales.Blues;
57+
58+
// reversescale is handled at the containerOut level
59+
inputContainer.colorscale = scl;
60+
if(container.reversescale) scl = flipScale(scl);
61+
container.colorscale = scl;
62+
}
63+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
var scales = require('./scales');
10+
11+
12+
module.exports = scales.RdBu;

src/components/colorscale/defaults.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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 Lib = require('../../lib');
15+
16+
var hasColorbar = require('../colorbar/has_colorbar');
17+
var colorbarDefaults = require('../colorbar/defaults');
18+
var isValidScale = require('./is_valid_scale');
19+
var flipScale = require('./flip_scale');
20+
21+
22+
module.exports = function colorScaleDefaults(traceIn, traceOut, layout, coerce, opts) {
23+
var prefix = opts.prefix,
24+
cLetter = opts.cLetter,
25+
containerStr = prefix.slice(0, prefix.length-1),
26+
containerIn = prefix ?
27+
Lib.nestedProperty(traceIn, containerStr).get() || {} :
28+
traceIn,
29+
containerOut = prefix ?
30+
Lib.nestedProperty(traceOut, containerStr).get() || {} :
31+
traceOut,
32+
minIn = containerIn[cLetter + 'min'],
33+
maxIn = containerIn[cLetter + 'max'],
34+
sclIn = containerIn.colorscale;
35+
36+
var validMinMax = isNumeric(minIn) && isNumeric(maxIn) && (minIn < maxIn);
37+
coerce(prefix + cLetter + 'auto', !validMinMax);
38+
coerce(prefix + cLetter + 'min');
39+
coerce(prefix + cLetter + 'max');
40+
41+
// handles both the trace case (autocolorscale is false by default) and
42+
// the marker and marker.line case (autocolorscale is true by default)
43+
var autoColorscaleDftl;
44+
if(sclIn!==undefined) autoColorscaleDftl = !isValidScale(sclIn);
45+
coerce(prefix + 'autocolorscale', autoColorscaleDftl);
46+
var sclOut = coerce(prefix + 'colorscale');
47+
48+
// reversescale is handled at the containerOut level
49+
var reverseScale = coerce(prefix + 'reversescale');
50+
if(reverseScale) containerOut.colorscale = flipScale(sclOut);
51+
52+
// ... until Scatter.colorbar can handle marker line colorbars
53+
if(prefix === 'marker.line.') return;
54+
55+
// handle both the trace case where the dflt is listed in attributes and
56+
// the marker case where the dflt is determined by hasColorbar
57+
var showScaleDftl;
58+
if(prefix) showScaleDftl = hasColorbar(containerIn);
59+
var showScale = coerce(prefix + 'showscale', showScaleDftl);
60+
61+
if(showScale) colorbarDefaults(containerIn, containerOut, layout);
62+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
module.exports = function flipScale(scl) {
13+
var N = scl.length,
14+
sclNew = new Array(N),
15+
si;
16+
17+
for(var i = N-1, j = 0; i >= 0; i--, j++) {
18+
si = scl[i];
19+
sclNew[j] = [1 - si[0], si[1]];
20+
}
21+
22+
return sclNew;
23+
};
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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 scales = require('./scales');
13+
var defaultScale = require('./default_scale');
14+
var isValidScaleArray = require('./is_valid_scale_array');
15+
16+
17+
module.exports = function getScale(scl, dflt) {
18+
if(!dflt) dflt = defaultScale;
19+
if(!scl) return dflt;
20+
21+
function parseScale() {
22+
try {
23+
scl = scales[scl] || JSON.parse(scl);
24+
}
25+
catch(e) {
26+
scl = dflt;
27+
}
28+
}
29+
30+
if(typeof scl === 'string') {
31+
parseScale();
32+
// occasionally scl is double-JSON encoded...
33+
if(typeof scl === 'string') parseScale();
34+
}
35+
36+
if(!isValidScaleArray(scl)) return dflt;
37+
return scl;
38+
};
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 Lib = require('../../lib');
15+
16+
var isValidScale = require('./is_valid_scale');
17+
18+
19+
module.exports = function hasColorscale(trace, containerStr) {
20+
var container = containerStr ?
21+
Lib.nestedProperty(trace, containerStr).get() || {} :
22+
trace,
23+
color = container.color,
24+
isArrayWithOneNumber = false;
25+
26+
if(Array.isArray(color)) {
27+
for(var i = 0; i < color.length; i++) {
28+
if(isNumeric(color[i])) {
29+
isArrayWithOneNumber = true;
30+
break;
31+
}
32+
}
33+
}
34+
35+
return (
36+
(typeof container==='object' && container!==null) && (
37+
isArrayWithOneNumber ||
38+
container.showscale===true ||
39+
(isNumeric(container.cmin) && isNumeric(container.cmax)) ||
40+
isValidScale(container.colorscale) ||
41+
(typeof container.colorbar==='object' && container.colorbar!==null)
42+
)
43+
);
44+
};

0 commit comments

Comments
 (0)