-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGunDashboard.js
More file actions
169 lines (145 loc) · 6.18 KB
/
GunDashboard.js
File metadata and controls
169 lines (145 loc) · 6.18 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
var overview={
display: async function () {
var svg = d3.select("#Overview").append("svg").attr("Width",600).attr("height",300),
width = +svg.attr("width"),
height = +svg.attr("height");
usMap = await(d3.json("https://raw.githubusercontent.com/PublicaMundi/MappingAPI/master/data/geojson/us-states.json"))
// Load external data and boot
stateData = await(d3.csv("https://github.com/sudiptobilu/sudiptibilu.github.io/blob/main/GunViolence.csv"))
var statelist = [];
var temp = {};
var y = {};
var total = 0;
var killed = 0;
for(var i = 0; i < stateData.length; i++){
temp = {};
y = statelist.find(o=>o.state === stateData[i].state);
if(y !== undefined)
{
y["killed"] = y["killed"] + parseInt(stateData[i].n_killed);
y["injured"] = y["injured"] + parseInt(stateData[i].n_injured);
y["incidents"] = y["incidents"] + parseInt(stateData[i].n_killed + stateData[i].n_injured);
total = total + y["incidents"];
killed = killed + y["killed"];
}
else
{
temp["state"] = stateData[i].state;
temp["killed"] = parseInt(stateData[i].n_killed);
temp["injured"] = parseInt(stateData[i].n_injured);
temp["incidents"] = parseInt(stateData[i].n_killed) + parseInt(stateData[i].n_injured);
total = total + temp["incidents"];
killed = killed + temp["killed"];
statelist.push(temp);
}
}
temp = statelist.find(o=>o.state === "California");
temp["state"] = "California";
temp["killed"] = temp["killed"];
temp["injured"] = temp["injured"];
temp["incidents"] = temp["incidents"];
statelist.push(temp);
var maxcase = 0, maxstate = '';
for(i = 0; i < statelist.length; i++)
{
var x = parseInt(statelist[i]["incidents"]);
if (maxcase < x)
{
maxcase = x;
maxstate = statelist[i]["state"]
}
}
//d3.select('svg')
svg.append('g').append('text').transition().duration(300).attr("x", 0).attr("y", 10).attr("id","anno")
.text("*Hover over the states for more details").attr("font-size", "14px")
.attr("font-weight","italic").style("fill", "red").attr("font-weight","bold")
var rect = svg.append('rect').transition().duration(2000).attr("x", 1000).attr("y", 160).attr("width", 250)
.attr("height", 190).attr("fill","lightblue").attr("stroke","black").text("As of July 2022")
svg.append('g').append('text').transition().duration(2000).attr("x", 1010).attr("y", 175).attr("id","anno")
.text("Overview").attr("font-size", "14px").attr("font-weight","italic").style("fill", "Black").attr("font-weight","bold")
svg.append('g').append('text').transition().duration(2000).attr("x", 1010).attr("y", 190).attr("id","anno")
.text(">Gun Violence Data for last 3 Years").attr("font-size", "14px").attr("font-weight","italic").style("fill", "Black")
svg.append('g').append('text').transition().duration(2000).attr("x", 1010).attr("y", 205).attr("id","anno")
.text(">Total number of Cases: " + total).attr("font-size", "14px").attr("font-weight","italic").style("fill", "Black")
svg.append('g').append('text').transition().duration(2000).attr("x", 1010).attr("y", 220).attr("id","anno")
.text(">Total number of Deaths: " + killed).attr("font-size", "14px").attr("font-weight","italic").style("fill", "Black")
svg.append('g').append('text').transition().duration(2000).attr("x", 1010).attr("y", 250).attr("id","anno")
.text("Most Affected State: " + maxstate).attr("font-size", "14px")
.attr("font-weight","italic").style("fill", "Black").attr("font-weight","bold")
svg.append('g').append('text').transition().duration(2000).attr("x", 1010).attr("y", 267).attr("id","anno")
.text("Cases in State of " + maxstate + " is: " + maxcase).attr("font-size", "14px")
.attr("font-weight","italic").style("fill", "Black")
var width = 1300;
var height = 600;
var margin = 50;
var projection = d3.geoAlbersUsa()
.translate([(width/2 -100 ), (height/2)-100])
.scale([1000]);
// Define path generator
var path = d3.geoPath()
.projection(projection);
const colorRange = d3.scaleLinear()
//.interpolator(d3.interpolateInferno)
.domain([1,15000])
.range(["white","pink"]);
var div = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
for(var i = 0; i < statelist.length; i++){
var stateName = (statelist[i].state);
var killed = (statelist[i].killed);
var injured = (statelist[i].injured);
for(var n = 0; n < usMap.features.length; n++){
var userState = usMap.features[n].properties.name;
if(stateName == userState){
usMap.features[n].properties.killed = killed;
usMap.features[n].properties.injured = injured;
usMap.features[n].properties.incidents = killed + injured;
break;
}
}
}
svg.attr("width",width )
.attr("height",height)
.append("g")
.attr("transform","translate(" + margin + "," + margin + ")")
.selectAll("path")
.data(usMap.features)
.enter()
.append("path")
.attr("d", function(eachfeature) {return path(eachfeature);})
.attr("opacity",0.8)
.attr("stroke","black")
.attr("fill", function(d)
{
return colorRange(d.properties.incidents);
})
.on("mouseover", function(d)
{
if(d3.select(this).style("opacity") != 0){
d3.select(this).transition()
.duration(200)
.style("opacity", .65);
}
var html = "<span style = 'font-size:15px;color:black'><b>" + d.properties.name + "</b></span></br>" +
"<span style='font-size:12px;color:black'><b> Killed: </b>" + d.properties.killed + "</span></br>" +
"<span style='font-size:12px;color:black'><b> Injured: </b>" + d.properties.injured + "</span>";
div.transition()
.duration(200)
.style("opacity", 1);
div.html(html)
.style("left", (d3.event.pageX + 10) + "px")
.style("top", (d3.event.pageY +10 ) + "px")
.style("width",130)
.style("height",80)
.style("background",function(){ return("lightblue");})
})
.on("mouseout", function(d) {
div.transition()
.duration(300)
.style("opacity", 0);
d3.select(this).transition()
.duration(200)
.style("opacity", 1);
})
}};