-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmyscript.js
45 lines (39 loc) · 973 Bytes
/
myscript.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
const w = 800;
const h = 400;
const datalen = 10;
let svg = d3.select("svg")
.attr("width",w)
.attr("height",h);
let background = svg.append("rect")
.attr("class","background")
.attr("width",w)
.attr("height",h);
let bands = svg.append("g")
bands.selectAll("rect.band")
.data(_.range(datalen))
.enter()
.append("rect")
.attr("height",h)
.attr("width",w/datalen)
.attr("class","band")
.attr("x",d=>d*w/datalen);
let line = d3.line()
.x(d=>d[0])
.y(d=>d[1]);
let pathdata = {};
let path = svg.append("path")
.attr("class","yourpath");
background
.on("mousedown",()=>{
background
.on("mousemove",function(d,i){
position = Math.round(d3.event.offsetX/(w/datalen));
pathdata[position] = [position*w/datalen,d3.mouse(this)[1]];
path.datum(_.values(pathdata)).attr("d",line);
})
.on("mouseup",()=>{
background
.on("mousemove",null)
.on("mouseup",null);
});
});