-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathscales.js
98 lines (75 loc) · 1.94 KB
/
scales.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
/**
* 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';
function createWave(n, minOpacity) {
var arr = [];
var steps = 32; // Max: 256
for(var i = 0; i < steps; i++) {
var u = i / (steps - 1);
var v = minOpacity + (1 - minOpacity) * (1 - Math.pow(Math.sin(n * u * Math.PI), 2));
arr.push([
u,
Math.max(1, Math.min(0, v))
]);
}
return arr;
}
var min = 0.2;
var scales = {
'max': [
[0, min], [1, 1]
],
'min': [
[0, 1], [1, min]
],
'extremes': createWave(1, min),
'zigzag': createWave(8, min)
};
var defaultScale = scales.uniform;
function getScale(scl, dflt) {
if(!dflt) dflt = defaultScale;
if(!scl) return dflt;
function parseScale() {
try {
scl = scales[scl] || JSON.parse(scl);
} catch(e) {
scl = dflt;
}
}
if(typeof scl === 'string') {
parseScale();
// occasionally scl is double-JSON encoded...
if(typeof scl === 'string') parseScale();
}
if(!isValidScaleArray(scl)) return dflt;
return scl;
}
function isValidScaleArray(scl) {
var highestVal = 0;
if(!Array.isArray(scl) || scl.length < 2) return false;
if(!scl[0] || !scl[scl.length - 1]) return false;
if(+scl[0][0] !== 0 || +scl[scl.length - 1][0] !== 1) return false;
for(var i = 0; i < scl.length; i++) {
var si = scl[i];
if(si.length !== 2 || +si[0] < highestVal) {
return false;
}
highestVal = +si[0];
}
return true;
}
function isValidScale(scl) {
if(scales[scl] !== undefined) return true;
else return isValidScaleArray(scl);
}
module.exports = {
scales: scales,
defaultScale: defaultScale,
get: getScale,
isValid: isValidScale
};