/
AbstractScale.js
286 lines (260 loc) Β· 8.88 KB
/
AbstractScale.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import AbstractScaleDispatcher from './AbstractScaleDispatcher.js';
import {
interpolateBrBG as d3_interpolateBrBG,
interpolatePRGn as d3_interpolatePRGn,
interpolatePiYG as d3_interpolatePiYG,
interpolatePuOr as d3_interpolatePuOr,
interpolateRdBu as d3_interpolateRdBu,
interpolateRdGy as d3_interpolateRdGy,
interpolateRdYlBu as d3_interpolateRdYlBu,
interpolateRdYlGn as d3_interpolateRdYlGn,
interpolateSpectral as d3_interpolateSpectral,
interpolateBlues as d3_interpolateBlues,
interpolateGreens as d3_interpolateGreens,
interpolateGreys as d3_interpolateGreys,
interpolateOranges as d3_interpolateOranges,
interpolatePurples as d3_interpolatePurples,
interpolateReds as d3_interpolateReds,
interpolateTurbo as d3_interpolateTurbo,
interpolateViridis as d3_interpolateViridis,
interpolateInferno as d3_interpolateInferno,
interpolateMagma as d3_interpolateMagma,
interpolatePlasma as d3_interpolatePlasma,
interpolateWarm as d3_interpolateWarm,
interpolateCool as d3_interpolateCool,
interpolateCubehelixDefault as d3_interpolateCubehelixDefault,
interpolateBuGn as d3_interpolateBuGn,
interpolateBuPu as d3_interpolateBuPu,
interpolateGnBu as d3_interpolateGnBu,
interpolateOrRd as d3_interpolateOrRd,
interpolatePuBuGn as d3_interpolatePuBuGn,
interpolatePuBu as d3_interpolatePuBu,
interpolatePuRd as d3_interpolatePuRd,
interpolateRdPu as d3_interpolateRdPu,
interpolateYlGnBu as d3_interpolateYlGnBu,
interpolateYlGn as d3_interpolateYlGn,
interpolateYlOrBr as d3_interpolateYlOrBr,
interpolateYlOrRd as d3_interpolateYlOrRd,
interpolateRainbow as d3_interpolateRainbow,
interpolateSinebow as d3_interpolateSinebow
} from "d3-scale-chromatic";
/**
* Abstract class representing a scale.
* @interface
*/
export default class AbstractScale extends AbstractScaleDispatcher {
/**
* @enum
* @readonly
*/
static colorScales = Object.freeze({
"BrBG": d3_interpolateBrBG,
"PRGn": d3_interpolatePRGn,
"PiYG": d3_interpolatePiYG,
"PuOr": d3_interpolatePuOr,
"RdBu": d3_interpolateRdBu,
"RdGy": d3_interpolateRdGy,
"RdYlBu": d3_interpolateRdYlBu,
"RdYlGn": d3_interpolateRdYlGn,
"Spectral": d3_interpolateSpectral,
"Blues": d3_interpolateBlues,
"Greens": d3_interpolateGreens,
"Greys": d3_interpolateGreys,
"Oranges": d3_interpolateOranges,
"Purples": d3_interpolatePurples,
"Reds": d3_interpolateReds,
"Turbo": d3_interpolateTurbo,
"Viridis": d3_interpolateViridis,
"Inferno": d3_interpolateInferno,
"Magma": d3_interpolateMagma,
"Plasma": d3_interpolatePlasma,
"Warm": d3_interpolateWarm,
"Cool": d3_interpolateCool,
"CubehelixDefault": d3_interpolateCubehelixDefault,
"BuGn": d3_interpolateBuGn,
"BuPu": d3_interpolateBuPu,
"GnBu": d3_interpolateGnBu,
"OrRd": d3_interpolateOrRd,
"PuBuGn": d3_interpolatePuBuGn,
"PuBu": d3_interpolatePuBu,
"PuRd": d3_interpolatePuRd,
"RdPu": d3_interpolateRdPu,
"YlGnBu": d3_interpolateYlGnBu,
"YlGn": d3_interpolateYlGn,
"YlOrBr": d3_interpolateYlOrBr,
"YlOrRd": d3_interpolateYlOrRd,
"Rainbow": d3_interpolateRainbow,
"Sinebow": d3_interpolateSinebow
});
static unknownColor = "#E3E3E3";
static unknownString = "Unknown";
static defaultColorScale = d3_interpolateRdYlBu;
/**
* Create a scale.
* @param {*} id The ID for the scale.
* @param {*} name The name for the scale.
* @param {*} domain The domain for the scale.
* @param {*} colorScaleKey The key of the color scale to be used.
* @param {Expected} expected An object on which to subscribe to data.
*/
constructor(id, name, domain, colorScaleKey, expected) {
super();
this._id = id;
this._name = name;
if(colorScaleKey === undefined) {
this._colorScaleOriginal = AbstractScale.defaultColorScale;
this._colorScale = AbstractScale.defaultColorScale;
} else {
this._colorScaleOriginal = AbstractScale.colorScales[colorScaleKey];
this._colorScale = AbstractScale.colorScales[colorScaleKey];
}
this._domain = [];
this._domainFiltered = [];
this._domainOriginal = [];
this._isLoading = true;
if(domain) {
Promise.resolve(domain).then((d) => {
this._domain = d.slice();
this._domainFiltered = d.slice();
this._domainOriginal = d.slice();
this._isLoading = false;
this.emitUpdate();
});
}
if(expected) {
expected.onData(this.constructor.name, this._id, (expectedData) => {
if(expectedData.hasOwnProperty("scales") && expectedData["scales"].hasOwnProperty(this._id)) {
const d = expectedData["scales"][this._id];
this._domain = d.slice();
this._domainFiltered = d.slice();
this._domainOriginal = d.slice();
this._isLoading = false;
this.emitUpdate();
}
});
}
}
/**
* @returns {string} The ID for the scale.
*/
get id() {
return this._id;
}
/**
* @returns {string} The name for the scale.
*/
get name() {
return this._name;
}
/**
* @returns {boolean} The loading status.
*/
get isLoading() {
return this._isLoading;
}
/**
* @returns {array} The values that variables using this scale can take.
*/
get domain() {
return this._domain;
}
/**
* @returns {array} The values that variables using this scale can take after filtering.
*/
get domainFiltered() {
return this._domainFiltered;
}
/**
* @returns {function} Function that converts a value between [0, 1] to a color
*/
get colorScale() {
return this._colorScale;
}
/**
* Converts a domain value to a color
* @param {*} domainValue A domain value
* @returns {color} A color value
*
*/
color(domainValue) {
throw new Error('You have to implement the method color!');
}
/**
* Compares two domain values
* @param {*} a A domain value
* @param {*} b Another domain value
* @returns {number} Comparison result of -1, 0, or 1.
*/
comparator(a, b, ascending) {
throw new Error('You have to implement the method comparator!');
}
/**
* Check whether a domain value should be considered unknown
* @param {*} domainValue A domain value
* @returns {boolean} True if the domain value should be considered unknown
*/
static isUnknown(domainValue) {
return (domainValue == "nan" || domainValue === undefined);
}
/**
* Convert a domain value to a human-readable value.
* @param {*} domainValue A domain value
* @returns {*} The corresponding humanDomain value
*/
toHuman(domainValue) {
if(AbstractScale.isUnknown(domainValue)) {
return AbstractScale.unknownString;
}
// The default implementation does nothing except the unknown check
return domainValue;
}
/**
* Set the domain directly.
* @param {array} newDomain An array of new domain values, or a promise that will return the array.
*/
setDomain(newDomain) {
this._isLoading = true;
this.emitUpdate();
Promise.resolve(newDomain).then((d) => {
this._domain = d.slice();
this._isLoading = false;
this.emitUpdate();
});
}
/**
* Set the filtered domain directly.
* @param {array} newDomainFiltered An array of new filtered domain values.
*/
setDomainFiltered(newDomainFiltered) {
this._isLoading = true;
this.emitUpdate();
Promise.resolve(newDomainFiltered).then((d) => {
this._domainFiltered = d.slice();
this._isLoading = false;
this.emitUpdate();
});
}
/**
* Set the color scale function by its name.
* @param {string} scaleKey The string key for the color scale.
*/
setColorScaleByKey(scaleKey) {
if(Object.keys(AbstractScale.colorScales).includes(scaleKey)) {
this._colorScale = AbstractScale.colorScales[scaleKey];
this.emitUpdate();
}
}
/**
* Resets the color scale.
*/
resetColorScale() {
this._colorScale = this._colorScaleOriginal;
this.emitUpdate();
}
/**
* Resets the filtered domain, using the full domain.
*/
resetFilter() {
this.setDomainFiltered(this._domain.slice());
}
}