-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMap.js
More file actions
181 lines (150 loc) · 5.19 KB
/
Map.js
File metadata and controls
181 lines (150 loc) · 5.19 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
170
171
172
173
174
175
176
177
178
179
var width = 960;
var height = 500;
var lowColor = '#EAEEDA'
var highColor = '#758636'
//var highColor = '#622556'
//var lowColor = '#FCF7FB'
// D3 Projection
var projection = d3.geoAlbersUsa()
.translate([width / 2, height / 2]) // translate to center of screen
.scale([1000]); // scale things down so see entire US
// Define path generator
var path = d3.geoPath() // path generator that will convert GeoJSON to SVG paths
.projection(projection); // tell path generator to use albersUsa projection
//Create SVG element and append map to the SVG
var svg = d3.select("#map")
.append("svg")
// .attr("preserveAspectRatio", "xMinYMin meet")
// .attr("viewBox", "0 0 960 500")
.attr("width", width)
.attr("height", height)
svg.append("rect")
.attr("width", "100%")
.attr("height", "100%")
.attr("fill", "white");
//d3.select("#map").attr("align","center");
var div = d3.select("#map")
.append("div")
.attr("class", "tooltip");
var t = d3.transition()
.on("interrupt", function(d,i){
console.info(i);
});
// Load in my states data!
d3.csv("sample.csv", function(data) {
var dataArray = [];
for (var d = 0; d < data.length; d++) {
dataArray.push(parseFloat(data[d].value))
}
var minVal = d3.min(dataArray)
var maxVal = d3.max(dataArray)
var ramp = d3.scaleLinear().domain([minVal,maxVal]).range([lowColor,highColor])
// Load GeoJSON data and merge with states data
d3.json("us-states.json", function(json) {
// Loop through each state data value in the .csv file
for (var i = 0; i < data.length; i++) {
// Grab State Name
var dataState = data[i].state.toLowerCase();
//console.log(dataState);
// Grab data value
var dataValue = data[i].value;
//console.log(dataValue);
//Find the corresponding state inside the GeoJSON
for (var j = 0; j < json.features.length; j++) {
var jsonState = json.features[j].properties.name.replace(/\s+/g, '').toLowerCase();
console.log(jsonState , dataState);
if (dataState == jsonState) {
json.features[j].properties.value = parseFloat(dataValue);
//console.log(dataValue) ;
}
// break;
}
}
// Bind the data to the SVG and create one path per GeoJSON feature
svg.selectAll("path")
.data(json.features)
.enter()
.append("path")
.attr("d", path)
.style("stroke", "#fff")
.style("stroke-width", "1")
.style("fill", function(d) {
if(d.properties.value == -1){
return '#5d5d5e';
} else {
return ramp(d.properties.value);
}
}).on("mouseover", function(d) {
div.transition()
.duration(200)
.style("opacity", 1);
d3.select(this)
.transition(t)
//.style("fill", "red");
var format = d3.format("$,");
// div.text(d.properties.name)
div.text(d.properties.name + " : " + format(Math.round(d.properties.value)))
//div.text(parseFloat(d.properties.value))
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY) + "px");
})
// fade out tooltip on mouse out
.on("mouseout", function(d) {
div.transition()
.duration(500)
.style("opacity", 0)
d3.select(this).interrupt();
d3.select(this)
.transition(t)
// .style("fill", "#aca");;
})
.on("click", function(d){
var queryString = "?para1=" + d.properties.name;
location.href = "index_bar.html" + queryString ;
})
;
//add a legend
//
var w = 140, h = 300;
var key = d3.select("#map")
.append("svg")
//.attr("viewBox", "0 0 1800 1300")
// .attr("preserveAspectRatio", "xMinYMin meet")
// .attr("viewBox", "0 0 1800 1300")
.attr("width", w)
.attr("height", h)
.attr("class", "legend")
var legend = key.append("defs")
.append("svg:linearGradient")
.attr("id", "gradient")
.attr("x1", "100%")
.attr("y1", "0%")
.attr("x2", "100%")
.attr("y2", "100%")
.attr("spreadMethod", "pad")
;
legend.append("stop")
.attr("offset", "0%")
.attr("stop-color", highColor)
.attr("stop-opacity", 1);
legend.append("stop")
.attr("offset", "100%")
.attr("stop-color", lowColor)
.attr("stop-opacity", 1);
key.append("rect")
.attr("width", w - 100)
.attr("height", h)
.style("fill", "url(#gradient)")
//.attr('x', 1000 + 10)
//.attr('y', legendRectSize - legendSpacing)
.attr("transform", "translate(0,10)");
var y = d3.scaleLinear()
.range([h, 0])
.domain([minVal, maxVal]);
var yAxis = d3.axisRight(y);
key.append("g")
.attr("class", "y axis")
.attr("transform", "translate(41,10)")
.call(yAxis.ticks(10, "s"))
});
});