Skip to content

Commit 9bcc165

Browse files
committed
rebased
1 parent 8aafa7c commit 9bcc165

File tree

6 files changed

+43
-22
lines changed

6 files changed

+43
-22
lines changed

src/axis.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ export class AxisX {
1414
label,
1515
labelAnchor,
1616
labelOffset,
17-
tickRotate
17+
tickRotate,
18+
className
1819
} = {}) {
1920
this.name = name;
2021
this.axis = keyword(axis, "axis", ["top", "bottom"]);
@@ -27,6 +28,7 @@ export class AxisX {
2728
this.labelAnchor = maybeKeyword(labelAnchor, "labelAnchor", ["center", "left", "right"]);
2829
this.labelOffset = number(labelOffset);
2930
this.tickRotate = number(tickRotate);
31+
this.className = className;
3032
}
3133
render(
3234
index,
@@ -51,12 +53,14 @@ export class AxisX {
5153
label,
5254
labelAnchor,
5355
labelOffset,
54-
tickRotate
56+
tickRotate,
57+
className
5558
} = this;
5659
const offset = this.name === "x" ? 0 : axis === "top" ? marginTop - facetMarginTop : marginBottom - facetMarginBottom;
5760
const offsetSign = axis === "top" ? -1 : 1;
5861
const ty = offsetSign * offset + (axis === "top" ? marginTop : height - marginBottom);
5962
return create("svg:g")
63+
.attr("class", className)
6064
.attr("transform", `translate(0,${ty})`)
6165
.call(createAxis(axis === "top" ? axisTop : axisBottom, x, this))
6266
.call(maybeTickRotate, tickRotate)
@@ -94,7 +98,8 @@ export class AxisY {
9498
label,
9599
labelAnchor,
96100
labelOffset,
97-
tickRotate
101+
tickRotate,
102+
className
98103
} = {}) {
99104
this.name = name;
100105
this.axis = keyword(axis, "axis", ["left", "right"]);
@@ -107,6 +112,7 @@ export class AxisY {
107112
this.labelAnchor = maybeKeyword(labelAnchor, "labelAnchor", ["center", "top", "bottom"]);
108113
this.labelOffset = number(labelOffset);
109114
this.tickRotate = number(tickRotate);
115+
this.className = className;
110116
}
111117
render(
112118
index,
@@ -129,12 +135,14 @@ export class AxisY {
129135
label,
130136
labelAnchor,
131137
labelOffset,
132-
tickRotate
138+
tickRotate,
139+
className
133140
} = this;
134141
const offset = this.name === "y" ? 0 : axis === "left" ? marginLeft - facetMarginLeft : marginRight - facetMarginRight;
135142
const offsetSign = axis === "left" ? -1 : 1;
136143
const tx = offsetSign * offset + (axis === "right" ? width - marginRight : marginLeft);
137144
return create("svg:g")
145+
.attr("class", className)
138146
.attr("transform", `translate(${tx},0)`)
139147
.call(createAxis(axis === "right" ? axisRight : axisLeft, y, this))
140148
.call(maybeTickRotate, tickRotate)

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();
@@ -70,13 +71,14 @@ class Facet extends Mark {
7071
return {index, channels: [...channels, ...subchannels]};
7172
}
7273
render(index, scales, channels, dimensions, axes) {
73-
const {marks, marksChannels, marksIndex, marksIndexByFacet} = this;
74+
const {marks, marksChannels, marksIndex, marksIndexByFacet, className} = this;
7475
const {fx, fy} = scales;
7576
const fyMargins = fy && {marginTop: 0, marginBottom: 0, height: fy.bandwidth()};
7677
const fxMargins = fx && {marginRight: 0, marginLeft: 0, width: fx.bandwidth()};
7778
const subdimensions = {...dimensions, ...fxMargins, ...fyMargins};
7879
const marksValues = marksChannels.map(channels => values(channels, scales));
7980
return create("svg:g")
81+
.attr("class", className)
8082
.call(g => {
8183
if (fy && axes.y) {
8284
const domain = fy.domain();

src/plot.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {Scales, autoScaleRange} from "./scales.js";
66
import {offset} from "./style.js";
77

88
export function plot(options = {}) {
9-
const {facet, style, caption} = options;
9+
const {facet, style, caption, className = "plot"} = options;
1010

1111
// When faceting, wrap all marks in a faceting mark.
1212
if (facet !== undefined) {
@@ -69,7 +69,7 @@ export function plot(options = {}) {
6969
const {width, height} = dimensions;
7070

7171
const svg = create("svg")
72-
.attr("class", "plot")
72+
.attr("class", className)
7373
.attr("fill", "currentColor")
7474
.attr("text-anchor", "middle")
7575
.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: 10 additions & 10 deletions
Loading

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,23 @@ 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: [
2229
Plot.frame(),
@@ -25,7 +32,8 @@ export default async function() {
2532
y: "culmen_length_mm",
2633
stroke: "species",
2734
fill: "species",
28-
fillOpacity: 0.1
35+
fillOpacity: 0.1,
36+
className: "bin"
2937
}))
3038
]
3139
});

0 commit comments

Comments
 (0)