Skip to content

font-variant on the axes #606

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

Merged
merged 5 commits into from
Dec 10, 2021
Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ Plot automatically generates axes for position scales. You can configure these a
* *scale*.**label** - a string to label the axis
* *scale*.**labelAnchor** - the label anchor: *top*, *right*, *bottom*, *left*, or *center*
* *scale*.**labelOffset** - the label position offset (in pixels; default 0, typically for facet axes)
* *scale*.**fontVariant** - the font-variant attribute for axis ticks; defaults to tabular-nums for quantitative axes.

Top-level options are also supported as shorthand: **grid** (for *x* and *y* only; see [facet.grid](#facet-options)), **label**, **axis**, **inset**, **round**, **align**, and **padding**.

Expand Down
8 changes: 6 additions & 2 deletions src/axes.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export function Axes(
if (!fxScale) fxAxis = null; else if (fxAxis === true) fxAxis = xAxis === "bottom" ? "top" : "bottom";
if (!fyScale) fyAxis = null; else if (fyAxis === true) fyAxis = yAxis === "left" ? "right" : "left";
return {
...xAxis && {x: new AxisX({grid, line, label, ...x, axis: xAxis})},
...yAxis && {y: new AxisY({grid, line, label, ...y, axis: yAxis})},
...xAxis && {x: new AxisX({grid, line, label, fontVariant: inferFontVariant(xScale), ...x, axis: xAxis})},
...yAxis && {y: new AxisY({grid, line, label, fontVariant: inferFontVariant(yScale), ...y, axis: yAxis})},
...fxAxis && {fx: new AxisX({name: "fx", grid: facetGrid, label: facetLabel, ...fx, axis: fxAxis})},
...fyAxis && {fy: new AxisY({name: "fy", grid: facetGrid, label: facetLabel, ...fy, axis: fyAxis})}
};
Expand Down Expand Up @@ -142,3 +142,7 @@ function inferLabel(channels = [], scale, axis, key) {
}
return candidate;
}

function inferFontVariant(scale) {
return isOrdinalScale(scale) ? undefined : "tabular-nums";
}
9 changes: 9 additions & 0 deletions src/axis.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {axisTop, axisBottom, axisRight, axisLeft, create, format, utcFormat} from "d3";
import {formatIsoDate} from "./format.js";
import {boolean, take, number, string, keyword, maybeKeyword, constant, isTemporal} from "./mark.js";
import {impliedString} from "./style.js";

export class AxisX {
constructor({
Expand All @@ -10,6 +11,7 @@ export class AxisX {
tickSize = name === "fx" ? 0 : 6,
tickPadding = tickSize === 0 ? 9 : 3,
tickFormat,
fontVariant,
grid,
label,
labelAnchor,
Expand All @@ -23,6 +25,7 @@ export class AxisX {
this.tickSize = number(tickSize);
this.tickPadding = number(tickPadding);
this.tickFormat = tickFormat;
this.fontVariant = impliedString(fontVariant, "normal");
this.grid = boolean(grid);
this.label = string(label);
this.labelAnchor = maybeKeyword(labelAnchor, "labelAnchor", ["center", "left", "right"]);
Expand All @@ -49,6 +52,7 @@ export class AxisX {
) {
const {
axis,
fontVariant,
grid,
label,
labelAnchor,
Expand All @@ -65,6 +69,7 @@ export class AxisX {
.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)
Expand Down Expand Up @@ -93,6 +98,7 @@ export class AxisY {
tickSize = name === "fy" ? 0 : 6,
tickPadding = tickSize === 0 ? 9 : 3,
tickFormat,
fontVariant,
grid,
label,
labelAnchor,
Expand All @@ -106,6 +112,7 @@ export class AxisY {
this.tickSize = number(tickSize);
this.tickPadding = number(tickPadding);
this.tickFormat = tickFormat;
this.fontVariant = impliedString(fontVariant, "normal");
this.grid = boolean(grid);
this.label = string(label);
this.labelAnchor = maybeKeyword(labelAnchor, "labelAnchor", ["center", "top", "bottom"]);
Expand All @@ -130,6 +137,7 @@ export class AxisY {
) {
const {
axis,
fontVariant,
grid,
label,
labelAnchor,
Expand All @@ -146,6 +154,7 @@ export class AxisY {
.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)
Expand Down
1 change: 0 additions & 1 deletion src/legends/ramp.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export function legendRamp(color, {
.attr("class", className)
.attr("font-family", "system-ui, sans-serif")
.attr("font-size", 10)
.attr("font-variant", "tabular-nums")
.attr("width", width)
.attr("height", height)
.attr("viewBox", `0 0 ${width} ${height}`)
Expand Down
1 change: 0 additions & 1 deletion src/legends/swatches.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ export function legendSwatches(color, {
.${className} {
font-family: system-ui, sans-serif;
font-size: 10px;
font-variant: tabular-nums;
margin-bottom: 0.5em;${marginLeft === undefined ? "" : `
margin-left: ${+marginLeft}px;`}${width === undefined ? "" : `
width: ${width}px;`}
Expand Down
1 change: 0 additions & 1 deletion src/plot.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ export function plot(options = {}) {
.attr("fill", "currentColor")
.attr("font-family", "system-ui, sans-serif")
.attr("font-size", 10)
.attr("font-variant", "tabular-nums")
.attr("text-anchor", "middle")
.attr("width", width)
.attr("height", height)
Expand Down
6 changes: 3 additions & 3 deletions test/output/aaplBollinger.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions test/output/aaplCandlestick.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions test/output/aaplChangeVolume.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions test/output/aaplClose.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions test/output/aaplCloseUntyped.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions test/output/aaplMonthly.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions test/output/aaplVolume.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions test/output/aaplVolumeRect.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading