Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
153 lines (127 loc) · 3.91 KB
/
Copy pathmain.js
File metadata and controls
153 lines (127 loc) · 3.91 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
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("#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,"uk-counties.json")
.defer(d3.json,"http://34.78.46.186/Circles/Towns/50")
.await(ready);
// var projection = d3.geoMercator().translate([width / 2, height / 2]);
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 ready(error,data,towns){
var countries = topojson.feature(data, data.objects.GBR_adm2).features;
svg.selectAll(".country")
.data(countries)
.enter().append("path")
.attr("class", "country")
.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)
})
// creating circles
var Tooltip = d3
.select('#map')
.append('div')
.data(towns)
.attr('class', 'tooltip')
.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 mousehover = function (d) {
console.log(d);
Tooltip.style('opacity', 1);
};
var mousemove = function (d) {
Tooltip.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 mouseleave = function (d) {
Tooltip.style('opacity', 0);
};
svg
.selectAll('.town-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 town-circle')
.on('mouseover', mousehover)
.on('mousemove', mousemove)
.on('mouseleave', mouseleave);
}
// //Creating tooltip
// var Tooltip = d3.select("#map")
// .append("div")
// .data(towns)
// .attr("class", "tooltip")
// .style("opacity", 1)
// .style("background-color", "white")
// .style("border", "solid")
// .style("border-width", "2px")
// .style("border-radius", "5px")
// .style("padding", "5px")
// .style("position", "absolute")
// // Three functions that will add the tooltip
// var mousehover = function(d) {
// console.log(d)
// Tooltip.style("opacity", 1)
// }
// var mousemove = function(d) {
// Tooltip
// .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 mouseleave = function(d) {
// Tooltip.style("opacity", 0)
// }
// // Add circles:
// svg
// .selectAll(".town-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", 7)
// .attr("class", "circle")
// .on("mouseover", mousehover)
// .on("mousemove", mousemove)
// .on("mouseleave", mouseleave)
// }