comparison src/DML/MainVisBundle/Resources/assets/marionette/modules/GraphicsRenderingModule/GraphicsRenderingModule.20-Renderer.geography.js @ 0:493bcb69166c

added public content
author Daniel Wolff
date Tue, 09 Feb 2016 20:54:02 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:493bcb69166c
1 "use strict";
2
3 App.module("GraphicsRenderingModule", function(GraphicsRenderingModule, App, Backbone, Marionette, $, _, Logger) {
4
5 GraphicsRenderingModule.addInitializer(function(options){
6
7 // Map data source: http://data.okfn.org/data/datasets/geo-boundaries-world-110m#resource-countries
8 GraphicsRenderingModule.registerRenderer({
9 id: "geography",
10 inherit: "_",
11
12 defaultVegaConfig: {
13 comparisonMode: null,
14
15 transformPropertiesByRegion: {
16 "planet": {
17 defaultScale: 500,
18 translateProportionX: 1/2,
19 translateProportionY: 1/2,
20 circleSizeMult: 20
21 },
22 "africa": {
23 defaultScale: 100,
24 translateProportionX: 0.20,
25 translateProportionY: .5,
26 circleSizeMult: 50
27 },
28 "europe": {
29 defaultScale: 70,
30 translateProportionX: 0.20,
31 translateProportionY: 1.90,
32 circleSizeMult: 50
33 }
34 },
35
36 symbolSize: 20,
37 internalSpaceOffset: 6,
38 colorForData: "#3182bd",
39
40 padding: {"top": 10, "left": 10, "bottom": 10, "right": 10},
41 },
42
43
44 _formVC: function(vc, data) {
45 vc.width = vc.totalWidth - vc.padding.left - vc.padding.right;
46 vc.height = vc.totalHeight - vc.padding.top - vc.padding.bottom;
47
48
49 // :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
50 // Data
51
52 var countryStatsByCountryNumericCode = {};
53 var hist = data.self.stats ? data.self.stats.hist : null;
54 if (!hist) {
55 hist = {
56 counts: [],
57 places: []
58 }
59 }
60
61 var countTotal = data.self.coverage.errors_count + data.self.coverage.failed_count;
62 var countFailures = countTotal;
63
64 for (var i = hist.counts.length - 1; i >= 0; --i) {
65 var countryNumericCode = vc.placeCountryNumericCodes[hist.places[i]];
66 countTotal += hist.counts[i];
67 if (!countryNumericCode) {
68 countFailures += hist.counts[i];
69 continue;
70 }
71 var country = vc.countriesByCountryNumericCode[countryNumericCode];
72
73 var countryStats = countryStatsByCountryNumericCode[countryNumericCode];
74 if (!countryStats) {
75 countryStats = {
76 numericCode: country[1],
77 name: country[2],
78 count: 0
79 };
80 countryStatsByCountryNumericCode[countryNumericCode] = countryStats;
81 }
82 countryStats.count += hist.counts[i];
83 }
84
85 var countryStats = _.values(countryStatsByCountryNumericCode);
86
87 for (var i = countryStats.length - 1; i >= 0; --i) {
88 var count = countryStats[i].count;
89 var percentage = 100 * count / countTotal;
90 countryStats[i].percentage = percentage;
91 }
92
93 // .............................................................
94 // Data - points
95 vc.data.push({
96 "name": "countries",
97 "values": vc.countries
98 });
99
100 // .............................................................
101 // Data - country outlines
102 vc.data.push({
103 "name": "countryOutlines",
104 "values": vc.countryOutlines,
105 "format": {
106 "type": "topojson",
107 "feature": "countries"
108 }
109 });
110
111 // .............................................................
112 // Data - country stats
113 vc.data.push({
114 "name": "countryStats",
115 "values": countryStats
116 });
117
118 // .............................................................
119 // Data - country summary
120 var transformProperties = vc.transformPropertiesByRegion[vc.region];
121 vc.data.push({
122 "name": "countrySummary",
123 "source": "countryOutlines",
124 "transform": [
125 {
126 "type": "geopath",
127 "value": "data",
128 "projection": "eckert3",
129 "scale": 100 / transformProperties.defaultScale * vc.width,
130 "translate": [
131 vc.width * transformProperties.translateProportionX,
132 vc.height * transformProperties.translateProportionY
133 ]
134 },
135 { "type": "lookup", "on": "countryStats", "onKey": "numericCode",
136 "keys": ["id"], "as": ["cs"], "default": {"count": 0, "percentage": 0}},
137
138 { "type": "lookup", "on": "countries", "onKey": "1",
139 "keys": ["id"], "as": ["tooltip"], "default": null}
140 ]
141 });
142
143
144 // :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
145 // Scales
146
147 // .............................................................
148 // Scale - color
149 vc.scales.push({
150 "name": "color",
151 "type": "linear",
152 "domain": [0, 0.01, 50],
153 "clamp": true,
154 "range": ["#ccc", "#c6dbef", "#055893"]
155 });
156
157
158 // :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
159 // Marks
160
161 // .............................................................
162 // Mark - background (to catch mouse events and remove tooltips)
163 vc.marks.push({
164 "type": "rect",
165 "properties": {
166 "enter": {
167 "x": {"value": -vc.padding.left},
168 "y": {"value": -vc.padding.top},
169 "fill": {"value": "#fff"},
170 "y2": {"field": {"group": "height"}, "offset": vc.padding.left + vc.padding.right},
171 "x2": {"field": {"group": "width"}, "offset": vc.padding.top + vc.padding.bottom},
172 }
173 }
174 });
175
176
177 // .............................................................
178 // Mark - countries as polygons
179 if (!vc.showCountriesAsCircles) {
180 vc.marks.push({
181 "type": "path",
182 "from": {
183 "data": "countrySummary",
184 },
185 "properties": {
186 "enter": {
187 "stroke": {"value": "#fff"},
188 "path": {"field": "layout_path"}
189 },
190 "update": {
191 "fill": {"scale": "color", "field": "cs.percentage"}
192 },
193 "hover": {
194 "fill": {"value": "#000"},
195 }
196 }
197 });
198
199 // .............................................................
200 // Mark - countries as circles
201 } else {
202 vc.marks.push({
203 "type": "path",
204 "from": {
205 "data": "countrySummary",
206 },
207 "properties": {
208 "enter": {
209 "stroke": {"value": "#fff"},
210 "path": {"field": "layout_path"}
211 },
212 "update": {
213 "fill": {"value": "#f0f0f0"}
214 }
215 }
216 });
217 vc.marks.push({
218 "type": "symbol",
219 "from": {
220 "data": "countrySummary",
221 "transform": [{"type":"centroid", "field": "layout_path"}],
222 },
223 "properties": {
224 "enter": {
225 //"size": {/*"scale": "color",*/ "value": 100},
226 "size": {/*"scale": "color",*/ "field": "cs.percentage", "mult": transformProperties.circleSizeMult},
227 "opacity": {"value": 0.5},
228 "x": {"field": "centroid_x"},
229 "y": {"field": "centroid_y"}
230 },
231 "update": {
232 "fill": {"value": vc.colorForData}
233 },
234 "hover": {
235 "fill": {"value": "#000"},
236 }
237 }
238 });
239 }
240 },
241 });
242 });
243 }, Logger);