-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
128 lines (100 loc) · 2.56 KB
/
script.js
File metadata and controls
128 lines (100 loc) · 2.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
var width = 800,
height = 800;
var trows = 1000;
var data = dataGenerator(trows);
var svg = d3.select("#chart")
.append("svg")
.attr("width", width)
.attr("height", height);
// X axis start from 0 to n flips
var xMax = d3.max([d3.max(data, function (d) {
return d.x;
})]);
var xMin = d3.min([d3.min(data, function (d) {
return d.x;
})]);
// Y axis start from lowest toss value to highest
var yMax = d3.max([d3.max(data, function (d) {
return d.y;
})]);
var yMin = d3.min([d3.min(data, function (d) {
return d.y;
})]);
var xscale = d3.scaleLinear()
.domain([xMin, xMax])
.range([0, width]);
var yscale = d3.scaleLinear()
.domain([yMax, yMin])
.range([0, height]);
var x_axis = d3.axisBottom()
.scale(xscale);
var y_axis = d3.axisLeft()
.scale(yscale);
svg.append("g")
.attr("transform", "translate(" + xscale(0) + ",0)")
.call(y_axis);
// X axes need to stick to 0 Y axis point
svg.append("g")
.attr("transform", "translate(0, " + yscale(0) + ")") // set the xAxis to Y 0 dinamically
.call(x_axis);
// import data
svg.selectAll("circle")
.attr("class", "points")
.data(data)
.enter().append("circle")
.attr("cx", function (d) {
return xscale(d.x);
})
.attr("cy", function (d) {
return yscale(d.y);
})
.attr("r", 1);
var line = d3.line()
.x(function (d) {
return xscale(d.x);
})
.y(function (d) {
return yscale(d.y);
});
// Add the valueline path.
svg.append("path")
.datum(data)
.attr("class", "line")
.attr("d", line);
// data generator
function dataGenerator(n) {
var graph = []; // main array
var i = 0;
var y = 0;
var x = 0;
while (i < n) {
switch (Math.floor(Math.random() * 4)) {
case 0: // 1 y++
var point = new Object();
point.x = x;
point.y = y += 1;
graph.push(point);
break;
case 1: // 2 y--
var point = new Object();
point.x = x;
point.y = y -= 1;
graph.push(point);
break;
case 2: // 3 x--
var point = new Object();
point.x = x -= 1;
point.y = y;
graph.push(point);
break;
case 3: // 4 x++
var point = new Object();
point.x = x += 1;
point.y = y;
graph.push(point);
break;
}
i++;
}
return graph;
}