Skip to content

Commit 51f1f11

Browse files
committed
fix #173; more Text font properties
1 parent b340d13 commit 51f1f11

File tree

7 files changed

+38
-19
lines changed

7 files changed

+38
-19
lines changed

src/marks/text.js

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {ascending} from "d3-array";
22
import {create} from "d3-selection";
33
import {filter, nonempty} from "../defined.js";
44
import {Mark, indexOf, identity, string, title, maybeColor, first, second} from "../mark.js";
5-
import {Style, applyDirectStyles, applyIndirectStyles, applyAttr, applyTransform} from "../style.js";
5+
import {Style, applyDirectStyles, applyIndirectStyles, applyAttr, applyStyle, applyTransform} from "../style.js";
66

77
export class Text extends Mark {
88
constructor(
@@ -16,6 +16,11 @@ export class Text extends Mark {
1616
fill,
1717
transform,
1818
textAnchor,
19+
fontFamily,
20+
fontSize,
21+
fontStyle,
22+
fontVariant,
23+
fontWeight,
1924
dx,
2025
dy = "0.32em",
2126
...style
@@ -36,6 +41,11 @@ export class Text extends Mark {
3641
);
3742
Style(this, {fill: cfill, ...style});
3843
this.textAnchor = string(textAnchor);
44+
this.fontFamily = string(fontFamily);
45+
this.fontSize = string(fontSize);
46+
this.fontStyle = string(fontStyle);
47+
this.fontVariant = string(fontVariant);
48+
this.fontWeight = string(fontWeight);
3949
this.dx = string(dx);
4050
this.dy = string(dy);
4151
}
@@ -48,13 +58,12 @@ export class Text extends Mark {
4858
const index = filter(I, X, Y, F).filter(i => nonempty(T[i]));
4959
if (Z) index.sort((i, j) => ascending(Z[i], Z[j]));
5060
return create("svg:g")
51-
.call(applyIndirectStyles, this)
61+
.call(applyIndirectTextStyles, this)
5262
.call(applyTransform, x, y, 0.5, 0.5)
5363
.call(g => g.selectAll()
5464
.data(index)
5565
.join("text")
56-
.call(applyDirectStyles, this)
57-
.call(applyTextStyles, this)
66+
.call(applyDirectTextStyles, this)
5867
.attr("x", X ? i => x(X[i]) : (marginLeft + width - marginRight) / 2)
5968
.attr("y", Y ? i => y(Y[i]) : (marginTop + height - marginBottom) / 2)
6069
.attr("fill", F && (i => color(F[i])))
@@ -76,8 +85,18 @@ export function textY(data, {y = identity, ...options} = {}) {
7685
return new Text(data, {...options, y, x: null});
7786
}
7887

79-
function applyTextStyles(selection, style) {
80-
applyAttr(selection, "text-anchor", style.textAnchor);
81-
applyAttr(selection, "dx", style.dx);
82-
applyAttr(selection, "dy", style.dy);
88+
function applyIndirectTextStyles(selection, mark) {
89+
applyIndirectStyles(selection, mark);
90+
applyAttr(selection, "text-anchor", mark.textAnchor);
91+
applyStyle(selection, "font-family", mark.fontFamily);
92+
applyStyle(selection, "font-size", mark.fontSize);
93+
applyStyle(selection, "font-style", mark.fontStyle);
94+
applyStyle(selection, "font-variant", mark.fontVariant);
95+
applyStyle(selection, "font-weight", mark.fontWeight);
96+
}
97+
98+
function applyDirectTextStyles(selection, mark) {
99+
applyDirectStyles(selection, mark);
100+
applyAttr(selection, "dx", mark.dx);
101+
applyAttr(selection, "dy", mark.dy);
83102
}

src/style.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ export function applyTransform(selection, x, y, tx = 0, ty = 0) {
5454
selection.attr("transform", `translate(${tx},${ty})`);
5555
}
5656

57-
function impliedString(value, impliedValue) {
57+
export function impliedString(value, impliedValue) {
5858
if ((value = string(value)) !== impliedValue) return value;
5959
}
6060

61-
function impliedNumber(value, impliedValue) {
61+
export function impliedNumber(value, impliedValue) {
6262
if ((value = number(value)) !== impliedValue) return value;
6363
}

test/output/caltrain.svg

Lines changed: 4 additions & 4 deletions
Loading

test/output/covidIhmeProjectedDeaths.svg

Lines changed: 1 addition & 1 deletion
Loading

test/output/policeDeaths.svg

Lines changed: 2 additions & 2 deletions
Loading

test/output/stocksIndex.svg

Lines changed: 1 addition & 1 deletion
Loading

0 commit comments

Comments
 (0)