-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzigzag.js
89 lines (67 loc) · 1.81 KB
/
zigzag.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
89
var sequence = (x,r,len) =>{
var data=[]
for (let i=0;i<len;i++){
data.push({x:i,y:x});
x=logistic(x,r);
}
return data
}
// parte dello slider
var sliderR1 = document.getElementById("r1");
var len=70;
displayR1.innerHTML="r = "+r;
data=sequence(x_0,r,len);
// append the svg object to the body of the page
var g1 = d3.select("#zigzag")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform",
"translate(" + margin.left + "," + margin.top + ")");
// Add X axis --> it is a date format
var x = d3.scaleLinear()
.domain([0,len])
.range([ 0, width ]);
// Add Y axis
var y = d3.scaleLinear()
.domain([0,1])
.range([height, 0]);
var ym1 = d3.scaleLinear()
.domain([height,0])
.range([0,1])
const line=d3.line()
.x(function(d){return x(d.x);})
.y(function(d){return y(d.y);})
// Add the line
g1.append("svg:path")
.data([data])
.attr("id","zigzag")
.attr("d",line)
.style("fill",'none')
.style("stroke",'#ff7300')
.style("stroke-width","3px");
g1.append("circle")
.attr("cx",0)
.attr("cy",y(x_0))
.attr("r",7.5)
.attr("id","dragx_1")
.style("fill",'#ff7300');
//aggiungere il fatto che il mouse diventa una mano che trascina
var drag2=d3.drag()
.on("drag", function(){
x_0=ym1(d3.event.y);
updateX0(x_0);
})
drag2(g1.selectAll("#dragx_1"));
const syncR = function(){
r = sliderR1.value;
g1.select("#zigzag").data([sequence(x_0,r,len)]).attr("d", line);
displayR1.innerHTML=="r = "+r;
}
sliderR1.addEventListener("input", syncR)
g1.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
g1.append("g")
.call(d3.axisLeft(y));