-
Notifications
You must be signed in to change notification settings - Fork 18
/
script.js
114 lines (96 loc) · 3.22 KB
/
script.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
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
/*globals alert, document, d3, console*/
// These keep JSHint quiet if you're using it (highly recommended!)
function staircase() {
// ****** TODO: PART II ******
var x=document.getElementByID().children;
var scale=10;
var index;
alert(x.length);
alert('Over here') ;
for (index=1;index<= x.length;x++)
{
x[index].setAttribute("height",index*scale)
}
}
function update(error, data) {
if (error !== null) {
alert("Couldn't load the dataset!");
} else {
// D3 loads all CSV data as strings;
// while Javascript is pretty smart
// about interpreting strings as
// numbers when you do things like
// multiplication, it will still
// treat them as strings where it makes
// sense (e.g. adding strings will
// concatenate them, not add the values
// together, or comparing strings
// will do string comparison, not
// numeric comparison).
// We need to explicitly convert values
// to numbers so that comparisons work
// when we call d3.max()
data.forEach(function (d) {
d.a = parseInt(d.a);
d.b = parseFloat(d.b);
});
}
// Set up the scales
var aScale = d3.scale.linear()
.domain([0, d3.max(data, function (d) {
return d.a;
})])
.range([0, 150]);
var bScale = d3.scale.linear()
.domain([0, d3.max(data, function (d) {
return d.b;
})])
.range([0, 150]);
var iScale = d3.scale.linear()
.domain([0, data.length])
.range([0, 110]);
// ****** TODO: PART III (you will also edit in PART V) ******
// TODO: Select and update the 'a' bar chart bars
// TODO: Select and update the 'b' bar chart bars
// TODO: Select and update the 'a' line chart path using this line generator
var aLineGenerator = d3.svg.line()
.x(function (d, i) {
return iScale(i);
})
.y(function (d) {
return aScale(d.a);
});
// TODO: Select and update the 'b' line chart path (create your own generator)
// TODO: Select and update the 'a' area chart path using this line generator
var aAreaGenerator = d3.svg.area()
.x(function (d, i) {
return iScale(i);
})
.y0(0)
.y1(function (d) {
return aScale(d.a);
});
// TODO: Select and update the 'b' area chart path (create your own generator)
// TODO: Select and update the scatterplot points
// ****** TODO: PART IV ******
}
function changeData() {
// Load the file indicated by the select menu
var dataFile = document.getElementById('dataset').value;
d3.csv('data/' + dataFile + '.csv', update);
}
function randomSubset() {
// Load the file indicated by the select menu,
// and then slice out a random chunk before
// passing the data to update()
var dataFile = document.getElementById('dataset').value;
d3.csv('data/' + dataFile + '.csv', function (error, data) {
var subset = [];
data.forEach(function (d) {
if (Math.random() > 0.5) {
subset.push(d);
}
});
update(error, subset);
});
}