Skip to content

Commit 14492bd

Browse files
committed
Option to show an explicit label for swatches
Note: the scale’s label is not shown for swatches, since most of the time it is obvious from the context
1 parent ebe9187 commit 14492bd

File tree

5 files changed

+52
-38
lines changed

5 files changed

+52
-38
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,8 @@ mylegend = myplot.legend("color")
509509

510510
Categorical and ordinal color legends are rendered as swatches, unless *options*.**legend** is set to *ramp*. The swatches can be configured with the following options:
511511

512-
* *options*.**tickFormat** - a format function for the labels
512+
* *options*.**tickFormat** - a format function for the domain values
513+
* *options*.**label** - explicit label for the legend—by default, no label is shown
513514
* *options*.**swatchSize** - the size of the swatch (if square)
514515
* *options*.**swatchWidth** - the swatches’ width
515516
* *options*.**swatchHeight** - the swatches’ height

src/legends.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export function exposeLegends(scales, defaults = {}) {
2727
}
2828

2929
function legendOptions({label, ticks, tickFormat} = {}, options = {}) {
30-
return {label, ticks, tickFormat, ...options};
30+
return {label, ticks, tickFormat, ...options, explicitLabel: options.label != null};
3131
}
3232

3333
export function Legends(scales, options) {

src/legends/swatches.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ export function legendSwatches(color, {
77
columns,
88
tickFormat,
99
fontVariant = inferFontVariant(color),
10-
// TODO label,
10+
explicitLabel,
11+
label,
1112
swatchSize = 15,
1213
swatchWidth = swatchSize,
1314
swatchHeight = swatchSize,
@@ -26,6 +27,13 @@ export function legendSwatches(color, {
2627
--swatchHeight: ${+swatchHeight}px;
2728
`);
2829

30+
const palette = explicitLabel
31+
? swatches.call(div => div.append("p")
32+
.text(label)
33+
.style("font-weight", "bold"))
34+
.append("div")
35+
: swatches;
36+
2937
let extraStyle;
3038

3139
if (columns != null) {
@@ -46,7 +54,7 @@ export function legendSwatches(color, {
4654
}
4755
`;
4856

49-
swatches
57+
palette
5058
.style("columns", columns)
5159
.selectAll()
5260
.data(color.domain)
@@ -72,8 +80,7 @@ export function legendSwatches(color, {
7280
}
7381
`;
7482

75-
swatches
76-
.selectAll()
83+
palette.selectAll()
7784
.data(color.domain)
7885
.join("span")
7986
.attr("class", `${className}-swatch`)
@@ -102,4 +109,4 @@ export function legendSwatches(color, {
102109
.style("font-variant", impliedString(fontVariant, "normal"))
103110
.call(applyInlineStyles, style)
104111
.node();
105-
}
112+
}
Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
<div class="plot" style="--swatchWidth: 15px; --swatchHeight: 15px; columns: 180px;">
1+
<div class="plot" style="
2+
--swatchWidth: 15px;
3+
--swatchHeight: 15px;
4+
">
25
<style>
36
.plot {
47
font-family: system-ui, sans-serif;
@@ -32,34 +35,37 @@
3235
text-overflow: ellipsis;
3336
}
3437
</style>
35-
<div class="plot-swatch" style="--color: #4e79a7;">
36-
<div class="plot-label" title="Wholesale and Retail Trade">Wholesale and Retail Trade</div>
37-
</div>
38-
<div class="plot-swatch" style="--color: #f28e2c;">
39-
<div class="plot-label" title="Manufacturing">Manufacturing</div>
40-
</div>
41-
<div class="plot-swatch" style="--color: #e15759;">
42-
<div class="plot-label" title="Leisure and hospitality">Leisure and hospitality</div>
43-
</div>
44-
<div class="plot-swatch" style="--color: #76b7b2;">
45-
<div class="plot-label" title="Business services">Business services</div>
46-
</div>
47-
<div class="plot-swatch" style="--color: #59a14f;">
48-
<div class="plot-label" title="Construction">Construction</div>
49-
</div>
50-
<div class="plot-swatch" style="--color: #edc949;">
51-
<div class="plot-label" title="Education and Health">Education and Health</div>
52-
</div>
53-
<div class="plot-swatch" style="--color: #af7aa1;">
54-
<div class="plot-label" title="Government">Government</div>
55-
</div>
56-
<div class="plot-swatch" style="--color: #ff9da7;">
57-
<div class="plot-label" title="Finance">Finance</div>
58-
</div>
59-
<div class="plot-swatch" style="--color: #9c755f;">
60-
<div class="plot-label" title="Self-employed">Self-employed</div>
61-
</div>
62-
<div class="plot-swatch" style="--color: #bab0ab;">
63-
<div class="plot-label" title="Other">Other</div>
38+
<p style="font-weight: bold;">Industry</p>
39+
<div style="columns: 180px;">
40+
<div class="plot-swatch" style="--color: #4e79a7;">
41+
<div class="plot-label" title="Wholesale and Retail Trade">Wholesale and Retail Trade</div>
42+
</div>
43+
<div class="plot-swatch" style="--color: #f28e2c;">
44+
<div class="plot-label" title="Manufacturing">Manufacturing</div>
45+
</div>
46+
<div class="plot-swatch" style="--color: #e15759;">
47+
<div class="plot-label" title="Leisure and hospitality">Leisure and hospitality</div>
48+
</div>
49+
<div class="plot-swatch" style="--color: #76b7b2;">
50+
<div class="plot-label" title="Business services">Business services</div>
51+
</div>
52+
<div class="plot-swatch" style="--color: #59a14f;">
53+
<div class="plot-label" title="Construction">Construction</div>
54+
</div>
55+
<div class="plot-swatch" style="--color: #edc949;">
56+
<div class="plot-label" title="Education and Health">Education and Health</div>
57+
</div>
58+
<div class="plot-swatch" style="--color: #af7aa1;">
59+
<div class="plot-label" title="Government">Government</div>
60+
</div>
61+
<div class="plot-swatch" style="--color: #ff9da7;">
62+
<div class="plot-label" title="Finance">Finance</div>
63+
</div>
64+
<div class="plot-swatch" style="--color: #9c755f;">
65+
<div class="plot-label" title="Self-employed">Self-employed</div>
66+
</div>
67+
<div class="plot-swatch" style="--color: #bab0ab;">
68+
<div class="plot-label" title="Other">Other</div>
69+
</div>
6470
</div>
6571
</div>

test/plots/legend-color.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export function colorLegendCategoricalColumns() {
2121
"Other"
2222
]
2323
},
24-
label: "Hello",
24+
label: "Industry",
2525
columns: "180px"
2626
});
2727
}

0 commit comments

Comments
 (0)