forked from susielu/d3-legend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
indexRollup.js
765 lines (627 loc) · 22.1 KB
/
indexRollup.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
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('d3-dispatch'), require('d3-scale'), require('d3-format'), require('d3-array')) :
typeof define === 'function' && define.amd ? define(['d3-dispatch', 'd3-scale', 'd3-format', 'd3-array'], factory) :
(global.indexRollup = factory(global.d3Dispatch,global.d3Scale,global.d3Format,global.d3Array));
}(this, function (d3Dispatch,d3Scale,d3Format,d3Array) { 'use strict';
var helper = {
d3_identity: function d3_identity(d) {
return d;
},
d3_mergeLabels: function d3_mergeLabels(gen, labels) {
if (labels.length === 0) return gen;
gen = gen ? gen : [];
var i = labels.length;
for (; i < gen.length; i++) {
labels.push(gen[i]);
}
return labels;
},
d3_linearLegend: function d3_linearLegend(scale, cells, labelFormat) {
var data = [];
if (cells.length > 1) {
data = cells;
} else {
var domain = scale.domain(),
increment = (domain[domain.length - 1] - domain[0]) / (cells - 1),
i = 0;
for (; i < cells; i++) {
data.push(domain[0] + i * increment);
}
}
var labels = data.map(labelFormat);
return { data: data,
labels: labels,
feature: function feature(d) {
return scale(d);
} };
},
d3_quantLegend: function d3_quantLegend(scale, labelFormat, labelDelimiter) {
var labels = scale.range().map(function (d) {
var invert = scale.invertExtent(d),
a = labelFormat(invert[0]),
b = labelFormat(invert[1]);
return labelFormat(invert[0]) + " " + labelDelimiter + " " + labelFormat(invert[1]);
});
return { data: scale.range(),
labels: labels,
feature: this.d3_identity
};
},
d3_ordinalLegend: function d3_ordinalLegend(scale) {
return { data: scale.domain(),
labels: scale.domain(),
feature: function feature(d) {
return scale(d);
} };
},
d3_drawShapes: function d3_drawShapes(shape, shapes, shapeHeight, shapeWidth, shapeRadius, path) {
if (shape === "rect") {
shapes.attr("height", shapeHeight).attr("width", shapeWidth);
} else if (shape === "circle") {
shapes.attr("r", shapeRadius); //.attr("cx", shapeRadius).attr("cy", shapeRadius);
} else if (shape === "line") {
shapes.attr("x1", 0).attr("x2", shapeWidth).attr("y1", 0).attr("y2", 0);
} else if (shape === "path") {
shapes.attr("d", path);
}
},
d3_addText: function d3_addText(svg, enter, labels, classPrefix) {
enter.append("text").attr("class", classPrefix + "label");
svg.selectAll("g." + classPrefix + "cell text").data(labels).text(this.d3_identity);
},
d3_calcType: function d3_calcType(scale, ascending, cells, labels, labelFormat, labelDelimiter) {
var type = scale.invertExtent ? this.d3_quantLegend(scale, labelFormat, labelDelimiter) : scale.ticks ? this.d3_linearLegend(scale, cells, labelFormat) : this.d3_ordinalLegend(scale);
type.labels = this.d3_mergeLabels(type.labels, labels);
if (ascending) {
type.labels = this.d3_reverse(type.labels);
type.data = this.d3_reverse(type.data);
}
return type;
},
d3_reverse: function d3_reverse(arr) {
var mirror = [];
for (var i = 0, l = arr.length; i < l; i++) {
mirror[i] = arr[l - i - 1];
}
return mirror;
},
d3_placement: function d3_placement(orient, cell, cellTrans, text, textTrans, labelAlign) {
cell.attr("transform", cellTrans);
text.attr("transform", textTrans);
if (orient === "horizontal") {
text.style("text-anchor", labelAlign);
}
},
d3_addEvents: function d3_addEvents(cells, dispatcher) {
var _ = this;
cells.on("mouseover.legend", function (d) {
_.d3_cellOver(dispatcher, d, this);
}).on("mouseout.legend", function (d) {
_.d3_cellOut(dispatcher, d, this);
}).on("click.legend", function (d) {
_.d3_cellClick(dispatcher, d, this);
});
},
d3_cellOver: function d3_cellOver(cellDispatcher, d, obj) {
cellDispatcher.call("cellover", obj, d);
},
d3_cellOut: function d3_cellOut(cellDispatcher, d, obj) {
cellDispatcher.call("cellout", obj, d);
},
d3_cellClick: function d3_cellClick(cellDispatcher, d, obj) {
cellDispatcher.call("cellclick", obj, d);
},
d3_title: function d3_title(svg, title, classPrefix) {
if (title !== "") {
var titleText = svg.selectAll('text.' + classPrefix + 'legendTitle');
titleText.data([title]).enter().append('text').attr('class', classPrefix + 'legendTitle');
svg.selectAll('text.' + classPrefix + 'legendTitle').text(title);
var cellsSvg = svg.select('.' + classPrefix + 'legendCells');
var yOffset = svg.select('.' + classPrefix + 'legendTitle').nodes().map(function (d) {
return d.getBBox().height;
})[0],
xOffset = -cellsSvg.nodes().map(function (d) {
return d.getBBox().x;
})[0];
cellsSvg.attr('transform', 'translate(' + xOffset + ',' + (yOffset + 10) + ')');
}
}
};
function color() {
var scale = d3Scale.scaleLinear(),
shape = "rect",
shapeWidth = 15,
shapeHeight = 15,
shapeRadius = 10,
shapePadding = 2,
cells = [5],
labels = [],
classPrefix = "",
useClass = false,
title = "",
labelFormat = d3Format.format(".01f"),
labelOffset = 10,
labelAlign = "middle",
labelDelimiter = "to",
orient = "vertical",
ascending = false,
path,
legendDispatcher = d3Dispatch.dispatch("cellover", "cellout", "cellclick");
function legend(svg) {
var type = helper.d3_calcType(scale, ascending, cells, labels, labelFormat, labelDelimiter),
legendG = svg.selectAll('g').data([scale]);
legendG.enter().append('g').attr('class', classPrefix + 'legendCells');
var cell = svg.select('.' + classPrefix + 'legendCells').selectAll("." + classPrefix + "cell").data(type.data),
cellEnter = cell.enter().append("g").attr("class", classPrefix + "cell"),
//.merge(cell).style("opacity", 1e-6),
shapeEnter = cellEnter.append(shape).attr("class", classPrefix + "swatch"),
shapes = svg.selectAll("g." + classPrefix + "cell " + shape);
//add event handlers
helper.d3_addEvents(cellEnter, legendDispatcher);
cell.exit().transition().style("opacity", 0).remove();
helper.d3_drawShapes(shape, shapes, shapeHeight, shapeWidth, shapeRadius, path);
helper.d3_addText(svg, cellEnter, type.labels, classPrefix);
// sets placement
var text = cellEnter.selectAll("text"),
shapeSize = shapes.nodes().map(function (d) {
return d.getBBox();
});
//sets scale
//everything is fill except for line which is stroke,
if (!useClass) {
if (shape == "line") {
shapes.style("stroke", type.feature);
} else {
shapes.style("fill", type.feature);
}
} else {
shapes.attr("class", function (d) {
return classPrefix + "swatch " + type.feature(d);
});
}
var cellTrans,
textTrans,
textAlign = labelAlign == "start" ? 0 : labelAlign == "middle" ? 0.5 : 1;
//positions cells and text
if (orient === "vertical") {
cellTrans = function cellTrans(d, i) {
return "translate(0, " + i * (shapeSize[i].height + shapePadding) + ")";
};
textTrans = function textTrans(d, i) {
return "translate(" + (shapeSize[i].width + shapeSize[i].x + labelOffset) + "," + (shapeSize[i].y + shapeSize[i].height / 2 + 5) + ")";
};
} else if (orient === "horizontal") {
cellTrans = function cellTrans(d, i) {
return "translate(" + i * (shapeSize[i].width + shapePadding) + ",0)";
};
textTrans = function textTrans(d, i) {
return "translate(" + (shapeSize[i].width * textAlign + shapeSize[i].x) + "," + (shapeSize[i].height + shapeSize[i].y + labelOffset + 8) + ")";
};
}
helper.d3_placement(orient, cellEnter, cellTrans, text, textTrans, labelAlign);
helper.d3_title(svg, title, classPrefix);
cell.transition().style("opacity", 1);
}
legend.scale = function (_) {
if (!arguments.length) return scale;
scale = _;
return legend;
};
legend.cells = function (_) {
if (!arguments.length) return cells;
if (_.length > 1 || _ >= 2) {
cells = _;
}
return legend;
};
legend.shape = function (_, d) {
if (!arguments.length) return shape;
if (_ == "rect" || _ == "circle" || _ == "line" || _ == "path" && typeof d === 'string') {
shape = _;
path = d;
}
return legend;
};
legend.shapeWidth = function (_) {
if (!arguments.length) return shapeWidth;
shapeWidth = +_;
return legend;
};
legend.shapeHeight = function (_) {
if (!arguments.length) return shapeHeight;
shapeHeight = +_;
return legend;
};
legend.shapeRadius = function (_) {
if (!arguments.length) return shapeRadius;
shapeRadius = +_;
return legend;
};
legend.shapePadding = function (_) {
if (!arguments.length) return shapePadding;
shapePadding = +_;
return legend;
};
legend.labels = function (_) {
if (!arguments.length) return labels;
labels = _;
return legend;
};
legend.labelAlign = function (_) {
if (!arguments.length) return labelAlign;
if (_ == "start" || _ == "end" || _ == "middle") {
labelAlign = _;
}
return legend;
};
legend.labelFormat = function (_) {
if (!arguments.length) return labelFormat;
labelFormat = _;
return legend;
};
legend.labelOffset = function (_) {
if (!arguments.length) return labelOffset;
labelOffset = +_;
return legend;
};
legend.labelDelimiter = function (_) {
if (!arguments.length) return labelDelimiter;
labelDelimiter = _;
return legend;
};
legend.useClass = function (_) {
if (!arguments.length) return useClass;
if (_ === true || _ === false) {
useClass = _;
}
return legend;
};
legend.orient = function (_) {
if (!arguments.length) return orient;
_ = _.toLowerCase();
if (_ == "horizontal" || _ == "vertical") {
orient = _;
}
return legend;
};
legend.ascending = function (_) {
if (!arguments.length) return ascending;
ascending = !!_;
return legend;
};
legend.classPrefix = function (_) {
if (!arguments.length) return classPrefix;
classPrefix = _;
return legend;
};
legend.title = function (_) {
if (!arguments.length) return title;
title = _;
return legend;
};
legend.on = function () {
var value = legendDispatcher.on.apply(legendDispatcher, arguments);
return value === legendDispatcher ? legend : value;
};
return legend;
};
function size() {
var scale = d3Scale.scaleLinear(),
shape = "rect",
shapeWidth = 15,
shapePadding = 2,
cells = [5],
labels = [],
useStroke = false,
classPrefix = "",
title = "",
labelFormat = d3Format.format(".01f"),
labelOffset = 10,
labelAlign = "middle",
labelDelimiter = "to",
orient = "vertical",
ascending = false,
path,
legendDispatcher = d3Dispatch.dispatch("cellover", "cellout", "cellclick");
function legend(svg) {
var type = helper.d3_calcType(scale, ascending, cells, labels, labelFormat, labelDelimiter),
legendG = svg.selectAll('g').data([scale]);
legendG.enter().append('g').attr('class', classPrefix + 'legendCells');
var cell = svg.select('.' + classPrefix + 'legendCells').selectAll("." + classPrefix + "cell").data(type.data),
cellEnter = cell.enter().append("g").attr("class", classPrefix + "cell"),
//.merge(cell).style("opacity", 1e-6),
shapeEnter = cellEnter.append(shape).attr("class", classPrefix + "swatch"),
shapes = svg.selectAll("g." + classPrefix + "cell " + shape);
//add event handlers
helper.d3_addEvents(cellEnter, legendDispatcher);
cell.exit().transition().style("opacity", 0).remove();
//creates shape
if (shape === "line") {
helper.d3_drawShapes(shape, shapes, 0, shapeWidth);
shapes.attr("stroke-width", type.feature);
} else {
helper.d3_drawShapes(shape, shapes, type.feature, type.feature, type.feature, path);
}
helper.d3_addText(svg, cellEnter, type.labels, classPrefix);
//sets placement
var text = cellEnter.selectAll("text"),
shapeSize = shapes.nodes().map(function (d, i) {
var bbox = d.getBBox();
var stroke = scale(type.data[i]);
if (shape === "line" && orient === "horizontal") {
bbox.height = bbox.height + stroke;
} else if (shape === "line" && orient === "vertical") {
bbox.width = bbox.width;
}
return bbox;
});
var maxH = d3Array.max(shapeSize, function (d) {
return d.height + d.y;
}),
maxW = d3Array.max(shapeSize, function (d) {
return d.width + d.x;
});
var cellTrans,
textTrans,
textAlign = labelAlign == "start" ? 0 : labelAlign == "middle" ? 0.5 : 1;
//positions cells and text
if (orient === "vertical") {
cellTrans = function cellTrans(d, i) {
var height = d3Array.sum(shapeSize.slice(0, i + 1), function (d) {
return d.height;
});
return "translate(0, " + (height + i * shapePadding) + ")";
};
textTrans = function textTrans(d, i) {
return "translate(" + (maxW + labelOffset) + "," + (shapeSize[i].y + shapeSize[i].height / 2 + 5) + ")";
};
} else if (orient === "horizontal") {
cellTrans = function cellTrans(d, i) {
var width = d3Array.sum(shapeSize.slice(0, i + 1), function (d) {
return d.width;
});
return "translate(" + (width + i * shapePadding) + ",0)";
};
textTrans = function textTrans(d, i) {
return "translate(" + (shapeSize[i].width * textAlign + shapeSize[i].x) + "," + (maxH + labelOffset) + ")";
};
}
helper.d3_placement(orient, cellEnter, cellTrans, text, textTrans, labelAlign);
helper.d3_title(svg, title, classPrefix);
cell.transition().style("opacity", 1);
}
legend.scale = function (_) {
if (!arguments.length) return scale;
scale = _;
return legend;
};
legend.cells = function (_) {
if (!arguments.length) return cells;
if (_.length > 1 || _ >= 2) {
cells = _;
}
return legend;
};
legend.shape = function (_, d) {
if (!arguments.length) return shape;
if (_ == "rect" || _ == "circle" || _ == "line") {
shape = _;
path = d;
}
return legend;
};
legend.shapeWidth = function (_) {
if (!arguments.length) return shapeWidth;
shapeWidth = +_;
return legend;
};
legend.shapePadding = function (_) {
if (!arguments.length) return shapePadding;
shapePadding = +_;
return legend;
};
legend.labels = function (_) {
if (!arguments.length) return labels;
labels = _;
return legend;
};
legend.labelAlign = function (_) {
if (!arguments.length) return labelAlign;
if (_ == "start" || _ == "end" || _ == "middle") {
labelAlign = _;
}
return legend;
};
legend.labelFormat = function (_) {
if (!arguments.length) return labelFormat;
labelFormat = _;
return legend;
};
legend.labelOffset = function (_) {
if (!arguments.length) return labelOffset;
labelOffset = +_;
return legend;
};
legend.labelDelimiter = function (_) {
if (!arguments.length) return labelDelimiter;
labelDelimiter = _;
return legend;
};
legend.orient = function (_) {
if (!arguments.length) return orient;
_ = _.toLowerCase();
if (_ == "horizontal" || _ == "vertical") {
orient = _;
}
return legend;
};
legend.ascending = function (_) {
if (!arguments.length) return ascending;
ascending = !!_;
return legend;
};
legend.classPrefix = function (_) {
if (!arguments.length) return classPrefix;
classPrefix = _;
return legend;
};
legend.title = function (_) {
if (!arguments.length) return title;
title = _;
return legend;
};
legend.on = function () {
var value = legendDispatcher.on.apply(legendDispatcher, arguments);
return value === legendDispatcher ? legend : value;
};
return legend;
};
function symbol() {
var scale = d3Scale.scaleLinear(),
shape = "path",
shapeWidth = 15,
shapeHeight = 15,
shapeRadius = 10,
shapePadding = 5,
cells = [5],
labels = [],
classPrefix = "",
useClass = false,
title = "",
labelFormat = d3Format.format(".01f"),
labelAlign = "middle",
labelOffset = 10,
labelDelimiter = "to",
orient = "vertical",
ascending = false,
legendDispatcher = d3Dispatch.dispatch("cellover", "cellout", "cellclick");
function legend(svg) {
var type = helper.d3_calcType(scale, ascending, cells, labels, labelFormat, labelDelimiter),
legendG = svg.selectAll('g').data([scale]);
legendG.enter().append('g').attr('class', classPrefix + 'legendCells');
var cell = svg.select('.' + classPrefix + 'legendCells').selectAll("." + classPrefix + "cell").data(type.data),
cellEnter = cell.enter().append("g").attr("class", classPrefix + "cell"),
//.style("opacity", 1e-6),
shapeEnter = cellEnter.append(shape).attr("class", classPrefix + "swatch"),
shapes = svg.selectAll("g." + classPrefix + "cell " + shape);
//add event handlers
helper.d3_addEvents(cellEnter, legendDispatcher);
//remove old shapes
cell.exit().transition().style("opacity", 0).remove();
helper.d3_drawShapes(shape, shapes, shapeHeight, shapeWidth, shapeRadius, type.feature);
helper.d3_addText(svg, cellEnter, type.labels, classPrefix);
// sets placement
var text = cellEnter.selectAll("text"),
shapeSize = shapes.nodes().map(function (d) {
return d.getBBox();
});
var maxH = d3Array.max(shapeSize, function (d) {
return d.height;
}),
maxW = d3Array.max(shapeSize, function (d) {
return d.width;
});
var cellTrans,
textTrans,
textAlign = labelAlign == "start" ? 0 : labelAlign == "middle" ? 0.5 : 1;
//positions cells and text
if (orient === "vertical") {
cellTrans = function cellTrans(d, i) {
return "translate(0, " + i * (maxH + shapePadding) + ")";
};
textTrans = function textTrans(d, i) {
return "translate(" + (maxW + labelOffset) + "," + (shapeSize[i].y + shapeSize[i].height / 2 + 5) + ")";
};
} else if (orient === "horizontal") {
cellTrans = function cellTrans(d, i) {
return "translate(" + i * (maxW + shapePadding) + ",0)";
};
textTrans = function textTrans(d, i) {
return "translate(" + (shapeSize[i].width * textAlign + shapeSize[i].x) + "," + (maxH + labelOffset) + ")";
};
}
helper.d3_placement(orient, cellEnter, cellTrans, text, textTrans, labelAlign);
helper.d3_title(svg, title, classPrefix);
cell.transition().style("opacity", 1);
}
legend.scale = function (_) {
if (!arguments.length) return scale;
scale = _;
return legend;
};
legend.cells = function (_) {
if (!arguments.length) return cells;
if (_.length > 1 || _ >= 2) {
cells = _;
}
return legend;
};
legend.shapePadding = function (_) {
if (!arguments.length) return shapePadding;
shapePadding = +_;
return legend;
};
legend.labels = function (_) {
if (!arguments.length) return labels;
labels = _;
return legend;
};
legend.labelAlign = function (_) {
if (!arguments.length) return labelAlign;
if (_ == "start" || _ == "end" || _ == "middle") {
labelAlign = _;
}
return legend;
};
legend.labelFormat = function (_) {
if (!arguments.length) return labelFormat;
labelFormat = _;
return legend;
};
legend.labelOffset = function (_) {
if (!arguments.length) return labelOffset;
labelOffset = +_;
return legend;
};
legend.labelDelimiter = function (_) {
if (!arguments.length) return labelDelimiter;
labelDelimiter = _;
return legend;
};
legend.orient = function (_) {
if (!arguments.length) return orient;
_ = _.toLowerCase();
if (_ == "horizontal" || _ == "vertical") {
orient = _;
}
return legend;
};
legend.ascending = function (_) {
if (!arguments.length) return ascending;
ascending = !!_;
return legend;
};
legend.classPrefix = function (_) {
if (!arguments.length) return classPrefix;
classPrefix = _;
return legend;
};
legend.title = function (_) {
if (!arguments.length) return title;
title = _;
return legend;
};
legend.on = function () {
var value = legendDispatcher.on.apply(legendDispatcher, arguments);
return value === legendDispatcher ? legend : value;
};
return legend;
};
var index = {
legendColor: color,
legendSize: size,
legendSymbol: symbol
};
return index;
}));
//# sourceMappingURL=indexRollup.js.map