-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
182 lines (143 loc) · 4.26 KB
/
index.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
<!--colors:
alphabet Purps
cyrillic, latin, hangul, other alphabets
logographic, syllabaries blues
chinese, kana, other syllabaries
abjad magentas
Devanagari, Bengali, other
abugida yellows
Arabic, other
darkerpurp 330969
darkerblue 005C8A
gold E3D000
magent B551A2
darkpurp 846CA3
brightblue 83C4E6
yellow EDE69A
desatpink E3BFDC
blue 2EA1DB
desatyellow E3E1D1
-->
<!DOCTYPE html>
<html>
<head>
<title>Scriptmap</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<style type="text/css">
body {
font: 300 13px "Helvetica Neue", Helvetica;
}
#chart {
max-width: 800px;
margin: 0 auto;
}
path { fill: #CCC; stroke: #FFF; }
circle { stroke-width:0.5px; vector-effect:non-scaling-stroke }
circle.f { stroke:#ffffff; fill-opacity: 0.8}
circle.m { stroke:#ffffff; fill-opacity: 0.8}
</style>
</head>
<body>
<div id="chart"></div>
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.js?2.5.0"></script>
<script type="text/javascript">
// Pretty circles --------->
color = d3.scale.linear()
.domain([12, 30])
.range(["red", "green"])
size = d3.scale.linear()
.domain([0, 10]);
// <----- Pretty circles
var countryFeature;
var datacentreFeature;
var projection = d3.geo.azimuthal()
.scale(380)
.origin([-71.03,42.37])
.mode('orthographic')
.translate([400, 400]);
//640 400
var circle = d3.geo.greatCircle().origin(projection.origin());
// TODO fix d3.geo.azimuthal to be consistent with scale
var scale = { orthographic: 380, stereographic: 380, gnomonic: 380,
equidistant: 380 / Math.PI * 2, equalarea: 380 / Math.SQRT2};
var path = d3.geo.path().projection(projection);
var svg = d3.select("#chart").append('svg:svg')
.attr('width', 1280)
.attr('height', 800)
.on('mousedown', mousedown);
var countries = svg.append('g')
.attr('width', 1280)
.attr('height', 800)
.attr('id', 'countries');
var datacentres = svg.append('g')
.attr('width', 1280)
.attr('height', 800)
.attr('id', 'datacentres');
d3.json('world-countries.json', function(collection) { countryFeature = countries.selectAll('path')
.data(collection.features)
.enter().append('svg:path')
.attr('d', clip);
countryFeature.append("svg:title").text(function(d) { return d.properties.name; }).attr('text-anchor', 'middle');
});
d3.json('datacentres.json', function(json) {
datacentreFeature = datacentres.selectAll('g')
.data(json.features)
.enter()
.append('g')
.append('svg:circle')
.attr('class', function(d) { return assignClass(d.properties.kgCO2e); })
.attr('r', function(d) { return 10; })
//return size(ballSize(d.properties.kgCO2e))
.attr("fill", function(d) { return color((10 * Math.exp((1 - d.properties.kgCO2e), 2))); });
datacentreFeature = datacentres.selectAll('g');
});
d3.select(window).on('mousemove', mousemove)
.on('mouseup', mouseup);
d3.select('select').on('change',
function() {
projection.mode(this.value).scale(scale[this.value]);
refresh(750);
});
var m0, o0;
function mousedown() {
m0 = [d3.event.pageX, d3.event.pageY];
o0 = projection.origin();
d3.event.preventDefault();
}
function mousemove() {
if (m0) {
var m1 = [d3.event.pageX, d3.event.pageY],
o1 = [o0[0] + (m0[0] - m1[0]) / 8, o0[1] + (m1[1] - m0[1]) / 8];
projection.origin(o1);
circle.origin(o1)
refresh();
}
}
function mouseup() {
if (m0) {
mousemove();
m0 = null;
}
}
function refresh(duration) {
(duration ?
countryFeature.transition().duration(duration)
: countryFeature)
.attr('d', clip);
(duration ?
datacentreFeature.transition().duration(duration)
: datacentreFeature)
.attr('transform', function (d) { return "translate(" + clip(d) + ")"; });
}
function clip(d) {
return path(circle.clip(d));
}
function ballSize (datum) {
return (datum.kgCO2e > 1) ? 10 : (10 * Math.exp((1 - datum.kgCO2e), 2));
}
function assignClass (datum) {
return (datum.kgCO2e > 0.2) ? "f" : "m";
}
</script>
</body>
</html>