-
Notifications
You must be signed in to change notification settings - Fork 230
Expand file tree
/
Copy pathdot.js
More file actions
162 lines (153 loc) · 5.56 KB
/
Copy pathdot.js
File metadata and controls
162 lines (153 loc) · 5.56 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
import {pathRound as path, symbolCircle} from "d3";
import {create} from "../context.js";
import {negative, positive} from "../defined.js";
import {Mark} from "../mark.js";
import {identity, maybeFrameAnchor, maybeNumberChannel, maybeTuple} from "../options.js";
import {
applyChannelStyles,
applyDirectStyles,
applyFrameAnchor,
applyIndirectStyles,
applyTransform
} from "../style.js";
import {maybeSymbolChannel} from "../symbols.js";
import {template} from "../template.js";
import {sort} from "../transforms/basic.js";
import {maybeIntervalMidX, maybeIntervalMidY} from "../transforms/interval.js";
const defaults = {
ariaLabel: "dot",
fill: "none",
stroke: "currentColor",
strokeWidth: 1.5
};
export function withDefaultSort(options) {
return options.sort === undefined && options.reverse === undefined
? sort({channel: "r", order: "descending"}, options)
: options;
}
export class Dot extends Mark {
constructor(data, options = {}) {
const {x, y, r, rotate, symbol = symbolCircle, frameAnchor} = options;
const [vrotate, crotate] = maybeNumberChannel(rotate, 0);
const [vsymbol, csymbol] = maybeSymbolChannel(symbol);
const [vr, cr] = maybeNumberChannel(r, vsymbol == null ? 3 : 4.5);
super(
data,
{
x: {value: x, scale: "x", optional: true},
y: {value: y, scale: "y", optional: true},
r: {value: vr, scale: "r", filter: positive, optional: true},
rotate: {value: vrotate, optional: true},
symbol: {value: vsymbol, scale: "auto", optional: true}
},
withDefaultSort(options),
defaults
);
this.r = cr;
this.rotate = crotate;
this.symbol = csymbol;
this.frameAnchor = maybeFrameAnchor(frameAnchor);
// Give a hint to the symbol scale; this allows the symbol scale to chose
// appropriate default symbols based on whether the dots are filled or
// stroked, and for the symbol legend to match the appearance of the dots.
const {channels} = this;
const {symbol: symbolChannel} = channels;
if (symbolChannel) {
const {fill: fillChannel, stroke: strokeChannel} = channels;
symbolChannel.hint = {
fill: fillChannel ? (fillChannel.value === symbolChannel.value ? "color" : "currentColor") : this.fill,
stroke: strokeChannel ? (strokeChannel.value === symbolChannel.value ? "color" : "currentColor") : this.stroke
};
}
}
render(index, scales, channels, dimensions, context) {
const {x, y} = scales;
const {x: X, y: Y, r: R, rotate: A, symbol: S} = channels;
const {r, rotate, symbol} = this;
const [cx, cy] = applyFrameAnchor(this, dimensions);
const circle = this.symbol === symbolCircle;
const size = R ? undefined : r * r * Math.PI;
if (negative(r)) index = [];
return create("svg:g", context)
.call(applyIndirectStyles, this, dimensions, context)
.call(applyTransform, this, {x: X && x, y: Y && y})
.call((g) =>
g
.selectAll()
.data(index)
.enter()
.append(circle ? "circle" : "path")
.call(applyDirectStyles, this)
.call(
circle
? (selection) => {
selection
.attr("cx", X ? (i) => X[i] : cx)
.attr("cy", Y ? (i) => Y[i] : cy)
.attr("r", R ? (i) => R[i] : r);
}
: (selection) => {
selection
.attr(
"transform",
template`translate(${X ? (i) => X[i] : cx},${Y ? (i) => Y[i] : cy})${
A ? (i) => ` rotate(${A[i]})` : rotate ? ` rotate(${rotate})` : ``
}`
)
.attr(
"d",
R && S
? (i) => {
const p = path();
S[i].draw(p, R[i] * R[i] * Math.PI);
return p;
}
: R
? (i) => {
const p = path();
symbol.draw(p, R[i] * R[i] * Math.PI);
return p;
}
: S
? (i) => {
const p = path();
S[i].draw(p, size);
return p;
}
: (() => {
const p = path();
symbol.draw(p, size);
return p;
})()
);
}
)
.call(applyChannelStyles, this, channels)
)
.node();
}
}
/** @jsdoc dot */
export function dot(data, options = {}) {
let {x, y, ...remainingOptions} = options;
if (options.frameAnchor === undefined) [x, y] = maybeTuple(x, y);
return new Dot(data, {...remainingOptions, x, y});
}
/** @jsdoc dotX */
export function dotX(data, options = {}) {
const {x = identity, ...remainingOptions} = options;
return new Dot(data, maybeIntervalMidY({...remainingOptions, x}));
}
/** @jsdoc dotY */
export function dotY(data, options = {}) {
const {y = identity, ...remainingOptions} = options;
return new Dot(data, maybeIntervalMidX({...remainingOptions, y}));
}
/** @jsdoc circle */
export function circle(data, options) {
return dot(data, {...options, symbol: "circle"});
}
/** @jsdoc hexagon */
export function hexagon(data, options) {
return dot(data, {...options, symbol: "hexagon"});
}