-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
99 lines (94 loc) · 3.34 KB
/
index.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
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
<!DOCTYPE html>
<head>
<title>UK Map Assignment</title><h1>D3 Assignment: Nishant Thakur (2494602)</h1>
<style>
svg {
background:white;
}
.counties{
fill:#cccc;
stroke:#3333;
stroke-width:1.2;}
.addselection{
fill:rgb(37, 28, 15);
}
.unselected{
fill:white
}
.circle:hover{
stroke: rgb(0, 0, 0);
stroke-width: 4px;
}
.circle{
fill: #9b8877;
stroke: #053d30;
stroke-width: 3;
fill-opacity: 0.4;
}
</style>
</head>
<body>
<center> <button onclick="window.location.reload();">Click to play around new set of towns</button> </center>
<div id="Nishant_uk_map"></div>
<script src="d3.v4.min.js" charset="utf-8"></script>
<script src="topojson.v1.min.js"></script>
<script>
var margin = { top:50, left:50, right:50, bottom:50 };
var height = 900- margin.top - margin.bottom;
var width = 1000- margin.left - margin.right;
var svg = d3.select("#Nishant_uk_map")
.append("svg")
.attr("height", height + margin.top + margin.bottom)
.attr("width", width + margin.left, margin.right)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
d3.queue()
.defer(d3.json,"uktowns.json")
.defer(d3.json,"http://34.78.46.186/Circles/Towns/200")
.await(inject);
var projection = d3.geoAlbers()
.center([0, 55.4])
.rotate([4.4, 0])
.parallels([50, 60])
.scale(4000)
.translate([width / 2, height / 2]);
var path = d3.geoPath().projection(projection);
function inject(error,data,towns){
var counties = topojson.feature(data, data.objects.GBR_adm2).features;
svg.selectAll(".counties")
.data(counties)
.enter().append("path")
.attr("class", "counties")
.attr("d", path)
.attr("fill","#ccccc")
.on('mouseover', function(d){
console.log(d)
d3.select(this).classed("addselection", true)
})
.on('mouseout', function(d){
console.log(d)
d3.select(this).classed("addselection", false)
})
var pointer = d3.select('#Nishant_uk_map').append('div').data(towns).attr('class', 'pointer').style('opacity', 0).style('background-color', 'white').style('border', 'solid').style('border-width', '2px').style('border-radius', '5px').style('padding', '5px').style('position', 'absolute');
var move = function (d) {
console.log(d);
pointer.style('opacity', 1);
};
var hover = function (d) {
pointer.html('Town name: ' +d.Town +'<br>' +'Population: ' +d.Population +'<br>' +'County: ' +
d.County
)
.style('left', d3.mouse(this)[0] + 10 + 'px')
.style('top', d3.mouse(this)[1] + 'px');
};
var leave = function (d) {
pointer.style('opacity', 0);
};
svg.selectAll('.county-circle').data(towns).enter().append('circle').attr('cx', function (d) {
return projection([d.lng, d.lat])[0];
}).attr('cy', function (d) {
return projection([d.lng, d.lat])[1];
}).attr('r', 2).attr('class', 'circle county-circle').on('mouseover', move).on('mousemove', hover).on('mouseleave', leave);
} </script>
</body>
</html>