Skip to content

accept axis: "both" for x and y axes #631

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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ Align defaults to 0.5 (centered). Band scale padding defaults to 0.1 (10% of ava

Plot automatically generates axes for position scales. You can configure these axes with the following options:

* *scale*.**axis** - the orientation: *top* or *bottom* for *x*; *left* or *right* for *y*; null to suppress
* *scale*.**axis** - the orientation: *top*, *bottom* for *fx*, *top*, *bottom*, or *both* for *x*; *left*, *right* for *y*, *left*, *right*, or *both* for *y*; null to suppress
* *scale*.**ticks** - the approximate number of ticks to generate
* *scale*.**tickSize** - the size of each tick (in pixels; default 6)
* *scale*.**tickPadding** - the separation between the tick and its label (in pixels; default 3)
Expand Down
16 changes: 2 additions & 14 deletions src/axes.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,8 @@ export function autoScaleLabels(channels, scales, {x, y, fx, fy}, dimensions, op
fy.labelOffset = fy.axis === "left" ? facetMarginLeft : facetMarginRight;
}
}
if (x) {
autoAxisLabelsX(x, scales.x, channels.get("x"));
if (x.labelOffset === undefined) {
const {marginTop, marginBottom, facetMarginTop, facetMarginBottom} = dimensions;
x.labelOffset = x.axis === "top" ? marginTop - facetMarginTop : marginBottom - facetMarginBottom;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By moving this to src/axis.js, we effectively lose control over this option for the both axis.

}
}
if (y) {
autoAxisLabelsY(y, x, scales.y, channels.get("y"));
if (y.labelOffset === undefined) {
const {marginRight, marginLeft, facetMarginLeft, facetMarginRight} = dimensions;
y.labelOffset = y.axis === "left" ? marginLeft - facetMarginLeft : marginRight - facetMarginRight;
}
}
if (x) autoAxisLabelsX(x, scales.x, channels.get("x"));
if (y) autoAxisLabelsY(y, x, scales.y, channels.get("y"));
for (const [key, type] of registry) {
if (type !== position && scales[key]) { // not already handled above
autoScaleLabel(key, scales[key], channels.get(key), options[key]);
Expand Down
142 changes: 84 additions & 58 deletions src/axis.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class AxisX {
tickRotate
} = {}) {
this.name = name;
this.axis = keyword(axis, "axis", ["top", "bottom"]);
this.axis = keyword(axis, "axis", name === "x" ? ["top", "bottom", "both"] : ["top", "bottom"]);
this.ticks = ticks;
this.tickSize = number(tickSize);
this.tickPadding = number(tickPadding);
Expand Down Expand Up @@ -58,35 +58,48 @@ export class AxisX {
labelAnchor,
labelOffset,
line,
name,
tickRotate
} = this;
const offset = this.name === "x" ? 0 : axis === "top" ? marginTop - facetMarginTop : marginBottom - facetMarginBottom;
const offsetSign = axis === "top" ? -1 : 1;
const ty = offsetSign * offset + (axis === "top" ? marginTop : height - marginBottom);
return create("svg:g")
.attr("transform", `translate(0,${ty})`)
.call(createAxis(axis === "top" ? axisTop : axisBottom, x, this))
.call(maybeTickRotate, tickRotate)
.attr("font-size", null)
.attr("font-family", null)
.attr("font-variant", fontVariant)
.call(!line ? g => g.select(".domain").remove() : () => {})
.call(!grid ? () => {}
: fy ? gridFacetX(index, fy, -ty)
: gridX(offsetSign * (marginBottom + marginTop - height)))
.call(!label ? () => {} : g => g.append("text")
.attr("fill", "currentColor")
.attr("transform", `translate(${
labelAnchor === "center" ? (width + marginLeft - marginRight) / 2
: labelAnchor === "right" ? width + labelMarginRight
: -labelMarginLeft
},${labelOffset * offsetSign})`)
.attr("dy", axis === "top" ? "1em" : "-0.32em")
.attr("text-anchor", labelAnchor === "center" ? "middle"
: labelAnchor === "right" ? "end"
: "start")
.text(label))
.node();
const axes = Array.from(axis === "both" ? ["bottom", "top"] : [axis], (axis, secondary) => {
const offset = name === "x" ? 0 : axis === "top" ? marginTop - facetMarginTop : marginBottom - facetMarginBottom;
const offsetSign = axis === "top" ? -1 : 1;
const ty = offsetSign * offset + (axis === "top" ? marginTop : height - marginBottom);
return create("svg:g")
.attr("transform", `translate(0,${ty})`)
.call(createAxis(axis === "top" ? axisTop : axisBottom, x, this))
.call(maybeTickRotate, tickRotate)
.attr("font-size", null)
.attr("font-family", null)
.attr("font-variant", fontVariant)
.call(!line ? g => g.select(".domain").remove() : () => {})
.call(!grid || secondary ? () => {}
: fy ? gridFacetX(index, fy, -ty)
: gridX(offsetSign * (marginBottom + marginTop - height)))
.call(!label || secondary ? () => {} : g => g.append("text")
.attr("fill", "currentColor")
.attr("transform", `translate(${
labelAnchor === "center" ? (width + marginLeft - marginRight) / 2
: labelAnchor === "right" ? width + labelMarginRight
: -labelMarginLeft
},${
offsetSign * (labelOffset !== undefined
? labelOffset
: axis === "top" ? marginTop - facetMarginTop : marginBottom - facetMarginBottom
)})`)
.attr("dy", axis === "top" ? "1em" : "-0.32em")
.attr("text-anchor", labelAnchor === "center" ? "middle"
: labelAnchor === "right" ? "end"
: "start")
.text(label))
.node();
});
return axis === "both"
? create("svg:g")
.call(g => g.append(() => axes[0]))
.call(g => g.append(() => axes[1]))
.node()
Comment on lines +98 to +101
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using D3 here feels like overkill.

: axes[0];
}
}

