1 | <!DOCTYPE html> |
---|
2 | <html> |
---|
3 | <head> |
---|
4 | <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> |
---|
5 | <title>Mercator Projection</title> |
---|
6 | <script type="text/javascript" src="../../d3.js"></script> |
---|
7 | <script type="text/javascript" src="../../d3.geo.js"></script> |
---|
8 | <script type="text/javascript" src="../../lib/jquery/jquery.min.js"></script> |
---|
9 | <script type="text/javascript" src="../../lib/jquery-ui/jquery-ui.min.js"></script> |
---|
10 | <style type="text/css"> |
---|
11 | |
---|
12 | @import url("../../lib/jquery-ui/jquery-ui.css"); |
---|
13 | |
---|
14 | body, .ui-widget { |
---|
15 | font: 14px Helvetica Neue; |
---|
16 | } |
---|
17 | |
---|
18 | svg { |
---|
19 | width: 960px; |
---|
20 | height: 500px; |
---|
21 | border: solid 1px #ccc; |
---|
22 | background: #eee; |
---|
23 | } |
---|
24 | |
---|
25 | line { |
---|
26 | stroke: brown; |
---|
27 | stroke-dasharray: 4,2; |
---|
28 | } |
---|
29 | |
---|
30 | path { |
---|
31 | fill: #ccc; |
---|
32 | stroke: #fff; |
---|
33 | } |
---|
34 | |
---|
35 | div { |
---|
36 | width: 960px; |
---|
37 | } |
---|
38 | |
---|
39 | </style> |
---|
40 | </head> |
---|
41 | <body> |
---|
42 | <h3>Mercator Projection</h3> |
---|
43 | <script type="text/javascript"> |
---|
44 | |
---|
45 | // Our projection. |
---|
46 | var xy = d3.geo.mercator(), |
---|
47 | path = d3.geo.path().projection(xy); |
---|
48 | |
---|
49 | var states = d3.select("body") |
---|
50 | .append("svg:svg") |
---|
51 | .append("svg:g") |
---|
52 | .attr("id", "states"); |
---|
53 | |
---|
54 | var equator = d3.select("svg") |
---|
55 | .append("svg:line") |
---|
56 | .attr("x1", "0%") |
---|
57 | .attr("x2", "100%"); |
---|
58 | |
---|
59 | d3.json("../data/world-countries.json", function(collection) { |
---|
60 | |
---|
61 | states |
---|
62 | .selectAll("path") |
---|
63 | .data(collection.features) |
---|
64 | .enter().append("svg:path") |
---|
65 | .attr("d", path) |
---|
66 | .append("svg:title") |
---|
67 | .text(function(d) { return d.properties.name; }); |
---|
68 | |
---|
69 | equator |
---|
70 | .attr("y1", xy([0, 0])[1]) |
---|
71 | .attr("y2", xy([0, 0])[1]) |
---|
72 | }); |
---|
73 | |
---|
74 | function refresh() { |
---|
75 | states |
---|
76 | .selectAll("path") |
---|
77 | .attr("d", path); |
---|
78 | |
---|
79 | equator |
---|
80 | .attr("y1", xy([0, 0])[1]) |
---|
81 | .attr("y2", xy([0, 0])[1]) |
---|
82 | |
---|
83 | d3.select("#scale span") |
---|
84 | .text(xy.scale()); |
---|
85 | d3.select("#translate-x span") |
---|
86 | .text(xy.translate()[0]); |
---|
87 | d3.select("#translate-y span") |
---|
88 | .text(xy.translate()[1]); |
---|
89 | } |
---|
90 | |
---|
91 | </script><p> |
---|
92 | <div id="scale">scale: <span>500</span></div><p> |
---|
93 | <div id="translate-x">translate.x: <span>480</span></div> |
---|
94 | <div id="translate-y">translate.y: <span>250</span></div> |
---|
95 | <script type="text/javascript"> |
---|
96 | |
---|
97 | $("#scale").slider({ |
---|
98 | min: 0, |
---|
99 | max: 3000, |
---|
100 | value: 500, |
---|
101 | slide: function(event, ui) { |
---|
102 | xy.scale(ui.value); |
---|
103 | refresh(); |
---|
104 | } |
---|
105 | }); |
---|
106 | |
---|
107 | $("#translate-x").slider({ |
---|
108 | min: -2000, |
---|
109 | max: 2000, |
---|
110 | value: 480, |
---|
111 | slide: function(event, ui) { |
---|
112 | var translate = xy.translate(); |
---|
113 | translate[0] = ui.value; |
---|
114 | xy.translate(translate); |
---|
115 | refresh(); |
---|
116 | } |
---|
117 | }); |
---|
118 | |
---|
119 | $("#translate-y").slider({ |
---|
120 | min: -2000, |
---|
121 | max: 2000, |
---|
122 | value: 250, |
---|
123 | slide: function(event, ui) { |
---|
124 | var translate = xy.translate(); |
---|
125 | translate[1] = ui.value; |
---|
126 | xy.translate(translate); |
---|
127 | refresh(); |
---|
128 | } |
---|
129 | }); |
---|
130 | |
---|
131 | </script> |
---|
132 | </body> |
---|
133 | </html> |
---|