forked from erispoe/sf-election-map
/
index.html
72 lines (53 loc) · 1.81 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
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.precincts {
fill: #e5e5e5;
stroke: #fff;
stroke-width: 1px;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://d3js.org/topojson.v1.min.js"></script>
<script>
var width = 900,
height = 900;
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
d3.json("geo/precincts.json", function(error, geo){
if (error) return console.error(error);
var projection = d3.geo.mercator()
.center([-122.433701, 37.767683])
.scale(250000)
.translate([width / 2, height / 2]);;
var path = d3.geo.path()
.projection(projection);
var precincts = svg.append("g")
.attr("class", "precincts");
precincts.selectAll("path")
.data(topojson.feature(geo, geo.objects.precincts).features)
.enter().append("path")
//.style("fill","DrakGrey")
.attr("d", path)
.attr("id", function(d) {return d.PREC_2012});
var bounds = path.bounds(json);
var hscale = scale*width / (bounds[1][0] - bounds[0][0]);
var vscale = scale*height / (bounds[1][1] - bounds[0][1]);
var scale = (hscale < vscale) ? hscale : vscale;
var offset = [width - (bounds[0][0] + bounds[1][0])/2,
height - (bounds[0][1] + bounds[1][1])/2];
projection = d3.geo.mercator().center(center)
.scale(scale).translate(offset);
path = path.projection(projection);
})
/*d3.json("geo/precincts.json", function(error, geo) {
if (error) return console.error(error);
svg.append("path")
.datum(topojson.feature(geo, geo.objects.SFPrecincts))
.attr("d", d3.geo.path().projection(d3.geo.mercator().scale(50)))
.attr("class","precincts")
});*/
</script>
</body>