Skip to content

Commit 8046bb6

Browse files
committed
move all colorscale helpers to ./helpers.js and ./scales.js
1 parent 9bbe295 commit 8046bb6

29 files changed

+192
-274
lines changed

src/components/colorscale/attributes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
'use strict';
1010

11-
var palettes = require('./scales.js');
11+
var palettes = require('./scales.js').scales;
1212
var paletteStr = Object.keys(palettes);
1313

1414
function code(s) {

src/components/colorscale/calc.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,10 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9-
109
'use strict';
1110

1211
var Lib = require('../../lib');
13-
14-
var flipScale = require('./flip_scale');
15-
12+
var flipScale = require('./helpers').flipScale;
1613

1714
module.exports = function calc(gd, trace, opts) {
1815
var fullLayout = gd._fullLayout;

src/components/colorscale/default_scale.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/components/colorscale/defaults.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,15 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9-
109
'use strict';
1110

1211
var isNumeric = require('fast-isnumeric');
1312

1413
var Lib = require('../../lib');
15-
1614
var hasColorbar = require('../colorbar/has_colorbar');
1715
var colorbarDefaults = require('../colorbar/defaults');
18-
var isValidScale = require('./is_valid_scale');
19-
var flipScale = require('./flip_scale');
2016

17+
var isValidScale = require('./scales').isValid;
2118

2219
module.exports = function colorScaleDefaults(traceIn, traceOut, layout, coerce, opts) {
2320
var prefix = opts.prefix,
@@ -47,12 +44,9 @@ module.exports = function colorScaleDefaults(traceIn, traceOut, layout, coerce,
4744

4845
// reversescale is handled at the containerOut level
4946
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;
47+
if(reverseScale) containerOut.colorscale = helpers.flipScale(sclOut);
5448

55-
if(!opts.noScale) {
49+
if(!opts.noScale && prefix !== 'marker.line.') {
5650
// handles both the trace case where the dflt is listed in attributes and
5751
// the marker case where the dflt is determined by hasColorbar
5852
var showScaleDflt;

src/components/colorscale/extract_scale.js

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/components/colorscale/flip_scale.js

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/components/colorscale/get_scale.js

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/components/colorscale/has_colorscale.js

Lines changed: 0 additions & 40 deletions
This file was deleted.

src/components/colorscale/make_color_scale_func.js renamed to src/components/colorscale/helpers.js

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,82 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9-
109
'use strict';
1110

1211
var d3 = require('d3');
1312
var tinycolor = require('tinycolor2');
1413
var isNumeric = require('fast-isnumeric');
1514

15+
var Lib = require('../../lib');
1616
var Color = require('../color');
1717

18+
var isValidScale = require('./scales').isValid;
19+
20+
function hasColorscale(trace, containerStr) {
21+
var container = containerStr ?
22+
Lib.nestedProperty(trace, containerStr).get() || {} :
23+
trace;
24+
var color = container.color;
25+
26+
var isArrayWithOneNumber = false;
27+
if(Lib.isArrayOrTypedArray(color)) {
28+
for(var i = 0; i < color.length; i++) {
29+
if(isNumeric(color[i])) {
30+
isArrayWithOneNumber = true;
31+
break;
32+
}
33+
}
34+
}
35+
36+
return (
37+
Lib.isPlainObject(container) && (
38+
isArrayWithOneNumber ||
39+
container.showscale === true ||
40+
(isNumeric(container.cmin) && isNumeric(container.cmax)) ||
41+
isValidScale(container.colorscale) ||
42+
Lib.isPlainObject(container.colorbar)
43+
)
44+
);
45+
}
46+
47+
/**
48+
* Extract colorscale into numeric domain and color range.
49+
*
50+
* @param {array} scl colorscale array of arrays
51+
* @param {number} cmin minimum color value (used to clamp scale)
52+
* @param {number} cmax maximum color value (used to clamp scale)
53+
*/
54+
function extractScale(scl, cmin, cmax) {
55+
var N = scl.length;
56+
var domain = new Array(N);
57+
var range = new Array(N);
58+
59+
for(var i = 0; i < N; i++) {
60+
var si = scl[i];
61+
62+
domain[i] = cmin + si[0] * (cmax - cmin);
63+
range[i] = si[1];
64+
}
65+
66+
return {
67+
domain: domain,
68+
range: range
69+
};
70+
}
71+
72+
function flipScale(scl) {
73+
var N = scl.length;
74+
var sclNew = new Array(N);
75+
var si;
76+
77+
for(var i = N - 1, j = 0; i >= 0; i--, j++) {
78+
si = scl[i];
79+
sclNew[j] = [1 - si[0], si[1]];
80+
}
81+
82+
return sclNew;
83+
}
84+
1885
/**
1986
* General colorscale function generator.
2087
*
@@ -28,7 +95,7 @@ var Color = require('../color');
2895
*
2996
* @return {function}
3097
*/
31-
module.exports = function makeColorScaleFunc(specs, opts) {
98+
function makeColorScaleFunc(specs, opts) {
3299
opts = opts || {};
33100

34101
var domain = specs.domain,
@@ -80,7 +147,7 @@ module.exports = function makeColorScaleFunc(specs, opts) {
80147
sclFunc.range = function() { return range; };
81148

82149
return sclFunc;
83-
};
150+
}
84151

85152
function colorArray2rbga(colorArray) {
86153
var colorObj = {
@@ -92,3 +159,10 @@ function colorArray2rbga(colorArray) {
92159

93160
return tinycolor(colorObj).toRgbString();
94161
}
162+
163+
module.exports = {
164+
hasColorscale: hasColorscale,
165+
extractScale: extractScale,
166+
flipScale: flipScale,
167+
makeColorScaleFunc: makeColorScaleFunc
168+
};

src/components/colorscale/index.js

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,32 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9-
109
'use strict';
1110

12-
exports.moduleType = 'component';
13-
14-
exports.name = 'colorscale';
15-
16-
exports.scales = require('./scales');
17-
18-
exports.defaultScale = require('./default_scale');
19-
20-
exports.attributes = require('./attributes');
21-
22-
exports.layoutAttributes = require('./layout_attributes');
23-
24-
exports.supplyLayoutDefaults = require('./layout_defaults');
25-
26-
exports.handleDefaults = require('./defaults');
27-
28-
exports.calc = require('./calc');
11+
var scales = require('./scales');
12+
var helpers = require('./helpers');
2913

30-
exports.hasColorscale = require('./has_colorscale');
14+
module.exports = {
15+
moduleType: 'component',
16+
name: 'colorscale',
3117

32-
exports.isValidScale = require('./is_valid_scale');
18+
attributes: require('./attributes'),
19+
layoutAttributes: require('./layout_attributes'),
3320

34-
exports.getScale = require('./get_scale');
21+
supplyLayoutDefaults: require('./layout_defaults'),
22+
handleDefaults: require('./defaults'),
3523

36-
exports.flipScale = require('./flip_scale');
24+
calc: require('./calc'),
3725

38-
exports.extractScale = require('./extract_scale');
26+
// ./scales.js is required in lib/coerce.js ;
27+
// it needs to be a seperate module to avoid circular a dependency
28+
scales: scales.scales,
29+
defaultScale: scales.defaultScale,
30+
getScale: scales.get,
31+
isValidScale: scales.isValid,
3932

40-
exports.makeColorScaleFunc = require('./make_color_scale_func');
33+
hasColorscale: helpers.hasColorscale,
34+
flipScale: helpers.flipScale,
35+
extractScale: helpers.extractScale,
36+
makeColorScaleFunc: helpers.makeColorScaleFunc
37+
};

0 commit comments

Comments
 (0)