-
Notifications
You must be signed in to change notification settings - Fork 0
/
json-prep.html
59 lines (43 loc) · 1.1 KB
/
json-prep.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
</head>
<script>
// Using d3.map, d3.rollup, etc.
function nodify(d, i) {
function deString(str) {
if (str==="") {
return "";
} else {
return +str;
}
}
d.avg = deString(d.avg);
d.pra = deString(d.pra);
d.prb = deString(d.prb);
return d;
}
function viz(data) {
var parseDate = d3.time.format("%d%b%Y").parse;
var formatDate = d3.time.format("%Y-%m-%d");
dataset = d3.nest()
.key(function(d) { return d.itm; })
.key(function(d) { return d.div; })
.rollup(function(v) {
v.sort(function(a,b) {
return parseDate(a.date) - parseDate(b.date);
});
return v.map(function(d) {
return {"date": formatDate(parseDate(d.date)), "price": d.avg};
});
})
.map(data);
console.log(JSON.stringify(dataset));
// Then used Chrome dev tools to copy array of objects.
// Saved into example-data/(blah)/alldata.json.
}
d3.csv("example-data/Price data from year 2003 to date/alldata.csv", nodify, viz);
</script>
</html>