Skip to content

Commit 7e883c2

Browse files
Filmbostock
andauthored
imageFilter (support for the CSS filter attribute) (#409)
Co-authored-by: Mike Bostock <[email protected]>
1 parent ff2e501 commit 7e883c2

File tree

8 files changed

+121
-36
lines changed

8 files changed

+121
-36
lines changed

docs/features/marks.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,7 @@ All marks support the following style options:
476476
* **strokeDashoffset** - the [stroke dash offset](https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/stroke-dashoffset) (typically in pixels)
477477
* **opacity** - object opacity (a number between 0 and 1)
478478
* **mixBlendMode** - the [blend mode](https://developer.mozilla.org/en-US/docs/Web/CSS/mix-blend-mode) (*e.g.*, *multiply*)
479+
* **imageFilter** - a CSS [filter](https://developer.mozilla.org/en-US/docs/Web/CSS/filter) (*e.g.*, *blur(5px)*)
479480
* **shapeRendering** - the [shape-rendering mode](https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/shape-rendering) (*e.g.*, *crispEdges*)
480481
* **paintOrder** - the [paint order](https://developer.mozilla.org/en-US/docs/Web/CSS/paint-order) (*e.g.*, *stroke*)
481482
* **dx** - horizontal offset (in pixels; defaults to 0)

src/mark.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,14 @@ export interface MarkOptions {
399399
*/
400400
mixBlendMode?: string;
401401

402+
/**
403+
* A CSS [filter][1]; a constant string used to adjust the rendering of
404+
* images, such as *blur(5px)*.
405+
*
406+
* [1]: https://developer.mozilla.org/en-US/docs/Web/CSS/filter
407+
*/
408+
imageFilter?: string;
409+
402410
/**
403411
* The [paint-order][1]; a constant string specifying the order in which the
404412
* **fill**, **stroke**, and any markers are drawn; defaults to *normal*,

src/style.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export function styles(
4444
strokeDashoffset,
4545
opacity,
4646
mixBlendMode,
47+
imageFilter,
4748
paintOrder,
4849
pointerEvents,
4950
shapeRendering
@@ -135,6 +136,7 @@ export function styles(
135136
mark.ariaHidden = string(ariaHidden);
136137
mark.opacity = impliedNumber(copacity, 1);
137138
mark.mixBlendMode = impliedString(mixBlendMode, "normal");
139+
mark.imageFilter = impliedString(imageFilter, "none");
138140
mark.paintOrder = impliedString(paintOrder, "normal");
139141
mark.pointerEvents = impliedString(pointerEvents, "auto");
140142
mark.shapeRendering = impliedString(shapeRendering, "auto");
@@ -368,6 +370,7 @@ export function applyIndirectStyles(selection, mark, dimensions, context) {
368370
applyAttr(selection, "stroke-dasharray", mark.strokeDasharray);
369371
applyAttr(selection, "stroke-dashoffset", mark.strokeDashoffset);
370372
applyAttr(selection, "shape-rendering", mark.shapeRendering);
373+
applyAttr(selection, "filter", mark.imageFilter);
371374
applyAttr(selection, "paint-order", mark.paintOrder);
372375
applyAttr(selection, "pointer-events", mark.pointerEvents);
373376
}
Lines changed: 60 additions & 0 deletions
Loading

test/plots/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,7 @@ export * from "./penguin-quantile-unknown.js";
195195
export * from "./penguin-sex-mass-culmen-species.js";
196196
export * from "./penguin-sex.js";
197197
export * from "./penguin-size-symbols.js";
198-
export * from "./penguin-species-cheysson.js";
199-
export * from "./penguin-species-gradient.js";
200-
export * from "./penguin-species-group.js";
198+
export * from "./penguin-species.js";
201199
export * from "./penguin-species-island-relative.js";
202200
export * from "./penguin-species-island-sex.js";
203201
export * from "./penguin-species-island.js";

test/plots/penguin-species-gradient.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

test/plots/penguin-species-group.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

test/plots/penguin-species-cheysson.ts renamed to test/plots/penguin-species.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,51 @@ export async function penguinSpeciesCheysson() {
2828
]
2929
});
3030
}
31+
32+
export async function penguinSpeciesGradient() {
33+
const penguins = await d3.csv<any>("data/penguins.csv", d3.autoType);
34+
return Plot.plot({
35+
marks: [
36+
() => svg`<defs>
37+
<linearGradient id="gradient" gradientTransform="rotate(90)">
38+
<stop offset="5%" stop-color="purple" />
39+
<stop offset="75%" stop-color="red" />
40+
<stop offset="100%" stop-color="gold" />
41+
</linearGradient>
42+
</defs>`,
43+
Plot.barY(penguins, Plot.groupX({y: "count"}, {x: "species", fill: "url(#gradient)"})),
44+
Plot.ruleY([0])
45+
]
46+
});
47+
}
48+
49+
export async function penguinSpeciesGroup() {
50+
const penguins = await d3.csv<any>("data/penguins.csv", d3.autoType);
51+
return Plot.plot({
52+
marks: [
53+
Plot.barX(penguins, Plot.stackX(Plot.groupZ({x: "proportion"}, {fill: "species"}))),
54+
Plot.text(penguins, Plot.stackX(Plot.groupZ({x: "proportion", text: "first"}, {z: "species", text: "species"}))),
55+
Plot.ruleX([0, 1])
56+
]
57+
});
58+
}
59+
60+
export async function penguinSpeciesImageFilter() {
61+
const penguins = await d3.csv<any>("data/penguins.csv", d3.autoType);
62+
return Plot.plot({
63+
marks: [
64+
Plot.barY(
65+
penguins,
66+
Plot.groupX(
67+
{y: "count"},
68+
{
69+
x: "species",
70+
fill: "species",
71+
imageFilter: "drop-shadow(3px 3px red) sepia(40%) drop-shadow(-3px -3px currentColor)"
72+
}
73+
)
74+
),
75+
Plot.ruleY([0])
76+
]
77+
});
78+
}

0 commit comments

Comments
 (0)