-
Notifications
You must be signed in to change notification settings - Fork 0
/
D3_22.html
175 lines (142 loc) · 5.63 KB
/
D3_22.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="./D3.js" charset="utf-8"></script>
<script src="http://d3js.org/topojson.v1.min.js"></script>
<style>
html, body {
-webkit-user-select: none;
user-select: none;
}
.province{
stroke:'#ccc';
stroke-width:1px;
}
.centroid{
fill:#ccc;
}
.boundingbox{
fill:none;
stroke:#ccc;
stroke-width:1px;
}
.overlay{
fill: none;
pointer-events: all;
}
</style>
</head>
<body>
</body>
<script charset="utf-8">
var width = 500;
var height = 500;
var svg = d3.select('body').append('svg').attr('width',width).attr('height',height);
var projection = d3.geoOrthographic().translate([width/2,height/2]);
var path = d3.geoPath(projection);
var speed = 1e-2;
var start = Date.now();
var centered = '';
var colors = d3.scaleOrdinal(d3.schemeCategory10);
var geoGraticule = d3.geoGraticule();//设置地理网格生成器
var grid = geoGraticule();//获取地理网格数据信息
var mapLines = svg.append('path').datum(grid)//绘制地理网格
.attr('stroke','#ccc')
.attr('stroke-width','1px')
.attr('fill','none')
.attr('d',path)
.attr('marker-end','url(#arrow)')
.attr('marker-start','url(#startpoint)');
var pekingToWashington = {//从华盛顿到北京的连线数据
type: "LineString",
coordinates: [[ 116.4, 39.9],[-77.0, 38.9]]
};
var bjData = {//目标图片信息数组
"name": "地 点",
"location": [
{ "name":" 北 京", "log":"116.3", "lat":"39.9", "img":"img/bj.jpg" },
{ "name":" 上 海", "log":"121.4", "lat":"31.2", "img":"img/bj.jpg" }
]
}
d3.json('world.topojson').then((root,error) => {
var georoot = topojson.feature( root, root.objects.countries);
var groups = svg.append('g');
var map = groups.selectAll('path').data(georoot.features).enter().append('path').attr('class','province').style('fill',function(d,i){
return colors(i);
}).attr('d',path).on('click',clicked);
var initRotate = projection.rotate();
var initScale = projection.scale();
var mapArrow = svg.append('path')//绘制华盛顿到北京连线
.attr('stroke','#333')
.attr('stroke-width','3px')
.attr('fill','none')
.attr('d',path(pekingToWashington));
var location = svg.selectAll('.location').data(bjData.location).enter().append('g').attr('transform',function(d){//绘制坐标位置
var coor = projection([d.log,d.lat]);
return 'translate('+coor[0]+','+coor[1]+')';
});
location.append('circle').attr('r',5).attr('fill','#333');//以圆点表示目标坐标位置
location.append('image')//绘制目标图片
.attr('x',20)
.attr('y',-40)
.attr('width',90)
.attr('height',90)
.attr('xlink:href',function(d){
return d.img;
});
var zoom = d3.zoom().scaleExtent([1,10]).on('zoom',function(d){
projection.rotate([
initRotate[0] + 180 * d3.event.transform.x / width,
initRotate[1] + -180 * d3.event.transform.y / height,
initRotate[2]
]);
projection.scale(initScale * d3.event.transform.k);
map.attr('d',path);
mapLines.attr('d',path);//更新绘制地图网格
mapArrow.attr('d',path(pekingToWashington));//更新绘制华盛顿到北京连线
location.attr('transform',function(d){
var coor = projection([d.log,d.lat]);
return 'translate('+coor[0]+','+coor[1]+')';
})
});
svg.append('rect').attr('class','overlay').attr('x',0).attr('y',0).attr('width',width).attr('height',height).call(zoom);
// d3.timer(function() {
// projection.rotate([speed * (Date.now() - start),0,0]);
// groups.selectAll('path')
// .data(georoot.features)
// .attr('d',path);
// mapLines.attr('d',path);//更新绘制地图网格
// mapArrow.attr('d',path(pekingToWashington));//更新绘制华盛顿到北京连线
// location.attr('transform',function(d){
// var coor = projection([d.log,d.lat]);
// return 'translate('+coor[0]+','+coor[1]+')';
// })
// },0);
function clicked(d) {
var x, y, k;
if (d && centered !== d) {
var centroid = path.centroid(d);
x = centroid[0];
y = centroid[1];
k = 4;
centered = d;
} else {
x = width / 2;
y = height / 2;
k = 1;
centered = null;
}
map.selectAll("path")
.classed("active", centered && function(d) { return d === centered; });
map.transition()
.duration(750)
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")scale(" + k + ")translate(" + -x + "," + -y + ")")
.style("stroke-width", 1.5 / k + "px");
}
})
</script>
</html>