-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathhelpers.js
181 lines (153 loc) · 4.51 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
/**
* 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) {
var container = containerStr ?
Lib.nestedProperty(trace, containerStr).get() || {} :
trace;
var color = container.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)
)
);
}
/**
* 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}
* @param {object} opts
* - cLetter {string} 'c' (for cmin/cmax) or 'z' (for zmin/zmax)
*
* @return {object}
* - domain {array}
* - range {array}
*/
function extractScale(cont, opts) {
var cLetter = opts.cLetter;
var scl = cont.reversescale ?
flipScale(cont.colorscale) :
cont.colorscale;
// minimum color value (used to clamp scale)
var cmin = cont[cLetter + 'min'];
// maximum color value (used to clamp scale)
var cmax = cont[cLetter + 'max'];
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 colorArray2rbga(colorArray) {
var colorObj = {
r: colorArray[0],
g: colorArray[1],
b: colorArray[2],
a: colorArray[3]
};
return tinycolor(colorObj).toRgbString();
}
module.exports = {
hasColorscale: hasColorscale,
extractScale: extractScale,
flipScale: flipScale,
makeColorScaleFunc: makeColorScaleFunc
};