-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
88 lines (80 loc) · 2.18 KB
/
script.js
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
let countyURL = "./geography.json";
let educationURL = "./education-data.json";
let countyData;
let educationData;
let canvas = d3.select("#canvas");
let tooltip = d3.select("#tooltip");
let drawMap = () => {
canvas
.selectAll("path")
.data(countyData)
.enter()
.append("path")
.attr("d", d3.geoPath())
.attr("class", "county")
.attr("fill", (countyDataItem) => {
let id = countyDataItem["id"];
let county = educationData.find((item) => {
return item["fips"] === id;
});
let percentage = county["bachelorsOrHigher"];
if (percentage <= 15) {
return "tomato";
} else if (percentage <= 30) {
return "orange";
} else if (percentage <= 45) {
return "lightgreen";
} else {
return "limegreen";
}
})
.attr("data-fips", (countyDataItem) => {
return countyDataItem["id"];
})
.attr("data-education", (countyDataItem) => {
let id = countyDataItem["id"];
let county = educationData.find((item) => {
return item["fips"] === id;
});
let percentage = county["bachelorsOrHigher"];
return percentage;
})
.on("mouseover", (countyDataItem) => {
tooltip.transition().style("visibility", "visible");
let id = countyDataItem["id"];
let county = educationData.find((item) => {
return item["fips"] === id;
});
tooltip.text(
county["fips"] +
" - " +
county["area_name"] +
", " +
county["state"] +
" : " +
county["bachelorsOrHigher"] +
"%"
);
tooltip.attr("data-education", county["bachelorsOrHigher"]);
})
.on("mouseout", (countyDataItem) => {
tooltip.transition().style("visibility", "hidden");
});
};
d3.json(countyURL).then((data, error) => {
if (error) {
console.log(log);
} else {
countyData = topojson.feature(data, data.objects.counties).features;
console.log(countyData);
d3.json(educationURL).then((data, error) => {
if (error) {
console.log(error);
} else {
educationData = data;
console.log(educationData);
drawMap();
}
});
}
});