-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathD33.html
197 lines (150 loc) · 5.97 KB
/
D33.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
---
layout: page
---
<body>
<div id="D3background" style= "padding-bottom: 15px;">
<h3>Global Cocaine Prices</h3>
<button type="button" class="btn btn-primary btn-lg pull-center" id="retailButton">Retail</button>
<button type="button" class="btn btn-danger btn-lg pull-center" id="wholesaleButton">Wholesale</button>
<div id="map"></div>
</div>
<script>
var world, cocaine;
var selector = "retail";
// document.addEventListener('DOMcontentLoaded', function(){
$("#retailButton").on("click", function(){
selector = "retail";
cocaineMap();
});
$("#wholesaleButton").on("click", function(){
selector = "wholesale";
cocaineMap();
});
// })
var svg = d3.select("#map").append("svg").attr("viewBox", "0 0 1000 500");
var projection = d3.geo.robinson().translate([500, 250]);
var path = d3.geo.path().projection(projection);
var colorRetail = d3.scale.linear()
.domain([10, 1200])
.range(["#f89d61","#cc3300"]);
var colorWholesale = d3.scale.linear()
.domain([10, 200000])
.range(["#cc3366","#660033"]);
//var retailArray = [];
//var wholesaleArray = [];
function cocaineMap(){
var cokeObj = {};
for (i=0; i < cocaine.length; i++){
var datum = cocaine[i];
var name = datum["Country name"];
var code = datum["Alpha-2"];
var retail = datum["Retail price"];
var wholesale = datum["Wholesale price"];
cokeObj[code] = {
name: name,
code: code,
retail: retail,
wholesale: wholesale
};
}
function fillColor(id){
var prices = cokeObj[id];
console.log(prices);
if (typeof prices === "undefined"){
return ("fill", "#A6A6A6");
}
if ( prices && selector === "retail"){
return colorRetail(prices.retail*15);
}
if ( prices.wholesale === ""){
return ("fill", "#A6A6A6");
}
if ( prices && selector === "wholesale"){
return colorWholesale(prices.wholesale*3);
}
}
var countries = topojson.feature(world, world.objects["world.geo"]).features;
for (var i=0; i<countries.length; i++) {
svg.append("path")
//.data(cokeObj
.attr("id", countries[i].id)
.attr("d", path(countries[i]))
.attr("fill", fillColor(countries[i].id));
}
}
d3.json("/js/world.json", function(loadWorld) {
world = loadWorld;
if (typeof cocaine !== "undefined"){
cocaineMap();
}
});
d3.csv("/js/cocaine-per-gram.csv", function(loadData){
cocaine = loadData;
if(typeof world !== "undefined") {
cocaineMap();
}
});
</script>
</body>
<!--<script>
//SVG simiar to a DIV - its a <tag> its used to define shapes using path elements
//Think about it as a canvas, each point/co-ordinate in this case creating a map.
var svg = d3.select("#map").append("svg").attr("viewBox", "0 0 1000 500");
//projection is a way of laying out a 3D object into 2D plain. Most commonly used in maps.
var projection = d3.geo.robinson().translate([500, 250]);
//you will be using paths in the future and you will use the projection we noted the step before
var path = d3.geo.path().projection(projection);
var world, cocaine, countries;
function cocaineMap(){
//topojson stores geometry data .feature returns the data you want
//in this case the it is world data - this decodes the data into a usable format
countries = topojson.feature(world, world.objects["world.geo"]).features;
console.log(countries)
//loop through all the countries
for (var i=0; i<countries.length; i++) {
//for each country append the canvas with a path element,
//set its id attribute to the countries id
//set its d attribute to the path data (d atribute sets the outline co-ordinates)
svg.append("path").attr("id", countries[i].id).attr("d", path(countries[i]))}
testingCoke ();
};
//pulls the file data, one the data is back, its passed to the loadWorld function
//variable defined line 21
//they will only happen when the data is loaded
d3.json("/js/world.json", function(loadWorld) {
world = loadWorld;
if (typeof cocaine !== "undefined") cocaineMap();
});
//pulls the file data, one the data is back, its passed to the loadWorld function
//variable World defined line 21
//they will only happen when the data is loaded,
d3.json("/js/cocaine.json", function(loadData){
cocaine = loadData;
if(typeof world !== "undefined") cocaineMap();
//which ever fucntion is loaded second push the map.
});
function testingCoke() {
//select all ["world.geo"] it is the equivialnt to the JQuery Library
svg.selectAll(".countries")
//bind the array data to the countries data -
.data(countries)
//.enter selects the bits of data that are unbound to an HTML tag
//SVG is appended path tags that are bound to the lefover data.
.enter().append("path")
//when you pass a function to this method, it pass countries with the data's id.
.attr("class",function(d) {return "countries" + d.id})
//will set the co-ordinates to the path.
.attr("d", path)
.style("fill", "white");
}
//data fetch and processing
/* var prData = [];
minRetailPrice = 100000,
maxRetailPrice = 0,
minWholesalePrice = 100000,
maxWholesalePrice = 0;
var dataArray = [20, 40, 50, 60];
var cokeData = d3.scale.linear()
.domain([minRetailPrice, maxRetailPrice])
.range(["#a6fcd1 ", "#00773d "])*/
</script>-->