-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
index.html
345 lines (288 loc) · 16.5 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
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
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<link href='http://fonts.googleapis.com/css?family=PT+Serif' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="bower_components/simple/stylesheets/screen.css" />
<link rel="stylesheet" type="text/css" href="bower_components/simple/stylesheets/pygment_trac.css" />
<link rel="stylesheet" href="lib/css/simple-map-d3.css">
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<title>Simple Map D3</title>
</head>
<body>
<header>
<h1>Simple Map D3</h1>
<div>
<h4>Easy "choropleth" style maps with D3.</h4>
</div>
<ul>
<li><a href="https://github.com/MinnPost/simple-map-d3">view on github</a></li>
<li><a href="https://github.com/MinnPost/simple-map-d3/zipball/master"><span>download</span> zip</a></li>
<li><a href="https://github.com/MinnPost/simple-map-d3/tarball/master"><span>download</span> tar</a></li>
</ul>
</header>
<section>
<h1>How to use</h1>
<p>All you need is a dataset. Installing with <a href="http://bower.io">Bower</a> is easy:</p>
<pre><code>bower install simple-map-d3</code></pre>
<p>Include dependencies (<a href="https://github.com/mbostock/topojson">Topojson</a> if you are using that format).</p>
<pre><code><script src="bower_components/d3/d3.min.js" charset="utf-8"></script>
<script src="bower_components/topojson/topojson.js" charset="utf-8"></script></code></pre>
<p>Include library:</p>
<pre><code><link rel="stylesheet" href="bower_components/simple-map-d3/dist/simple-map-d3.css">
<script src="bower_components/simple-map-d3/dist/simple-map-d3.js" charset="utf-8"></script></code></pre>
<p>Make a container:</p>
<pre><code><div id="simple-map-d3-example"></div></code></pre>
<p>Make a map:</p>
<pre><code>
var map = new SimpleMapD3({
container: '#simple-map-d3-example',
datasource: 'mn-county-2010.geo.json'
});
</pre></code>
<h1>Examples</h1>
<h3>Population of Minnesota by County</h3>
<p>Minnesota census data per county, colored by population.</p>
<div class="simple-map-d3-mn-pop-map" style="background-color: #FFFFFF; width: 100%; height: 360px; border: 1px solid #898989; margin: 0 0 1em 0;"></div>
<pre><code>
var mnPopMap = SimpleMapD3({
container: '.simple-map-d3-mn-pop-map',
datasource: 'example-data/mn-county-2010.geo.json',
colorOn: true,
colorProperty: 'POPULATION',
legendFormatter: d3.format(',f0')
});
</pre></code>
<h3>Europe Population Density Map</h3>
<p>A simple map of European countries and their population density using a different projection, a slight rotation, custom color set, ability to pan, and a custom tooltip output.</p>
<div class="simple-map-d3-europe-pop-map" style="background-color: #FFFFFF; width: 100%; height: 500px; border: 1px solid #898989; margin: 0 0 1em 0;"></div>
<pre><code>
var europePopMap = SimpleMapD3({
container: '.simple-map-d3-europe-pop-map',
datasource: 'example-data/europe-population-density-geocommons.geo.json',
colorSet: 'Spectral',
colorOn: true,
colorProperty: 'population',
colorReverse: true,
projection: 'azimuthalEqualArea',
rotation: [0, 0, -20],
canvasDragOn: true,
tooltipContent: function(d) {
var p = d.properties;
return '<h5>' + p.country + '</h5>' +
p.population + ' population per square kilometer';
}
});
</pre></code>
<h3>Basic US Map</h3>
<p>A simple map of US counties using a Topojson data source with some custom styling.</p>
<div class="simple-map-d3-us-map" style="background-color: #FFFFFF; width: 100%; height: 300px; border: 1px solid #898989; margin: 0 0 1em 0;"></div>
<pre><code>
var usMap = SimpleMapD3({
container: '.simple-map-d3-us-map',
datasource: 'example-data/us-counties.topo.json',
tooltipOn: false,
styles: {
stroke: '#EDEDED',
fill: '#232323'
}
});
</pre></code>
<h3>World Map</h3>
<p>A simple map of 2005 world population with graticule and globe turned on and started "manually".</p>
<div class="simple-map-d3-world-map" style="background-color: #FFFFFF; width: 100%; height: 400px; border: 1px solid #898989; margin: 0 0 1em 0;"></div>
<pre><code>
var worldMap = SimpleMapD3({
container: '.simple-map-d3-world-map',
datasource: 'example-data/world-population.geo.json',
projection: 'equirectangular',
colorOn: true,
colorProperty: 'POP2005',
colorSet: 'Paired',
colorScale: 'quantize',
tooltipOn: true,
graticuleOn: true,
globeOn: true,
legendOn: false,
startManually: true
}).start();
</code></pre>
<h1>Data sources</h1>
<p>Simple Map supports any latitude and longitude (EPSG:4326) based GeoJSON or TopoJSON file.</p>
<h1>Options</h1>
<p>The following is a list of supported options when using Simple Map.</p>
<ul>
<li><code>container</code>: The DOM selector for the DOM element that will hold the map. This is required. <br> Type: <em>string</em>.</li>
<li><code>datasource</code>: String location of json file to use. This is required, unless you use the <code>data</code> property. <br> Type: <em>string</em>.</li>
<li><code>data</code>: Instead of using a JSON file source with the <code>datasource</code> property, you can directly use a dataset. <br> Type: <em>object</em>.</li>
<li><code>colorOn</code>: Whether to process fill colors. Use with <code>colorSet</code> and <code>colorProperty</code>. <br> Type: <em>boolean</em>; default: <em>false</em>.</li>
<li><code>colorSet</code>: An array of valid color strings or a string referring to a <a href="http://colorbrewer2.org/">ColorBrewer.org</a> color set; see below for keys to use. <br> Type: <em>array or string</em>; default: <em>a yellow, orange, brown combination</em>.</li>
<li><code>colorProperty</code>: The property that will be used to determine the fill color. This is a property found in the <code>properties</code> object of each feature in the GeoJSON file. This will depend on your data. <br> Type: <em>string</em>.</li>
<li><code>colorReverse</code>: Reverses the color set; mostly useful if using an existing ColorBrewer set. <br> Type: <em>boolean</em>; default <em>false</em>.</li>
<li><code>colorScale</code>: The scale function to use, either a custom function, or a valid <a href="https://github.com/mbostock/d3/wiki/Quantitative-Scales">D3 scale</a> such as <em>quantile</em> or <em>linear</em>. This will affect how colors are matched to values. <br> Type: <em>string or function</em>: default: <em>quantile</em>.</li>
<li><code>tooltipOn</code>: Whether to add a tooltip. <br> Type: <em>boolean</em>; default: <em>true</em>.</li>
<li><code>tooltipContent</code>: Function that takes the data item as the argument, where <code>d.properties</code> are the GeoJSON properties of the feature, and returns HTML to fill in the tooltip. <br> Type: <em>function</em>; default: <em>displays all properties</em>.</li>
<li><code>canvasDragOn</code>: Enables dragging and panning for the map from the whole canvas. this is a useful interaction for users. This will interfere with the other dragging options. <br> Type: <em>boolean</em>; default: <em>false</em>.</li>
<li><code>mapDragOn</code>: Enables dragging and panning of just the map. Useful for placement of the map through an interface. <br> Type: <em>boolean</em>; default: <em>false</em>.</li>
<li><code>legendDragOn</code>: Enables dragging and panning of just the legend. Useful for placement of the legend through an interface. <br> Type: <em>boolean</em>; default: <em>false</em>.</li>
<li>There are a number of style configuration options that will be set on SVG objects. It is suggested to use CSS for these sorts of thing, but there are use-cases for direct setting of the styles. The following are the available styles: <ul>
<li><code>styles</code>: Styles for each map feature. The fill will be overrided if color is on.</li>
<li><code>stylesHover</code>: Styles to apply to hover state of map feature.</li>
<li><code>stylesBackground</code>: Styles for a background container.</li>
<li><code>stylesLegendContainer</code>: Styles for legend container.</li>
<li><code>stylesLegendTitleText</code>: Styles for legend title text.</li>
<li><code>stylesLegendText</code>: Styles for each legend item text.</li>
<li><code>stylesLegendSwatch</code>: Styles for each legend item color swatch.</li>
<li><code>stylesGraticule</code>: Styles for the graticule.</li>
<li><code>stylesGlobe</code>: Styles for the globe.</li>
</ul>
<li><code>projection</code>: The name of the <a href="https://github.com/mbostock/d3/wiki/Geo-Projections#standard-projections">projection function provided by D3</a>. Projections describe how to display geographical information and will drastically change how the map looks. <br> Type: <em>string</em>; default: <em>albersUSA</em>.</li>
<li><code>rotation</code>: The rotation array to use with the projection. See the <a href="https://github.com/mbostock/d3/wiki/Geo-Paths#wiki-rotation">D3 documentation</a> for how it works. For example, if you just want to rotate around the center of the map 30 degrees, pass <code>[0, 0, 30]</code>. <br> Type: <em>array</em>.</li>
<li><code>legendOn</code>: Turns legend on, though not all scales support a legend at the moment. <br> Type: <em>boolean</em>; default: <em>true</em>.</li>
<li><code>legendTitle</code>: The title for the legend box. <br> Type: <em>string</em>; default: <em>Legend</em>.</li>
<li><code>legendFormatter</code>: Function for formatting numbers in legend. <br> Type: <em>function</em>; default: <em>d3.format(',')</em>.</li>
<li><code>legendWidth</code>: Width of legend box. <br> Type: <em>string</em>; default: <em>container width / 5</em>.</li>
<li><code>legendScale</code>: Scale of legend box. <br> Type: <em>number</em>; default: <em>1</em>.</li>
<li><code>graticuleOn</code>: Adds a simple <a href="http://en.wikipedia.org/wiki/Geographic_coordinate_system">graticule grid</a>. <br> Type: <em>boolean</em>; default: <em>false</em>.</li>
<li><code>globeOn</code>: Adds a container around the globe that could be used for an outline. <br> Type: <em>boolean</em>; default: <em>false</em>.</li>
<li><code>startManually</code>: Whether to automatically start the data and rendering process. If false, the configuration will be loaded, but data will not be loaded or the map will not be rendered. This can be helpful in order register events properly. <br> Type: <em>boolean</em>; default: <em>false</em>.</li>
<li><code>mapOffset</code>: An x, y array that offsets the map from the top left. <br> Type: <em>array</em>; default: <em>[0, 0]</em>.</li>
<li><code>legendOffset</code>: An x, y array that offsets the legend from the top left. <br> Type: <em>array</em>; default: <em>[10, 10]</em>.</li>
</ul>
<h2>Color set reference</h2>
<table class="color-set-reference">
<thead>
<tr><th>Key</th><th>Colors</th></tr>
</thead>
<tbody>
</tbody>
</table>
<h2>Events</h2>
<p>There are a couple custom events that are fired through the map rendering process that may be helpful. These are created with the <a href="https://github.com/mbostock/d3/wiki/Internals#wiki-dispatch">D3.dispatch system</a>, and internally under <code>.events</code> object.</p>
<ul>
<li><code>dataLoaded</code>: This event is trigger once the data is loaded, if you are not using a data source, this will be very instant. Use it like so: <br />
<pre><code>
var europePopMap = SimpleMapD3({ ... });
europePopMap.events.on('dataLoaded.YOUR_NAMESPACE', function(smd) {
// Do your thing
});
</code></pre>
</li>
<li><code>rendered</code>: This event is triggered after everything has been rendered and is a good place to do any modifications to the map: <br />
<pre><code>
var europePopMap = SimpleMapD3({ ... });
europePopMap.events.on('rendered.YOUR_NAMESPACE', function(smd) {
// Do your thing
});
</code></pre>
</li>
</ul>
<p></p>
<footer>
Simple Map D3 is maintained by Alan Palazzolo and <a href="//minnpost.com">MinnPost</a>.
</footer>
</section>
<script type="text/javascript" src="bower_components/jquery/jquery.min.js"></script>
<script type="text/javascript" src="bower_components/d3/d3.min.js"></script>
<script type="text/javascript" src="bower_components/topojson/topojson.js"></script>
<script type="text/javascript" src="lib/js/simple-map-d3.js"></script>
<script type="text/javascript">
(function($) {
$(document).ready(function() {
var mnPopMap = SimpleMapD3({
container: '.simple-map-d3-mn-pop-map',
datasource: 'example-data/mn-county-2010.geo.json',
colorOn: true,
colorProperty: 'POPULATION',
legendFormatter: d3.format(',f0')
});
var usMap = SimpleMapD3({
container: '.simple-map-d3-us-map',
datasource: 'example-data/us-counties.topo.json',
tooltipOn: false,
styles: {
stroke: '#EDEDED',
fill: '#232323'
}
});
var europePopMap = SimpleMapD3({
container: '.simple-map-d3-europe-pop-map',
datasource: 'example-data/europe-population-density-geocommons.geo.json',
colorSet: 'Spectral',
colorOn: true,
colorProperty: 'population',
colorReverse: true,
projection: 'azimuthalEqualArea',
rotation: [0, 0, -20],
canvasDragOn: true,
tooltipContent: function(d) {
var p = d.properties;
return '<h5>' + p.country + '</h5>' +
p.population + ' population per square kilometer';
}
});
var worldMap = SimpleMapD3({
container: '.simple-map-d3-world-map',
datasource: 'example-data/world-population.geo.json',
projection: 'equirectangular',
colorOn: true,
colorProperty: 'POP2005',
colorSet: 'Paired',
colorScale: 'quantize',
tooltipOn: true,
graticuleOn: true,
globeOn: true,
legendOn: false,
startManually: true
}).start();
// Basic test to ensure that a blank config will be ok
var mapBlank = SimpleMapD3({ });
// Sample event
europePopMap.events.on('dataLoaded.TEST', function() {
//console.log(arguments, this);
});
europePopMap.events.on('rendered.TEST', function() {
//console.log(arguments, this);
});
// Generate colors references (very hacky)
var smd = SimpleMapD3();
var $csContainer = $('.color-set-reference tbody');
var s, c, $row, $colors;
for (s in smd.brewer) {
if (smd.brewer.hasOwnProperty(s)) {
$colors = $('<td></td>');
for (c = 0; c < smd.brewer[s].length; c++) {
$('<div>').css({
'background-color': smd.brewer[s][c],
width: '1.5em',
height: '1.5em',
float: 'left',
'margin-right': '.25em'
}).appendTo($colors);
}
$row = $('<tr></tr>');
$row.append('<td>' + s + '</td>');
$row.append($colors);
$csContainer.append($row);
}
}
});
})(jQuery);
</script>
<!-- Google analytics -->
<script type="text/javascript">
if (window.location && window.location.hostname &&
window.location.hostname.toLowerCase().indexOf('minnpost.com') !== -1) {
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-3385191-3', 'auto');
ga('send', 'pageview');
}
</script>
</body>
</html>