forked from observablehq/plot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext.js
119 lines (113 loc) · 4.35 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
108
109
110
111
112
113
114
115
116
117
118
119
import {create} from "d3";
import {filter, nonempty} from "../defined.js";
import {Mark, indexOf, identity, string, title, maybeColor, maybeNumber, maybeTuple, numberChannel} from "../mark.js";
import {Style, applyDirectStyles, applyIndirectStyles, applyAttr, applyStyle, applyTransform} from "../style.js";
export class Text extends Mark {
constructor(
data,
{
x,
y,
text = indexOf,
title,
fill,
fillOpacity,
textAnchor,
fontFamily,
fontSize,
fontStyle,
fontVariant,
fontWeight,
dx,
dy = "0.32em",
rotate,
...options
} = {}
) {
const [vfill, cfill] = maybeColor(fill, "currentColor");
const [vfillOpacity, cfillOpacity] = maybeNumber(fillOpacity);
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},
{name: "title", value: title, optional: true},
{name: "fill", value: vfill, scale: "color", optional: true},
{name: "fillOpacity", value: vfillOpacity, scale: "opacity", optional: true}
],
options
);
Style(this, {fill: cfill, fillOpacity: cfillOpacity, ...options});
this.rotate = crotate;
this.textAnchor = string(textAnchor);
this.fontFamily = string(fontFamily);
this.fontSize = string(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},
{x: X, y: Y, rotate: R, text: T, title: L, fill: F, fillOpacity: FO, fontSize: FS},
{width, height, marginTop, marginRight, marginBottom, marginLeft}
) {
const {rotate} = this;
const index = filter(I, X, Y, F, FO, 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, 0.5, 0.5)
.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, "fill", F && (i => F[i]))
.call(applyAttr, "fill-opacity", FO && (i => FO[i]))
.call(applyAttr, "font-size", FS && (i => FS[i]))
.text(i => T[i])
.call(title(L)))
.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);
applyStyle(selection, "font-family", mark.fontFamily);
applyStyle(selection, "font-size", mark.fontSize);
applyStyle(selection, "font-style", mark.fontStyle);
applyStyle(selection, "font-variant", mark.fontVariant);
applyStyle(selection, "font-weight", mark.fontWeight);
}
function applyDirectTextStyles(selection, mark) {
applyDirectStyles(selection, mark);
applyAttr(selection, "dx", mark.dx);
applyAttr(selection, "dy", mark.dy);
}