/
AppAvailabilityZone.jsx
398 lines (317 loc) · 11 KB
/
AppAvailabilityZone.jsx
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
// Copyright (c) [2017] Dell Inc. or its subsidiaries. All Rights Reserved.
import React from 'react';
import { observer, inject } from 'mobx-react';
import styles from './styles-app-availability-zone.scss';
import textures from 'textures';
var topojson = require('topojson')
import world from './world-110m.json';
@observer
export default class AppAvailabilityZone extends React.Component{
constructor(props){
super(props)
}
static propTypes = {
appGeography: React.PropTypes.object.isRequired
}
render(){
const { appGeography } = this.props;
return(
<div className={styles.AppAvailabilityZone}>
<div className="title">app availability zone</div>
<div className={styles.inside}>
<div id="availability-zones-map"></div>
</div>
</div>
)
}
initChart(){
d3.select("#availability-zones-map").select('*').remove();
var width = 283.52,
height = 224;
var rotationDuration = 2500;
var rotationDegree = 360;
var scale = height / 2 - 5;
var that = this;
var svg = d3.select("#availability-zones-map").append("svg")
.attr("width", width)
.attr("height", height);
var g = svg.append("g");
//set location to display on map
//mock locations
var israel = [34.8516, 31.0461],
italy = [12.5674, 41.8719],
alaska = [149.4937, 64.2008],
ny = [-74.0059, 40.7128],
china = [104.1954, 35.8617],
sf = [-122.4194, 37.7749],
australia = [133.7751, -25.2744],
indonesia = [113.9213, -0.7893];
var locations = formatCoordinates(this.props.appGeography.locations)
//var locations = [israel, italy, alaska, ny, china, sf];
//var locations = [australia, indonesia];
//console.log(locations)
function formatCoordinates(locations){
var newLocations = locations.map((location)=>{
var coords = location.geometry.coordinates;
return [coords.lat, coords.lng ];
});
return newLocations;
}
//define two projections to interpolate
var projection = interpolatedProjection(
d3.geo.orthographic()
.scale(scale + 50)
.translate([width / 2, height / 2]),
d3.geo.equirectangular()
.scale(scale + 70)
.translate([width / 2, height / 2])).clipAngle(90);
var path = d3.geo.path()
.projection(projection);
//rotate
d3.transition()
.duration(rotationDuration)
.tween("rotate", function() {
var r = d3.interpolate(projection.rotate(), [rotationDegree, 0, 0]);
return function(t) {
projection.rotate(r(t));
g.selectAll("path").attr("d", path);
if(r(t)[0] == rotationDegree){
animation();
}
};
})
var feature = g.selectAll("path");
//animate projections and zoom to locations
function animation() {
g.transition()
.duration(rotationDuration)
.tween("projection", function() {
return function(_) {
projection.alpha(_);
g.selectAll("path").attr("d", path);
if(_ == 1){
projection.clipAngle(null);
g.selectAll("path").attr("d", path);
addLocations();
}
};
})
}
//add locations by coordinates
function addLocations(){
var pointsContainer = g.append("g").classed("pointsContainer", true);
var circles = pointsContainer.selectAll("g")
.data(that.props.appGeography.locations)
.enter()
.append("g")
.on('mouseover', showTooltip)
.on('mouseout', hideTooltip)
circles
.append("circle")
.classed("location", true)
.attr("r", "8px")
.attr("fill", "#d3e1ca")
.attr("fill-opacity", "0.5")
.attr("cx", function(d){
const coordinates = d.geometry.coordinates;
const coords = [coordinates.lng, coordinates.lat];
return projection(coords)[0]
})
.attr("cy", function(d){
const coordinates = d.geometry.coordinates;
const coords = [coordinates.lng, coordinates.lat];
return projection(coords)[1]
})
circles
.append("circle")
.classed("location", true)
.attr("r", "4px")
.attr("fill", "#84bd5d")
.attr("cx", function(d){
const coordinates = d.geometry.coordinates;
const coords = [coordinates.lng, coordinates.lat];
return projection(coords)[0]
})
.attr("cy", function(d){
const coordinates = d.geometry.coordinates;
const coords = [coordinates.lng, coordinates.lat];
return projection(coords)[1]
})
circles
.append("circle")
.classed("location", true)
.attr("r", "2px")
.attr("fill", "#fff")
.attr("cx", function(d){
const coordinates = d.geometry.coordinates;
const coords = [coordinates.lng, coordinates.lat];
return projection(coords)[0]
})
.attr("cy", function(d){
const coordinates = d.geometry.coordinates;
const coords = [coordinates.lng, coordinates.lat];
return projection(coords)[1]
})
var proportions = getProportions();
zoom(proportions);
}
//handle tooltip
function showTooltip(data){
var coordinates = data.geometry.coordinates;
var tooltip = d3.select(".pointsContainer")
.append("g")
.attr("class", "map-tooltip")
var rect = tooltip
.append("rect")
.attr("y", projection([coordinates.lat, coordinates.lng])[1] + 10 )
.attr("x", projection([coordinates.lat, coordinates.lng])[0] - 20 )
.attr("width", 60)
.attr("height", 20)
.attr("fill", "#303132")
.attr("rx", 1)
.attr("ry", 1)
var text = tooltip.append("text")
.attr("fill", "#fff")
.attr("y", projection([coordinates.lat, coordinates.lng])[1] + 22 )
.attr("x", projection([coordinates.lat, coordinates.lng])[0] - 17 )
.attr("font-size", "8px")
.text(data.properties.name)
var traingle = tooltip
.attr("fill", "#303132")
.append('path')
.attr('d', function(d) {
var x = projection([coordinates.lat, coordinates.lng])[1],
y = projection([coordinates.lat, coordinates.lng])[0] + 6.5;
return 'M ' + x +' '+ y + ' l 4 4 l -8 0 z';
});
}
function hideTooltip(){
d3.select(".map-tooltip").remove();
}
//get new proportions by location coordinates
function getProportions(){
var points = d3.selectAll(".location")[0];
var maxX = d3.max(points, function(d) { return d.cx.baseVal.value;} );
var minX = d3.min(points, function(d) { return d.cx.baseVal.value;} );
var maxY = d3.max(points, function(d) { return d.cy.baseVal.value;} );
var minY = d3.min(points, function(d) { return d.cy.baseVal.value;} );
return {maxX: maxX, minX: minX, maxY: maxY, minY: minY};
}
//translate and zoom to new proportions
function zoom(proportions){
if(proportions.maxX != undefined){
g.transition()
.duration(1500)
.attr("transform", "translate("+[
-proportions.minX + getPaddingX(proportions),
-proportions.minY + getPaddingY(proportions)
]+")"+getScale())
}
function getScale(){
if(locations.length == 1){
return "scale(2)";
}else{
if(proportions.maxX - proportions.minX < 65){
return "scale(2)"
}else if(proportions.maxX - proportions.minX > width){
return "scale(0.5)"
}else{
return "";
}
}
}
function getPaddingY(proportions){
//single location
if(locations.length == 1){
if(proportions.maxX - proportions.minX > width){
return 50;
}else{
return 20;
}
}
//multiple locations
else{
if(proportions.maxX - proportions.minX > width){
return 50;
}else{
return 0
}
}
}
function getPaddingX(proportions){
var loc = locations[0];
var leftX = projection([loc[0], loc[1]])[0];
var rightX = projection([loc[0], loc[1]])[1];
//single location
if(locations.length == 1){
if(loc[0] < 0){
return leftX + width > 200 ? 100 : 20;
}else{
if(rightX < 20){
return -150;
}else{
return -100;
}
}
//multiple locations
}else{
if(proportions.maxX - proportions.minX < width && proportions.maxX > width){
return -150
}else if(proportions.maxX - proportions.minX < 50){
return -40;
}else{
return 20;
}
}
}
}
//handle projection interpolation
function interpolatedProjection(a, b) {
var projection = d3.geo.projection(raw).scale(0.5),
center = projection.center,
translate = projection.translate,
α;
function raw(λ, φ) {
var pa = a([λ *= 180 / Math.PI, φ *= 180 / Math.PI]), pb = b([λ, φ]);
return [(1 - α) * pa[0] + α * pb[0], (α - 1) * pa[1] - α * pb[1]];
}
projection.alpha = function(_) {
if (!arguments.length) return α;
α = +_;
var ca = a.center(), cb = b.center(),
ta = a.translate(), tb = b.translate();
center([(1 - α) * ca[0] + α * cb[0], (1 - α) * ca[1] + α * cb[1]]);
translate([(1 - α) * ta[0] + α * tb[0], (1 - α) * ta[1] + α * tb[1]]);
return projection;
};
delete projection.scale;
delete projection.translate;
delete projection.center;
return projection.alpha(0);
}
var globe = {type: "Sphere"},
land = topojson.feature(world, world.objects.land),
countries = topojson.feature(world, world.objects.countries).features,
boundary = topojson.mesh(world, world.objects.countries, function(a, b) { return a !== b; });
var t = textures.circles().lighter().size(3).fill("#e1e1e1");
g.call(t);
//append countries path
feature
.data(countries)
.enter()
.append("path")
.attr("d", path(land))
.style("fill", t.url())
.attr("stroke", "#e1e1e1");
}
rotate: true;
componentDidMount(){
this.firstTime = true;
this.initChart();
}
componentWillReceiveProps(nextProps) {
if(this.props.appGeography !== nextProps.appGeography) {
this.initChart();
}
}
}