-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
109 lines (86 loc) · 2.21 KB
/
index.js
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
var d3 = require('d3')
, Tip = require('tip')
var farmland = {
type: 'FeatureCollection',
features: [{
type: 'Feature',
id: '01',
properties: { name: 'Farmland' },
geometry: {
type: 'Polygon',
coordinates: [
require('./farmland-outline-geo')
]
}
}]
}
var projection = d3.geo.mercator()
.center([25.077496, 35.304934])
.scale(window.innerWidth * 5353)
projection.translate([window.innerWidth / 2, window.innerHeight / 2])
var svg = d3.select('body').append('svg')
.attr('id', 'map')
.attr('width', '100%')
.attr('height', '100%')
.call(d3.behavior.zoom()
.translate(projection.translate())
.scale(projection.scale())
.on("zoom", redraw))
svg.append('rect')
.attr('width', '100%')
.attr('height', '100%')
.attr('opacity', '0')
var path = d3.geo.path().projection(projection)
var group = svg.append('g')
.attr('id', 'farmland')
group.append('path')
.attr('id', 'farmland-path')
.datum(farmland)
.attr('d', path)
var trees = require('./trees-geo')
var treesGroup = group.append('g').attr('id', 'trees-group')
treesGroup
.selectAll('circle').data(trees)
.enter().append('circle')
.attr('class', 'tree')
.attr('id', function (c) { return 'tree' + c.id })
.on('click', function (c) {
if (c.tip) {
c.tip.remove()
delete c.tip
}
else {
c.tip = (new Tip('tree ' + c.id)).position('south').show(this)
}
})
var treeRadiusScale = d3.scale.linear()
treeRadiusScale
.domain([projection.scale(), projection.scale() * 10])
.range([5, 30])
redraw()
function redraw() {
var scale, translate
if (d3.event) {
scale = d3.event.scale
translate = d3.event.translate
}
else {
scale = projection.scale()
translate = projection.translate()
}
if (d3.event) {
projection
.translate(translate)
.scale(scale)
}
// force re-projection
group.selectAll('path').attr('d', path)
var trees = group.selectAll('.tree')
trees
.attr('cx', function (c) { return projection(c.coordinates)[0] })
.attr('cy', function (c) { return projection(c.coordinates)[1] })
.attr('r', treeRadiusScale(scale))
.each(function (c) {
c.tip && c.tip.reposition()
})
}