Skip to content

Commit 9904cd5

Browse files
committed
rebased
1 parent 0c291ed commit 9904cd5

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
@@ -15,7 +15,8 @@ export class AxisX {
1515
labelAnchor,
1616
labelOffset,
1717
line,
18-
tickRotate
18+
tickRotate,
19+
className
1920
} = {}) {
2021
this.name = name;
2122
this.axis = keyword(axis, "axis", ["top", "bottom"]);
@@ -29,6 +30,7 @@ export class AxisX {
2930
this.labelOffset = number(labelOffset);
3031
this.line = boolean(line);
3132
this.tickRotate = number(tickRotate);
33+
this.className = className;
3234
}
3335
render(
3436
index,
@@ -54,12 +56,14 @@ export class AxisX {
5456
labelAnchor,
5557
labelOffset,
5658
line,
57-
tickRotate
59+
tickRotate,
60+
className
5861
} = this;
5962
const offset = this.name === "x" ? 0 : axis === "top" ? marginTop - facetMarginTop : marginBottom - facetMarginBottom;
6063
const offsetSign = axis === "top" ? -1 : 1;
6164
const ty = offsetSign * offset + (axis === "top" ? marginTop : height - marginBottom);
6265
return create("svg:g")
66+
.attr("class", className)
6367
.attr("transform", `translate(0,${ty})`)
6468
.call(createAxis(axis === "top" ? axisTop : axisBottom, x, this))
6569
.call(maybeTickRotate, tickRotate)
@@ -98,7 +102,8 @@ export class AxisY {
98102
labelAnchor,
99103
labelOffset,
100104
line,
101-
tickRotate
105+
tickRotate,
106+
className
102107
} = {}) {
103108
this.name = name;
104109
this.axis = keyword(axis, "axis", ["left", "right"]);
@@ -112,6 +117,7 @@ export class AxisY {
112117
this.labelOffset = number(labelOffset);
113118
this.line = boolean(line);
114119
this.tickRotate = number(tickRotate);
120+
this.className = className;
115121
}
116122
render(
117123
index,
@@ -135,12 +141,14 @@ export class AxisY {
135141
labelAnchor,
136142
labelOffset,
137143
line,
138-
tickRotate
144+
tickRotate,
145+
className
139146
} = this;
140147
const offset = this.name === "y" ? 0 : axis === "left" ? marginLeft - facetMarginLeft : marginRight - facetMarginRight;
141148
const offsetSign = axis === "left" ? -1 : 1;
142149
const tx = offsetSign * offset + (axis === "right" ? width - marginRight : marginLeft);
143150
return create("svg:g")
151+
.attr("class", className)
144152
.attr("transform", `translate(${tx},0)`)
145153
.call(createAxis(axis === "right" ? axisRight : axisLeft, y, this))
146154
.call(maybeTickRotate, tickRotate)

src/facet.js

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

1313
class Facet extends Mark {
14-
constructor(data, {x, y, ...options} = {}, marks = []) {
14+
constructor(data, {x, y, className, ...options} = {}, marks = []) {
1515
if (data == null) throw new Error("missing facet data");
1616
super(
1717
data,
@@ -25,6 +25,7 @@ class Facet extends Mark {
2525
// The following fields are set by initialize:
2626
this.marksChannels = undefined; // array of mark channels
2727
this.marksIndexByFacet = undefined; // map from facet key to array of mark indexes
28+
this.className = className;
2829
}
2930
initialize() {
3031
const {index, channels} = super.initialize();
@@ -69,7 +70,7 @@ class Facet extends Mark {
6970
return {index, channels: [...channels, ...subchannels]};
7071
}
7172
render(I, scales, channels, dimensions, axes) {
72-
const {marks, marksChannels, marksIndexByFacet} = this;
73+
const {marks, marksChannels, marksIndexByFacet, className} = this;
7374
const {fx, fy} = scales;
7475
const fyDomain = fy && fy.domain();
7576
const fxDomain = fx && fx.domain();
@@ -78,6 +79,7 @@ class Facet extends Mark {
7879
const subdimensions = {...dimensions, ...fxMargins, ...fyMargins};
7980
const marksValues = marksChannels.map(channels => applyScales(channels, scales));
8081
return create("svg:g")
82+
.attr("class", className)
8183
.call(g => {
8284
if (fy && axes.y) {
8385
const axis1 = axes.y, axis2 = nolabel(axis1);

src/plot.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {Scales, autoScaleRange, applyScales} from "./scales.js";
66
import {filterStyles, 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
@@ -6,6 +6,7 @@ export const offset = typeof window !== "undefined" && window.devicePixelRatio >
66
export function styles(
77
mark,
88
{
9+
className,
910
title,
1011
fill,
1112
fillOpacity,
@@ -66,6 +67,7 @@ export function styles(
6667
mark.fillOpacity = impliedNumber(cfillOpacity, 1);
6768
}
6869

70+
mark.className = string(className);
6971
mark.stroke = impliedString(cstroke, "none");
7072
mark.strokeWidth = impliedNumber(cstrokeWidth, 1);
7173
mark.strokeOpacity = impliedNumber(cstrokeOpacity, 1);
@@ -106,6 +108,7 @@ export function applyGroupedChannelStyles(selection, {title: L, fill: F, fillOpa
106108
}
107109

108110
export function applyIndirectStyles(selection, mark) {
111+
applyAttr(selection, "class", mark.className);
109112
applyAttr(selection, "fill", mark.fill);
110113
applyAttr(selection, "fill-opacity", mark.fillOpacity);
111114
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(),
@@ -29,7 +36,8 @@ export default async function() {
2936
y: "culmen_length_mm",
3037
stroke: "species",
3138
fill: "species",
32-
fillOpacity: 0.2
39+
fillOpacity: 0.2,
40+
className: "bin"
3341
}))
3442
]
3543
});

0 commit comments

Comments
 (0)