-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColorScale.js
829 lines (706 loc) · 33.4 KB
/
ColorScale.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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
/**
@external BaseClass
@see https://github.com/d3plus/d3plus-common#BaseClass
*/
import {extent, max, min, quantile, range, deviation} from "d3-array";
import {interpolateRgb} from "d3-interpolate";
import {scaleLinear, scaleThreshold} from "d3-scale";
import {select} from "d3-selection";
import {transition} from "d3-transition";
import {Axis} from "d3plus-axis";
import {colorDefaults, colorLighter} from "d3plus-color";
import {accessor, assign, BaseClass, constant, elem, unique} from "d3plus-common";
import {formatAbbreviate} from "d3plus-format";
import {Rect} from "d3plus-shape";
import {TextBox, textWidth} from "d3plus-text";
import ckmeans from "./ckmeans.js";
import Legend from "./Legend.js";
/**
@class ColorScale
@extends external:BaseClass
@desc Creates an SVG scale based on an array of data. If *data* is specified, immediately draws based on the specified array and returns the current class instance. If *data* is not specified on instantiation, it can be passed/updated after instantiation using the [data](#shape.data) method.
*/
export default class ColorScale extends BaseClass {
/**
@memberof ColorScale
@desc Invoked when creating a new class instance, and sets any default parameters.
@private
*/
constructor() {
super();
this._axisClass = new Axis();
this._axisConfig = {
gridSize: 0
};
this._axisTest = new Axis();
this._align = "middle";
this._buckets = 5;
this._bucketAxis = false;
this._bucketFormat = (tick, i, ticks, allValues) => {
const format = this._axisConfig.tickFormat
? this._axisConfig.tickFormat : formatAbbreviate;
const next = ticks[i + 1];
const prev = i ? ticks[i - 1] : false;
const last = i === ticks.length - 1;
if (tick === next || last) {
const suffix = last && tick < max(allValues) ? "+" : "";
return `${format(tick)}${suffix}`;
}
else {
const mod = next ? next / 100 : tick / 100;
const pow = mod >= 1 || mod <= -1 ? Math.round(mod).toString().length - 1 : mod.toString().split(".")[1].replace(/([1-9])[1-9].*$/, "$1").length * -1;
const ten = Math.pow(10, pow);
const prevValue = prev === tick && i === 1
? format(min([tick + ten, allValues.find(d => d > tick && d < next)]))
: format(tick);
const nextValue = tick && i === 1
? format(next)
: format(max([next - ten, allValues.reverse().find(d => d > tick && d < next)]));
return this._bucketJoiner(prevValue, nextValue);
}
};
this._bucketJoiner = (a, b) => a !== b ? `${a} - ${b}` : `${a}`;
this._centered = true;
this._color = ["#54478C", "#2C699A", "#0DB39E", "#83E377", "#EFEA5A"];
this._colorMax = colorDefaults.on;
this._colorMid = colorDefaults.light;
this._colorMin = colorDefaults.off;
this._data = [];
this._duration = 600;
this._height = 200;
this._labelClass = new TextBox();
this._labelConfig = {
fontColor: colorDefaults.dark,
fontSize: 12
};
this._legendClass = new Legend();
this._legendConfig = {
shapeConfig: {
stroke: colorDefaults.dark,
strokeWidth: 1
}
};
this._midpoint = 0;
this._orient = "bottom";
this._outerBounds = {width: 0, height: 0, x: 0, y: 0};
this._padding = 5;
this._rectClass = new Rect().parent(this);
this._rectConfig = {
stroke: "#999",
strokeWidth: 1
};
this._scale = "linear";
this._size = 10;
this._value = accessor("value");
this._width = 400;
}
/**
@memberof ColorScale
@desc Renders the current ColorScale to the page. If a *callback* is specified, it will be called once the ColorScale is done drawing.
@param {Function} [*callback* = undefined]
@chainable
*/
render(callback) {
if (this._select === void 0) this.select(select("body").append("svg").attr("width", `${this._width}px`).attr("height", `${this._height}px`).node());
const horizontal = ["bottom", "top"].includes(this._orient);
const height = horizontal ? "height" : "width",
width = horizontal ? "width" : "height",
x = horizontal ? "x" : "y",
y = horizontal ? "y" : "x";
// Shape <g> Group
this._group = elem("g.d3plus-ColorScale", {parent: this._select});
const allValues = this._data
.map(this._value)
.filter(d => d !== null && typeof d === "number")
.sort((a, b) => a - b);
const domain = this._domain || extent(allValues);
const negative = domain[0] < this._midpoint;
const positive = domain[1] > this._midpoint;
const diverging = negative && positive;
const numBuckets = min([
this._buckets instanceof Array ? this._buckets.length : this._buckets,
diverging && this._scale !== "jenks" ? 2 * Math.floor(unique(allValues).length / 2) - 1 : unique(allValues).length
]);
let colors = diverging
&& (!this._color || (this._color instanceof Array && !this._color.includes(this._colorMid)))
? undefined
: this._color,
labels,
ticks;
if (colors && !(colors instanceof Array)) {
colors = range(0, numBuckets, 1)
.map(i => colorLighter(colors, (i + 1) / numBuckets))
.reverse();
}
if (this._scale === "jenks") {
const buckets = min([colors ? colors.length : numBuckets, numBuckets, allValues.length]);
let jenks = [];
if (this._buckets instanceof Array) {
ticks = this._buckets;
}
else {
if (diverging && this._centered) {
const half = Math.floor(buckets / 2);
const residual = buckets % 2;
const negatives = allValues.filter(d => d < this._midpoint);
const negativesDeviation = deviation(negatives);
const positives = allValues.concat(this._midpoint).filter(d => d >= this._midpoint);
const positivesDeviation = deviation(positives);
const isNegativeMax = negativesDeviation > positivesDeviation ? 1 : 0;
const isPositiveMax = positivesDeviation > negativesDeviation ? 1 : 0;
const negativeJenks = ckmeans(negatives, min([half + residual * isNegativeMax, negatives.length]));
const positiveJenks = ckmeans(positives, min([half + residual * isPositiveMax, positives.length]));
jenks = negativeJenks.concat(positiveJenks);
}
else {
jenks = ckmeans(allValues, buckets);
}
ticks = jenks.map(c => c[0]);
}
const tickSet = new Set(ticks);
if (ticks.length !== tickSet.size) labels = Array.from(tickSet);
if (!colors) {
if (diverging) {
colors = [this._colorMin, this._colorMid, this._colorMax];
const negatives = ticks
.slice(0, buckets)
.filter((d, i) => d < this._midpoint && ticks[i + 1] <= this._midpoint);
const spanning = ticks
.slice(0, buckets)
.filter((d, i) => d <= this._midpoint && ticks[i + 1] > this._midpoint);
const positives = ticks
.slice(0, buckets)
.filter(d => d > this._midpoint);
const negativeColors = negatives.map((d, i) => !i ? colors[0] : colorLighter(colors[0], i / negatives.length));
const spanningColors = spanning.map(() => colors[1]);
const positiveColors = positives.map((d, i) => i === positives.length - 1 ? colors[2] : colorLighter(colors[2], 1 - (i + 1) / positives.length));
colors = negativeColors.concat(spanningColors).concat(positiveColors);
}
else {
colors = range(0, numBuckets, 1)
.map(i => colorLighter(this._colorMax, i / numBuckets))
.reverse();
}
}
if (ticks.length <= buckets) colors = colors.slice(-ticks.length);
colors = [colors[0]].concat(colors);
this._colorScale = scaleThreshold()
.domain(ticks)
.range(colors);
}
else {
let buckets = this._buckets instanceof Array ? this._buckets : undefined;
if (diverging && !colors) {
const half = Math.floor(numBuckets / 2);
const negativeColorScale = interpolateRgb.gamma(2.2)(this._colorMin, this._colorMid);
const negativeColors = range(0, half, 1).map(i => negativeColorScale(i / half));
const spanningColors = (numBuckets % 2 ? [0] : []).map(() => this._colorMid);
const positiveColorScale = interpolateRgb.gamma(2.2)(this._colorMax, this._colorMid);
const positiveColors = range(0, half, 1).map(i => positiveColorScale(i / half)).reverse();
colors = negativeColors.concat(spanningColors).concat(positiveColors);
if (!buckets) {
const step = (colors.length - 1) / 2;
buckets = [domain[0], this._midpoint, domain[1]];
buckets = range(domain[0], this._midpoint, -(domain[0] - this._midpoint) / step)
.concat(range(this._midpoint, domain[1], (domain[1] - this._midpoint) / step))
.concat([domain[1]]);
}
}
else {
if (!colors) {
if (this._scale === "buckets" || this._scale === "quantile") {
colors = range(0, numBuckets, 1)
.map(i => colorLighter(negative ? this._colorMin : this._colorMax, i / numBuckets));
if (positive) colors = colors.reverse();
}
else {
colors = negative ? [this._colorMin, colorLighter(this._colorMin, 0.8)]
: [colorLighter(this._colorMax, 0.8), this._colorMax];
}
}
if (!buckets) {
if (this._scale === "quantile") {
const step = 1 / (colors.length - 1);
buckets = range(0, 1 + step / 2, step)
.map(d => quantile(allValues, d));
}
else if (diverging && this._color && this._centered) {
const midIndex = colors.indexOf(this._colorMid);
const negativeStep = (this._midpoint - domain[0]) / midIndex;
const positiveStep = (domain[1] - this._midpoint) / (colors.length - midIndex);
const negativeBuckets = range(domain[0], this._midpoint, negativeStep);
const positiveBuckets = range(this._midpoint, domain[1] + positiveStep / 2, positiveStep);
buckets = negativeBuckets.concat(positiveBuckets);
}
else {
const step = (domain[1] - domain[0]) / (colors.length - 1);
buckets = range(domain[0], domain[1] + step / 2, step);
}
}
}
if (this._scale === "buckets" || this._scale === "quantile") {
ticks = buckets;
colors = [colors[0]].concat(colors);
}
else if (this._scale === "log") {
const negativeBuckets = buckets.filter(d => d < 0);
if (negativeBuckets.length) {
const minVal = negativeBuckets[0];
const newNegativeBuckets = negativeBuckets.map(d => -Math.pow(Math.abs(minVal), d / minVal));
negativeBuckets.forEach((bucket, i) => {
buckets[buckets.indexOf(bucket)] = newNegativeBuckets[i];
});
}
const positiveBuckets = buckets.filter(d => d > 0);
if (positiveBuckets.length) {
const maxVal = positiveBuckets[positiveBuckets.length - 1];
const newPositiveBuckets = positiveBuckets.map(d => Math.pow(maxVal, d / maxVal));
positiveBuckets.forEach((bucket, i) => {
buckets[buckets.indexOf(bucket)] = newPositiveBuckets[i];
});
}
if (buckets.includes(0)) buckets[buckets.indexOf(0)] = 1;
}
this._colorScale = (this._scale === "buckets" || this._scale === "quantile" ? scaleThreshold : scaleLinear)()
.domain(buckets)
.range(colors);
}
if (this._colorScale.clamp) this._colorScale.clamp(true);
const gradient = this._bucketAxis || !["buckets", "jenks", "quantile"].includes(this._scale);
const t = transition().duration(this._duration);
const groupParams = {enter: {opacity: 0}, exit: {opacity: 0}, parent: this._group, transition: t, update: {opacity: 1}};
const labelGroup = elem("g.d3plus-ColorScale-labels", Object.assign({condition: gradient}, groupParams));
const rectGroup = elem("g.d3plus-ColorScale-Rect", Object.assign({condition: gradient}, groupParams));
const legendGroup = elem("g.d3plus-ColorScale-legend", Object.assign({condition: !gradient}, groupParams));
if (gradient) {
const offsets = {x: 0, y: 0};
const axisDomain = domain.slice();
if (this._bucketAxis) {
const last = axisDomain[axisDomain.length - 1];
const prev = axisDomain[axisDomain.length - 2];
const mod = last ? last / 10 : prev / 10;
const pow = mod >= 1 || mod <= -1 ? Math.round(mod).toString().length - 1 : mod.toString().split(".")[1].replace(/([1-9])[1-9].*$/, "$1").length * -1;
const ten = Math.pow(10, pow);
axisDomain[axisDomain.length - 1] = last + ten;
}
const axisConfig = assign({
domain: horizontal ? axisDomain : axisDomain.slice().reverse(),
duration: this._duration,
height: this._height,
labels: labels || ticks,
orient: this._orient,
padding: this._padding,
scale: this._scale === "log" ? "log" : "linear",
ticks,
width: this._width
}, this._axisConfig);
const labelConfig = assign({
height: this[`_${height}`] / 2,
width: this[`_${width}`] / 2
}, this._labelConfig);
this._labelClass.config(labelConfig);
const labelData = [];
if (horizontal && this._labelMin) {
const labelCSS = {
"font-family": this._labelClass.fontFamily()(this._labelMin),
"font-size": this._labelClass.fontSize()(this._labelMin),
"font-weight": this._labelClass.fontWeight()(this._labelMin)
};
if (labelCSS["font-family"] instanceof Array) labelCSS["font-family"] = labelCSS["font-family"][0];
let labelMinWidth = textWidth(this._labelMin, labelCSS);
if (labelMinWidth && labelMinWidth < this[`_${width}`] / 2) {
labelData.push(this._labelMin);
labelMinWidth += this._padding;
if (horizontal) offsets.x += labelMinWidth;
axisConfig[width] -= labelMinWidth;
}
}
if (horizontal && this._labelMax) {
const labelCSS = {
"font-family": this._labelClass.fontFamily()(this._labelMax),
"font-size": this._labelClass.fontSize()(this._labelMax),
"font-weight": this._labelClass.fontWeight()(this._labelMax)
};
if (labelCSS["font-family"] instanceof Array) labelCSS["font-family"] = labelCSS["font-family"][0];
let labelMaxWidth = textWidth(this._labelMax, labelCSS);
if (labelMaxWidth && labelMaxWidth < this[`_${width}`] / 2) {
labelData.push(this._labelMax);
labelMaxWidth += this._padding;
if (!horizontal) offsets.y += labelMaxWidth;
axisConfig[width] -= labelMaxWidth;
}
}
this._axisTest
.select(elem("g.d3plus-ColorScale-axisTest", {enter: {opacity: 0}, parent: this._group}).node())
.config(axisConfig)
.duration(0)
.render();
const axisBounds = this._axisTest.outerBounds();
this._outerBounds[width] = this[`_${width}`] - this._padding * 2;
this._outerBounds[height] = axisBounds[height] + this._size;
this._outerBounds[x] = this._padding;
this._outerBounds[y] = this._padding;
if (this._align === "middle") this._outerBounds[y] = (this[`_${height}`] - this._outerBounds[height]) / 2;
else if (this._align === "end") this._outerBounds[y] = this[`_${height}`] - this._padding - this._outerBounds[height];
const axisGroupOffset = this._outerBounds[y] + (["bottom", "right"].includes(this._orient) ? this._size : 0) - (axisConfig.padding || this._axisClass.padding());
const transform = `translate(${offsets.x + (horizontal ? 0 : axisGroupOffset)}, ${offsets.y + (horizontal ? axisGroupOffset : 0)})`;
this._axisClass
.select(elem("g.d3plus-ColorScale-axis", assign(groupParams, {
condition: true,
enter: {transform},
update: {transform}
})).node())
.config(axisConfig)
.align("start")
.render();
const axisScale = this._axisTest._getPosition.bind(this._axisTest);
const scaleRange = this._axisTest._getRange();
let defs = this._group.selectAll("defs").data([0]);
const defsEnter = defs.enter().append("defs");
defsEnter.append("linearGradient").attr("id", `gradient-${this._uuid}`);
defs = defsEnter.merge(defs);
defs.select("linearGradient")
.attr(`${x}1`, horizontal ? "0%" : "100%")
.attr(`${x}2`, horizontal ? "100%" : "0%")
.attr(`${y}1`, "0%")
.attr(`${y}2`, "0%");
const stops = defs.select("linearGradient").selectAll("stop")
.data(colors);
const scaleDomain = this._colorScale.domain();
const offsetScale = scaleLinear()
.domain(scaleRange)
.range(horizontal ? [0, 100] : [100, 0]);
stops.enter().append("stop").merge(stops)
.attr("offset", (d, i) => `${i <= scaleDomain.length - 1 ? offsetScale(axisScale(scaleDomain[i])) : 100}%`)
.attr("stop-color", String);
/** determines the width of buckets */
const bucketWidth = (d, i) => {
const next = ticks[i + 1] || axisDomain[axisDomain.length - 1];
return Math.abs(axisScale(next) - axisScale(d));
};
const rectConfig = assign({
duration: this._duration,
fill: ticks ? d => this._colorScale(d) : `url(#gradient-${this._uuid})`,
[x]: ticks ? (d, i) => axisScale(d) + bucketWidth(d, i) / 2 - (["left", "right"].includes(this._orient) ? bucketWidth(d, i) : 0) : scaleRange[0] + (scaleRange[1] - scaleRange[0]) / 2 + offsets[x],
[y]: this._outerBounds[y] + (["top", "left"].includes(this._orient) ? axisBounds[height] : 0) + this._size / 2 + offsets[y],
[width]: ticks ? bucketWidth : scaleRange[1] - scaleRange[0],
[height]: this._size
}, this._rectConfig);
this._rectClass
.data(ticks || [0])
.id((d, i) => i)
.select(rectGroup.node())
.config(rectConfig)
.render();
labelConfig.height = this._outerBounds[height];
labelConfig.width = this._outerBounds[width];
this._labelClass
.config(labelConfig)
.data(labelData)
.select(labelGroup.node())
.x(d => d === this._labelMax
? rectConfig.x + rectConfig.width / 2 + this._padding
: this._outerBounds.x)
.y(d => rectConfig.y - this._labelClass.fontSize()(d) / 2)
.text(d => d)
.rotate(horizontal ? 0 : this._orient === "right" ? 90 : -90)
.render();
}
else {
elem("g.d3plus-ColorScale-axis", Object.assign({condition: gradient}, groupParams));
let legendData = ticks.reduce((arr, tick, i) => {
const label = this._bucketFormat.bind(this)(tick, i, ticks, allValues);
arr.push({color: colors[i + 1], id: label});
return arr;
}, []);
if (!horizontal) legendData = legendData.reverse();
const legendConfig = assign({
align: horizontal ? "center" : {start: "left", middle: "center", end: "right"}[this._align],
direction: horizontal ? "row" : "column",
duration: this._duration,
height: this._height,
padding: this._padding,
shapeConfig: assign({
duration: this._duration
}, this._axisConfig.shapeConfig || {}),
title: this._axisConfig.title,
titleConfig: this._axisConfig.titleConfig || {},
width: this._width,
verticalAlign: horizontal ? {start: "top", middle: "middle", end: "bottom"}[this._align] : "middle"
}, this._legendConfig);
this._legendClass
.data(legendData)
.select(legendGroup.node())
.config(legendConfig)
.render();
this._outerBounds = this._legendClass.outerBounds();
}
if (callback) setTimeout(callback, this._duration + 100);
return this;
}
/**
@memberof ColorScale
@desc The [ColorScale](http://d3plus.org/docs/#ColorScale) is constructed by combining an [Axis](http://d3plus.org/docs/#Axis) for the ticks/labels and a [Rect](http://d3plus.org/docs/#Rect) for the actual color box (or multiple boxes, as in a jenks scale). Because of this, there are separate configs for the [Axis](http://d3plus.org/docs/#Axis) class used to display the text ([axisConfig](http://d3plus.org/docs/#ColorScale.axisConfig)) and the [Rect](http://d3plus.org/docs/#Rect) class used to draw the color breaks ([rectConfig](http://d3plus.org/docs/#ColorScale.rectConfig)). This method acts as a pass-through to the config method of the [Axis](http://d3plus.org/docs/#Axis). An example usage of this method can be seen [here](http://d3plus.org/examples/d3plus-legend/colorScale-dark/).
@param {Object} [*value*]
@chainable
*/
axisConfig(_) {
return arguments.length ? (this._axisConfig = assign(this._axisConfig, _), this) : this._axisConfig;
}
/**
@memberof ColorScale
@desc If *value* is specified, sets the horizontal alignment to the specified value and returns the current class instance. If *value* is not specified, returns the current horizontal alignment.
@param {String} [*value* = "center"] Supports `"left"` and `"center"` and `"right"`.
@chainable
*/
align(_) {
return arguments.length ? (this._align = _, this) : this._align;
}
/**
@memberof ColorScale
@desc The number of discrete buckets to create in a bucketed color scale. Will be overridden by any custom Array of colors passed to the `color` method. Optionally, users can supply an Array of values used to separate buckets, such as `[0, 10, 25, 50, 90]` for a percentage scale. This value would create 4 buckets, with each value representing the break point between each bucket (so 5 values makes 4 buckets).
@param {Number|Array} [*value* = 5]
@chainable
*/
buckets(_) {
return arguments.length ? (this._buckets = _, this) : this._buckets;
}
/**
@memberof ColorScale
@desc Determines whether or not to use an Axis to display bucket scales (both "buckets" and "jenks"). When set to `false`, bucketed scales will use the `Legend` class to display squares for each range of data. When set to `true`, bucketed scales will be displayed on an `Axis`, similar to "linear" scales.
@param {Boolean} [*value* = false]
@chainable
*/
bucketAxis(_) {
return arguments.length ? (this._bucketAxis = _, this) : this._bucketAxis;
}
/**
@memberof ColorScale
@desc A function for formatting the labels associated to each bucket in a bucket-type scale ("jenks", "quantile", etc). The function is passed four arguments: the start value of the current bucket, it's index in the full Array of buckets, the full Array of buckets, and an Array of every value present in the data used to construct the buckets. Keep in mind that the end value for the bucket is not actually the next bucket in the list, but includes every value up until that next bucket value (less than, but not equal to). By default, d3plus will make the end value slightly less than it's current value, so that it does not overlap with the start label for the next bucket.
@param {Function} [*value*]
@chainable
*/
bucketFormat(_) {
return arguments.length ? (this._bucketFormat = _, this) : this._bucketFormat;
}
/**
@memberof ColorScale
@desc A function that receives the minimum and maximum values of a bucket, and is expected to return the full label.
@param {Function} [*value*]
@chainable
*/
bucketJoiner(_) {
return arguments.length ? (this._bucketJoiner = _, this) : this._bucketJoiner;
}
/**
@memberof ColorScale
@desc Determines whether or not to display a midpoint centered Axis. Does not apply to quantile scales.
@param {Boolean} [*value* = false]
@chainable
*/
centered(_) {
return arguments.length ? (this._centered = _, this) : this._centered;
}
/**
@memberof ColorScale
@desc Overrides the default internal logic of `colorMin`, `colorMid`, and `colorMax` to only use just this specified color. If a single color is given as a String, then the scale is interpolated by lightening that color. Otherwise, the function expects an Array of color values to be used in order for the scale.
@param {String|Array} [*value*]
@chainable
*/
color(_) {
return arguments.length ? (this._color = _, this) : this._color;
}
/**
@memberof ColorScale
@desc Defines the color to be used for numbers greater than the value of the `midpoint` on the scale (defaults to `0`). Colors in between this value and the value of `colorMid` will be interpolated, unless a custom Array of colors has been specified using the `color` method.
@param {String} [*value* = "#0C8040"]
@chainable
*/
colorMax(_) {
return arguments.length ? (this._colorMax = _, this) : this._colorMax;
}
/**
@memberof ColorScale
@desc Defines the color to be used for the midpoint of a diverging scale, based on the current value of the `midpoint` method (defaults to `0`). Colors in between this value and the values of `colorMin` and `colorMax` will be interpolated, unless a custom Array of colors has been specified using the `color` method.
@param {String} [*value* = "#f7f7f7"]
@chainable
*/
colorMid(_) {
return arguments.length ? (this._colorMid = _, this) : this._colorMid;
}
/**
@memberof ColorScale
@desc Defines the color to be used for numbers less than the value of the `midpoint` on the scale (defaults to `0`). Colors in between this value and the value of `colorMid` will be interpolated, unless a custom Array of colors has been specified using the `color` method.
@param {String} [*value* = "#b22200"]
@chainable
*/
colorMin(_) {
return arguments.length ? (this._colorMin = _, this) : this._colorMin;
}
/**
@memberof ColorScale
@desc If *data* is specified, sets the data array to the specified array and returns the current class instance. If *data* is not specified, returns the current data array. A shape key will be drawn for each object in the array.
@param {Array} [*data* = []]
@chainable
*/
data(_) {
return arguments.length ? (this._data = _, this) : this._data;
}
/**
@memberof ColorScale
@desc In a linear scale, this Array of 2 values defines the min and max values used in the color scale. Any values outside of this range will be mapped to the nearest color value.
@param {Array} [*value*]
@chainable
*/
domain(_) {
return arguments.length ? (this._domain = _, this) : this._domain;
}
/**
@memberof ColorScale
@desc If *value* is specified, sets the transition duration of the ColorScale and returns the current class instance. If *value* is not specified, returns the current duration.
@param {Number} [*value* = 600]
@chainable
*/
duration(_) {
return arguments.length ? (this._duration = _, this) : this._duration;
}
/**
@memberof ColorScale
@desc If *value* is specified, sets the overall height of the ColorScale and returns the current class instance. If *value* is not specified, returns the current height value.
@param {Number} [*value* = 100]
@chainable
*/
height(_) {
return arguments.length ? (this._height = _, this) : this._height;
}
/**
@memberof ColorScale
@desc A pass-through for the [TextBox](http://d3plus.org/docs/#TextBox) class used to style the labelMin and labelMax text.
@param {Object} [*value*]
@chainable
*/
labelConfig(_) {
return arguments.length ? (this._labelConfig = assign(this._labelConfig, _), this) : this._labelConfig;
}
/**
@memberof ColorScale
@desc Defines a text label to be displayed off of the end of the minimum point in the scale (currently only available in horizontal orientation).
@param {String} [*value*]
@chainable
*/
labelMin(_) {
return arguments.length ? (this._labelMin = _, this) : this._labelMin;
}
/**
@memberof ColorScale
@desc Defines a text label to be displayed off of the end of the maximum point in the scale (currently only available in horizontal orientation).
@param {String} [*value*]
@chainable
*/
labelMax(_) {
return arguments.length ? (this._labelMax = _, this) : this._labelMax;
}
/**
@memberof ColorScale
@desc The [ColorScale](http://d3plus.org/docs/#ColorScale) is constructed by combining an [Axis](http://d3plus.org/docs/#Axis) for the ticks/labels and a [Rect](http://d3plus.org/docs/#Rect) for the actual color box (or multiple boxes, as in a jenks scale). Because of this, there are separate configs for the [Axis](http://d3plus.org/docs/#Axis) class used to display the text ([axisConfig](http://d3plus.org/docs/#ColorScale.axisConfig)) and the [Rect](http://d3plus.org/docs/#Rect) class used to draw the color breaks ([rectConfig](http://d3plus.org/docs/#ColorScale.rectConfig)). This method acts as a pass-through to the config method of the [Axis](http://d3plus.org/docs/#Axis). An example usage of this method can be seen [here](http://d3plus.org/examples/d3plus-legend/colorScale-dark/).
@param {Object} [*value*]
@chainable
*/
legendConfig(_) {
return arguments.length ? (this._legendConfig = assign(this._legendConfig, _), this) : this._legendConfig;
}
/**
@memberof ColorScale
@desc The number value to be used as the anchor for `colorMid`, and defines the center point of the diverging color scale.
@param {Number} [*value* = 0]
@chainable
*/
midpoint(_) {
return arguments.length ? (this._midpoint = _, this) : this._midpoint;
}
/**
@memberof ColorScale
@desc Sets the flow of the items inside the ColorScale. If no value is passed, the current flow will be returned.
@param {String} [*value* = "bottom"]
@chainable
*/
orient(_) {
return arguments.length ? (this._orient = _, this) : this._orient;
}
/**
@memberof ColorScale
@desc If called after the elements have been drawn to DOM, will returns the outer bounds of the ColorScale content.
@example
{"width": 180, "height": 24, "x": 10, "y": 20}
*/
outerBounds() {
return this._outerBounds;
}
/**
@memberof ColorScale
@desc If *value* is specified, sets the padding between each key to the specified number and returns the current class instance. If *value* is not specified, returns the current padding value.
@param {Number} [*value* = 10]
@chainable
*/
padding(_) {
return arguments.length ? (this._padding = _, this) : this._padding;
}
/**
@memberof ColorScale
@desc The [ColorScale](http://d3plus.org/docs/#ColorScale) is constructed by combining an [Axis](http://d3plus.org/docs/#Axis) for the ticks/labels and a [Rect](http://d3plus.org/docs/#Rect) for the actual color box (or multiple boxes, as in a jenks scale). Because of this, there are separate configs for the [Axis](http://d3plus.org/docs/#Axis) class used to display the text ([axisConfig](http://d3plus.org/docs/#ColorScale.axisConfig)) and the [Rect](http://d3plus.org/docs/#Rect) class used to draw the color breaks ([rectConfig](http://d3plus.org/docs/#ColorScale.rectConfig)). This method acts as a pass-through to the config method of the [Rect](http://d3plus.org/docs/#Rect). An example usage of this method can be seen [here](http://d3plus.org/examples/d3plus-legend/colorScale-dark/).
@param {Object} [*value*]
@chainable
*/
rectConfig(_) {
return arguments.length ? (this._rectConfig = assign(this._rectConfig, _), this) : this._rectConfig;
}
/**
@memberof ColorScale
@desc If *value* is specified, sets the scale of the ColorScale and returns the current class instance. If *value* is not specified, returns the current scale value.
@param {String} [*value* = "linear"] Can either be "linear", "jenks", or "buckets".
@chainable
*/
scale(_) {
return arguments.length ? (this._scale = _, this) : this._scale;
}
/**
@memberof ColorScale
@desc If *selector* is specified, sets the SVG container element to the specified d3 selector or DOM element and returns the current class instance. If *selector* is not specified, returns the current SVG container element.
@param {String|HTMLElement} [*selector* = d3.select("body").append("svg")]
@chainable
*/
select(_) {
return arguments.length ? (this._select = select(_), this) : this._select;
}
/**
@memberof ColorScale
@desc The height of horizontal color scales, and width when positioned vertical.
@param {Number} [*value* = 10]
@chainable
*/
size(_) {
return arguments.length ? (this._size = _, this) : this._size;
}
/**
@memberof ColorScale
@desc If *value* is specified, sets the value accessor to the specified function or string and returns the current class instance. If *value* is not specified, returns the current value accessor.
@param {Function|String} [*value*]
@chainable
@example
function value(d) {
return d.value;
}
*/
value(_) {
return arguments.length ? (this._value = typeof _ === "function" ? _ : constant(_), this) : this._value;
}
/**
@memberof ColorScale
@desc If *value* is specified, sets the overall width of the ColorScale and returns the current class instance. If *value* is not specified, returns the current width value.
@param {Number} [*value* = 400]
@chainable
*/
width(_) {
return arguments.length ? (this._width = _, this) : this._width;
}
}