Skip to content

Commit 5cd8c08

Browse files
committed
rebase on main
1 parent 4db8fe9 commit 5cd8c08

File tree

5 files changed

+51
-37
lines changed

5 files changed

+51
-37
lines changed

README.md

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

518518
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:
519519

520-
* *options*.**tickFormat** - a format function for the labels
520+
* *options*.**tickFormat** - a format function for the domain values
521+
* *options*.**label** - explicit label for the legend—by default, no label is shown
521522
* *options*.**swatchSize** - the size of the swatch (if square)
522523
* *options*.**swatchWidth** - the swatches’ width
523524
* *options*.**swatchHeight** - the swatches’ height

src/legends.js

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

3131
function legendOptions({label, ticks, tickFormat} = {}, options = {}) {
32-
return {label, ticks, tickFormat, ...options};
32+
return {label, ticks, tickFormat, ...options, explicitLabel: options.label != null};
3333
}
3434

3535
function legendColor(color, {

src/legends/swatches.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ function legendItems(scale, {
7474
columns,
7575
tickFormat,
7676
fontVariant = inferFontVariant(scale),
77-
// TODO label,
77+
explicitLabel,
78+
label,
7879
swatchSize = 15,
7980
swatchWidth = swatchSize,
8081
swatchHeight = swatchSize,
@@ -93,6 +94,13 @@ function legendItems(scale, {
9394
--swatchHeight: ${+swatchHeight}px;
9495
`);
9596

97+
const palette = explicitLabel
98+
? swatches.call(div => div.append("p")
99+
.text(label)
100+
.style("font-weight", "bold"))
101+
.append("div")
102+
: swatches;
103+
96104
let extraStyle;
97105

98106
if (columns != null) {
@@ -113,7 +121,7 @@ function legendItems(scale, {
113121
}
114122
`;
115123

116-
swatches
124+
palette
117125
.style("columns", columns)
118126
.selectAll()
119127
.data(scale.domain)
@@ -139,8 +147,7 @@ function legendItems(scale, {
139147
}
140148
`;
141149

142-
swatches
143-
.selectAll()
150+
palette.selectAll()
144151
.data(scale.domain)
145152
.join("span")
146153
.attr("class", `${className}-swatch`)
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)