forked from noemiko/wojewodztwa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwoj.html
191 lines (152 loc) · 4.48 KB
/
woj.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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 12px sans-serif;
}
path {
stroke-width: 1px;
stroke: #000000;
cursor: pointer;
}
path:hover, path.highlighted {
fill: #ffffff;
}
div.tooltip {
position: absolute;
background-color: white;
border: 1px solid black;
color: black;
font-weight: bold;
padding: 4px 8px;
display: none;
}
text.label {
text-shadow: 0px 0px 12px white;
}
</style>
<body>
<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="https://d3js.org/d3-scale-chromatic.v1.min.js"></script>
<script src="https://d3js.org/topojson.v2.min.js"></script>
<script>
//Map dimensions (in pixels)
var width = 600,
height = 566;
//Map projection
var projection = d3.geoMercator()
.scale(3253.3828788669966)
.center([19.138079166412496,52.015740976086505]) //projection center
.translate([width/2,height/2]) //translate to center the map in view
//Generate paths based on projection
var path = d3.geoPath()
.projection(projection);
//Create an SVG
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
//Group for the map features
var features = svg.append("g")
.attr("class","features");
var labels = svg.append("g")
.attr("class","labels");
//Create a tooltip, hidden at the start
var tooltip = d3.select("body").append("div").attr("class","tooltip");
var color = d3.scaleThreshold()
.domain([108, 109, 110, 111])
.range(["#ffffcc","#c2e699","#78c679","#31a354","#006837"]);
// A position encoding for the key only.
var x = d3.scaleLinear()
.domain([107, 112])
.range([0, 200]);
var xAxis = d3.axisBottom()
.scale(x)
.tickSize(13)
.tickValues(color.domain());
var g = svg.append("g")
.attr("class", "key")
.attr("transform", "translate(300,20)");
// kolorowe paski
g.selectAll("rect")
.data(color.range().map(function(d, i) {
return {
x0: i ? x(color.domain()[i - 1]) : x.range()[0],
x1: i < color.domain().length ? x(color.domain()[i]) : x.range()[1],
z: d
};
}))
.enter().append("rect")
.attr("height", 8)
.attr("x", function(d) { return d.x0; })
.attr("width", function(d) { return d.x1 - d.x0; })
.style("fill", function(d) { return d.z; });
g.call(xAxis).append("text")
.attr("class", "caption")
.attr("y", -6)
.style("fill", "black")
.text("AVG_FWQ");
var datafile = d3.map();
Promise.all([
d3.json("woj.topojson"),
d3.tsv("dane.csv", function(d) { datafile.set(d.WOJEWÓDZTWO, +d.AVG_FWQ); }),
]).then(function(results) {
var geodata = results[0];
// Update color scale domain based on data
// color.domain([0, d3.max(features, d => d.properties.AVG_FWQ)]);
//Create a path for each map feature in the data
features.selectAll("path")
.data(topojson.feature(geodata, geodata.objects.collection).features) //generate features from TopoJSON
.enter()
.append("path")
.attr("d",path)
// .on("mouseover",showTooltip)
// .on("mousemove",moveTooltip)
// .on("mouseout",hideTooltip)
.style("fill", fillFn)
.on("click",clicked);
labels.selectAll("text")
.data(topojson.feature(geodata, geodata.objects.collection).features) //generate features from TopoJSON
.enter()
.append("text")
.attr("class", "label")
.attr("text-anchor","middle")
.attr("transform", function(d) { return "translate(" + path.centroid(d) + ")"; })
.text(nameFn);
}).catch(function(error) {
console.log(error); //unknown error, check the console
});
// Add optional onClick events for features here
// d.properties contains the attributes (e.g. d.properties.name, d.properties.population)
function clicked(d,i) {
}
// Get province color
function fillFn(d){
return color(datafile.get(nameFn(d)));
}
// Get province name
function nameFn(d){
return d && d.properties ? d.properties.VARNAME_1.toLowerCase() : null;
}
// Get province name length
function nameLength(d){
var n = nameFn(d);
return n ? n.length : 0;
}
//Position of the tooltip relative to the cursor
var tooltipOffset = {x: 5, y: -25};
//Create a tooltip, hidden at the start
function showTooltip(d) {
moveTooltip();
tooltip.style("display","block")
.text(nameFn(d));
}
//Move the tooltip to track the mouse
function moveTooltip() {
tooltip.style("top",(d3.event.pageY+tooltipOffset.y)+"px")
.style("left",(d3.event.pageX+tooltipOffset.x)+"px");
}
//Create a tooltip, hidden at the start
function hideTooltip() {
tooltip.style("display","none");
}
</script>