-
Notifications
You must be signed in to change notification settings - Fork 1
/
ScaleProxy.js
432 lines (363 loc) · 12.4 KB
/
ScaleProxy.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
import { dispatch } from 'd3-dispatch';
import { extent } from 'd3-array';
import * as d3Scale from 'd3-scale';
import supportedScales from './supportedScales';
import readFromScale from './readFromScale';
import { asString as colorInterpolatorAsString } from './d3ColorInterpolators';
import { asString as interpolatorAsString } from './d3Interpolators';
import { asString as colorSchemeAsString } from './d3ColorSchemes';
const Events = {
// when we change the scale, update is fired
update: 'update',
// any time a setter is called on the scale, proxySet is fired
proxySet: 'proxy-set',
};
// needed for on change scale by type to ensure we have valid initial settings
const defaultScales = {
scaleOrdinal: d3Scale.scaleOrdinal(d3Scale.schemeCategory10),
scaleSequential: d3Scale.scaleSequential(d3Scale.interpolateMagma),
scalePow: d3Scale.scalePow().exponent(10),
};
// list of continuous scales
const continuousScales = ['scaleLinear', 'scalePow', 'scaleLog', 'scaleTime', 'scaleUtc', 'scaleIdentity'];
const ordinalScales = ['scaleOrdinal', 'scalePoint', 'scaleBand'];
const timeScales = ['scaleTime', 'scaleUtc'];
/**
* Class for proxying a d3 scale so it can change type and keep stats
* without changing its reference or interface.
*/
export default class ScaleProxy {
/**
* Constructor
* @param {String} name The name of the scale
* @param {Function} update The function to call to redraw a chart
*/
constructor(name) {
this.name = name;
this.dispatch = dispatch(Events.update, Events.proxySet);
this.statsReset();
this.fakedKeys = {};
this.pinned = false;
this.scale = null;
this.originalScale = null;
this.proxyScale = this.proxyScale.bind(this);
}
on(event, callback) {
this.dispatch.on(event, callback);
return this;
}
update() {
this.dispatch.call(Events.update, this);
}
isContinuous() {
return continuousScales.includes(this.scaleType);
}
isOrdinal() {
return ordinalScales.includes(this.scaleType);
}
isTimeScale() {
return timeScales.includes(this.scaleType);
}
pin() {
this.pinned = true;
}
unpin() {
this.pinned = false;
}
/**
* Generate the js code to create this scale from scratch
*/
generateCode() {
const ignoreKeys = [
'bandwidth',
'copy',
'interpolate',
'interpolator',
'invert',
'nice',
'quantiles',
'range',
'rangeRound',
'step',
'ticks',
'tickFormat',
];
const newDefault = d3Scale[this.scaleType]();
// helper to get prettier stringified values
function stringifyValue(value) {
if (Array.isArray(value)) {
// get spaces after commas in arrays
return `[${value.map(v => JSON.stringify(v)).join(', ')}]`;
}
return JSON.stringify(value);
}
let settings = Object.keys(this.scale)
.filter(key => !ignoreKeys.includes(key))
.map(key => ({ key, value: this.scale[key]() }))
// filter out values that are the default
.filter(({ key, value }) => newDefault[key]() != value) // eslint-disable-line
.map(({ key, value }) => ({ key, value: stringifyValue(value) }));
// match the color interpolator name
if (this.scale.interpolator) {
const interpolator = this.scale.interpolator();
// if different from default
if (newDefault.interpolator() !== interpolator) {
const interpolatorString = colorInterpolatorAsString(interpolator);
if (interpolatorString) {
settings.push({ key: 'interpolator', value: `d3.${interpolatorString}` });
}
}
}
// match the interpolator name for .interpolate
if (this.scale.interpolate) {
const interpolate = this.scale.interpolate();
// if different from default
if (newDefault.interpolate() !== interpolate) {
const interpolatorString = interpolatorAsString(interpolate);
if (interpolatorString) {
settings.push({ key: 'interpolate', value: `d3.${interpolatorString}` });
}
}
}
// match a color scheme
if (this.scale.range) {
const range = JSON.stringify(this.scale.range());
let rangeSetting = colorSchemeAsString(range);
if (rangeSetting) {
settings.push({ key: 'range', value: `d3.${rangeSetting}` });
} else {
rangeSetting = { key: 'range', value: stringifyValue(this.scale.range()) };
}
// insert setting at 2nd spot, since first is typically domain in Object.keys(scale)
settings = [settings[0], rangeSetting, ...settings.slice(1)];
}
return `d3.${this.scaleType}()${settings.map(({ key, value }) => `\n .${key}(${value})`).join('')};`;
}
/**
* Changes the active scale and returns the proxy scale.
*
* @param {Function} scale The new active scale to set (e.g. d3.scaleLinear())
* @param {String} [scaleType] A key for identifying the type of scale being set
* @param {Boolean} [fromUser=false] Scale change came from d3.scaleInteractive().scaleLinear() or equivalent.
* Can be used to skip update and be ignored if pinned.
*
* @return {Function} the proxy scale
*/
changeScale(scale, scaleType, fromUser) {
if (fromUser && this.pinned) {
this.statsReset();
// add in no-ops for this type of scale
this.fakeMissingScaleFunctions(scale);
return this.proxyScale;
}
// handle special conversions
if (this.scaleType === 'scaleSequential') {
// sequential doesn't have a range, so take the ends of the domain to initialize our range
if (scale.range) {
const domain = this.scale.domain();
scale.range(domain.map(d => this.scale(d)));
}
} else if (this.scaleType === 'scaleOrdinal') {
// map the domain to the extent so we don't get a hundred entries
scale.domain(extent(this.scale.domain()));
}
this.scaleType = scaleType;
this.scale = scale;
this.updateProxyScaleFunctions(scale);
this.statsReset();
if (!fromUser) {
this.update();
}
return this.proxyScale;
}
/**
* Once the initial scale has been set up, save it with this function
* to get reset working properly.
*/
saveOriginalScale(scale = this.scale, scaleType = this.scaleType) {
this.originalScale = scale.copy();
this.originalScaleType = scaleType;
}
/**
* Change the current scale to a new scale type while retaining as much
* similar values as possible.
*
* @param {String} scaleFuncName The name of the scale to change to (e.g. "scaleLinear")
* @return {Function} the proxy scale
*/
changeScaleType(scaleType) {
if (this.originalScale === null) {
this.saveOriginalScale();
}
const newScale = readFromScale(d3Scale[scaleType](), this.scale, defaultScales[scaleType]);
// ensure minimum domain is non-zero for log scale
if (scaleType === 'scaleLog') {
const domain = newScale.domain();
newScale.domain([Math.max(1, domain[0]), Math.max(1, domain[1])]);
}
return this.changeScale(newScale, scaleType);
}
/**
* Change a property in the scale, such as domain, range, interpolator
*
* @param {String} property the name of the property (e.g. "domain")
* @param {Any} value the value to set the property to
* @return {Function} the proxy scale
*/
changeScaleProperty(property, value) {
if (this.originalScale === null) {
this.saveOriginalScale();
}
this.scale[property](value);
this.statsReset();
this.update();
return this.proxyScale;
}
/**
* Proxy whatever the currently set scale is so that we can change the
* scale type dynamically without the user code having to update their
* references to their scales.
*
* Also allows tracking of stats on each call to the scale.
*
* @param {Any} value Passed directly to the active d3 scale.
* @return {Any} Whatever the active d3 scale returns
*/
proxyScale(value) {
const result = this.scale(value);
this.statsRecordValueUsed(value, result);
return result;
}
/**
* Make sure all functions from the active scale are available on the
* proxy. This allows the proxy to be used directly in place of the
* active scale without user code knowing it.
*
* @param {Function} scale (e.g. d3.scaleLinear())
*/
updateProxyScaleFunctions(scale) {
const newScaleKeys = Object.keys(scale);
// clear faked keys list
this.fakedKeys = {};
// remove excess keys
Object.keys(this.proxyScale)
.filter(proxyKey => !newScaleKeys.includes(proxyKey))
.forEach(proxyKey => delete this.proxyScale[proxyKey]);
// add in needed keys
newScaleKeys.forEach(scaleKey => {
this.proxyScale[scaleKey] = (...args) => {
// check for pinned and a setter -- if so, ignore the setter
if (this.pinned) {
if (args.length || scaleKey === 'nice') {
return this.proxyScale;
}
}
const result = this.scale[scaleKey](...args);
// if the function returns the scale, return the proxy scale
if (result === this.scale) {
// this implies it was a setter
this.dispatch.call(Events.proxySet);
return this.proxyScale;
}
// otherwise return the result
return result;
};
});
}
/**
* Fake having functions from the scale to prevent errors in user code
* when we skip switching to the specified scale type due to being pinned.
* Not perfect, but hopefully helps in a few cases.
*
* @param {Function} scale (e.g. d3.scaleLinear())
*/
fakeMissingScaleFunctions(scale) {
const proxyKeys = Object.keys(this.proxyScale);
// find the ones we do not currently have
const missingKeys = Object.keys(scale).filter(newScaleKey => !proxyKeys.includes(newScaleKey));
// add in missing keys
missingKeys.forEach(scaleKey => {
this.proxyScale[scaleKey] = (...args) => {
if (args.length || scaleKey === 'nice') {
return this.proxyScale;
}
// otherwise return an empty object... hopefully this mutes enough errors.
return {};
};
});
this.fakedKeys = missingKeys.reduce((faked, key) => {
faked[key] = true;
return faked;
}, {});
}
/**
* Reset to the original scale
*
* @return {void}
*/
reset() {
// be sure to create a copy so we do not modify the original scale for future resets
if (this.originalScale) {
this.changeScale(this.originalScale.copy(), this.originalScaleType);
}
}
/**
* Reset stats about the scale
*
* @return {void}
*/
statsReset() {
this.stats = { domainCounts: {}, domainHistogram: [], rangeCounts: {}, rangeHistogram: [] };
}
/**
* Record stats each time value is called
*
* @param {Any} domainValue The domain value
* @param {Any} rangeValue The range value the scale produced
* @return {void}
*/
statsRecordValueUsed(domainValue, rangeValue) {
const { domainCounts, rangeCounts, domainHistogram, rangeHistogram } = this.stats;
if (!domainCounts[domainValue]) {
domainCounts[domainValue] = 1;
} else {
domainCounts[domainValue] += 1;
}
if (!rangeCounts[rangeValue]) {
rangeCounts[rangeValue] = 1;
} else {
rangeCounts[rangeValue] += 1;
}
domainHistogram.push(domainValue);
rangeHistogram.push(rangeValue);
this.debouncedProxySet();
}
debouncedProxySet() {
const now = Date.now();
const debounceThreshold = 500;
// enough time has passed, call proxy set
if (this.lastProxySetAttempt && now - this.lastProxySetAttempt > debounceThreshold) {
clearTimeout(this.proxySetTimeout);
this.proxySetTimeout = null;
this.lastProxySetAttempt = null;
// proxy set
this.dispatch.call(Events.proxySet, this);
// not enough time has passed - reset the timer
} else {
this.lastProxySetAttempt = now;
clearTimeout(this.proxySetTimeout);
this.proxySetTimeout = setTimeout(() => this.debouncedProxySet(), debounceThreshold);
}
}
}
ScaleProxy.Events = Events;
// Add in supported scales interface based on the supportedScales array
ScaleProxy.prototype.supportedScales = supportedScales;
// create interface for each d3 scale that is supported.
// this enables d3.scaleInteractive(...).scaleLinear()
supportedScales.forEach(scaleFuncName => {
// e.g. set this.scaleLinear
ScaleProxy.prototype[scaleFuncName] = function supportedScale(...args) {
return this.changeScale(d3Scale[scaleFuncName](...args), scaleFuncName, true);
};
});