-
Notifications
You must be signed in to change notification settings - Fork 184
line halo #870
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
base: main
Are you sure you want to change the base?
line halo #870
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import {isColor} from "../options.js"; | ||
|
||
let nextHaloId = 0; | ||
|
||
export function applyHalo(g, {color, radius}) { | ||
const id = `plot-linehalo-${nextHaloId++}`; | ||
g.selectChildren().style("filter", `url(#${id})`); | ||
g.append("filter").attr("id", id).html(` | ||
<feMorphology in="SourceAlpha" result="DILATED" operator="dilate" radius="${radius}"></feMorphology> | ||
<feFlood flood-color="${color}" result="BG"></feFlood> | ||
<feComposite in="BG" in2="DILATED" operator="in" result="OUTLINE"></feComposite> | ||
<feMerge> | ||
<feMergeNode in="OUTLINE" /> | ||
<feMergeNode in="SourceGraphic" /> | ||
</feMerge>`); | ||
} | ||
|
||
export function maybeHalo(halo, color, radius) { | ||
if (halo === undefined) halo = color !== undefined || radius !== undefined; | ||
if (!halo) return false; | ||
const defaults = {color: "white", radius: 2}; | ||
if (color === undefined) { | ||
color = isColor(halo) ? halo : defaults.color; | ||
} else if (!isColor(color)) { | ||
throw new Error(`Unsupported halo color: ${color}`); | ||
} | ||
if (radius === undefined) { | ||
radius = !isNaN(+halo) ? +halo : defaults.radius; | ||
} else if (isNaN(+radius)) { | ||
throw new Error(`Unsupported halo radius: ${radius}`); | ||
} | ||
return {color, radius}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import {curveLinear, geoPath, line as shapeLine} from "d3"; | ||
import {curveLinear, geoPath, group, line as shapeLine} from "d3"; | ||
import {create} from "../context.js"; | ||
import {Curve} from "../curve.js"; | ||
import {Mark} from "../mark.js"; | ||
|
@@ -13,6 +13,7 @@ import { | |
} from "../style.js"; | ||
import {maybeDenseIntervalX, maybeDenseIntervalY} from "../transforms/bin.js"; | ||
import {applyGroupedMarkers, markers} from "./marker.js"; | ||
import {applyHalo, maybeHalo} from "./halo.js"; | ||
|
||
const defaults = { | ||
ariaLabel: "line", | ||
|
@@ -39,7 +40,7 @@ function LineCurve({curve = curveAuto, tension}) { | |
|
||
export class Line extends Mark { | ||
constructor(data, options = {}) { | ||
const {x, y, z} = options; | ||
const {x, y, z, halo, haloColor, haloRadius} = options; | ||
super( | ||
data, | ||
{ | ||
|
@@ -52,6 +53,7 @@ export class Line extends Mark { | |
); | ||
this.z = z; | ||
this.curve = LineCurve(options); | ||
this.halo = maybeHalo(halo, haloColor, haloRadius); | ||
markers(this, options); | ||
} | ||
filter(index) { | ||
|
@@ -66,8 +68,8 @@ export class Line extends Mark { | |
render(index, scales, channels, dimensions, context) { | ||
const {x: X, y: Y} = channels; | ||
const {curve} = this; | ||
return create("svg:g", context) | ||
.call(applyIndirectStyles, this, dimensions, context) | ||
const g = create("svg:g", context) | ||
.call(applyIndirectStyles, this, scales, dimensions) | ||
.call(applyTransform, this, scales) | ||
.call((g) => | ||
g | ||
|
@@ -88,8 +90,25 @@ export class Line extends Mark { | |
.x((i) => X[i]) | ||
.y((i) => Y[i]) | ||
) | ||
) | ||
.node(); | ||
); | ||
|
||
if (this.halo) { | ||
// With variable aesthetics, we need to regroup segments by line | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is hard to follow. I think it would be clearer to initialize the DOM the desired way from the beginning, rather than creating it once (above using groupIndex) and then moving things around afterwards to apply the halo. (Especially since this already requires changing groupIndex to populate a segment property on the data, whose purpose isn’t clear until you go look at the code here.) |
||
let line = -1; | ||
let segmented = false; | ||
const groups = group(g.selectAll("path"), (d) => | ||
d.__data__.segment === undefined ? ++line : ((segmented = true), line) | ||
); | ||
if (segmented) { | ||
for (const [, paths] of groups) { | ||
const l = g.append("g").node(); | ||
for (const p of paths) l.appendChild(p); | ||
} | ||
} | ||
applyHalo(g, this.halo); | ||
} | ||
|
||
return g.node(); | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You only need the result+in for dilated; otherwise in defaults to the previous filter primitive.