Skip to content
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

href channel #645

Merged
merged 4 commits into from
Jan 2, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 src/marks/bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class AbstractBar extends Mark {
.attr("height", this._height(scales, channels, dimensions))
.call(applyAttr, "rx", rx)
.call(applyAttr, "ry", ry)
.call(applyChannelStyles, channels))
.call(applyChannelStyles, this, channels))
.node();
}
_x(scales, {x: X}, {marginLeft}) {
Expand Down
2 changes: 1 addition & 1 deletion src/marks/dot.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class Dot extends Mark {
.attr("cx", X ? i => X[i] : (marginLeft + width - marginRight) / 2)
.attr("cy", Y ? i => Y[i] : (marginTop + height - marginBottom) / 2)
.attr("r", R ? i => R[i] : this.r)
.call(applyChannelStyles, channels))
.call(applyChannelStyles, this, channels))
.node();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/marks/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class Image extends Mark {
.call(applyAttr, "href", S ? i => S[i] : this.src)
.call(applyAttr, "preserveAspectRatio", this.preserveAspectRatio)
.call(applyAttr, "crossorigin", this.crossOrigin)
.call(applyChannelStyles, channels))
.call(applyChannelStyles, this, channels))
.node();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/marks/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class Link extends Mark {
c.lineEnd();
return `${p}`;
})
.call(applyChannelStyles, channels))
.call(applyChannelStyles, this, channels))
.node();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/marks/rect.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export class Rect extends Mark {
.attr("height", Y1 && Y2 && !isCollapsed(y) ? i => Math.max(0, Math.abs(Y1[i] - Y2[i]) - insetTop - insetBottom) : height - marginTop - marginBottom - insetTop - insetBottom)
.call(applyAttr, "rx", rx)
.call(applyAttr, "ry", ry)
.call(applyChannelStyles, channels))
.call(applyChannelStyles, this, channels))
.node();
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/marks/rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class RuleX extends Mark {
.attr("x2", X ? i => X[i] : (marginLeft + width - marginRight) / 2)
.attr("y1", Y1 && !isCollapsed(y) ? i => Y1[i] + insetTop : marginTop + insetTop)
.attr("y2", Y2 && !isCollapsed(y) ? (y.bandwidth ? i => Y2[i] + y.bandwidth() - insetBottom : i => Y2[i] - insetBottom) : height - marginBottom - insetBottom)
.call(applyChannelStyles, channels))
.call(applyChannelStyles, this, channels))
.node();
}
}
Expand Down Expand Up @@ -93,7 +93,7 @@ export class RuleY extends Mark {
.attr("x2", X2 && !isCollapsed(x) ? (x.bandwidth ? i => X2[i] + x.bandwidth() - insetRight : i => X2[i] - insetRight) : width - marginRight - insetRight)
.attr("y1", Y ? i => Y[i] : (marginTop + height - marginBottom) / 2)
.attr("y2", Y ? i => Y[i] : (marginTop + height - marginBottom) / 2)
.call(applyChannelStyles, channels))
.call(applyChannelStyles, this, channels))
.node();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/marks/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export class Text extends Mark {
: text => text.attr("x", X ? i => X[i] : cx).attr("y", Y ? i => Y[i] : cy))
.call(applyAttr, "font-size", FS && (i => FS[i]))
.text(i => T[i])
.call(applyChannelStyles, channels))
.call(applyChannelStyles, this, channels))
.node();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/marks/tick.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class AbstractTick extends Mark {
.attr("x2", this._x2(scales, channels, dimensions))
.attr("y1", this._y1(scales, channels, dimensions))
.attr("y2", this._y2(scales, channels, dimensions))
.call(applyChannelStyles, channels))
.call(applyChannelStyles, this, channels))
.node();
}
}
Expand Down
43 changes: 30 additions & 13 deletions src/style.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {namespaces} from "d3";
import {string, number, maybeColor, maybeNumber, title, titleGroup} from "./mark.js";
import {filter} from "./defined.js";

