-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
91 lines (76 loc) · 2.79 KB
/
test.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
<script defer>
var width = 1200, height = 500, padding = 50;
var svg = d3.select("span").append("svg").attr("width",width).attr("height",height);
d3.json("states.json",function(json){
svg.append("g").attr("class","mapGroup");
var states = svg.select(".mapGroup")
.selectAll(".stateLine")
.data(json.features);
states.enter()
.append("path")
.attr("class","stateLine")
.attr("d", d3.geo.path());
// .attr("projection", d3.geo.orthographic);
function getCentroid(stateName) {
var stateData;
json.features.forEach(function(d,i){
if (d.properties.NAME == stateName) {
stateData = d;
return;
}
});
return d3.geo.centroid(stateData);
}
d3.csv("crime_each.csv",function(csv){
var dataTitles = ["Murder","Robbery","Aggravated Assault","Burglary","Motor Vehicle Theft"];
var dataPoint = dataTitles[Math.floor(Math.random()*5)];
var button = d3.select("body").insert("div","svg")
.attr("class","button").text("randomize")
.on("click",function(){
dataPoint = dataTitles[Math.floor(Math.random()*5)];
displayData();
});
function setupData() {
svg.append("text").attr("class","title")
.attr("x",width/2).attr("y",padding)
.text(dataPoint);
var states = svg.append("g").attr("class","dotGroup")
.selectAll(".state").data(csv);
var entering = states.enter().append("g").attr("class","state");
entering.append("path").datum(function(d) {
var stateName = d.State.replace(/^\s+|\s+$/g, '');
return {
type: "Point",
coordinates: getCentroid(stateName),
radius: parseFloat(d[dataPoint]),
dataset: d
};
});
}
function displayData() {
var extent = d3.extent(csv,function(d){
return parseFloat(d[dataPoint]);
});
// console.log(extent);
var radiusScale = d3.scale
.sqrt()
.domain(extent)
.range([3,30]);
var circlePath = d3.geo.path()
.pointRadius(function(d){
return radiusScale(d.radius);
})
svg.select(".dotGroup")
.selectAll("path")
.each(function(d){
d.radius = parseFloat(d.dataset[dataPoint])
})
.attr("d",circlePath);
svg.select(".title")
.text(dataPoint);
}
setupData();
displayData();
});
});
</script>