forked from observablehq/plot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdot.js
88 lines (84 loc) · 2.95 KB
/
dot.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
import {create} from "d3";
import {filter, positive} from "../defined.js";
import {Mark, identity, maybeColor, maybeNumber, maybeTuple, title} from "../mark.js";
import {Style, applyDirectStyles, applyIndirectStyles, applyTransform, applyAttr} from "../style.js";
export class Dot extends Mark {
constructor(
data,
{
x,
y,
r,
title,
fill,
fillOpacity,
stroke,
strokeOpacity,
...options
} = {}
) {
const [vr, cr] = maybeNumber(r, 3);
const [vfill, cfill] = maybeColor(fill, "none");
const [vfillOpacity, cfillOpacity] = maybeNumber(fillOpacity);
const [vstroke, cstroke] = maybeColor(stroke, cfill === "none" ? "currentColor" : "none");
const [vstrokeOpacity, cstrokeOpacity] = maybeNumber(strokeOpacity);
super(
data,
[
{name: "x", value: x, scale: "x", optional: true},
{name: "y", value: y, scale: "y", optional: true},
{name: "r", value: vr, scale: "r", optional: true},
{name: "title", value: title, optional: true},
{name: "fill", value: vfill, scale: "color", optional: true},
{name: "fillOpacity", value: vfillOpacity, scale: "opacity", optional: true},
{name: "stroke", value: vstroke, scale: "color", optional: true},
{name: "strokeOpacity", value: vstrokeOpacity, scale: "opacity", optional: true}
],
options
);
this.r = cr;
Style(this, {
fill: cfill,
fillOpacity: cfillOpacity,
stroke: cstroke,
strokeOpacity: cstrokeOpacity,
strokeWidth: cstroke === "none" ? undefined : 1.5,
...options
});
}
render(
I,
{x, y},
{x: X, y: Y, r: R, title: L, fill: F, fillOpacity: FO, stroke: S, strokeOpacity: SO},
{width, height, marginTop, marginRight, marginBottom, marginLeft}
) {
let index = filter(I, X, Y, F, FO, S, SO);
if (R) index = index.filter(i => positive(R[i]));
return create("svg:g")
.call(applyIndirectStyles, this)
.call(applyTransform, x, y, 0.5, 0.5)
.call(g => g.selectAll()
.data(index)
.join("circle")
.call(applyDirectStyles, this)
.attr("cx", X ? i => X[i] : (marginLeft + width - marginRight) / 2)
.attr("cy", Y ? i => Y[i] : (marginTop + height - marginBottom) / 2)
.attr("r", R ? i => R[i] : this.r)
.call(applyAttr, "fill", F && (i => F[i]))
.call(applyAttr, "fill-opacity", FO && (i => FO[i]))
.call(applyAttr, "stroke", S && (i => S[i]))
.call(applyAttr, "stroke-opacity", SO && (i => SO[i]))
.call(title(L)))
.node();
}
}
export function dot(data, {x, y, ...options} = {}) {
([x, y] = maybeTuple(x, y));
return new Dot(data, {...options, x, y});
}
export function dotX(data, {x = identity, ...options} = {}) {
return new Dot(data, {...options, x});
}
export function dotY(data, {y = identity, ...options} = {}) {
return new Dot(data, {...options, y});
}