-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDemo2.ts
91 lines (77 loc) · 3.3 KB
/
Demo2.ts
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
/**
* create by yanlele
* create time 2018-11-01 12:08
*/
import {geoMercator, geoPath} from "d3-geo";
import {schemeCategory10} from "d3-scale-chromatic";
import {json} from "d3-fetch";
import Base from "./Base";
import {selectAll} from "d3-selection";
/*复杂使用示例: 点击某一省份, 显示出该省份的中心和边界框, 在控制台输出面积,中心, 边界信息*/
class Demo2 extends Base {
constructor() {
super();
}
main() {
// 地图投影
let projection = geoMercator()
.center([107, 31])
.scale(700)
.translate([this.width / 2, this.height / 2]);
// 地理路径生成器
let path = geoPath().projection(projection);
let color = schemeCategory10;
json('china.geojson')
.then( (root: any) => {
let groups = this.svg.append('g');
let paths = groups.selectAll('path')
.data(root.features)
.enter()
.append('path')
.attr('class', 'province')
.style('fill', function (d: any, i: number) {
return color[i%10];
})
.attr('d', path)
.on('click', (d:any) => {
console.log(d);
// 计算面积、中心、边界框
let area = path.area(d);
let centroid = path.centroid(d);
let bounds = path.bounds(d);
let name = d.properties.name && d.properties.name;
let name_zh = d.properties.name_zh;
let currentAction = this.svg.selectAll(`#${name_zh}`);
let size: number = currentAction.size();
if(!size) {
// 输出到控制台
console.log('省份:', name);
console.log('面积: ', area );
console.log('中心: ', centroid);
console.log('边界框:');
console.log(bounds);
// 显示中心
this.svg.append('circle')
.attr('id', name_zh)
.attr('cx', centroid[0])
.attr('cy', centroid[1])
.attr('r', 8)
.attr('fill', 'white');
// 边界框
this.svg.append('rect')
.attr('x', bounds[0][0])
.attr('id', name_zh)
.attr('y', bounds[0][1])
.attr('width', bounds[1][0] - bounds[0][0])
.attr('height', bounds[1][1] - bounds[0][1])
.attr('fill', 'none')
.attr('stroke', '#aeffda')
.attr('stroke-width', 2);
} else {
currentAction.remove();
}
})
});
}
}
export default Demo2;