Expand All @@ -107,7 +120,7 @@ export class AxisY {
tickRotate
} = {}) {
this.name = name;
this.axis = keyword(axis, "axis", ["left", "right"]);
this.axis = keyword(axis, "axis", name === "y" ? ["left", "right", "both"] : ["left", "right"]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about both for facet axes?

this.ticks = ticks;
this.tickSize = number(tickSize);
this.tickPadding = number(tickPadding);
Expand Down Expand Up @@ -143,37 +156,50 @@ export class AxisY {
labelAnchor,
labelOffset,
line,
name,
tickRotate
} = this;
const offset = this.name === "y" ? 0 : axis === "left" ? marginLeft - facetMarginLeft : marginRight - facetMarginRight;
const offsetSign = axis === "left" ? -1 : 1;
const tx = offsetSign * offset + (axis === "right" ? width - marginRight : marginLeft);
return create("svg:g")
.attr("transform", `translate(${tx},0)`)
.call(createAxis(axis === "right" ? axisRight : axisLeft, y, this))
.call(maybeTickRotate, tickRotate)
.attr("font-size", null)
.attr("font-family", null)
.attr("font-variant", fontVariant)
.call(!line ? g => g.select(".domain").remove() : () => {})
.call(!grid ? () => {}
: fx ? gridFacetY(index, fx, -tx)
: gridY(offsetSign * (marginLeft + marginRight - width)))
.call(!label ? () => {} : g => g.append("text")
.attr("fill", "currentColor")
.attr("transform", `translate(${labelOffset * offsetSign},${
labelAnchor === "center" ? (height + marginTop - marginBottom) / 2
: labelAnchor === "bottom" ? height - marginBottom
: marginTop
})${labelAnchor === "center" ? ` rotate(-90)` : ""}`)
.attr("dy", labelAnchor === "center" ? (axis === "right" ? "-0.32em" : "0.75em")
: labelAnchor === "bottom" ? "1.4em"
: "-1em")
.attr("text-anchor", labelAnchor === "center" ? "middle"
: axis === "right" ? "end"
: "start")
.text(label))
.node();
const axes = Array.from(axis === "both" ? ["right", "left"] : [axis], (axis, secondary) => {
const offset = name === "y" ? 0 : axis === "left" ? marginLeft - facetMarginLeft : marginRight - facetMarginRight;
const offsetSign = axis === "left" ? -1 : 1;
const tx = offsetSign * offset + (axis === "right" ? width - marginRight : marginLeft);
return create("svg:g")
.attr("transform", `translate(${tx},0)`)
.call(createAxis(axis === "right" ? axisRight : axisLeft, y, this))
.call(maybeTickRotate, tickRotate)
.attr("font-size", null)
.attr("font-family", null)
.attr("font-variant", fontVariant)
.call(!line ? g => g.select(".domain").remove() : () => {})
.call(!grid || secondary ? () => {}
: fx ? gridFacetY(index, fx, -tx)
: gridY(offsetSign * (marginLeft + marginRight - width)))
.call(!label || secondary ? () => {} : g => g.append("text")
.attr("fill", "currentColor")
.attr("transform", `translate(${
offsetSign * (labelOffset !== undefined
? labelOffset
: axis === "left" ? marginLeft - facetMarginLeft : marginRight - facetMarginRight
)},${
labelAnchor === "center" ? (height + marginTop - marginBottom) / 2
: labelAnchor === "bottom" ? height - marginBottom
: marginTop
})${labelAnchor === "center" ? ` rotate(-90)` : ""}`)
.attr("dy", labelAnchor === "center" ? (axis === "right" ? "-0.32em" : "0.75em")
: labelAnchor === "bottom" ? "1.4em"
: "-1em")
.attr("text-anchor", labelAnchor === "center" ? "middle"
: axis === "right" ? "end"
: "start")
.text(label))
.node();
});
return axis === "both"
? create("svg:g")
.call(g => g.append(() => axes[0]))
.call(g => g.append(() => axes[1]))
.node()
: axes[0];
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/plot.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,10 @@ function Dimensions(
marginLeft: facetMarginLeft = facetMargin !== undefined ? facetMargin : fyAxis === "left" ? 40 : 0
} = {},
margin,
marginTop = margin !== undefined ? margin : Math.max((xAxis === "top" ? 30 : 0) + facetMarginTop, yAxis || fyAxis ? 20 : 0.5 - offset),
marginRight = margin !== undefined ? margin : Math.max((yAxis === "right" ? 40 : 0) + facetMarginRight, xAxis || fxAxis ? 20 : 0.5 + offset),
marginBottom = margin !== undefined ? margin : Math.max((xAxis === "bottom" ? 30 : 0) + facetMarginBottom, yAxis || fyAxis ? 20 : 0.5 + offset),
marginLeft = margin !== undefined ? margin : Math.max((yAxis === "left" ? 40 : 0) + facetMarginLeft, xAxis || fxAxis ? 20 : 0.5 - offset)
marginTop = margin !== undefined ? margin : Math.max((xAxis === "top" || xAxis === "both" ? 30 : 0) + facetMarginTop, yAxis || fyAxis ? 20 : 0.5 - offset),
marginRight = margin !== undefined ? margin : Math.max((yAxis === "right" || yAxis === "both" ? 40 : 0) + facetMarginRight, xAxis || fxAxis ? 20 : 0.5 + offset),
marginBottom = margin !== undefined ? margin : Math.max((xAxis === "bottom" || xAxis === "both" ? 30 : 0) + facetMarginBottom, yAxis || fyAxis ? 20 : 0.5 + offset),
marginLeft = margin !== undefined ? margin : Math.max((yAxis === "left" || yAxis === "both" ? 40 : 0) + facetMarginLeft, xAxis || fxAxis ? 20 : 0.5 - offset)
} = {}
) {
return {
Expand Down
Loading