Expand All @@ -7,6 +8,8 @@ export function styles(
mark,
{
title,
href,
target,
fill,
fillOpacity,
stroke,
Expand Down Expand Up @@ -87,13 +90,15 @@ export function styles(
mark.strokeDasharray = string(strokeDasharray);
}

mark.target = string(target);
mark.opacity = impliedNumber(copacity, 1);
mark.mixBlendMode = impliedString(mixBlendMode, "normal");
mark.shapeRendering = impliedString(shapeRendering, "auto");

return [
...channels,
{name: "title", value: title, optional: true},
{name: "href", value: href, optional: true},
{name: "fill", value: vfill, scale: "color", optional: true},
{name: "fillOpacity", value: vfillOpacity, scale: "opacity", optional: true},
{name: "stroke", value: vstroke, scale: "color", optional: true},
Expand All @@ -103,23 +108,24 @@ export function styles(
];
}

export function applyChannelStyles(selection, {title: L, fill: F, fillOpacity: FO, stroke: S, strokeOpacity: SO, strokeWidth: SW, opacity: O}) {
applyAttr(selection, "fill", F && (i => F[i]));
applyAttr(selection, "fill-opacity", FO && (i => FO[i]));
applyAttr(selection, "stroke", S && (i => S[i]));
applyAttr(selection, "stroke-opacity", SO && (i => SO[i]));
applyAttr(selection, "stroke-width", SW && (i => SW[i]));
applyAttr(selection, "opacity", O && (i => O[i]));
export function applyChannelStyles(selection, {target}, {title: L, fill: F, fillOpacity: FO, stroke: S, strokeOpacity: SO, strokeWidth: SW, opacity: O, href: H}) {
if (F) applyAttr(selection, "fill", i => F[i]);
if (FO) applyAttr(selection, "fill-opacity", i => FO[i]);
if (S) applyAttr(selection, "stroke", i => S[i]);
if (SO) applyAttr(selection, "stroke-opacity", i => SO[i]);
if (SW) applyAttr(selection, "stroke-width", i => SW[i]);
if (O) applyAttr(selection, "opacity", i => O[i]);
if (H) applyHref(selection, H, target);
title(L)(selection);
}

export function applyGroupedChannelStyles(selection, {title: L, fill: F, fillOpacity: FO, stroke: S, strokeOpacity: SO, strokeWidth: SW, opacity: O}) {
applyAttr(selection, "fill", F && (([i]) => F[i]));
applyAttr(selection, "fill-opacity", FO && (([i]) => FO[i]));
applyAttr(selection, "stroke", S && (([i]) => S[i]));
applyAttr(selection, "stroke-opacity", SO && (([i]) => SO[i]));
applyAttr(selection, "stroke-width", SW && (([i]) => SW[i]));
applyAttr(selection, "opacity", O && (([i]) => O[i]));
if (F) applyAttr(selection, "fill", ([i]) => F[i]);
if (FO) applyAttr(selection, "fill-opacity", ([i]) => FO[i]);
if (S) applyAttr(selection, "stroke", ([i]) => S[i]);
if (SO) applyAttr(selection, "stroke-opacity", ([i]) => SO[i]);
if (SW) applyAttr(selection, "stroke-width", ([i]) => SW[i]);
if (O) applyAttr(selection, "opacity", ([i]) => O[i]);
titleGroup(L)(selection);
}

Expand All @@ -141,6 +147,17 @@ export function applyDirectStyles(selection, mark) {
applyAttr(selection, "opacity", mark.opacity);
}

function applyHref(selection, href, target) {
selection.each(function(i) {
if (href[i] != null) {
const a = document.createElementNS(namespaces.svg, "a");
a.setAttributeNS(namespaces.xlink, "href", href[i]);
if (target != null) a.setAttribute("target", target);
this.parentNode.insertBefore(a, this).appendChild(this);
Copy link
Contributor

Choose a reason for hiding this comment

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

makes we wish we could do selection.wrap("svg:a") (related: d3/d3-selection#296)

Copy link
Contributor

Choose a reason for hiding this comment

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

selection.wrap: d3/d3-selection#297

}
});
}

export function applyAttr(selection, name, value) {
if (value != null) selection.attr(name, value);
}
Expand Down