You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@echarts.apache.org by sh...@apache.org on 2022/01/10 07:03:37 UTC
[echarts] branch enhance-map updated: fix(map): use centroid as label position
This is an automated email from the ASF dual-hosted git repository.
shenyi pushed a commit to branch enhance-map
in repository https://gitbox.apache.org/repos/asf/echarts.git
The following commit(s) were added to refs/heads/enhance-map by this push:
new 3020484 fix(map): use centroid as label position
3020484 is described below
commit 3020484d4e354c302f0467119472e55688d16a36
Author: pissang <bm...@gmail.com>
AuthorDate: Mon Jan 10 15:02:39 2022 +0800
fix(map): use centroid as label position
---
src/chart/bar/BarView.ts | 2 -
src/coord/geo/GeoJSONResource.ts | 2 -
src/coord/geo/Region.ts | 79 ++++++++++++++++++++++++++++++----------
src/coord/geo/fix/geoCoord.ts | 40 --------------------
test/mapWorld.html | 11 ++++++
5 files changed, 71 insertions(+), 63 deletions(-)
diff --git a/src/chart/bar/BarView.ts b/src/chart/bar/BarView.ts
index 9c465ea..5366527 100644
--- a/src/chart/bar/BarView.ts
+++ b/src/chart/bar/BarView.ts
@@ -67,8 +67,6 @@ import {createSectorCalculateTextPosition, SectorTextPosition, setSectorTextRota
import { saveOldStyle } from '../../animation/basicTrasition';
import Element from 'zrender/src/Element';
-const _eventPos = [0, 0];
-
const mathMax = Math.max;
const mathMin = Math.min;
diff --git a/src/coord/geo/GeoJSONResource.ts b/src/coord/geo/GeoJSONResource.ts
index 55a7aec..93a1d71 100644
--- a/src/coord/geo/GeoJSONResource.ts
+++ b/src/coord/geo/GeoJSONResource.ts
@@ -23,7 +23,6 @@ import parseGeoJson from './parseGeoJson';
// Built-in GEO fixer.
import fixNanhai from './fix/nanhai';
import fixTextCoord from './fix/textCoord';
-import fixGeoCoord from './fix/geoCoord';
import fixDiaoyuIsland from './fix/diaoyuIsland';
import BoundingRect from 'zrender/src/core/BoundingRect';
import { GeoJSONRegion } from './Region';
@@ -114,7 +113,6 @@ export class GeoJSONResource implements GeoResource {
const regionName = region.name;
fixTextCoord(mapName, region);
- fixGeoCoord(mapName, region);
fixDiaoyuIsland(mapName, region);
// Some area like Alaska in USA map needs to be tansformed
diff --git a/src/coord/geo/Region.ts b/src/coord/geo/Region.ts
index e073c55..ccf51e0 100644
--- a/src/coord/geo/Region.ts
+++ b/src/coord/geo/Region.ts
@@ -34,7 +34,48 @@ function transformPoints(points: number[][], transform: matrix.MatrixArray) {
vec2.applyTransform(points[p], points[p], transform);
}
}
+function updateBBoxFromPoints(
+ points: ArrayLike<number>[],
+ min: vec2.VectorArray,
+ max: vec2.VectorArray,
+ projection: GeoProjection
+) {
+ for (let i = 0; i < points.length; i++) {
+ let p = points[i];
+ if (projection) {
+ // projection may return null point.
+ p = projection.project(p as number[]);
+ }
+ if (p && isFinite(p[0]) && isFinite(p[1])) {
+ vec2.min(min, min, p as vec2.VectorArray);
+ vec2.max(max, max, p as vec2.VectorArray);
+ }
+ }
+}
+function centroid(points: number[][]) {
+ let signedArea = 0;
+ let cx = 0;
+ let cy = 0;
+ const len = points.length;
+ let x0 = points[len - 1][0];
+ let y0 = points[len - 1][1];
+ // Polygon should been closed.
+ for (let i = 0; i < len; i++) {
+ const x1 = points[i][0];
+ const y1 = points[i][1];
+ const a = x0 * y1 - x1 * y0;
+ signedArea += a;
+ cx += (x0 + x1) * a;
+ cy += (y0 + y1) * a;
+ x0 = x1;
+ y0 = y1;
+ }
+
+ return signedArea
+ ? [cx / signedArea / 3, cy / signedArea / 3, signedArea]
+ : [points[0][0] || 0, points[0][1] || 0];
+}
export abstract class Region {
readonly name: string;
@@ -88,25 +129,6 @@ export class GeoJSONLineStringGeometry {
this.points = points;
}
}
-function updateBBoxFromPoints(
- points: ArrayLike<number>[],
- min: vec2.VectorArray,
- max: vec2.VectorArray,
- projection: GeoProjection
-) {
- for (let i = 0; i < points.length; i++) {
- let p = points[i];
- if (projection) {
- // projection may return null point.
- p = projection.project(p as number[]);
- }
- if (p && isFinite(p[0]) && isFinite(p[1])) {
- vec2.min(min, min, p as vec2.VectorArray);
- vec2.max(max, max, p as vec2.VectorArray);
- }
- }
-}
-
export class GeoJSONRegion extends Region {
readonly type = 'geoJSON';
@@ -129,6 +151,25 @@ export class GeoJSONRegion extends Region {
}
calcCenter() {
+ const geometries = this.geometries;
+ let largestGeo: GeoJSONPolygonGeometry;
+ let largestGeoSize = 0;
+ for (let i = 0; i < geometries.length; i++) {
+ const geo = geometries[i] as GeoJSONPolygonGeometry;
+ const exterior = geo.exterior;
+ // Simple trick to use points count instead of polygon area as region size.
+ // Ignore linestring
+ const size = exterior && exterior.length;
+ if (size > largestGeoSize) {
+ largestGeo = geo;
+ largestGeoSize = size;
+ }
+ }
+ if (largestGeo) {
+ return centroid(largestGeo.exterior);
+ }
+
+ // from bounding rect by default.
const rect = this.getBoundingRect();
return [
rect.x + rect.width / 2,
diff --git a/src/coord/geo/fix/geoCoord.ts b/src/coord/geo/fix/geoCoord.ts
deleted file mode 100644
index ea03c64..0000000
--- a/src/coord/geo/fix/geoCoord.ts
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
-* Licensed to the Apache Software Foundation (ASF) under one
-* or more contributor license agreements. See the NOTICE file
-* distributed with this work for additional information
-* regarding copyright ownership. The ASF licenses this file
-* to you under the Apache License, Version 2.0 (the
-* "License"); you may not use this file except in compliance
-* with the License. You may obtain a copy of the License at
-*
-* http://www.apache.org/licenses/LICENSE-2.0
-*
-* Unless required by applicable law or agreed to in writing,
-* software distributed under the License is distributed on an
-* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-* KIND, either express or implied. See the License for the
-* specific language governing permissions and limitations
-* under the License.
-*/
-
-import { Dictionary } from 'zrender/src/core/types';
-import { GeoJSONRegion } from '../Region';
-
-const geoCoordMap = {
- 'Russia': [100, 60],
- 'United States': [-99, 38],
- 'United States of America': [-99, 38]
-} as Dictionary<number[]>;
-
-export default function fixGeoCoords(mapType: string, region: GeoJSONRegion) {
- if (mapType === 'world') {
- const geoCoord = geoCoordMap[region.name];
- if (geoCoord) {
- const cp = [
- geoCoord[0],
- geoCoord[1]
- ];
- region.setCenter(cp);
- }
- }
-}
\ No newline at end of file
diff --git a/test/mapWorld.html b/test/mapWorld.html
index 5d25913..58abe60 100644
--- a/test/mapWorld.html
+++ b/test/mapWorld.html
@@ -41,6 +41,12 @@ under the License.
'./data/map/json/world.json'
], function (echarts, worldJson) {
+
+ // Test for label calculation.
+ worldJson.features.forEach(feature => {
+ feature.properties && (feature.properties.cp = null);
+ });
+
echarts.registerMap('world', worldJson);
var chart = echarts.init(document.getElementById('main'), null, {
@@ -94,6 +100,11 @@ under the License.
roam: true,
top: 60,
width: '80%',
+ label: {
+ show: true,
+ textBorderColor: '#fff',
+ textBorderWidth: 1
+ },
itemStyle: itemStyle,
data:[
{name : 'Afghanistan', value : 28397.812},
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@echarts.apache.org
For additional commands, e-mail: commits-help@echarts.apache.org