-
Notifications
You must be signed in to change notification settings - Fork 230
Expand file tree
/
Copy pathmark.js
More file actions
120 lines (118 loc) · 4.56 KB
/
Copy pathmark.js
File metadata and controls
120 lines (118 loc) · 4.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
import {Channels, channelDomain, valueObject} from "./channel.js";
import {defined} from "./defined.js";
import {maybeFacetAnchor} from "./facet.js";
import {arrayify, isDomainSort, isOptions, range} from "./options.js";
import {keyword, maybeNamed} from "./options.js";
import {maybeProject} from "./projection.js";
import {maybeClip, styles} from "./style.js";
import {basic, initializer} from "./transforms/basic.js";
export class Mark {
constructor(data, channels = {}, options = {}, defaults) {
const {
facet = "auto",
facetAnchor,
fx,
fy,
sort,
dx = 0,
dy = 0,
margin = 0,
marginTop = margin,
marginRight = margin,
marginBottom = margin,
marginLeft = margin,
clip,
channels: extraChannels
} = options;
this.data = data;
this.sort = isDomainSort(sort) ? sort : null;
this.initializer = initializer(options).initializer;
this.transform = this.initializer ? options.transform : basic(options).transform;
if (facet === null || facet === false) {
this.facet = null;
} else {
this.facet = keyword(facet === true ? "include" : facet, "facet", ["auto", "include", "exclude", "super"]);
this.fx = fx;
this.fy = fy;
}
this.facetAnchor = maybeFacetAnchor(facetAnchor);
channels = maybeNamed(channels);
if (extraChannels !== undefined) channels = {...maybeNamed(extraChannels), ...channels};
if (defaults !== undefined) channels = {...styles(this, options, defaults), ...channels};
this.channels = Object.fromEntries(
Object.entries(channels)
.map(([name, channel]) => {
const {value} = channel;
if (isOptions(value)) {
channel = {...channel, value: value.value};
if (value.scale !== undefined) channel.scale = value.scale;
}
return [name, channel];
})
.filter(([name, {value, optional}]) => {
if (value != null) return true;
if (optional) return false;
throw new Error(`missing channel value: ${name}`);
})
);
this.dx = +dx;
this.dy = +dy;
this.marginTop = +marginTop;
this.marginRight = +marginRight;
this.marginBottom = +marginBottom;
this.marginLeft = +marginLeft;
this.clip = maybeClip(clip);
// Super-faceting currently disallow position channels; in the future, we
// could allow position to be specified in fx and fy in addition to (or
// instead of) x and y.
if (this.facet === "super") {
if (fx || fy) throw new Error(`super-faceting cannot use fx or fy`);
for (const name in this.channels) {
const {scale} = channels[name];
if (scale !== "x" && scale !== "y") continue;
throw new Error(`super-faceting cannot use x or y`);
}
}
}
initialize(facets, facetChannels) {
let data = arrayify(this.data);
if (facets === undefined && data != null) facets = [range(data)];
if (this.transform != null) ({facets, data} = this.transform(data, facets)), (data = arrayify(data));
const channels = Channels(this.channels, data);
if (this.sort != null) channelDomain(channels, facetChannels, data, this.sort); // mutates facetChannels!
return {data, facets, channels};
}
filter(index, channels, values) {
for (const name in channels) {
const {filter = defined} = channels[name];
if (filter !== null) {
const value = values[name];
index = index.filter((i) => filter(value[i]));
}
}
return index;
}
// If there is a projection, and there are both x and y channels (or x1 and
// y1, or x2 and y2 channels), and those channels are associated with the x
// and y scale respectively (and not already in screen coordinates as with an
// initializer), then apply the projection, replacing the x and y values. Note
// that the x and y scales themselves don’t exist if there is a projection,
// but whether the channels are associated with scales still determines
// whether the projection should apply; think of the projection as a
// combination xy-scale.
project(channels, values, context) {
maybeProject("x", "y", channels, values, context);
maybeProject("x1", "y1", channels, values, context);
maybeProject("x2", "y2", channels, values, context);
}
scale(channels, scales, context) {
const values = valueObject(channels, scales);
if (context.projection) this.project(channels, values, context);
return values;
}
}
/** @jsdoc marks */
export function marks(...marks) {
marks.plot = Mark.prototype.plot; // Note: depends on side-effect in plot!
return marks;
}