observablehq / observablehq/plot
Swatches legends should support the label option
Nobody has claimed this yet.
- Dominant language
- HTML
- Stars
- 5.4k
- Forks
- 244
- PR merge metrics
- No merged PRs in 30d
Description
I have three kinds of plots, all using binX:
- quantitative data with automatically computed range, ramp legend created with
myplot.legend('color', ...) - quantitative data with manually assigned ranged, threshold legend
Plot.legend({ color: ... - categorical data with automatically computed range, categorical legend created with
myplot.legend('color', ... )
In the first two cases, a scale legend is shown, defaulting to the name of the channel (right terminology?) containing the values being color-coded.


In the third case, a scale legend is not shown, and passing label: has no effect.

This is my code:
const colorBy = window.ChartControls.options.colorBy;
const scheme = {
participantsCount: 'turbo',
graticuleLatitude: 'rdylbu',
graticuleLongitude: 'rdylbu',
success: 'set1',
}[colorBy];
const plotEl = Plot.plot({
color:
{
participantsCount: {
type: 'threshold',
domain: d3.range(0, 10),
scheme,
},
}[colorBy] || (scheme ? { scheme } : undefined),
marks: [
Plot.rectY(
expeditions,
Plot.binX(
{
y: 'count',
},
{
x: {
value: 'date',
interval: d3.utcMonth,
},
fill:
{
weekDay: 'weekDayName',
participants: 'participantsOrMultiple',
graticuleName: 'graticuleNameShort',
}[colorBy] || colorBy,
}
)
),
],
background: '#222',
width: document.getElementById('chart').getClientRects()[0]
.width,
height:
document
.getElementById('Chart-container')
.getClientRects()[0].height - 50,
style: {
background: 'transparent',
color: 'white',
},
});
let legendEl;
if (colorBy === 'participantsCount') {
legendEl = Plot.legend({
color: {
type: 'threshold',
domain: d3.range(0, 14),
scheme,
},
label: 'Particpants',
style: { color: 'white', background: 'transparent' },
});
} else {
legendEl = plotEl.legend('color', {
style: { color: 'white', background: 'transparent' },
label: 'hello',
});
}
document.getElementById('chart').replaceChildren(plotEl);
document.getElementById('chart-legend').replaceChildren(legendEl);
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the Plot.legend and plotEl.legend('color', ...) entry points, reproducing the categorical-color example with label: 'hello'. Compare it with the ramp and threshold legend cases shown in the issue. Done means the categorical legend displays the supplied label option.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- d3, javascript
- Domain
- data-visualization
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100