Skip to content

Commit d518c87

Browse files
Filmbostock
andauthored
font-variant on the axes (#606)
* remove font-variant from the global styles; introduce the axis fontVariant option, which defaults to tabular-nums closes #601 * Update src/axis.js Co-authored-by: Mike Bostock <mbostock@gmail.com> * Update src/axis.js Co-authored-by: Mike Bostock <mbostock@gmail.com> * fontVariant defaults to tabular-nums on quantitative axes only * inferFontVariant returns string | undefined Co-authored-by: Mike Bostock <mbostock@gmail.com>
1 parent f542d8f commit d518c87

File tree

179 files changed

+387
-384
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

179 files changed

+387
-384
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ Plot automatically generates axes for position scales. You can configure these a
273273
* *scale*.**label** - a string to label the axis
274274
* *scale*.**labelAnchor** - the label anchor: *top*, *right*, *bottom*, *left*, or *center*
275275
* *scale*.**labelOffset** - the label position offset (in pixels; default 0, typically for facet axes)
276+
* *scale*.**fontVariant** - the font-variant attribute for axis ticks; defaults to tabular-nums for quantitative axes.
276277

277278
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**.
278279

src/axes.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ export function Axes(
1616
if (!fxScale) fxAxis = null; else if (fxAxis === true) fxAxis = xAxis === "bottom" ? "top" : "bottom";
1717
if (!fyScale) fyAxis = null; else if (fyAxis === true) fyAxis = yAxis === "left" ? "right" : "left";
1818
return {
19-
...xAxis && {x: new AxisX({grid, line, label, ...x, axis: xAxis})},
20-
...yAxis && {y: new AxisY({grid, line, label, ...y, axis: yAxis})},
19+
...xAxis && {x: new AxisX({grid, line, label, fontVariant: inferFontVariant(xScale), ...x, axis: xAxis})},
20+
...yAxis && {y: new AxisY({grid, line, label, fontVariant: inferFontVariant(yScale), ...y, axis: yAxis})},
2121
...fxAxis && {fx: new AxisX({name: "fx", grid: facetGrid, label: facetLabel, ...fx, axis: fxAxis})},
2222
...fyAxis && {fy: new AxisY({name: "fy", grid: facetGrid, label: facetLabel, ...fy, axis: fyAxis})}
2323
};
@@ -142,3 +142,7 @@ function inferLabel(channels = [], scale, axis, key) {
142142
}
143143
return candidate;
144144
}
145+
146+
function inferFontVariant(scale) {
147+
return isOrdinalScale(scale) ? undefined : "tabular-nums";
148+
}

src/axis.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {axisTop, axisBottom, axisRight, axisLeft, create, format, utcFormat} from "d3";
22
import {formatIsoDate} from "./format.js";
33
import {boolean, take, number, string, keyword, maybeKeyword, constant, isTemporal} from "./mark.js";
4+
import {impliedString} from "./style.js";
45

56
export class AxisX {
67
constructor({
@@ -10,6 +11,7 @@ export class AxisX {
1011
tickSize = name === "fx" ? 0 : 6,
1112
tickPadding = tickSize === 0 ? 9 : 3,
1213
tickFormat,
14+
fontVariant,
1315
grid,
1416
label,
1517
labelAnchor,
@@ -23,6 +25,7 @@ export class AxisX {
2325
this.tickSize = number(tickSize);
2426
this.tickPadding = number(tickPadding);
2527
this.tickFormat = tickFormat;
28+
this.fontVariant = impliedString(fontVariant, "normal");
2629
this.grid = boolean(grid);
2730
this.label = string(label);
2831
this.labelAnchor = maybeKeyword(labelAnchor, "labelAnchor", ["center", "left", "right"]);
@@ -49,6 +52,7 @@ export class AxisX {
4952
) {
5053
const {
5154
axis,
55+
fontVariant,
5256
grid,
5357
label,
5458
labelAnchor,
@@ -65,6 +69,7 @@ export class AxisX {
6569
.call(maybeTickRotate, tickRotate)
6670
.attr("font-size", null)
6771
.attr("font-family", null)
72+
.attr("font-variant", fontVariant)
6873
.call(!line ? g => g.select(".domain").remove() : () => {})
6974
.call(!grid ? () => {}
7075
: fy ? gridFacetX(index, fy, -ty)
@@ -93,6 +98,7 @@ export class AxisY {
9398
tickSize = name === "fy" ? 0 : 6,
9499
tickPadding = tickSize === 0 ? 9 : 3,
95100
tickFormat,
101+
fontVariant,
96102
grid,
97103
label,
98104
labelAnchor,
@@ -106,6 +112,7 @@ export class AxisY {
106112
this.tickSize = number(tickSize);
107113
this.tickPadding = number(tickPadding);
108114
this.tickFormat = tickFormat;
115+
this.fontVariant = impliedString(fontVariant, "normal");
109116
this.grid = boolean(grid);
110117
this.label = string(label);
111118
this.labelAnchor = maybeKeyword(labelAnchor, "labelAnchor", ["center", "top", "bottom"]);
@@ -130,6 +137,7 @@ export class AxisY {
130137
) {
131138
const {
132139
axis,
140+
fontVariant,
133141
grid,
134142
label,
135143
labelAnchor,
@@ -146,6 +154,7 @@ export class AxisY {
146154
.call(maybeTickRotate, tickRotate)
147155
.attr("font-size", null)
148156
.attr("font-family", null)
157+
.attr("font-variant", fontVariant)
149158
.call(!line ? g => g.select(".domain").remove() : () => {})
150159
.call(!grid ? () => {}
151160
: fx ? gridFacetY(index, fx, -tx)

src/legends/ramp.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ export function legendRamp(color, {
2323
.attr("class", className)
2424
.attr("font-family", "system-ui, sans-serif")
2525
.attr("font-size", 10)
26-
.attr("font-variant", "tabular-nums")
2726
.attr("width", width)
2827
.attr("height", height)
2928
.attr("viewBox", `0 0 ${width} ${height}`)

src/legends/swatches.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ export function legendSwatches(color, {
8383
.${className} {
8484
font-family: system-ui, sans-serif;
8585
font-size: 10px;
86-
font-variant: tabular-nums;
8786
margin-bottom: 0.5em;${marginLeft === undefined ? "" : `
8887
margin-left: ${+marginLeft}px;`}${width === undefined ? "" : `
8988
width: ${width}px;`}

src/plot.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ export function plot(options = {}) {
7070
.attr("fill", "currentColor")
7171
.attr("font-family", "system-ui, sans-serif")
7272
.attr("font-size", 10)
73-
.attr("font-variant", "tabular-nums")
7473
.attr("text-anchor", "middle")
7574
.attr("width", width)
7675
.attr("height", height)

test/output/aaplBollinger.svg

Lines changed: 3 additions & 3 deletions
Loading

test/output/aaplCandlestick.svg

Lines changed: 2 additions & 2 deletions
Loading

test/output/aaplChangeVolume.svg

Lines changed: 3 additions & 3 deletions
Loading

test/output/aaplClose.svg

Lines changed: 3 additions & 3 deletions
Loading

0 commit comments

Comments
 (0)