forked from observablehq/plot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
line.js
71 lines (66 loc) · 2.16 KB
/
line.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
import {create, line as shapeLine} from "d3";
import {Curve} from "../curve.js";
import {Mark} from "../plot.js";
import {indexOf, identity, maybeTuple, maybeZ} from "../options.js";
import {applyDirectStyles, applyIndirectStyles, applyTransform, applyGroupedChannelStyles, groupIndex} from "../style.js";
import {maybeDenseIntervalX, maybeDenseIntervalY} from "../transforms/bin.js";
import {applyGroupedMarkers, markers} from "./marker.js";
const defaults = {
ariaLabel: "line",
fill: "none",
stroke: "currentColor",
strokeWidth: 1.5,
strokeLinecap: "round",
strokeLinejoin: "round",
strokeMiterlimit: 1
};
export class Line extends Mark {
constructor(data, options = {}) {
const {x, y, z, curve, tension} = options;
super(
data,
[
{name: "x", value: x, scale: "x"},
{name: "y", value: y, scale: "y"},
{name: "z", value: maybeZ(options), optional: true}
],
options,
defaults
);
this.z = z;
this.curve = Curve(curve, tension);
markers(this, options);
}
filter(index) {
return index;
}
render(index, scales, channels, dimensions) {
const {x: X, y: Y} = channels;
return create("svg:g")
.call(applyIndirectStyles, this, scales, dimensions)
.call(applyTransform, this, scales)
.call(g => g.selectAll()
.data(groupIndex(index, [X, Y], this, channels))
.enter()
.append("path")
.call(applyDirectStyles, this)
.call(applyGroupedChannelStyles, this, channels)
.call(applyGroupedMarkers, this, channels)
.attr("d", shapeLine()
.curve(this.curve)
.defined(i => i >= 0)
.x(i => X[i])
.y(i => Y[i])))
.node();
}
}
export function line(data, {x, y, ...options} = {}) {
([x, y] = maybeTuple(x, y));
return new Line(data, {...options, x, y});
}
export function lineX(data, {x = identity, y = indexOf, ...options} = {}) {
return new Line(data, maybeDenseIntervalY({...options, x, y}));
}
export function lineY(data, {x = indexOf, y = identity, ...options} = {}) {
return new Line(data, maybeDenseIntervalX({...options, x, y}));
}