-
Notifications
You must be signed in to change notification settings - Fork 236
Expand file tree
/
Copy pathtip.ts
More file actions
365 lines (330 loc) · 11.1 KB
/
Copy pathtip.ts
File metadata and controls
365 lines (330 loc) · 11.1 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import * as Plot from "@observablehq/plot";
import * as d3 from "d3";
import {feature, mesh} from "topojson-client";
import {test} from "test/plot";
test(async function tipAreaBand() {
const aapl = await d3.csv<any>("data/aapl.csv", d3.autoType);
return Plot.areaY(aapl, {x: "Date", y1: "Low", y2: "High", tip: true, curve: "step", stroke: "currentColor"}).plot();
});
test(async function tipAreaStack() {
const industries = await d3.csv<any>("data/bls-industry-unemployment.csv", d3.autoType);
return Plot.areaY(industries, {x: "date", y: "unemployed", fill: "industry", tip: true}).plot({marginLeft: 50});
});
test(async function tipBar() {
const olympians = await d3.csv<any>("data/athletes.csv", d3.autoType);
return Plot.plot({
marginLeft: 100,
marks: [Plot.barX(olympians, Plot.groupY({x: "count"}, {y: "sport", sort: {y: "x"}, tip: true}))]
});
});
test(async function tipBin() {
const olympians = await d3.csv<any>("data/athletes.csv", d3.autoType);
return Plot.rectY(olympians, Plot.binX({y: "count"}, {x: "weight", tip: true})).plot();
});
test(async function tipBinStack() {
const olympians = await d3.csv<any>("data/athletes.csv", d3.autoType);
return Plot.rectY(olympians, Plot.binX({y: "count", sort: "z"}, {x: "weight", fill: "sex", tip: true})).plot();
});
test(async function tipCell() {
const olympians = await d3.csv<any>("data/athletes.csv", d3.autoType);
return Plot.plot({
height: 400,
marginLeft: 100,
color: {scheme: "blues"},
marks: [Plot.cell(olympians, Plot.group({fill: "count"}, {x: "sex", y: "sport", tip: "y"}))]
});
});
test(async function tipCellFacet() {
const olympians = await d3.csv<any>("data/athletes.csv", d3.autoType);
return Plot.plot({
height: 400,
marginLeft: 100,
color: {scheme: "blues"},
marks: [Plot.cell(olympians, Plot.groupY({fill: "count"}, {fx: "sex", y: "sport", tip: "y"}))]
});
});
test(async function tipDodge() {
const penguins = await d3.csv<any>("data/penguins.csv", d3.autoType);
return Plot.dot(penguins, Plot.dodgeY({x: "culmen_length_mm", r: "body_mass_g", tip: true})).plot({height: 160});
});
test(async function tipDot() {
const penguins = await d3.csv<any>("data/penguins.csv", d3.autoType);
return Plot.dot(penguins, {x: "culmen_length_mm", y: "culmen_depth_mm", stroke: "sex", tip: true}).plot();
});
test(async function tipDotX() {
return Plot.dotX(d3.range(10), {tip: true}).plot();
});
test(async function tipDotFacets() {
const athletes = await d3.csv<any>("data/athletes.csv", d3.autoType);
return Plot.plot({
grid: true,
fy: {
label: "decade of birth",
interval: "10 years"
},
marks: [
Plot.dot(athletes, {
x: "weight",
y: "height",
fx: "sex",
fy: "date_of_birth",
stroke: "#aaa",
filter: (d) => !d.info
}),
Plot.dot(athletes, {
x: "weight",
y: "height",
fx: "sex",
fy: "date_of_birth",
filter: (d) => d.info,
title: (d) => [d.name, d.info].join("\n"),
tip: true
})
]
});
});
test(async function tipDotFilter() {
const penguins = await d3.csv<any>("data/penguins.csv", d3.autoType);
const xy = {x: "culmen_length_mm", y: "culmen_depth_mm", stroke: "sex"};
return Plot.plot({
marks: [
Plot.dot(penguins, {...xy, filter: (d) => d.sex === "MALE", tip: true}),
Plot.dot(penguins, {...xy, filter: (d) => d.sex === "FEMALE", tip: true})
]
});
});
test(async function tipGeoNoProjection() {
const counties = await d3.json<any>("data/us-counties-10m.json").then((us) => feature(us, us.objects.counties));
counties.features = counties.features.filter((d) => {
const [x, y] = d3.geoCentroid(d);
return x > -126 && x < -68 && y > 25 && y < 49;
});
return Plot.geo(counties, Plot.centroid({title: (d) => d.properties.name, tip: true})).plot();
});
test(async function tipGeoProjection() {
const counties = await d3.json<any>("data/us-counties-10m.json").then((us) => feature(us, us.objects.counties));
counties.features = counties.features.filter((d) => {
const [x, y] = d3.geoCentroid(d);
return x > -126 && x < -68 && y > 25 && y < 49;
});
return Plot.geo(counties, Plot.centroid({title: (d) => d.properties.name, tip: true})).plot({projection: "albers"});
});
test(async function tipGeoCentroid() {
const [[counties, countymesh]] = await Promise.all([
d3
.json<any>("data/us-counties-10m.json")
.then((us) => [feature(us, us.objects.counties), mesh(us, us.objects.counties)])
]);
// Alternatively, using centroid (slower):
// const pointer = Plot.pointer(Plot.centroid());
const {x, y} = Plot.geoCentroid();
const pointer = Plot.pointer({px: x, py: y, x, y});
return Plot.plot({
width: 960,
height: 600,
projection: "albers-usa",
marks: [
Plot.geo(countymesh),
Plot.geo(counties, {...pointer, stroke: "red", strokeWidth: 2}),
Plot.tip(counties.features, {...pointer, channels: {name: (d) => d.properties.name}})
]
});
});
test(async function tipGroupPrimitives() {
return Plot.plot({
height: 80,
x: {type: "band"},
marks: [Plot.barY("de156a2fc8", Plot.groupX({y: "count"}, {x: (d) => d, tip: true}))]
});
});
test(async function tipHexbin() {
const olympians = await d3.csv<any>("data/athletes.csv", d3.autoType);
return Plot.hexagon(olympians, Plot.hexbin({r: "count"}, {x: "weight", y: "height", tip: true})).plot();
});
// Normally you would slap a tip: true on the hexagon, as above, but here we
// want to test that the hexbin transform isn’t applying an erroneous stroke:
// none to the tip options (which would change the tip appearance).
test(async function tipHexbinExplicit() {
const olympians = await d3.csv<any>("data/athletes.csv", d3.autoType);
return Plot.plot({
marks: [
Plot.hexagon(olympians, Plot.hexbin({fill: "count"}, {x: "weight", y: "height"})),
Plot.tip(olympians, Plot.pointer(Plot.hexbin({fill: "count"}, {x: "weight", y: "height"})))
]
});
});
test(async function tipLineX() {
const aapl = await d3.csv<any>("data/aapl.csv", d3.autoType);
return Plot.lineX(aapl, {y: "Date", x: "Close", tip: true}).plot();
});
test(async function tipLineY() {
const aapl = await d3.csv<any>("data/aapl.csv", d3.autoType);
return Plot.lineY(aapl, {x: "Date", y: "Close", tip: true}).plot();
});
test(async function tipTextOverflowNull() {
return Plot.tip([{x: "Long sentence that does not get clipped no matter how long it gets; it can be really long"}], {
x: "x",
textOverflow: null,
anchor: "top" // otherwise it would be bottom
}).plot();
});
test(async function tipTextOverflowClipEnd() {
return Plot.tip([{x: "Long sentence that gets clipped at the end"}], {
x: "x",
textOverflow: "clip" // shorthand for "clip-end"
}).plot();
});
test(async function tipTextOverflowDefault() {
return Plot.tip([{x: "Long sentence that gets an ellipsis at the end"}], {
x: "x"
}).plot();
});
test(async function tipTextOverflowEllipsisEnd() {
return Plot.tip([{x: "Long sentence that gets an ellipsis at the end"}], {
x: "x",
textOverflow: "ellipsis" // shorthand for "ellipsis-end"
}).plot();
});
test(async function tipTextOverflowEllipsisMiddle() {
return Plot.tip([{x: "Long sentence that gets an ellipsis in the middle"}], {
x: "x",
textOverflow: "ellipsis-middle"
}).plot();
});
test(async function tipTextOverflowEllipsisStart() {
return Plot.tip([{x: "Long sentence that gets an ellipsis at the start"}], {
x: "x",
textOverflow: "ellipsis-start"
}).plot();
});
test(async function tipNewLines() {
return Plot.plot({
height: 40,
style: "overflow: visible;",
x: {axis: "top", label: null},
marks: [
Plot.tip(
[
{x: "after", label: `Hello\n\n`},
{x: "before", label: `\n\nWorld`},
{x: "between", label: `{\n\n}`}
],
{
x: "x",
anchor: "top",
title: "label"
}
),
Plot.tip([{x: "no name"}], {
x: "x",
channels: {a: ["first"], b: ["second"], "": [""]}
})
]
});
});
test(async function tipRaster() {
const ca55 = await d3.csv<any>("data/ca55-south.csv", d3.autoType);
const domain = {type: "MultiPoint", coordinates: ca55.map((d) => [d.GRID_EAST, d.GRID_NORTH])} as const;
return Plot.plot({
width: 640,
height: 484,
projection: {type: "reflect-y", inset: 3, domain},
color: {type: "diverging"},
marks: [Plot.raster(ca55, {x: "GRID_EAST", y: "GRID_NORTH", fill: "MAG_IGRF90", interpolate: "nearest", tip: true})]
});
});
test(async function tipRule() {
const penguins = await d3.csv<any>("data/penguins.csv", d3.autoType);
return Plot.ruleX(penguins, {x: "body_mass_g", tip: true}).plot();
});
test(async function tipRuleAnchored() {
const penguins = await d3.csv<any>("data/penguins.csv", d3.autoType);
return Plot.plot({
x: {insetLeft: 110},
marks: [
Plot.ruleX(penguins, {x: "body_mass_g"}),
Plot.tip(penguins, Plot.pointer({px: "body_mass_g", frameAnchor: "left", anchor: "middle", dx: 42}))
]
});
});
test(async function tipTransform() {
return Plot.plot({
width: 245,
color: {percent: true, legend: true},
marks: [Plot.dotX([0, 0.1, 0.3, 1], {fill: Plot.identity, r: 10, frameAnchor: "middle", tip: true})]
});
});
test(async function tipFacetX() {
const data = d3.range(100).map((i) => ({f: i > 60 || i % 2 ? "b" : "a", x: i, y: i / 10}));
return Plot.plot({
inset: 10,
y: {domain: [0, 7]},
marks: [
Plot.frame(),
Plot.dot(data, {fy: "f", x: "x", y: "y", tip: "x", fill: "f"}),
Plot.dot(
[
{f: "a", y: 3},
{f: "b", y: 1}
],
{fy: "f", x: 90, y: "y", r: 30, fill: "f", fillOpacity: 0.1, stroke: "currentColor", strokeDasharray: 4}
)
]
});
});
test(async function tipColorLiteral() {
const penguins = await d3.csv<any>("data/penguins.csv", d3.autoType);
return Plot.plot({
grid: true,
marks: [
Plot.dot(penguins, {
x: "culmen_length_mm",
y: "culmen_depth_mm",
fill: (d) => (d.species === "Adelie" ? "orange" : "steelblue"),
tip: true
})
]
});
});
test(async function tipDispatch() {
const penguins = await d3.csv<any>("data/penguins.csv", d3.autoType);
const plot = Plot.plot({
marks: [
Plot.dot(penguins, {
x: "culmen_length_mm",
y: "culmen_depth_mm",
title: "island",
tip: true
})
]
});
plot.dispatchEvent(
new PointerEvent("pointermove", {
pointerType: "mouse",
clientX: 200,
clientY: 200
})
);
return Object.assign(plot, {ready: new Promise((resolve) => setTimeout(resolve, 100))}); // postrender
});
test(async function tipNull() {
const penguins = await d3.csv<any>("data/penguins.csv", d3.autoType);
const plot = Plot.plot({
marks: [
Plot.dot(penguins, {
x: "culmen_length_mm",
y: "culmen_depth_mm",
title: (d) => (d.island === "Torgersen" ? null : d.island),
tip: true
})
]
});
plot.dispatchEvent(
new PointerEvent("pointermove", {
pointerType: "mouse",
clientX: 200,
clientY: 200
})
);
return plot;
});