Skip to content

className #126

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

Closed
wants to merge 1 commit into from
Closed
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
10 changes: 8 additions & 2 deletions src/axis.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export class AxisX {
grid,
label,
labelAnchor,
labelOffset
labelOffset,
className = "axisX"
} = {}) {
this.name = name;
this.axis = (axis + "").toLowerCase();
Expand All @@ -26,6 +27,7 @@ export class AxisX {
this.label = label;
this.labelAnchor = labelAnchor;
this.labelOffset = labelOffset;
this.className = className;
}
render(
index,
Expand Down Expand Up @@ -59,6 +61,7 @@ export class AxisX {
const offsetSign = axis === "top" ? -1 : 1;
const ty = offsetSign * offset + (axis === "top" ? marginTop : height - marginBottom);
return create("svg:g")
.classed(this.className, true)
.attr("transform", `translate(0,${ty})`)
.call((axis === "top" ? axisTop : axisBottom)(round(x))
.ticks(Array.isArray(ticks) ? null : ticks, typeof tickFormat === "function" ? null : tickFormat)
Expand Down Expand Up @@ -100,7 +103,8 @@ export class AxisY {
grid,
label,
labelAnchor,
labelOffset
labelOffset,
className = "axisY"
} = {}) {
this.name = name;
this.axis = axis = (axis + "").toLowerCase();
Expand All @@ -113,6 +117,7 @@ export class AxisY {
this.label = label;
this.labelAnchor = labelAnchor;
this.labelOffset = labelOffset;
this.className = className;
}
render(
index,
Expand Down Expand Up @@ -144,6 +149,7 @@ export class AxisY {
const offsetSign = axis === "left" ? -1 : 1;
const tx = offsetSign * offset + (axis === "right" ? width - marginRight : marginLeft);
return create("svg:g")
.classed(this.className, true)
.attr("transform", `translate(${tx},0)`)
.call((axis === "right" ? axisRight : axisLeft)(round(y))
.ticks(Array.isArray(ticks) ? null : ticks, typeof tickFormat === "function" ? null : tickFormat)
Expand Down
5 changes: 4 additions & 1 deletion src/facet.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function facets(data, {x, y, ...options}, marks) {
}

class Facet extends Mark {
constructor(data, {x, y, transform} = {}, marks = []) {
constructor(data, {x, y, className = "facet", transform} = {}, marks = []) {
super(
data,
[
Expand All @@ -23,6 +23,7 @@ class Facet extends Mark {
this.marksChannels = undefined; // array of mark channels
this.marksIndex = undefined; // array of mark indexes (for non-faceted marks)
this.marksIndexByFacet = undefined; // map from facet key to array of mark indexes
this.className = className;
}
initialize() {
const {index, channels} = super.initialize();
Expand Down Expand Up @@ -73,7 +74,9 @@ class Facet extends Mark {
const fyMargins = fy && {marginTop: 0, marginBottom: 0, height: fy.bandwidth()};
const fxMargins = fx && {marginRight: 0, marginLeft: 0, width: fx.bandwidth()};
const subdimensions = {...dimensions, ...fxMargins, ...fyMargins};
const className = this.className;
return create("svg:g")
.classed(className, true)
.call(g => {
if (fy && axes.y) {
const domain = fy.domain();
Expand Down
6 changes: 3 additions & 3 deletions src/marks/area.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class Area extends Mark {
transform
);
this.curve = Curve(curve);
Style(this, {fill: cfill, stroke: cstroke, ...style});
Style(this, {fill: cfill, stroke: cstroke, className: "area", ...style});
}
render(I, {x, y, color}, {x1: X1, y1: Y1, x2: X2 = X1, y2: Y2 = Y1, z: Z, title: L, fill: F, stroke: S}) {
return create("svg:g")
Expand Down Expand Up @@ -73,10 +73,10 @@ export function area(data, options) {

export function areaX(data, {x, x1, x2, y = indexOf, ...options} = {}) {
([x1, x2] = maybeZero(x, x1, x2));
return new Area(data, {...options, x1, x2, y1: y, y2: undefined});
return new Area(data, {className: "areaX", ...options, x1, x2, y1: y, y2: undefined});
}

export function areaY(data, {x = indexOf, y, y1, y2, ...options} = {}) {
([y1, y2] = maybeZero(y, y1, y2));
return new Area(data, {...options, x1: x, x2: undefined, y1, y2});
return new Area(data, {className: "areaY", ...options, x1: x, x2: undefined, y1, y2});
}
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 {
],
transform
);
Style(this, {fill: cfill, stroke: cstroke, ...style});
Style(this, {fill: cfill, stroke: cstroke, className: "bar", ...style});
this.insetTop = number(insetTop);
this.insetRight = number(insetRight);
this.insetBottom = number(insetBottom);
Expand Down
3 changes: 3 additions & 0 deletions src/marks/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export function bin(data, {x, y, domain, thresholds, normalize, transform, ...op
{
insetTop: 1,
insetLeft: 1,
className: "bin",
...options,
transform: bin2({x, y, domain, thresholds}),
fill: normalize ? normalizer(normalize, data.length) : length,
Expand All @@ -36,6 +37,7 @@ export function binX(data, {
data,
{
insetLeft: 1,
className: "binX",
...options,
transform: bin1({value: x, domain, thresholds, cumulative}),
y: normalize ? normalizer(normalize, data.length) : length,
Expand All @@ -60,6 +62,7 @@ export function binY(data, {
data,
{
insetTop: 1,
className: "binY",
...options,
transform: bin1({value: y, domain, thresholds, cumulative}),
x: normalize ? normalizer(normalize, data.length) : length,
Expand Down
6 changes: 3 additions & 3 deletions src/marks/cell.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class Cell extends AbstractBar {
{name: "x", value: x, scale: "x", type: "band", optional: true},
{name: "y", value: y, scale: "y", type: "band", optional: true}
],
options
{className: "cell", ...options}
);
}
_transform() {
Expand All @@ -26,9 +26,9 @@ export function cell(data, options) {
}

export function cellX(data, {x = identity, ...options} = {}) {
return new Cell(data, {...options, x, y: null});
return new Cell(data, {className: "cellX", ...options, x, y: null});
}

export function cellY(data, {y = identity, ...options} = {}) {
return new Cell(data, {...options, y, x: null});
return new Cell(data, {className: "cellY", ...options, y, x: null});
}
1 change: 1 addition & 0 deletions src/marks/dot.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export class Dot extends Mark {
fill: cfill,
stroke: cstroke,
strokeWidth: cstroke === "none" ? undefined : 1.5,
className: "dot",
...style
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/marks/frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class Frame extends Mark {
...style
} = {}) {
super();
Style(this, {fill, stroke, ...style});
Style(this, {fill, stroke, className: "frame", ...style});
}
render(
index,
Expand Down
3 changes: 3 additions & 0 deletions src/marks/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export function group(data, {
return cell(
data,
{
className: "group",
...options,
transform: group2(x, y),
x: maybeLabel(first, x),
Expand All @@ -36,6 +37,7 @@ export function groupX(data, {
return barY(
data,
{
className: "groupX",
...options,
transform: group1(x),
x: maybeLabel(first, x),
Expand All @@ -59,6 +61,7 @@ export function groupY(data, {
return barX(
data,
{
className: "groupY",
...options,
transform: group1(y),
x1,
Expand Down
5 changes: 3 additions & 2 deletions src/marks/line.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export class Line extends Mark {
fill: cfill,
stroke: cstroke,
strokeWidth: cstroke === "none" ? undefined : 1.5,
className: "line",
...style
});
}
Expand Down Expand Up @@ -71,9 +72,9 @@ export function line(data, {x = first, y = second, ...options} = {}) {
}

export function lineX(data, {x = identity, ...options} = {}) {
return new Line(data, {...options, x, y: indexOf});
return new Line(data, {className: "lineX", ...options, x, y: indexOf});
}

export function lineY(data, {y = identity, ...options} = {}) {
return new Line(data, {...options, x: indexOf, y});
return new Line(data, {className: "lineY", ...options, x: indexOf, y});
}
2 changes: 1 addition & 1 deletion src/marks/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class Link extends Mark {
],
transform
);
Style(this, {stroke: cstroke, ...style});
Style(this, {stroke: cstroke, className: "link", ...style});
}
render(
I,
Expand Down
6 changes: 3 additions & 3 deletions src/marks/rect.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class Rect extends Mark {
],
transform
);
Style(this, {fill: cfill, stroke: cstroke, ...style});
Style(this, {fill: cfill, stroke: cstroke, className: "rect", ...style});
this.insetTop = number(insetTop);
this.insetRight = number(insetRight);
this.insetBottom = number(insetBottom);
Expand Down Expand Up @@ -84,9 +84,9 @@ export function rect(data, options) {
}

export function rectX(data, {x, y1, y2, ...options} = {}) {
return new Rect(data, {...options, x1: zero, x2: x, y1, y2});
return new Rect(data, {className: "rectX", ...options, x1: zero, x2: x, y1, y2});
}

export function rectY(data, {x1, x2, y, ...options} = {}) {
return new Rect(data, {...options, x1, x2, y1: zero, y2: y});
return new Rect(data, {className: "rectY", ...options, x1, x2, y1: zero, y2: y});
}
4 changes: 2 additions & 2 deletions src/marks/rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class RuleX extends Mark {
],
transform
);
Style(this, {stroke: cstroke, ...style});
Style(this, {stroke: cstroke, className: "ruleX", ...style});
}
render(
I,
Expand Down Expand Up @@ -85,7 +85,7 @@ export class RuleY extends Mark {
],
transform
);
Style(this, {stroke: cstroke, ...style});
Style(this, {stroke: cstroke, className: "ruleY", ...style});
}
render(
I,
Expand Down
8 changes: 4 additions & 4 deletions src/marks/stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ import {areaX, areaY} from "./area.js";
import {barX, barY} from "./bar.js";

export function stackAreaX(data, options) {
return areaX(...stackX(data, options));
return areaX(...stackX(data, {className: "stackAreaX", ...options}));
}

export function stackAreaY(data, options) {
return areaY(...stackY(data, options));
return areaY(...stackY(data, {className: "stackAreaY", ...options}));
}

export function stackBarX(data, options) {
return barX(...stackX(data, options));
return barX(...stackX(data, {className: "stackBarX", ...options}));
}

export function stackBarY(data, options) {
return barY(...stackY(data, options));
return barY(...stackY(data, {className: "stackBarY", ...options}));
}
6 changes: 3 additions & 3 deletions src/marks/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class Text extends Mark {
],
transform
);
Style(this, {fill: cfill, ...style});
Style(this, {fill: cfill, className: "text", ...style});
this.textAnchor = string(textAnchor);
this.dx = string(dx);
this.dy = string(dy);
Expand Down Expand Up @@ -68,11 +68,11 @@ export function text(data, options) {
}

export function textX(data, {x = identity, ...options} = {}) {
return new Text(data, {...options, x, y: indexOf});
return new Text(data, {className: "textX", ...options, x, y: indexOf});
}

export function textY(data, {y = identity, ...options} = {}) {
return new Text(data, {...options, x: indexOf, y});
return new Text(data, {className: "textY", ...options, x: indexOf, y});
}

function applyTextStyles(selection, style) {
Expand Down
6 changes: 3 additions & 3 deletions src/marks/tick.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class AbstractTick extends Mark {
],
transform
);
Style(this, {stroke: cstroke, ...style});
Style(this, {stroke: cstroke, className: "tick", ...style});
}
render(I, scales, channels) {
const {color} = scales;
Expand Down Expand Up @@ -59,7 +59,7 @@ export class TickX extends AbstractTick {
{name: "x", value: x, scale: "x"},
{name: "y", value: y, scale: "y", type: "band"}
],
options
{className: "tickX", ...options}
);
}
_transform(selection, {x}) {
Expand Down Expand Up @@ -87,7 +87,7 @@ export class TickY extends AbstractTick {
{name: "x", value: x, scale: "x", type: "band"},
{name: "y", value: y, scale: "y"}
],
options
{className: "tickY", ...options}
);
}
_transform(selection, {y}) {
Expand Down
5 changes: 4 additions & 1 deletion src/plot.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ export function plot(options = {}) {
const channels = markChannels.get(mark);
const index = markIndex.get(mark);
const node = mark.render(index, scales, channels, dimensions, axes);
if (node != null) svg.append(() => node);
if (node != null) {
if (mark.className != null) node.classList.add(mark.className);
svg.append(() => node);
}
}

return svg.node();
Expand Down
9 changes: 8 additions & 1 deletion src/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ export function Style(mark, {
strokeLinecap,
strokeMiterlimit,
strokeDasharray,
mixBlendMode
mixBlendMode,
className
} = {}) {
mark.fill = impliedString(fill, "currentColor");
mark.fillOpacity = impliedNumber(fillOpacity, 1);
Expand All @@ -22,6 +23,7 @@ export function Style(mark, {
mark.strokeMiterlimit = impliedNumber(strokeMiterlimit, 1);
mark.strokeDasharray = string(strokeDasharray);
mark.mixBlendMode = impliedString(mixBlendMode, "normal");
mark.className = impliedString(className);
}

export function applyIndirectStyles(selection, mark) {
Expand All @@ -34,6 +36,7 @@ export function applyIndirectStyles(selection, mark) {
applyAttr(selection, "stroke-linecap", mark.strokeLinecap);
applyAttr(selection, "stroke-miterlimit", mark.strokeMiterlimit);
applyAttr(selection, "stroke-dasharray", mark.strokeDasharray);
applyClass(selection, mark.className);
}

export function applyDirectStyles(selection, mark) {
Expand All @@ -44,6 +47,10 @@ export function applyAttr(selection, name, value) {
if (value != null) selection.attr(name, value);
}

export function applyClass(selection, value) {
if (value != null) selection.classed(value, true);
}

export function applyStyle(selection, name, value) {
if (value != null) selection.style(name, value);
}
Expand Down
Loading