Skip to content
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
20 changes: 14 additions & 6 deletions src/legends.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {rgb} from "d3";
import {select} from "d3";
import {createContext} from "./context.js";
import {legendRamp} from "./legends/ramp.js";
import {isSymbolColorLegend, legendSwatches, legendSymbols} from "./legends/swatches.js";
import {inherit, isScaleOptions} from "./options.js";
import {getFilterId} from "./style.js";
import {normalizeScale} from "./scales.js";

const legendRegistry = new Map([
Expand Down Expand Up @@ -56,16 +57,23 @@ function legendColor(color, {legend = true, ...options}) {
}
}

function legendOpacity({type, interpolate, ...scale}, {legend = true, color = rgb(0, 0, 0), ...options}) {
function legendOpacity({type, interpolate, ...scale}, {legend = true, color = "currentColor", ...options}) {
if (!interpolate) throw new Error(`${type} opacity scales are not supported`);
if (legend === true) legend = "ramp";
if (`${legend}`.toLowerCase() !== "ramp") throw new Error(`${legend} opacity legends are not supported`);
return legendColor({type, ...scale, interpolate: interpolateOpacity(color)}, {legend, ...options});
const node = legendColor({type, ...scale, interpolate: interpolateOpacity}, {legend, ...options});
if (!node) return;
const fid = getFilterId();
const svg = select(node);
svg.select("image").attr("filter", `url(#${fid})`);
const filter = svg.append("filter").attr("id", fid);
filter.append("feFlood").attr("flood-color", color);
filter.append("feComposite").attr("in2", "SourceGraphic").attr("operator", "in");
return node;
}

function interpolateOpacity(color) {
const {r, g, b} = rgb(color) || rgb(0, 0, 0); // treat invalid color as black
return (t) => `rgba(${r},${g},${b},${t})`;
function interpolateOpacity(t) {
return `rgba(0,0,0,${t})`;
}

export function createLegends(scales, context, options) {
Expand Down
5 changes: 5 additions & 0 deletions src/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ export function setOffset(o) {
}

let nextClipId = 0;
let nextFilterId = 0;
let nextPatternId = 0;

export function getClipId() {
return `plot-clip-${++nextClipId}`;
}

export function getFilterId() {
return `plot-filter-${++nextFilterId}`;
}

export function getPatternId() {
return `plot-pattern-${++nextPatternId}`;
}
Expand Down
6 changes: 5 additions & 1 deletion 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.
47 changes: 47 additions & 0 deletions test/output/opacityLegendCSS4.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 5 additions & 1 deletion 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.
6 changes: 5 additions & 1 deletion 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.
6 changes: 5 additions & 1 deletion 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.
6 changes: 5 additions & 1 deletion 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.
6 changes: 5 additions & 1 deletion 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.
49 changes: 8 additions & 41 deletions test/plot-snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ export function test(plot) {
svg.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:xlink", "http://www.w3.org/1999/xlink");
}
reindexStyle(root);
reindexMarker(root);
reindexClip(root);
reindexPattern(root);
reindexIri(root, "clip", ["clip-path"]);
reindexIri(root, "filter", ["filter"]);
reindexIri(root, "marker", ["marker-start", "marker-mid", "marker-end"]);
reindexIri(root, "pattern", ["fill", "stroke"]);
const actual = normalizeHtml(root.outerHTML);
const outfile = join("test", "output", `${name}.${ext}`);
const diffile = join("test", "output", `${name}-changed.${ext}`);
Expand Down Expand Up @@ -63,50 +64,16 @@ function reindexStyle(root) {
}
}

function reindexMarker(root) {
function reindexIri(root, name, attributes) {
let index = 0;
const map = new Map();
for (const node of root.querySelectorAll("[id^=plot-marker-]")) {
for (const node of root.querySelectorAll(`[id^=plot-${name}-]`)) {
let id = node.getAttribute("id");
if (map.has(id)) id = map.get(id);
else map.set(id, (id = `plot-marker-${++index}`));
else map.set(id, (id = `plot-${name}-${++index}`));
node.setAttribute("id", id);
}
for (const key of ["marker-start", "marker-mid", "marker-end"]) {
for (const node of root.querySelectorAll(`[${key}]`)) {
let id = node.getAttribute(key).slice(5, -1);
if (map.has(id)) node.setAttribute(key, `url(#${map.get(id)})`);
}
}
}

function reindexClip(root) {
let index = 0;
const map = new Map();
for (const node of root.querySelectorAll("[id^=plot-clip-]")) {
let id = node.getAttribute("id");
if (map.has(id)) id = map.get(id);
else map.set(id, (id = `plot-clip-${++index}`));
node.setAttribute("id", id);
}
for (const key of ["clip-path"]) {
for (const node of root.querySelectorAll(`[${key}]`)) {
let id = node.getAttribute(key).slice(5, -1);
if (map.has(id)) node.setAttribute(key, `url(#${map.get(id)})`);
}
}
}

function reindexPattern(root) {
let index = 0;
const map = new Map();
for (const node of root.querySelectorAll("[id^=plot-pattern-]")) {
let id = node.getAttribute("id");
if (map.has(id)) id = map.get(id);
else map.set(id, (id = `plot-pattern-${++index}`));
node.setAttribute("id", id);
}
for (const key of ["fill", "stroke"]) {
for (const key of attributes) {
for (const node of root.querySelectorAll(`[${key}]`)) {
let id = node.getAttribute(key).slice(5, -1);
if (map.has(id)) node.setAttribute(key, `url(#${map.get(id)})`);
Expand Down
4 changes: 4 additions & 0 deletions test/plots/legend-opacity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@ test(function opacityLegendLog() {
test(function opacityLegendSqrt() {
return Plot.legend({opacity: {type: "sqrt", domain: [0, 1], label: "Sqrt"}});
});

test(function opacityLegendCSS4() {
return Plot.legend({opacity: {type: "linear", domain: [0, 1], label: "p3"}, color: "color(display-p3 0 1 0)"});
});