Skip to content

opacity legend #587

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ Continuous color legends are rendered as a ramp, and can be configured with the

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

Returns a legend for the given *scale* definition, passing the options described in the previous section. The only supported name for now is *color*.
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.

### Position options

Expand Down
7 changes: 5 additions & 2 deletions src/legends.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import {normalizeScale} from "./scales.js";
import {legendColor} from "./legends/color.js";
import {legendOpacity} from "./legends/opacity.js";
import {isObject} from "./mark.js";

const legendRegistry = new Map([
["color", legendColor]
["color", legendColor],
["opacity", legendOpacity]
]);

export function legend(options = {}) {
for (const [key, value] of legendRegistry) {
const scale = options[key];
if (scale != null) {
if (isObject(scale)) { // e.g., ignore {color: "red"}
return value(normalizeScale(key, scale), legendOptions(scale, options));
}
}
Expand Down
19 changes: 19 additions & 0 deletions src/legends/opacity.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {rgb} from "d3";
import {legendColor} from "./color.js";

const black = rgb(0, 0, 0);

export function legendOpacity({type, interpolate, ...scale}, {
legend = "ramp",
color = black,
...options
}) {
if (!interpolate) throw new Error(`${type} opacity scales are not supported`);
if (`${legend}`.toLowerCase() !== "ramp") throw new Error(`${legend} opacity legends are not supported`);
Comment on lines +11 to +12
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added some explicit limitations here; I imagine we’ll add support for other opacity scale types in the future.

return legendColor({type, ...scale, interpolate: interpolateOpacity(color)}, {legend, ...options});
}

function interpolateOpacity(color) {
const {r, g, b} = rgb(color) || black; // treat invalid color as black
return t => `rgba(${r},${g},${b},${t})`;
}
9 changes: 6 additions & 3 deletions src/mark.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,15 @@ export function arrayify(data, type) {
: (data instanceof type ? data : type.from(data)));
}

// Disambiguates an options object (e.g., {y: "x2"}) from a primitive value.
export function isObject(option) {
return option && option.toString === objectToString;
}

// Disambiguates an options object (e.g., {y: "x2"}) from a channel value
// definition expressed as a channel transform (e.g., {transform: …}).
export function isOptions(option) {
return option
&& option.toString === objectToString
&& typeof option.transform !== "function";
return isObject(option) && typeof option.transform !== "function";
}

// For marks specified either as [0, x] or [x1, x2], such as areas and bars.
Expand Down
37 changes: 37 additions & 0 deletions test/output/opacityLegend.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 37 additions & 0 deletions test/output/opacityLegendColor.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 37 additions & 0 deletions test/output/opacityLegendLinear.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
49 changes: 49 additions & 0 deletions test/output/opacityLegendLog.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 37 additions & 0 deletions test/output/opacityLegendRange.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 37 additions & 0 deletions test/output/opacityLegendSqrt.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion test/plots/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ export {default as industryUnemployment} from "./industry-unemployment.js";
export {default as industryUnemploymentShare} from "./industry-unemployment-share.js";
export {default as industryUnemploymentStream} from "./industry-unemployment-stream.js";
export {default as learningPoverty} from "./learning-poverty.js";
export * from "./legend-color.js";
export {default as letterFrequencyBar} from "./letter-frequency-bar.js";
export {default as letterFrequencyCloud} from "./letter-frequency-cloud.js";
export {default as letterFrequencyColumn} from "./letter-frequency-column.js";
Expand Down Expand Up @@ -129,3 +128,6 @@ export {default as usRetailSales} from "./us-retail-sales.js";
export {default as usStatePopulationChange} from "./us-state-population-change.js";
export {default as wordCloud} from "./word-cloud.js";
export {default as wordLengthMobyDick} from "./word-length-moby-dick.js";

export * from "./legend-color.js";
export * from "./legend-opacity.js";
25 changes: 25 additions & 0 deletions test/plots/legend-opacity.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import * as Plot from "@observablehq/plot";

export function opacityLegend() {
return Plot.legend({opacity: {domain: [0, 10], label: "Quantitative"}});
}

export function opacityLegendRange() {
return Plot.legend({opacity: {domain: [0, 1], range: [0.5, 1], label: "Range"}});
}

export function opacityLegendLinear() {
return Plot.legend({opacity: {type: "linear", domain: [0, 10], label: "Linear"}});
}

export function opacityLegendColor() {
return Plot.legend({opacity: {type: "linear", domain: [0, 10], label: "Linear"}, color: "steelblue"});
}

export function opacityLegendLog() {
return Plot.legend({opacity: {type: "log", domain: [1, 10], label: "Log"}});
}

export function opacityLegendSqrt() {
return Plot.legend({opacity: {type: "sqrt", domain: [0, 1], label: "Sqrt"}});
}