observablehq / plot

A concise API for exploratory data visualization implementing a layered grammar of graphics
https://observablehq.com/plot/
ISC License
4.27k stars 174 forks source link

Swatches legends should support the label option #834

Open stevage opened 2 years ago

stevage commented 2 years ago

I have three kinds of plots, all using binX:

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.

image

image

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

image

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);
mbostock commented 2 years ago

Swatches legends do not currently support a label option, but it’s noted as a TODO here:

https://github.com/observablehq/plot/blob/3f8ac1369b9838e0f0036a6fe19aed6e841af0fb/src/legends/swatches.js#L77

Fil commented 2 years ago

there's even a pull-request https://github.com/observablehq/plot/pull/621

tophtucker commented 1 year ago

This came up today in discussing how the chart cell height jumps when you switch between an ordinal and continuous color scale. Fil pointed out that continuous also gets a label and ordinal doesn't, which contributes to the height difference.

(Separately, it'd be nice if one row of swatches matched the height of the continuous legend. Though I know it's tricky, since continuous demands value labels below, whereas ordinal can have value labels between. I guess it's in the nature of continuity that it leaves no room for inline labels!)

jheer commented 5 months ago

I'd like to +1 / bump this issue. I was surprised to be unable to add a title to categorical legends!

mkfreeman commented 3 months ago

Also chiming in to 👍 this issue (now closed by #1885).