-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkde.js
More file actions
49 lines (39 loc) · 1.32 KB
/
Copy pathkde.js
File metadata and controls
49 lines (39 loc) · 1.32 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
// Based on http://bl.ocks.org/900762 by John Firebaugh
d3.json("../data/faithful.json", function(faithful) {
data = faithful;
var width = 800,
height = 400;
var x = d3.scale.linear()
.domain([30, 110]).range([0, width]);
var y = d3.scale.linear()
.domain([0, .1])
.range([0, height]);
var bins = d3.layout.histogram().frequency(false).bins(x.ticks(60))(data),
max = d3.max(bins, function(d) { return d.y; });
var kde = science.stats.kde()
.sample(data);
var vis = d3.select("body")
.append("svg")
.attr("width", width)
.attr("height", height);
var bars = vis.selectAll("g.bar")
.data(bins)
.enter().append("g")
.attr("class", "bar")
.attr("transform", function(d, i) {
return "translate(" + x(d.x) + "," + (height - y(d.y)) + ")";
});
bars.append("rect")
.attr("fill", "steelblue")
.attr("width", function(d) { return x(d.dx + 30) - 1; })
.attr("height", function(d) { return y(d.y); });
var line = d3.svg.line()
.x(function(d) { return x(d[0]); })
.y(function(d) { return height - y(d[1]); });
vis.selectAll("path")
.data(d3.values(science.stats.bandwidth))
.enter().append("path")
.attr("d", function(h) {
return line(kde.bandwidth(h)(d3.range(30, 110, .1)));
});
});