Skip to content

Commit bab4012

Browse files
committed
Modification - VueUiWorld - Add optional config attribute to flag Taiwan as part of China
1 parent d5b4cb2 commit bab4012

File tree

4 files changed

+55
-10
lines changed

4 files changed

+55
-10
lines changed

src/components/vue-ui-world.vue

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script setup>
22
import { ref, computed, watch, nextTick } from 'vue';
3-
import worldGeo from "../resources/worldGeo.json"
3+
import WORLD_DATA from "../resources/worldGeo.json"
44
import { useConfig } from '../useConfig';
55
import { applyDataLabel, convertColorToHex, convertCustomPalette, createCsvContent, createUid, darkenHexColor, dataLabel, downloadCsv, hasDeepProperty, interpolateColorHex, isFunction, lightenHexColor, palette, XMLNS } from '../lib';
66
import { useNestedProp } from '../useNestedProp';
@@ -64,8 +64,11 @@ const isTooltip = ref(false);
6464
const tooltipContent = ref('');
6565
const step = ref(0);
6666
67+
let worldGeo = WORLD_DATA;
68+
const { projections, getProjectedBounds, setupTerritories } = geo;
69+
6770
function prepareConfig() {
68-
const mergedConfig = useNestedProp({
71+
let mergedConfig = useNestedProp({
6972
userConfig: props.config,
7073
defaultConfig: DEFAULT_CONFIG
7174
});
@@ -96,6 +99,8 @@ function prepareConfig() {
9699
mergedConfig.style.chart.dimensions.height = null;
97100
}
98101
102+
worldGeo = setupTerritories(mergedConfig, worldGeo);
103+
99104
// --------------------------------------------------------------------
100105
101106
return mergedConfig;
@@ -154,8 +159,6 @@ const sizes = computed(() => {
154159
}[projection.value];
155160
});
156161
157-
const { projections, getProjectedBounds } = geo;
158-
159162
const viewBox = computed(() => {
160163
const { minX, minY, width, height } = getProjectedBounds(projections[projection.value], worldGeo.features, sizes.value.width, sizes.value.height);
161164
return {

src/geoProjections.js

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ const projections = {
114114
},
115115
aitoff([lon, lat], width, height, center) {
116116
if (!Math.sinc) {
117-
Math.sinc = function(x) {
117+
Math.sinc = function (x) {
118118
return x === 0 ? 1 : Math.sin(Math.PI * x) / (Math.PI * x);
119119
};
120120
}
@@ -140,7 +140,7 @@ const projections = {
140140
const y = height / 2 - height / 2 * (Math.SQRT2 * Math.sin(φ) / denom) / 1.4142135623730951;
141141
return [x, y];
142142
},
143-
bonne([lon, lat], width, height, center = [0,0]) {
143+
bonne([lon, lat], width, height, center = [0, 0]) {
144144
const φ = 45 * Math.PI / 180;
145145
const [lon0, _lat0] = center;
146146
const λ = (lon - lon0) * Math.PI / 180;
@@ -196,7 +196,7 @@ const projections = {
196196
x = Math.sign(λ) * pi * (A * G_P2 + Math.sqrt(Math.max(0, A2 * G_P2 * G_P2 - P2_A2 * (G * G - P2)))) / P2_A2;
197197
y = Math.sign(φ) * pi * (P * Q - A * Math.sqrt(Math.max(0, (A2 + 1) * P2_A2 - Q * Q))) / P2_A2;
198198
}
199-
const scale = (width/2) / pi * 0.98;
199+
const scale = (width / 2) / pi * 0.98;
200200
const cx = width / 2, cy = height / 2;
201201
return [cx + x * scale, cy - y * scale];
202202
},
@@ -253,7 +253,7 @@ function geoToPath(geometry) {
253253
return '';
254254
}
255255

256-
function getProjectedBounds(projection, features, width, height, center = [0,0]) {
256+
function getProjectedBounds(projection, features, width, height, center = [0, 0]) {
257257
let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
258258
for (const feature of features) {
259259
const geom = feature.geometry;
@@ -281,10 +281,50 @@ function getProjectedBounds(projection, features, width, height, center = [0,0])
281281
}
282282
}
283283

284+
function mergeCountries(features, parent, child) {
285+
const parentFeature = features.find(
286+
f => (f.properties.admin === parent || f.properties.name === parent)
287+
);
288+
const childFeature = features.find(
289+
f => (f.properties.admin === child || f.properties.name === child)
290+
);
291+
if (parentFeature && childFeature) {
292+
if (parentFeature.geometry.type === 'Polygon') {
293+
parentFeature.geometry = {
294+
type: 'MultiPolygon',
295+
coordinates: [parentFeature.geometry.coordinates]
296+
};
297+
}
298+
if (childFeature.geometry.type === 'Polygon') {
299+
parentFeature.geometry.coordinates.push(childFeature.geometry.coordinates);
300+
} else if (childFeature.geometry.type === 'MultiPolygon') {
301+
parentFeature.geometry.coordinates.push(...childFeature.geometry.coordinates);
302+
}
303+
features = features.filter(
304+
f => !(f.properties.admin === child || f.properties.name === child)
305+
);
306+
}
307+
return features
308+
}
309+
310+
function setupTerritories(config, geoData) {
311+
let features = Array.isArray(geoData)
312+
? geoData.map(f => ({ ...f }))
313+
: (geoData.features ? geoData.features.map(f => ({ ...f })) : []);
314+
315+
if (config.style.chart.territory.showTaiwanAsPartOfChina) {
316+
features = mergeCountries(features, 'China', 'Taiwan');
317+
}
318+
if (geoData.type === 'FeatureCollection') {
319+
return { ...geoData, features };
320+
}
321+
return features;
322+
}
284323

285324
const geo = {
286325
projections,
287326
getProjectedBounds,
327+
setupTerritories
288328
}
289329

290330
export default geo

src/useConfig.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5314,7 +5314,8 @@ export function useConfig() {
53145314
colors: {
53155315
min: '#E0E0E0',
53165316
max: null, // defaults to palette[0] if kept null
5317-
}
5317+
},
5318+
showTaiwanAsPartOfChina: false,
53185319
},
53195320
tooltip: {
53205321
...TOOLTIP,

types/vue-data-ui.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6735,7 +6735,8 @@ declare module "vue-data-ui" {
67356735
colors?: {
67366736
min?: string;
67376737
max?: string | null;
6738-
}
6738+
},
6739+
showTaiwanAsPartOfChina?: boolean;
67396740
};
67406741
tooltip?: ChartTooltip & {
67416742
showMinimap?: boolean;

0 commit comments

Comments
 (0)