Skip to content

Commit 630f1df

Browse files
committed
className option for plot, facets, axes and marks
supersedes #126
1 parent 37326d8 commit 630f1df

File tree

6 files changed

+49
-26
lines changed

6 files changed

+49
-26
lines changed

src/axis.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ export class AxisX {
1313
grid,
1414
label,
1515
labelAnchor,
16-
labelOffset
16+
labelOffset,
17+
className
1718
} = {}) {
1819
this.name = name;
1920
this.axis = (axis + "").toLowerCase();
@@ -26,6 +27,7 @@ export class AxisX {
2627
this.label = label;
2728
this.labelAnchor = labelAnchor;
2829
this.labelOffset = labelOffset;
30+
this.className = className;
2931
}
3032
render(
3133
index,
@@ -53,12 +55,14 @@ export class AxisX {
5355
grid,
5456
label,
5557
labelAnchor,
56-
labelOffset
58+
labelOffset,
59+
className
5760
} = this;
5861
const offset = this.name === "x" ? 0 : axis === "top" ? marginTop - facetMarginTop : marginBottom - facetMarginBottom;
5962
const offsetSign = axis === "top" ? -1 : 1;
6063
const ty = offsetSign * offset + (axis === "top" ? marginTop : height - marginBottom);
6164
return create("svg:g")
65+
.attr("class", className)
6266
.attr("transform", `translate(0,${ty})`)
6367
.call((axis === "top" ? axisTop : axisBottom)(round(x))
6468
.ticks(Array.isArray(ticks) ? null : ticks, typeof tickFormat === "function" ? null : tickFormat)
@@ -100,7 +104,8 @@ export class AxisY {
100104
grid,
101105
label,
102106
labelAnchor,
103-
labelOffset
107+
labelOffset,
108+
className
104109
} = {}) {
105110
this.name = name;
106111
this.axis = axis = (axis + "").toLowerCase();
@@ -113,6 +118,7 @@ export class AxisY {
113118
this.label = label;
114119
this.labelAnchor = labelAnchor;
115120
this.labelOffset = labelOffset;
121+
this.className = className;
116122
}
117123
render(
118124
index,
@@ -138,12 +144,14 @@ export class AxisY {
138144
grid,
139145
label,
140146
labelAnchor,
141-
labelOffset
147+
labelOffset,
148+
className
142149
} = this;
143150
const offset = this.name === "y" ? 0 : axis === "left" ? marginLeft - facetMarginLeft : marginRight - facetMarginRight;
144151
const offsetSign = axis === "left" ? -1 : 1;
145152
const tx = offsetSign * offset + (axis === "right" ? width - marginRight : marginLeft);
146153
return create("svg:g")
154+
.attr("class", className)
147155
.attr("transform", `translate(${tx},0)`)
148156
.call((axis === "right" ? axisRight : axisLeft)(round(y))
149157
.ticks(Array.isArray(ticks) ? null : ticks, typeof tickFormat === "function" ? null : tickFormat)

src/facet.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export function facets(data, {x, y, ...options}, marks) {
99
}
1010

1111
class Facet extends Mark {
12-
constructor(data, {x, y, ...options} = {}, marks = []) {
12+
constructor(data, {x, y, className, ...options} = {}, marks = []) {
1313
super(
1414
data,
1515
[
@@ -23,6 +23,7 @@ class Facet extends Mark {
2323
this.marksChannels = undefined; // array of mark channels
2424
this.marksIndex = undefined; // array of mark indexes (for non-faceted marks)
2525
this.marksIndexByFacet = undefined; // map from facet key to array of mark indexes
26+
this.className = className;
2627
}
2728
initialize() {
2829
const {index, channels} = super.initialize();
@@ -68,12 +69,13 @@ class Facet extends Mark {
6869
return {index, channels: [...channels, ...subchannels]};
6970
}
7071
render(index, scales, channels, dimensions, axes) {
71-
const {marks, marksChannels, marksIndex, marksIndexByFacet} = this;
72+
const {marks, marksChannels, marksIndex, marksIndexByFacet, className} = this;
7273
const {fx, fy} = scales;
7374
const fyMargins = fy && {marginTop: 0, marginBottom: 0, height: fy.bandwidth()};
7475
const fxMargins = fx && {marginRight: 0, marginLeft: 0, width: fx.bandwidth()};
7576
const subdimensions = {...dimensions, ...fxMargins, ...fyMargins};
7677
return create("svg:g")
78+
.attr("class", className)
7779
.call(g => {
7880
if (fy && axes.y) {
7981
const domain = fy.domain();

src/plot.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {facets} from "./facet.js";
44
import {Scales, autoScaleRange} from "./scales.js";
55

66
export function plot(options = {}) {
7-
const {facet, style} = options;
7+
const {facet, style, className = "plot"} = options;
88

99
// When faceting, wrap all marks in a faceting mark.
1010
if (facet !== undefined) {
@@ -73,7 +73,7 @@ export function plot(options = {}) {
7373
const {width, height} = dimensions;
7474

7575
const svg = create("svg")
76-
.attr("class", "plot")
76+
.attr("class", className)
7777
.attr("fill", "currentColor")
7878
.attr("text-anchor", "middle")
7979
.attr("width", width)

src/style.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {string, number} from "./mark.js";
33
export const offset = typeof window !== "undefined" && window.devicePixelRatio > 1 ? 0 : 0.5;
44

55
export function Style(mark, {
6+
className,
67
fill,
78
fillOpacity,
89
stroke,
@@ -14,6 +15,7 @@ export function Style(mark, {
1415
strokeDasharray,
1516
mixBlendMode
1617
} = {}) {
18+
mark.className = string(className);
1719
mark.fill = impliedString(fill, "currentColor");
1820
mark.fillOpacity = impliedNumber(fillOpacity, 1);
1921
mark.stroke = impliedString(stroke, "none");
@@ -27,6 +29,7 @@ export function Style(mark, {
2729
}
2830

2931
export function applyIndirectStyles(selection, mark) {
32+
applyAttr(selection, "class", mark.className);
3033
applyAttr(selection, "fill", mark.fill);
3134
applyAttr(selection, "fill-opacity", mark.fillOpacity);
3235
applyAttr(selection, "stroke", mark.stroke);

test/output/penguinSexMassCulmenSpecies.svg

Lines changed: 13 additions & 13 deletions
Loading

test/plots/penguin-sex-mass-culmen-species.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,35 @@ export default async function() {
77
inset: 10,
88
height: 320,
99
grid: true,
10+
className: "plot classtest",
1011
x: {
1112
ticks: 10,
12-
tickFormat: "~s"
13+
tickFormat: "~s",
14+
className: "axis-x"
1315
},
1416
y: {
15-
ticks: 10
17+
ticks: 10,
18+
className: "axis-y"
1619
},
1720
facet: {
1821
data,
19-
x: "sex"
22+
x: "sex",
23+
className: "facet"
24+
},
25+
fx: {
26+
className: "axis-fx"
2027
},
2128
marks: [
22-
Plot.frame(),
29+
Plot.frame({
30+
className: "frame"
31+
}),
2332
Plot.dot(data, Plot.binR({
2433
x: "body_mass_g",
2534
y: "culmen_length_mm",
2635
stroke: "species",
2736
fill: "species",
28-
fillOpacity: 0.1
37+
fillOpacity: 0.1,
38+
className: "bin"
2939
}))
3040
]
3141
});

0 commit comments

Comments
 (0)