Skip to content

Added an optional css class to the x/y axes #976

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
19 changes: 19 additions & 0 deletions src/axis.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export class AxisX {
label,
labelAnchor,
labelOffset,
axisClass,
line,
tickRotate,
ariaLabel,
Expand All @@ -34,6 +35,7 @@ export class AxisX {
this.label = string(label);
this.labelAnchor = maybeKeyword(labelAnchor, "labelAnchor", ["center", "left", "right"]);
this.labelOffset = number(labelOffset);
this.axisClass = boolean(axisClass);
this.line = boolean(line);
this.tickRotate = number(tickRotate);
this.ariaLabel = string(ariaLabel);
Expand Down Expand Up @@ -64,6 +66,7 @@ export class AxisX {
label,
labelAnchor,
labelOffset,
axisClass,
line,
name,
tickRotate
Expand All @@ -72,6 +75,7 @@ export class AxisX {
const offsetSign = axis === "top" ? -1 : 1;
const ty = offsetSign * offset + (axis === "top" ? marginTop : height - marginBottom);
return create("svg:g", context)
.call(applyCssClass, this, 'axis')
.call(applyAria, this)
.attr("transform", `translate(${offsetLeft},${ty})`)
.call(createAxis(axis === "top" ? axisTop : axisBottom, x, this))
Expand All @@ -84,6 +88,7 @@ export class AxisX {
: fy ? gridFacetX(index, fy, -ty)
: gridX(offsetSign * (marginBottom + marginTop - height)))
.call(!label ? () => {} : g => g.append("text")
.call(applyCssClass, this, 'axis-label')
.attr("fill", "currentColor")
.attr("transform", `translate(${
labelAnchor === "center" ? (width + marginLeft - marginRight) / 2
Expand Down Expand Up @@ -112,6 +117,7 @@ export class AxisY {
label,
labelAnchor,
labelOffset,
axisClass,
line,
tickRotate,
ariaLabel,
Expand All @@ -128,6 +134,7 @@ export class AxisY {
this.label = string(label);
this.labelAnchor = maybeKeyword(labelAnchor, "labelAnchor", ["center", "top", "bottom"]);
this.labelOffset = number(labelOffset);
this.axisClass = boolean(axisClass);
this.line = boolean(line);
this.tickRotate = number(tickRotate);
this.ariaLabel = string(ariaLabel);
Expand Down Expand Up @@ -156,6 +163,7 @@ export class AxisY {
label,
labelAnchor,
labelOffset,
axisClass,
line,
name,
tickRotate
Expand All @@ -164,6 +172,7 @@ export class AxisY {
const offsetSign = axis === "left" ? -1 : 1;
const tx = offsetSign * offset + (axis === "right" ? width - marginRight : marginLeft);
return create("svg:g", context)
.call(applyCssClass, this, "axis")
.call(applyAria, this)
.attr("transform", `translate(${tx},${offsetTop})`)
.call(createAxis(axis === "right" ? axisRight : axisLeft, y, this))
Expand All @@ -176,6 +185,7 @@ export class AxisY {
: fx ? gridFacetY(index, fx, -tx)
: gridY(offsetSign * (marginLeft + marginRight - width)))
.call(!label ? () => {} : g => g.append("text")
.call(applyCssClass, this, 'axis-label')
.attr("fill", "currentColor")
.attr("font-variant", fontVariant == null ? null : "normal")
.attr("transform", `translate(${labelOffset * offsetSign},${
Expand Down Expand Up @@ -204,6 +214,15 @@ function applyAria(selection, {
applyAttr(selection, "aria-description", ariaDescription);
}

function applyCssClass(selection, {
name,
axisClass,
}, cssClass) {
if(axisClass === true) {
applyAttr(selection, "class", `${name}-${cssClass}`);
}
}

function gridX(y2) {
return g => g.selectAll(".tick line")
.clone(true)
Expand Down
6 changes: 5 additions & 1 deletion test/plots/aapl-bollinger.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import * as d3 from "d3";
export default async function() {
const AAPL = await d3.csv("data/aapl.csv", d3.autoType);
return Plot.plot({
x: {
axisClass: true
},
y: {
grid: true
grid: true,
axisClass: true
},
marks: [
Plot.areaY(AAPL, Plot.map({y1: bollinger(20, -2), y2: bollinger(20, 2)}, {x: "Date", y: "Close", fillOpacity: 0.2})),
Expand Down