-
Notifications
You must be signed in to change notification settings - Fork 0
/
wix.html
181 lines (149 loc) · 5.86 KB
/
wix.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
<!doctype html>
<!--<meta charset="utf-8">-->
<head>
<style>
#wrapper {
width: 100%;
height: auto;
/* display: flex;
align-items: center;
justify-content: center; */
}
.country-boundary {
stroke: #999;
stroke-width: .3px;
opacity: 0.8;
}
.hidden {
display: none;
}
div.tooltip {
background-color: #fff;
padding: 7px;
text-shadow: #f5f5f5 0 1px 0;
font: 13px Helvetica Neue;
border: 2.5px solid;
border-color: black;
border-radius: 3px;
opacity: 0.95;
position: absolute;
box-shadow: rgba(0, 0, 0, 0.3) 0 2px 10px;
}
</style>
</head>
<body onLoad="ready()">
<script src="https://d3js.org/d3.v7.min.js"></script>
<script src="https://unpkg.com/topojson@3"></script>
<div id="wrapper">
<div id="chart"></div>
</div>
<script type="text/javascript">
var dataEV = []
var width = 960,
height = 600,
svg_dx = 100
svg_dy = 100;
function zoomed({ transform }) {
d3.select('g').attr("transform", transform);
}
const zoom = d3.zoom()
.extent([[svg_dx, svg_dy], [width*1.2-svg_dx, height*2-svg_dy]])
.scaleExtent([1, 8])
.translateExtent([[svg_dx, svg_dy], [width*1.2-svg_dx, height*2-svg_dy]])
.on("zoom", zoomed)
var svg = d3.select('#chart')
.append('svg')
.attr("viewBox", "0 0 " + width + " " + height)
.attr("preserveAspectRatio", "xMinYMin")
// .attr('width', width)
// .attr('height', height)
.call(zoom);
var g = svg.append('g');
var tooltip = d3.select('#chart').append('div')
.attr('class', 'hidden tooltip');
const projection = d3.geoMercator()
.scale(140)
.translate([width / 2, height / 1.4]);
var path = d3.geoPath()
.projection(projection);
var mouseover = function (d) {
tooltip.classed('hidden', false);
d3.select(this)
.style("stroke", "black")
.style("opacity", 1)
}
var mousemove = (event, d) => {
tooltip
.attr('style', 'left:' + (event.clientX + 20) + 'px; top:' + (event.clientY - 10) + 'px')
.html(d.properties.name + '<br>EV Percentage: ' + d.properties.evPercentageTag
+ '<br>Total EVs: ' + d.properties.totalEvTag);
}
var mouseout = function (d) {
tooltip.classed('hidden', true);
d3.select(this)
.style("stroke", "#999")
.style("opacity", 0.8)
}
var t=setInterval(function() {window.parent.postMessage("hello", "*");},1000);
if(dataEV.length > 0) { // if no locations, let page know
clearInterval(t);
}
window.onmessage = function (event) {
if (event.data && Array.isArray(event.data)) {
event.data.forEach((d) => {
if (d.evPercentage == undefined) {
d.evPercentage = 0
}
if (d.totalEv == undefined) {
d.totalEv = "Unknown"
}
dataEV.push({ "country": d.country, "evPercentage": d.evPercentage, "totalEv": d.totalEv })
})
}
d3.json("https://cdn.jsdelivr.net/npm/world-atlas@2/countries-110m.json").then((data) => {
var features = topojson.feature(data, data.objects.countries).features
// Set whatever relative data to your countries here, modify the function if necessary
features.forEach((element, index) => {
var relevantCountry = dataEV.find((e) => {
return e.country === element.properties.name
})
if (relevantCountry != undefined) {
element.properties = {
...element.properties,
"evPercentage": relevantCountry.evPercentage,
"totalEv": relevantCountry.totalEv,
"evPercentageTag": relevantCountry.evPercentage != 0 ? relevantCountry.evPercentage : 'Unknown',
"totalEvTag": relevantCountry.totalEv != 0 ? relevantCountry.totalEv : 'Unknown',
}
} else {
element.properties = {
...element.properties,
"evPercentage": 0,
"totalEv": 0,
"evPercentageTag": "Unknown",
"totalEvTag": "Unknown",
}
}
});
// Set the colors for the data you wish to appear here
var myColor = d3.scaleLinear().domain([0, 100])
.range(["white", "green"])
g.selectAll("path")
.data(features)
.enter()
.append("path")
.attr("d", path)
.attr("class", "country-boundary")
.attr("fill", (d) => { return myColor(d.properties.evPercentage) }) // set your color scale variable
.on('mouseover', mouseover)
.on('mousemove', mousemove)
.on('mouseout', mouseout);
});
}
function ready() {
window.parent.postMessage({ "type": "ready" }, "*");
}
</script>
</body>
</html>
<!--HTTPS only...-->