Last active
November 24, 2017 16:26
-
-
Save JulienAssouline/d3a1825597413cfbbe02c979c4064ab7 to your computer and use it in GitHub Desktop.
two linked charts [hover over bars]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Country_Residence | Origin | Year | Count | Lat_Residence | Lon_Residence | Lat_Origin | Lon_Origin | ||
---|---|---|---|---|---|---|---|---|---|
159 | Canada | Ghana | 2016 | 5.0 | 56.130366 | -106.34677099999999 | 7.946527000000001 | -1.0231940000000002 | |
134 | Canada | Angola | 2016 | 6.0 | 56.130366 | -106.34677099999999 | -11.202691999999999 | 17.873887 | |
202 | Canada | Yemen | 2016 | 6.0 | 56.130366 | -106.34677099999999 | 15.552726999999999 | 48.516388 | |
195 | Canada | Thailand | 2016 | 6.0 | 56.130366 | -106.34677099999999 | 15.870032 | 100.992541 | |
136 | Canada | Armenia | 2016 | 7.0 | 56.130366 | -106.34677099999999 | 40.069099 | 45.038189 | |
170 | Canada | Cambodia | 2016 | 7.0 | 56.130366 | -106.34677099999999 | 12.565679 | 104.990963 | |
177 | Canada | Nepal | 2016 | 8.0 | 56.130366 | -106.34677099999999 | 28.394857000000002 | 84.12400799999999 | |
194 | Canada | Togo | 2016 | 8.0 | 56.130366 | -106.34677099999999 | 8.619543 | 0.824782 | |
198 | Canada | Uganda | 2016 | 9.0 | 56.130366 | -106.34677099999999 | 1.373333 | 32.290275 | |
173 | Canada | Sri Lanka | 2016 | 9.0 | 56.130366 | -106.34677099999999 | 7.873054 | 80.77179699999999 | |
200 | Canada | United States | 2016 | 9.0 | 56.130366 | -106.34677099999999 | 37.09024 | -95.712891 | |
203 | Canada | South Africa | 2016 | 10.0 | 56.130366 | -106.34677099999999 | -30.559482 | 22.937506 | |
196 | Canada | Turkey | 2016 | 10.0 | 56.130366 | -106.34677099999999 | 38.963745 | 35.243322 | |
154 | Canada | Ecuador | 2016 | 11.0 | 56.130366 | -106.34677099999999 | -1.8312389999999998 | -78.183406 | |
163 | Canada | India | 2016 | 12.0 | 56.130366 | -106.34677099999999 | 20.593684 | 78.96288 | |
193 | Canada | Chad | 2016 | 12.0 | 56.130366 | -106.34677099999999 | 15.454166 | 18.732207 | |
161 | Canada | Gambia | 2016 | 17.0 | 56.130366 | -106.34677099999999 | 13.443182 | -15.310139000000001 | |
186 | Canada | Senegal | 2016 | 18.0 | 56.130366 | -106.34677099999999 | 14.497401000000002 | -14.452361999999999 | |
146 | Canada | China | 2016 | 22.0 | 56.130366 | -106.34677099999999 | 35.86166 | 104.195397 | |
187 | Canada | Sierra Leone | 2016 | 43.0 | 56.130366 | -106.34677099999999 | 8.460555000000001 | -11.779889 | |
167 | Canada | Jordan | 2016 | 43.0 | 56.130366 | -106.34677099999999 | 30.585164000000002 | 36.238414 | |
189 | Canada | Sudan | 2016 | 65.0 | 56.130366 | -106.34677099999999 | 12.862807 | 30.217636 | |
144 | Canada | Central African Republic | 2016 | 76.0 | 56.130366 | -106.34677099999999 | 6.611111 | 20.939444 | |
183 | Canada | Rwanda | 2016 | 90.0 | 56.130366 | -106.34677099999999 | -1.940278 | 29.873888 | |
171 | Canada | Lebanon | 2016 | 100.0 | 56.130366 | -106.34677099999999 | 33.854721000000005 | 35.862285 | |
142 | Canada | Bhutan | 2016 | 106.0 | 56.130366 | -106.34677099999999 | 27.514162 | 90.433601 | |
172 | Canada | Liberia | 2016 | 114.0 | 56.130366 | -106.34677099999999 | 6.4280550000000005 | -9.429499 | |
180 | Canada | Palestinian Territories | 2016 | 129.0 | 56.130366 | -106.34677099999999 | 31.952162 | 35.233154 | |
155 | Canada | Egypt | 2016 | 134.0 | 56.130366 | -106.34677099999999 | 26.820553000000004 | 30.802497999999996 | |
147 | Canada | Cote d'Ivoire | 2016 | 152.0 | 56.130366 | -106.34677099999999 | 7.539989 | -5.54708 | |
151 | Canada | Colombia | 2016 | 178.0 | 56.130366 | -106.34677099999999 | 4.570868 | -74.297333 | |
185 | Canada | Sudan | 2016 | 190.0 | 56.130366 | -106.34677099999999 | 12.862807 | 30.217636 | |
138 | Canada | Burundi | 2016 | 218.0 | 56.130366 | -106.34677099999999 | -3.3730559999999996 | 29.918885999999997 | |
178 | Canada | Pakistan | 2016 | 226.0 | 56.130366 | -106.34677099999999 | 30.375321000000003 | 69.345116 | |
164 | Canada | Iran | 2016 | 248.0 | 56.130366 | -106.34677099999999 | 32.427908 | 53.68804599999999 | |
175 | Canada | Myanmar | 2016 | 368.0 | 56.130366 | -106.34677099999999 | 21.913965 | 95.956223 | |
158 | Canada | Ethiopia | 2016 | 725.0 | 56.130366 | -106.34677099999999 | 9.145 | 40.489672999999996 | |
188 | Canada | Somalia | 2016 | 902.0 | 56.130366 | -106.34677099999999 | 5.152149 | 46.199616 | |
133 | Canada | Afghanistan | 2016 | 1354.0 | 56.130366 | -106.34677099999999 | 33.93911 | 67.709953 | |
165 | Canada | Iraq | 2016 | 1650.0 | 56.130366 | -106.34677099999999 | 33.223191 | 43.679291 | |
149 | Canada | Congo | 2016 | 1655.0 | 56.130366 | -106.346771 | -4.038333 | 21.758664 | |
156 | Canada | Eritrea | 2016 | 3934.0 | 56.130366 | -106.34677099999999 | 15.179383999999999 | 39.782334000000006 | |
192 | Canada | Syria | 2016 | 33266.0 | 56.130366 | -106.34677099999999 | 34.802075 | 38.996815000000005 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
159 | Canada | Ghana | 2016 | 5.0 | 56.130366 | -106.34677099999999 | 7.946527000000001 | -1.0231940000000002 | |
---|---|---|---|---|---|---|---|---|---|
134 | Canada | Angola | 2016 | 6.0 | 56.130366 | -106.34677099999999 | -11.202691999999999 | 17.873887 | |
202 | Canada | Yemen | 2016 | 6.0 | 56.130366 | -106.34677099999999 | 15.552726999999999 | 48.516388 | |
195 | Canada | Thailand | 2016 | 6.0 | 56.130366 | -106.34677099999999 | 15.870032 | 100.992541 | |
136 | Canada | Armenia | 2016 | 7.0 | 56.130366 | -106.34677099999999 | 40.069099 | 45.038189 | |
170 | Canada | Cambodia | 2016 | 7.0 | 56.130366 | -106.34677099999999 | 12.565679 | 104.990963 | |
177 | Canada | Nepal | 2016 | 8.0 | 56.130366 | -106.34677099999999 | 28.394857000000002 | 84.12400799999999 | |
194 | Canada | Togo | 2016 | 8.0 | 56.130366 | -106.34677099999999 | 8.619543 | 0.824782 | |
198 | Canada | Uganda | 2016 | 9.0 | 56.130366 | -106.34677099999999 | 1.373333 | 32.290275 | |
173 | Canada | Sri Lanka | 2016 | 9.0 | 56.130366 | -106.34677099999999 | 7.873054 | 80.77179699999999 | |
200 | Canada | United States | 2016 | 9.0 | 56.130366 | -106.34677099999999 | 37.09024 | -95.712891 | |
203 | Canada | South Africa | 2016 | 10.0 | 56.130366 | -106.34677099999999 | -30.559482 | 22.937506 | |
196 | Canada | Turkey | 2016 | 10.0 | 56.130366 | -106.34677099999999 | 38.963745 | 35.243322 | |
154 | Canada | Ecuador | 2016 | 11.0 | 56.130366 | -106.34677099999999 | -1.8312389999999998 | -78.183406 | |
163 | Canada | India | 2016 | 12.0 | 56.130366 | -106.34677099999999 | 20.593684 | 78.96288 | |
193 | Canada | Chad | 2016 | 12.0 | 56.130366 | -106.34677099999999 | 15.454166 | 18.732207 | |
161 | Canada | Gambia | 2016 | 17.0 | 56.130366 | -106.34677099999999 | 13.443182 | -15.310139000000001 | |
186 | Canada | Senegal | 2016 | 18.0 | 56.130366 | -106.34677099999999 | 14.497401000000002 | -14.452361999999999 | |
146 | Canada | China | 2016 | 22.0 | 56.130366 | -106.34677099999999 | 35.86166 | 104.195397 | |
187 | Canada | Sierra Leone | 2016 | 43.0 | 56.130366 | -106.34677099999999 | 8.460555000000001 | -11.779889 | |
167 | Canada | Jordan | 2016 | 43.0 | 56.130366 | -106.34677099999999 | 30.585164000000002 | 36.238414 | |
189 | Canada | Sudan | 2016 | 65.0 | 56.130366 | -106.34677099999999 | 12.862807 | 30.217636 | |
144 | Canada | Central African Republic | 2016 | 76.0 | 56.130366 | -106.34677099999999 | 6.611111 | 20.939444 | |
183 | Canada | Rwanda | 2016 | 90.0 | 56.130366 | -106.34677099999999 | -1.940278 | 29.873888 | |
171 | Canada | Lebanon | 2016 | 100.0 | 56.130366 | -106.34677099999999 | 33.854721000000005 | 35.862285 | |
142 | Canada | Bhutan | 2016 | 106.0 | 56.130366 | -106.34677099999999 | 27.514162 | 90.433601 | |
172 | Canada | Liberia | 2016 | 114.0 | 56.130366 | -106.34677099999999 | 6.4280550000000005 | -9.429499 | |
180 | Canada | Palestinian Territories | 2016 | 129.0 | 56.130366 | -106.34677099999999 | 31.952162 | 35.233154 | |
155 | Canada | Egypt | 2016 | 134.0 | 56.130366 | -106.34677099999999 | 26.820553000000004 | 30.802497999999996 | |
147 | Canada | Cote d'Ivoire | 2016 | 152.0 | 56.130366 | -106.34677099999999 | 7.539989 | -5.54708 | |
151 | Canada | Colombia | 2016 | 178.0 | 56.130366 | -106.34677099999999 | 4.570868 | -74.297333 | |
185 | Canada | Sudan | 2016 | 190.0 | 56.130366 | -106.34677099999999 | 12.862807 | 30.217636 | |
138 | Canada | Burundi | 2016 | 218.0 | 56.130366 | -106.34677099999999 | -3.3730559999999996 | 29.918885999999997 | |
178 | Canada | Pakistan | 2016 | 226.0 | 56.130366 | -106.34677099999999 | 30.375321000000003 | 69.345116 | |
164 | Canada | Iran | 2016 | 248.0 | 56.130366 | -106.34677099999999 | 32.427908 | 53.68804599999999 | |
175 | Canada | Myanmar | 2016 | 368.0 | 56.130366 | -106.34677099999999 | 21.913965 | 95.956223 | |
158 | Canada | Ethiopia | 2016 | 725.0 | 56.130366 | -106.34677099999999 | 9.145 | 40.489672999999996 | |
188 | Canada | Somalia | 2016 | 902.0 | 56.130366 | -106.34677099999999 | 5.152149 | 46.199616 | |
133 | Canada | Afghanistan | 2016 | 1354.0 | 56.130366 | -106.34677099999999 | 33.93911 | 67.709953 | |
165 | Canada | Iraq | 2016 | 1650.0 | 56.130366 | -106.34677099999999 | 33.223191 | 43.679291 | |
149 | Canada | Congo | 2016 | 1655.0 | 56.130366 | -106.346771 | -4.038333 | 21.758664 | |
156 | Canada | Eritrea | 2016 | 3934.0 | 56.130366 | -106.34677099999999 | 15.179383999999999 | 39.782334000000006 | |
192 | Canada | Syria | 2016 | 33266.0 | 56.130366 | -106.34677099999999 | 34.802075 | 38.996815000000005 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
//Width and height | |
var w = 600; | |
var h = 600; | |
var margin = { | |
top: 100, | |
bottom: 0, | |
left: 0, | |
right: 0 | |
}; | |
var width = w - margin.left - margin.right; | |
var height = h - margin.top - margin.bottom; | |
var lineScale = d3.scaleSqrt() | |
.domain([5,33266]) | |
.range([0.5, 3]) | |
// define map projection | |
var projection = d3.geoAzimuthalEquidistant() | |
.translate([w/2, h/2 + 60]) | |
.scale(240) | |
// .scale([500]); | |
//Define default path generator | |
var path = d3.geoPath() | |
.projection(projection) | |
var svg = d3.select("#Canadian_refugees") | |
.append("svg") | |
.attr("id", "chart") | |
.attr("width", w) | |
.attr("height", h) | |
.append("g") | |
.attr("transform", "translate(0" + margin.left + "," + margin.top + ")"); | |
d3.json("world.topojson", function(json){ | |
d3.csv("Canada_refugees_2016.csv", function(error, data){ | |
// data.forEach(function(d){ | |
// d.Lon_Residence = +d.Lon_Residence | |
// d.Lat_Residence = +d.Lat_Residence | |
// }) | |
var countries = topojson.feature(json, json.objects.countries).features | |
svg.selectAll(".country") | |
.data(countries) | |
.enter() | |
.append("path") | |
.attr("class", "country") | |
.attr("d", path) | |
svg.selectAll(".arcs") | |
.data(data) | |
.enter() | |
.append("path") | |
.attr("class", function(d) { | |
return "arc " + d.Origin.replace(/[^A-Za-z0-9]/g, "")// this adds a class to every arc. DONT FORGET THE SPACE | |
}) | |
.style("stroke", "#cd0d0e") | |
.attr("d", function(d){ | |
return lngLatToArc(d, 0.7); | |
}) | |
.style("fill", "none") | |
.style("opacity", function(d){ | |
if(d.Origin == "Syria"){ | |
return 0.7 | |
} else { | |
return 0.4 | |
} | |
}) | |
.style("stroke-width", function(d){ | |
return lineScale(d.Count) | |
}) | |
svg.selectAll("circles") | |
.data(data) | |
.enter() | |
.append("circle") | |
.attr("class", "Resident") | |
.attr("r", 2) | |
.attr("cx", function(d){ | |
var coords = projection([d.Lon_Residence, d.Lat_Residence]) | |
return coords[0]; | |
}) | |
.attr("cy", function(d){ | |
var coords = projection([d.Lon_Residence, d.Lat_Residence]) | |
return coords[1] | |
}) | |
.style("fill", "none") | |
.style("opacity", 0) | |
svg.selectAll("points") | |
.data(data) | |
.enter() | |
.append("circle") | |
.attr("class", "Origin") | |
.attr("r", 2) | |
.attr("cx", function(d){ | |
var coords = projection([d.Lon_Origin, d.Lat_Origin]) | |
return coords[0]; | |
}) | |
.attr("cy", function(d){ | |
var coords = projection([d.Lon_Origin, d.Lat_Origin]) | |
return coords[1] | |
}) | |
.style("fill", "none") | |
.style("opacity", 1) | |
svg.selectAll(".label") | |
.data(data.filter(function(d){ | |
return d.Count == 33266 | |
})) | |
.enter() | |
.append("text") | |
.attr("class", "label") | |
.attr("x", function(d){ | |
var coords = projection([d.Lon_Origin, d.Lat_Origin]) | |
return coords[0] | |
}) | |
.attr("y", function(d){ | |
var coords = projection([d.Lon_Origin, d.Lat_Origin]) | |
return coords[1] + 13 | |
}) | |
.text("Syria") | |
svg.append("text") | |
.attr("x", 250) | |
.attr("y", 0) | |
.text("Canada") | |
.style("font-weight", "bold") | |
// This function takes an object, the key names where it will find an array of lng/lat pairs, e.g. `[-74, 40]` | |
// And a bend parameter for how much bend you want in your arcs, the higher the number, the less bend. | |
function lngLatToArc(d, bend){ | |
// If no bend is supplied, then do the plain square root | |
bend = bend || 1; | |
// `[d.Lon_Origin, d.Lat_Origin]` and `[d.Lon_Residence, d.Lat_Residence]` are arrays of `[lng, lat]` | |
// Note, people often put these in lat then lng, but mathematically we want x then y which is `lng,lat` | |
var sourceLngLat = [d.Lon_Origin, d.Lat_Origin], | |
targetLngLat = [d.Lon_Residence, d.Lat_Residence]; | |
if(targetLngLat && sourceLngLat){ | |
var sourceXY = projection(sourceLngLat), | |
targetXY = projection(targetLngLat); | |
// Uncomment this for testing, useful to see if you have any null lng/lat values | |
// if (!targetXY) console.log(d, targetLngLat, targetXY) | |
var sourceX = sourceXY[0], | |
sourceY = sourceXY[1]; | |
var targetX = targetXY[0], | |
targetY = targetXY[1]; | |
var dx = targetX - sourceX, | |
dy = targetY - sourceY | |
dr = Math.sqrt(dx * dx + dy * dy) * bend; | |
// To avoid a whirlpool effect, make the bend direction consistent regardless of whether the source is east or west of the target | |
var west_of_source = (targetX - sourceX) < 0; | |
if (west_of_source) return "M" + targetX + "," + targetY + "A" + dr + "," + dr + " 0 0,1 " + sourceX + "," + sourceY; | |
return "M" + sourceX + "," + sourceY + "A" + dr + "," + dr + " 0 0,1 " + targetX + "," + targetY; | |
} else{ | |
return "M0,0,l0,0z"; | |
} | |
} | |
}) | |
}) | |
})() | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title>D3: Loading TopoJSON data and generating SVG paths</title> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
<script src="//d3js.org/topojson.v1.min.js"></script> | |
<style type="text/css"> | |
/* No style rules here yet */ | |
body,html{ | |
margin: 0; | |
padding: 0; | |
font-family: "Arial", sans-serif; | |
font-size: 11px; | |
text-align: center; | |
} | |
#chart{ | |
background-color: white; | |
stroke-width: 1; | |
} | |
.country{ | |
fill: #eeedeb; | |
stroke: lightgrey; | |
} | |
#chart1{ | |
background-color: white; | |
stroke-width: 0; | |
} | |
.yaxis line, | |
.yaxis path{ | |
fill: none; | |
stroke: none; | |
} | |
.container{ | |
float: right; | |
} | |
.container_1{ | |
float: left; | |
} | |
</style> | |
</head> | |
<body> | |
<div class="container" id="Canadian_refugees"></div> | |
<script src="Canadian_refugees.js"></script> | |
<div class="container_1" id= "count_refugee"></div> | |
<script src="Refugees_origin_2016_Canada.js"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
var w = 200 | |
var h = 300 | |
var margin = { | |
right: 40, | |
left: 150, | |
top: 40, | |
bottom: 40 | |
} | |
var width = w - margin.right - margin.left; | |
var height = h - margin.top - margin.bottom; | |
var svg = d3.select("#count_refugee") | |
.append("svg") | |
.attr("id", "chart1") | |
.attr("width", w) | |
.attr("height", h) | |
.append("g") | |
.attr("transform", "translate(0" + margin.left + "," + margin.top + ")"); | |
var xScale = d3.scaleLinear() | |
.range([0, width]); | |
var yScale = d3.scaleBand() | |
.range([height, 0]); | |
var yAxis = d3.axisLeft() | |
.scale(yScale) | |
d3.csv("Canada_refugees_2016_bar.csv", function(error, data){ | |
data.forEach(function(d){ | |
d.Count = +d.Count; | |
d.Country_Residence = d.Country_Residence | |
d.Origin = d.Origin | |
}) | |
xScale.domain([0, d3.max(data, function(d){ return d.Count })]); | |
yScale.domain(data.map(function(d){ return d.Origin })); | |
svg.append("g") | |
.attr("class", "yaxis") | |
.attr("transform", "translate(0," + 0+ ")") | |
.call(yAxis) | |
.selectAll("text") | |
.style("fill", "black") | |
.style("font-size", "12px") | |
.style("font-weight", "Myriad Pro") | |
svg.selectAll(".rect") | |
.data(data) | |
.enter() | |
.append("rect") | |
.classed("bar", true) | |
.attr("x", 0) | |
.attr("y", function(d){ | |
return yScale(d.Origin) | |
}) | |
.attr("width", function(d){ | |
return xScale(d.Count) + 1 | |
}) | |
.attr("height", 10) | |
.style("fill", "#cd0d0e") | |
.on("mouseover", function(d){ | |
d3.selectAll(".arc").style("opacity", 0.1) /// selecting the arcs from map | |
d3.select("."+ d.Origin.replace(/[^A-Za-z0-9]/g, "")).style("opacity", 1) // . means selecting things based on class. (.) + Origin is selecting the country based on the class | |
}) | |
.on("mouseout", function(d){ | |
d3.selectAll(".arc").style("opacity", function(e){ | |
if(e.Origin == "Syria"){ | |
return 0.7 | |
} else { | |
return 0.4 | |
} | |
}) | |
// d3.select(this).style("fill", "#cd0d0e") | |
}) | |
}) | |
})() | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment