Skip to content

Commit 30ed257

Browse files
Filmbostock
authored andcommitted
opacity legend
1 parent 740605f commit 30ed257

10 files changed

+236
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ Continuous color legends are rendered as a ramp, and can be configured with the
258258

259259
#### Plot.legend({[*name*]: *scale*, ...*options*})
260260

261-
Returns a legend for the given *scale* definition, passing the options described in the previous section. The only supported name for now is *color*.
261+
Returns a legend for the given *scale* definition, passing the options described in the previous section. Currently supports only *color* and *opacity* scales. An opacity scale is treated as a color scale with varying transparency.
262262

263263
### Position options
264264

src/legends.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import {normalizeScale} from "./scales.js";
22
import {legendColor} from "./legends/color.js";
3+
import {legendOpacity} from "./legends/opacity.js";
34

45
const legendRegistry = new Map([
5-
["color", legendColor]
6+
["color", legendColor],
7+
["opacity", legendOpacity]
68
]);
79

810
export function legend(options = {}) {

src/legends/opacity.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import {legendColor} from "./color.js";
2+
3+
export function legendOpacity({type, interpolate, ...scale}, {legend = "ramp", ...options}) {
4+
if (!interpolate) throw new Error(`${type} opacity scales are not supported`);
5+
if (`${legend}`.toLowerCase() !== "ramp") throw new Error(`${legend} opacity legends are not supported`);
6+
return legendColor({type, ...scale, interpolate: interpolateOpacity}, {legend, ...options});
7+
}
8+
9+
function interpolateOpacity(t) {
10+
return `rgba(0,0,0,${t})`;
11+
}

test/output/opacityLegend.svg

Lines changed: 37 additions & 0 deletions
Loading

test/output/opacityLegendLinear.svg

Lines changed: 37 additions & 0 deletions
Loading

test/output/opacityLegendLog.svg

Lines changed: 49 additions & 0 deletions
Loading

test/output/opacityLegendRange.svg

Lines changed: 37 additions & 0 deletions
Loading

test/output/opacityLegendSqrt.svg

Lines changed: 37 additions & 0 deletions
Loading

test/plots/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ export {default as industryUnemployment} from "./industry-unemployment.js";
5252
export {default as industryUnemploymentShare} from "./industry-unemployment-share.js";
5353
export {default as industryUnemploymentStream} from "./industry-unemployment-stream.js";
5454
export {default as learningPoverty} from "./learning-poverty.js";
55-
export * from "./legend-color.js";
5655
export {default as letterFrequencyBar} from "./letter-frequency-bar.js";
5756
export {default as letterFrequencyCloud} from "./letter-frequency-cloud.js";
5857
export {default as letterFrequencyColumn} from "./letter-frequency-column.js";
@@ -129,3 +128,6 @@ export {default as usRetailSales} from "./us-retail-sales.js";
129128
export {default as usStatePopulationChange} from "./us-state-population-change.js";
130129
export {default as wordCloud} from "./word-cloud.js";
131130
export {default as wordLengthMobyDick} from "./word-length-moby-dick.js";
131+
132+
export * from "./legend-color.js";
133+
export * from "./legend-opacity.js";

test/plots/legend-opacity.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import * as Plot from "@observablehq/plot";
2+
3+
export function opacityLegend() {
4+
return Plot.legend({opacity: {domain: [0, 10], label: "Quantitative"}});
5+
}
6+
7+
export function opacityLegendRange() {
8+
return Plot.legend({opacity: {domain: [0, 1], range: [0.5, 1], label: "Range"}});
9+
}
10+
11+
export function opacityLegendLinear() {
12+
return Plot.legend({opacity: {type: "linear", domain: [0, 10], label: "Linear"}});
13+
}
14+
15+
export function opacityLegendLog() {
16+
return Plot.legend({opacity: {type: "log", domain: [1, 10], label: "Log"}});
17+
}
18+
19+
export function opacityLegendSqrt() {
20+
return Plot.legend({opacity: {type: "sqrt", domain: [0, 1], label: "Sqrt"}});
21+
}

0 commit comments

Comments
 (0)