Skip to content

Commit ee94f44

Browse files
authored
Merge pull request #1451 from easyops-cn/steve/d3-geo
feat(): new brick: china-map
2 parents 4ff9f4f + 4eae43a commit ee94f44

File tree

13 files changed

+103643
-6
lines changed

13 files changed

+103643
-6
lines changed

bricks/data-view/docs/china-map.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
构件 `data-view.china-map`
2+
3+
## Examples
4+
5+
### Basic
6+
7+
```yaml preview height="600px"
8+
brick: div
9+
properties:
10+
style:
11+
height: calc(100vh - 4em)
12+
position: relative
13+
children:
14+
- brick: data-view.china-map
15+
properties:
16+
dataSource:
17+
- text: "西藏 12311"
18+
province: 西藏
19+
- text: "四川 89781169"
20+
province: 四川
21+
- text: "台湾 234181"
22+
province: 台湾
23+
- text: "江西 21348"
24+
province: 江西
25+
# Currently this brick only looks well within dark theme
26+
lifeCycle:
27+
onPageLoad:
28+
action: theme.setTheme
29+
args:
30+
- dark-v2
31+
```
32+
33+
### Province map
34+
35+
```yaml preview height="600px"
36+
brick: div
37+
properties:
38+
style:
39+
height: calc(100vh - 4em)
40+
position: relative
41+
children:
42+
- brick: data-view.china-map
43+
properties:
44+
province: 广东
45+
dataSource:
46+
- text: "广州 12311"
47+
city: 广州
48+
- text: "深圳 89781169"
49+
city: 深圳
50+
- text: "湛江 234181"
51+
city: 湛江
52+
# Currently this brick only looks well within dark theme
53+
lifeCycle:
54+
onPageLoad:
55+
action: theme.setTheme
56+
args:
57+
- dark-v2
58+
```

bricks/data-view/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"@tweenjs/tween.js": "^19.0.0",
4848
"classnames": "^2.3.2",
4949
"d3-force": "^3.0.0",
50+
"d3-geo": "^3.1.1",
5051
"d3-hierarchy": "^3.1.2",
5152
"react": "0.0.0-experimental-ee8509801-20230117",
5253
"resize-observer-polyfill": "^1.5.1",
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import path from "path";
2+
import { fileURLToPath } from "url";
3+
import { writeFile } from "fs/promises";
4+
import ChinaGeoJson from "../src/china-map/cities.json" assert { type: "json" };
5+
6+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
7+
8+
await writeFile(path.join(__dirname, "../src/china-map/cities.json"), JSON.stringify(rewind(ChinaGeoJson, true)));
9+
10+
function rewind(gj, outer) {
11+
var type = gj && gj.type, i;
12+
13+
if (type === 'FeatureCollection') {
14+
for (i = 0; i < gj.features.length; i++) rewind(gj.features[i], outer);
15+
16+
} else if (type === 'GeometryCollection') {
17+
for (i = 0; i < gj.geometries.length; i++) rewind(gj.geometries[i], outer);
18+
19+
} else if (type === 'Feature') {
20+
rewind(gj.geometry, outer);
21+
22+
} else if (type === 'Polygon') {
23+
rewindRings(gj.coordinates, outer);
24+
25+
} else if (type === 'MultiPolygon') {
26+
for (i = 0; i < gj.coordinates.length; i++) rewindRings(gj.coordinates[i], outer);
27+
}
28+
29+
return gj;
30+
}
31+
32+
function rewindRings(rings, outer) {
33+
if (rings.length === 0) return;
34+
35+
rewindRing(rings[0], outer);
36+
for (var i = 1; i < rings.length; i++) {
37+
rewindRing(rings[i], !outer);
38+
}
39+
}
40+
41+
function rewindRing(ring, dir) {
42+
var area = 0, err = 0;
43+
for (var i = 0, len = ring.length, j = len - 1; i < len; j = i++) {
44+
var k = (ring[i][0] - ring[j][0]) * (ring[j][1] + ring[i][1]);
45+
var m = area + k;
46+
err += Math.abs(area) >= Math.abs(k) ? area - m + k : k - m + area;
47+
area = m;
48+
}
49+
if (area + err >= 0 !== !!dir) ring.reverse();
50+
}

bricks/data-view/src/bootstrap.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,4 @@ import "./globe-with-halo-indicator/index.js";
4444
import "./globe-with-gear-indicator/index.js";
4545
import "./bubbles-indicator/index.js";
4646
import "./globe-with-orbit-indicator/index.js";
47+
import "./china-map/index.js";

bricks/data-view/src/china-map-chart/index.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -453,10 +453,10 @@ export function ChinaMapChartComponent(props: ChinaMapChartProps) {
453453
// }); //自定义选择高亮
454454

455455
scene.addLayer(seventhPolygonLayer);
456-
scene.addLayer(sixthPolygonLayer);
457-
scene.addLayer(fifthPolygonLayer);
458-
scene.addLayer(fourthPolygonLayer);
459-
scene.addLayer(thirdPolygonLayer);
456+
// scene.addLayer(sixthPolygonLayer);
457+
// scene.addLayer(fifthPolygonLayer);
458+
// // scene.addLayer(fourthPolygonLayer);
459+
// scene.addLayer(thirdPolygonLayer);
460460
scene.addLayer(secondPolygonLayer);
461461
if (chinaImgLayer) {
462462
scene.addLayer(chinaImgLayer);

0 commit comments

Comments
 (0)