-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathhelpers.js
238 lines (206 loc) · 6.07 KB
/
helpers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/**
* Copyright 2012-2019, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var d3 = require('d3');
var tinycolor = require('tinycolor2');
var isNumeric = require('fast-isnumeric');
var Lib = require('../../lib');
var Color = require('../color');
var isValidScale = require('./scales').isValid;
function hasColorscale(trace, containerStr, colorKey) {
var container = containerStr ?
Lib.nestedProperty(trace, containerStr).get() || {} :
trace;
var color = container[colorKey || 'color'];
var isArrayWithOneNumber = false;
if(Lib.isArrayOrTypedArray(color)) {
for(var i = 0; i < color.length; i++) {
if(isNumeric(color[i])) {
isArrayWithOneNumber = true;
break;
}
}
}
return (
Lib.isPlainObject(container) && (
isArrayWithOneNumber ||
container.showscale === true ||
(isNumeric(container.cmin) && isNumeric(container.cmax)) ||
isValidScale(container.colorscale) ||
Lib.isPlainObject(container.colorbar)
)
);
}
var constantAttrs = ['showscale', 'autocolorscale', 'colorscale', 'reversescale', 'colorbar'];
var letterAttrs = ['min', 'max', 'mid', 'auto'];
/**
* Extract 'c' / 'z', trace / color axis colorscale options
*
* Note that it would be nice to replace all z* with c* equivalents in v2
*
* @param {object} cont : attribute container
* @return {object}:
* - min: cmin or zmin
* - max: cmax or zmax
* - mid: cmid or zmid
* - auto: cauto or zauto
* - *scale: *scale attrs
* - colorbar: colorbar
* - _sync: function syncing attr and underscore dual (useful when calc'ing min/max)
*/
function extractOpts(cont) {
var colorAx = cont._colorAx;
var cont2 = colorAx ? colorAx : cont;
var out = {};
var cLetter;
var i, k;
for(i = 0; i < constantAttrs.length; i++) {
k = constantAttrs[i];
out[k] = cont2[k];
}
if(colorAx) {
cLetter = 'c';
for(i = 0; i < letterAttrs.length; i++) {
k = letterAttrs[i];
out[k] = cont2['c' + k];
}
} else {
var k2;
for(i = 0; i < letterAttrs.length; i++) {
k = letterAttrs[i];
k2 = 'c' + k;
if(k2 in cont2) {
out[k] = cont2[k2];
continue;
}
k2 = 'z' + k;
if(k2 in cont2) {
out[k] = cont2[k2];
}
}
cLetter = k2.charAt(0);
}
out._sync = function(k, v) {
var k2 = letterAttrs.indexOf(k) !== -1 ? cLetter + k : k;
cont2[k2] = cont2['_' + k2] = v;
};
return out;
}
/**
* Extract colorscale into numeric domain and color range.
*
* @param {object} cont colorscale container (e.g. trace, marker)
* - colorscale {array of arrays}
* - cmin/zmin {number}
* - cmax/zmax {number}
* - reversescale {boolean}
*
* @return {object}
* - domain {array}
* - range {array}
*/
function extractScale(cont) {
var cOpts = extractOpts(cont);
var cmin = cOpts.min;
var cmax = cOpts.max;
var scl = cOpts.reversescale ?
flipScale(cOpts.colorscale) :
cOpts.colorscale;
var N = scl.length;
var domain = new Array(N);
var range = new Array(N);
for(var i = 0; i < N; i++) {
var si = scl[i];
domain[i] = cmin + si[0] * (cmax - cmin);
range[i] = si[1];
}
return {domain: domain, range: range};
}
function flipScale(scl) {
var N = scl.length;
var sclNew = new Array(N);
for(var i = N - 1, j = 0; i >= 0; i--, j++) {
var si = scl[i];
sclNew[j] = [1 - si[0], si[1]];
}
return sclNew;
}
/**
* General colorscale function generator.
*
* @param {object} specs output of Colorscale.extractScale or precomputed domain, range.
* - domain {array}
* - range {array}
*
* @param {object} opts
* - noNumericCheck {boolean} if true, scale func bypasses numeric checks
* - returnArray {boolean} if true, scale func return 4-item array instead of color strings
*
* @return {function}
*/
function makeColorScaleFunc(specs, opts) {
opts = opts || {};
var domain = specs.domain;
var range = specs.range;
var N = range.length;
var _range = new Array(N);
for(var i = 0; i < N; i++) {
var rgba = tinycolor(range[i]).toRgb();
_range[i] = [rgba.r, rgba.g, rgba.b, rgba.a];
}
var _sclFunc = d3.scale.linear()
.domain(domain)
.range(_range)
.clamp(true);
var noNumericCheck = opts.noNumericCheck;
var returnArray = opts.returnArray;
var sclFunc;
if(noNumericCheck && returnArray) {
sclFunc = _sclFunc;
} else if(noNumericCheck) {
sclFunc = function(v) {
return colorArray2rbga(_sclFunc(v));
};
} else if(returnArray) {
sclFunc = function(v) {
if(isNumeric(v)) return _sclFunc(v);
else if(tinycolor(v).isValid()) return v;
else return Color.defaultLine;
};
} else {
sclFunc = function(v) {
if(isNumeric(v)) return colorArray2rbga(_sclFunc(v));
else if(tinycolor(v).isValid()) return v;
else return Color.defaultLine;
};
}
// colorbar draw looks into the d3 scale closure for domain and range
sclFunc.domain = _sclFunc.domain;
sclFunc.range = function() { return range; };
return sclFunc;
}
function makeColorScaleFuncFromTrace(trace, opts) {
return makeColorScaleFunc(extractScale(trace), opts);
}
function colorArray2rbga(colorArray) {
var colorObj = {
r: colorArray[0],
g: colorArray[1],
b: colorArray[2],
a: colorArray[3]
};
return tinycolor(colorObj).toRgbString();
}
module.exports = {
hasColorscale: hasColorscale,
extractOpts: extractOpts,
extractScale: extractScale,
flipScale: flipScale,
makeColorScaleFunc: makeColorScaleFunc,
makeColorScaleFuncFromTrace: makeColorScaleFuncFromTrace
};