Skip to content

Commit 3e5bec8

Browse files
committed
add Colorscale.extractOpts helpers
... to help with per-trace min/max/auto or per-coloraxis defs
1 parent ae3f407 commit 3e5bec8

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

Diff for: src/components/colorscale/helpers.js

+61
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,66 @@ function hasColorscale(trace, containerStr) {
4444
);
4545
}
4646

47+
var constantAttrs = ['showscale', 'autocolorscale', 'colorscale', 'reversescale', 'colorbar'];
48+
var letterAttrs = ['min', 'max', 'mid', 'auto'];
49+
50+
/**
51+
* Extract 'c' / 'z', trace / color axis colorscale options
52+
*
53+
* Note that it would be nice to replace all z* with c* equivalents in v2
54+
*
55+
* @param {object} cont : attribute container
56+
* @return {object}:
57+
* - min: cmin or zmin
58+
* - max: cmax or zmax
59+
* - mid: cmid or zmid
60+
* - auto: cauto or zauto
61+
* - *scale: *scale attrs
62+
* - colorbar: colorbar
63+
* - _sync: function syncing attr and underscore dual (useful when calc'ing min/max)
64+
*/
65+
function extractOpts(cont) {
66+
var colorAx = cont._colorAx;
67+
var cont2 = colorAx ? colorAx : cont;
68+
var out = {};
69+
var cLetter;
70+
var i, k;
71+
72+
for(i = 0; i < constantAttrs.length; i++) {
73+
k = constantAttrs[i];
74+
out[k] = cont2[k];
75+
}
76+
77+
if(colorAx) {
78+
cLetter = 'c';
79+
for(i = 0; i < letterAttrs.length; i++) {
80+
k = letterAttrs[i];
81+
out[k] = cont2['c' + k];
82+
}
83+
} else {
84+
var k2;
85+
for(i = 0; i < letterAttrs.length; i++) {
86+
k = letterAttrs[i];
87+
k2 = 'c' + k;
88+
if(k2 in cont2) {
89+
out[k] = cont2[k2];
90+
continue;
91+
}
92+
k2 = 'z' + k;
93+
if(k2 in cont2) {
94+
out[k] = cont2[k2];
95+
}
96+
}
97+
cLetter = k2.charAt(0);
98+
}
99+
100+
out._sync = function(k, v) {
101+
var k2 = letterAttrs.indexOf(k) !== -1 ? cLetter + k : k;
102+
cont2[k2] = cont2['_' + k2] = v;
103+
};
104+
105+
return out;
106+
}
47107
/**
48108
* Extract colorscale into numeric domain and color range.
49109
*
@@ -173,6 +233,7 @@ function colorArray2rbga(colorArray) {
173233

174234
module.exports = {
175235
hasColorscale: hasColorscale,
236+
extractOpts: extractOpts,
176237
extractScale: extractScale,
177238
flipScale: flipScale,
178239
makeColorScaleFunc: makeColorScaleFunc

Diff for: src/components/colorscale/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ module.exports = {
3232
isValidScale: scales.isValid,
3333

3434
hasColorscale: helpers.hasColorscale,
35+
extractOpts: helpers.extractOpts,
3536
flipScale: helpers.flipScale,
3637
extractScale: helpers.extractScale,
3738
makeColorScaleFunc: helpers.makeColorScaleFunc

0 commit comments

Comments
 (0)