forked from observablehq/plot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext.js
107 lines (100 loc) · 3.86 KB
/
text.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import {create} from "d3";
import {filter, nonempty} from "../defined.js";
import {Mark, indexOf, identity, string, maybeNumber, maybeTuple, numberChannel} from "../mark.js";
import {applyChannelStyles, applyDirectStyles, applyIndirectStyles, applyAttr, applyTransform, offset} from "../style.js";
const defaults = {
strokeLinejoin: "round"
};
export class Text extends Mark {
constructor(data, options = {}) {
const {
x,
y,
text = indexOf,
textAnchor,
fontFamily,
fontSize,
fontStyle,
fontVariant,
fontWeight,
dx,
dy = "0.32em",
rotate
} = options;
const [vrotate, crotate] = maybeNumber(rotate, 0);
const [vfontSize, cfontSize] = maybeNumber(fontSize);
super(
data,
[
{name: "x", value: x, scale: "x", optional: true},
{name: "y", value: y, scale: "y", optional: true},
{name: "fontSize", value: numberChannel(vfontSize), optional: true},
{name: "rotate", value: numberChannel(vrotate), optional: true},
{name: "text", value: text}
],
options,
defaults
);
this.rotate = crotate;
this.textAnchor = string(textAnchor);
this.fontFamily = string(fontFamily);
this.fontSize = cfontSize;
this.fontStyle = string(fontStyle);
this.fontVariant = string(fontVariant);
this.fontWeight = string(fontWeight);
this.dx = string(dx);
this.dy = string(dy);
}
render(I, {x, y}, channels, dimensions) {
const {x: X, y: Y, rotate: R, text: T, fontSize: FS} = channels;
const {width, height, marginTop, marginRight, marginBottom, marginLeft} = dimensions;
const {rotate} = this;
const index = filter(I, X, Y, R).filter(i => nonempty(T[i]));
const cx = (marginLeft + width - marginRight) / 2;
const cy = (marginTop + height - marginBottom) / 2;
return create("svg:g")
.call(applyIndirectTextStyles, this)
.call(applyTransform, x, y, offset, offset)
.call(g => g.selectAll()
.data(index)
.join("text")
.call(applyDirectTextStyles, this)
.call(R ? text => text.attr("transform", X && Y ? i => `translate(${X[i]},${Y[i]}) rotate(${R[i]})`
: X ? i => `translate(${X[i]},${cy}) rotate(${R[i]})`
: Y ? i => `translate(${cx},${Y[i]}) rotate(${R[i]})`
: i => `translate(${cx},${cy}) rotate(${R[i]})`)
: rotate ? text => text.attr("transform", X && Y ? i => `translate(${X[i]},${Y[i]}) rotate(${rotate})`
: X ? i => `translate(${X[i]},${cy}) rotate(${rotate})`
: Y ? i => `translate(${cx},${Y[i]}) rotate(${rotate})`
: `translate(${cx},${cy}) rotate(${rotate})`)
: text => text.attr("x", X ? i => X[i] : cx).attr("y", Y ? i => Y[i] : cy))
.call(applyAttr, "font-size", FS && (i => FS[i]))
.text(i => T[i])
.call(applyChannelStyles, channels))
.node();
}
}
export function text(data, {x, y, ...options} = {}) {
([x, y] = maybeTuple(x, y));
return new Text(data, {...options, x, y});
}
export function textX(data, {x = identity, ...options} = {}) {
return new Text(data, {...options, x});
}
export function textY(data, {y = identity, ...options} = {}) {
return new Text(data, {...options, y});
}
function applyIndirectTextStyles(selection, mark) {
applyIndirectStyles(selection, mark);
applyAttr(selection, "text-anchor", mark.textAnchor);
applyAttr(selection, "font-family", mark.fontFamily);
applyAttr(selection, "font-size", mark.fontSize);
applyAttr(selection, "font-style", mark.fontStyle);
applyAttr(selection, "font-variant", mark.fontVariant);
applyAttr(selection, "font-weight", mark.fontWeight);
}
function applyDirectTextStyles(selection, mark) {
applyDirectStyles(selection, mark);
applyAttr(selection, "dx", mark.dx);
applyAttr(selection, "dy", mark.dy);
}