Skip to content

Commit 7dc8d9b

Browse files
committed
legends show the label of ordinal scales
1 parent 2e14c35 commit 7dc8d9b

File tree

7 files changed

+27
-24
lines changed

7 files changed

+27
-24
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,8 +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*.**label** - the scale’s label
520521
* *options*.**tickFormat** - a format function for the domain values
521-
* *options*.**label** - explicit label for the legend—by default, no label is shown
522522
* *options*.**swatchSize** - the size of the swatch (if square)
523523
* *options*.**swatchWidth** - the swatches’ width
524524
* *options*.**swatchHeight** - the swatches’ height

src/legends.js

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

4141
function legendOptions({label, ticks, tickFormat} = {}, options = {}) {
42-
return {label, ticks, tickFormat, ...options, explicitLabel: options.label != null};
42+
return {label, ticks, tickFormat, ...options};
4343
}
4444

4545
function legendColor(color, {

src/legends/swatches.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,10 @@ export function legendSymbols(symbol, {
7171
}
7272

7373
function legendItems(scale, {
74+
label = scale.label,
7475
columns,
7576
tickFormat,
7677
fontVariant = inferFontVariant(scale),
77-
explicitLabel,
78-
label,
7978
swatchSize = 15,
8079
swatchWidth = swatchSize,
8180
swatchHeight = swatchSize,
@@ -94,10 +93,11 @@ function legendItems(scale, {
9493
--swatchHeight: ${+swatchHeight}px;
9594
`);
9695

97-
const palette = explicitLabel
96+
const palette = label
9897
? swatches.call(div => div.append("p")
9998
.text(label)
100-
.style("font-weight", "bold"))
99+
.style("font-weight", "bold")
100+
.style("width", "100%"))
101101
.append("div")
102102
: swatches;
103103

test/output/colorLegendCategoricalColumns.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
text-overflow: ellipsis;
3636
}
3737
</style>
38-
<p style="font-weight: bold;">Industry</p>
38+
<p style="font-weight: bold; width: 100%;">Industry</p>
3939
<div style="columns: 180px;">
4040
<div class="plot-swatch" style="--color: #4e79a7;">
4141
<div class="plot-label" title="Wholesale and Retail Trade">Wholesale and Retail Trade</div>

test/output/decathlon.html

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,23 @@
3535
align-items: center;
3636
margin-right: 1em;
3737
}
38-
</style><span class="plot-swatch"><svg viewBox="-8 -8 16 16" stroke="#4e79a7" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
39-
<path d="M4.5,0A4.5,4.5,0,1,1,-4.5,0A4.5,4.5,0,1,1,4.5,0"></path>
40-
</svg>BLS</span><span class="plot-swatch"><svg viewBox="-8 -8 16 16" stroke="#f28e2c" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
41-
<path d="M-6.873088769818877,0L6.873088769818877,0M0,6.873088769818877L0,-6.873088769818877"></path>
42-
</svg>DDR</span><span class="plot-swatch"><svg viewBox="-8 -8 16 16" stroke="#e15759" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
43-
<path d="M-4.869970345289333,-4.869970345289333L4.869970345289333,4.869970345289333M-4.869970345289333,4.869970345289333L4.869970345289333,-4.869970345289333"></path>
44-
</svg>FRG</span><span class="plot-swatch"><svg viewBox="-8 -8 16 16" stroke="#76b7b2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
45-
<path d="M0,-5.442851285360659L4.713647482143115,2.7214256426803294L-4.713647482143115,2.7214256426803294Z"></path>
46-
</svg>GBR</span><span class="plot-swatch"><svg viewBox="-8 -8 16 16" stroke="#59a14f" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
47-
<path d="M0,4.768502950069832L0,-4.768502950069832M-4.129644692781513,-2.384251475034916L4.129644692781513,2.384251475034916M-4.129644692781513,2.384251475034916L4.129644692781513,-2.384251475034916"></path>
48-
</svg>SOV</span><span class="plot-swatch"><svg viewBox="-8 -8 16 16" stroke="#edc949" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
49-
<path d="M3.5341843560130535,3.5341843560130535L3.5341843560130535,-3.5341843560130535L-3.5341843560130535,-3.5341843560130535L-3.5341843560130535,3.5341843560130535Z"></path>
50-
</svg>TCH</span><span class="plot-swatch"><svg viewBox="-8 -8 16 16" stroke="#af7aa1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
51-
<path d="M0,-4.994996508583107L4.994996508583107,0L0,4.994996508583107L-4.994996508583107,0Z"></path>
52-
</svg>USA</span>
38+
</style>
39+
<p style="font-weight: bold; width: 100%;">Country</p>
40+
<div><span class="plot-swatch"><svg viewBox="-8 -8 16 16" stroke="#4e79a7" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
41+
<path d="M4.5,0A4.5,4.5,0,1,1,-4.5,0A4.5,4.5,0,1,1,4.5,0"></path>
42+
</svg>BLS</span><span class="plot-swatch"><svg viewBox="-8 -8 16 16" stroke="#f28e2c" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
43+
<path d="M-6.873088769818877,0L6.873088769818877,0M0,6.873088769818877L0,-6.873088769818877"></path>
44+
</svg>DDR</span><span class="plot-swatch"><svg viewBox="-8 -8 16 16" stroke="#e15759" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
45+
<path d="M-4.869970345289333,-4.869970345289333L4.869970345289333,4.869970345289333M-4.869970345289333,4.869970345289333L4.869970345289333,-4.869970345289333"></path>
46+
</svg>FRG</span><span class="plot-swatch"><svg viewBox="-8 -8 16 16" stroke="#76b7b2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
47+
<path d="M0,-5.442851285360659L4.713647482143115,2.7214256426803294L-4.713647482143115,2.7214256426803294Z"></path>
48+
</svg>GBR</span><span class="plot-swatch"><svg viewBox="-8 -8 16 16" stroke="#59a14f" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
49+
<path d="M0,4.768502950069832L0,-4.768502950069832M-4.129644692781513,-2.384251475034916L4.129644692781513,2.384251475034916M-4.129644692781513,2.384251475034916L4.129644692781513,-2.384251475034916"></path>
50+
</svg>SOV</span><span class="plot-swatch"><svg viewBox="-8 -8 16 16" stroke="#edc949" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
51+
<path d="M3.5341843560130535,3.5341843560130535L3.5341843560130535,-3.5341843560130535L-3.5341843560130535,-3.5341843560130535L-3.5341843560130535,3.5341843560130535Z"></path>
52+
</svg>TCH</span><span class="plot-swatch"><svg viewBox="-8 -8 16 16" stroke="#af7aa1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
53+
<path d="M0,-4.994996508583107L4.994996508583107,0L0,4.994996508583107L-4.994996508583107,0Z"></path>
54+
</svg>USA</span></div>
5355
</div><svg class="plot-2" fill="currentColor" font-family="system-ui, sans-serif" font-size="10" text-anchor="middle" width="640" height="400" viewBox="0 0 640 400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
5456
<style>
5557
.plot-2 {

test/plots/caltrain.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ export default async function() {
1212
color: {
1313
domain: "NLB",
1414
range: ["currentColor", "peru", "brown"],
15-
legend: true
15+
legend: true,
16+
label: null
1617
},
1718
marks: [
1819
Plot.text([[1, "3"]], {

test/plots/legend-color.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export function colorLegendCategorical() {
88
export function colorLegendCategoricalColumns() {
99
return Plot.legend({
1010
color: {
11+
label: "Industry",
1112
domain: [
1213
"Wholesale and Retail Trade",
1314
"Manufacturing",
@@ -21,7 +22,6 @@ export function colorLegendCategoricalColumns() {
2122
"Other"
2223
]
2324
},
24-
label: "Industry",
2525
columns: "180px"
2626
});
2727
}

0 commit comments

Comments
 (0)