vue-data-ui
Version:
A user-empowering data visualization Vue 3 components library for eloquent data storytelling
155 lines (154 loc) • 7.6 kB
JavaScript
const A = {
mercator([M, n], o, s, r) {
n = Math.max(Math.min(n, 85.05113), -85.05113);
const [t, i] = r, c = (M - t) * Math.PI / 180, l = o / (2 * Math.PI) * (c + Math.PI), e = (I) => Math.log(Math.tan(Math.PI / 4 + I * Math.PI / 180 / 2)), y = e(85.05113), P = e(-72), x = (e(n) - y) / (P - y) * s;
return [l, x];
},
equirectangular([M, n], o, s, r) {
const [h, t] = r;
n = Math.max(Math.min(n, 90), -80);
const i = (M - h + 180) / 360 * o, c = (90 - (n - t)) / 170 * s;
return [i, c];
},
robinson([M, n], o, s, r) {
const a = [
[1, 0],
[0.9986, 0.062],
[0.9954, 0.124],
[0.99, 0.186],
[0.9822, 0.248],
[0.973, 0.31],
[0.96, 0.372],
[0.9427, 0.434],
[0.9216, 0.4958],
[0.8962, 0.5571],
[0.8679, 0.6176],
[0.835, 0.6769],
[0.7986, 0.7346],
[0.7597, 0.7903],
[0.7186, 0.8435],
[0.6732, 0.8936],
[0.6213, 0.9394],
[0.5722, 0.9761],
[0.5322, 1]
], [m, h] = r;
M = M - m, n = Math.max(-89.9999, Math.min(89.9999, n));
const t = Math.abs(n), i = Math.floor(t / 5), c = t % 5 / 5, [l, e] = a[i], [y, P] = a[Math.min(i + 1, a.length - 1)], u = l + (y - l) * c, x = e + (P - e) * c, I = M * Math.PI / 180, f = o / 2 + o / 2 * u * I / Math.PI, p = s / 2 - Math.sign(n) * (s / 2) * x / 1;
return !Number.isFinite(f) || !Number.isFinite(p) ? [-9999, -9999] : [f, p];
},
mollweide([M, n], o, s, r) {
n = Math.max(Math.min(n, 89.9999), -89.9999);
const [a] = r, m = (M - a) * Math.PI / 180, h = n * Math.PI / 180;
let t = h;
const i = 1e-10;
let c = 1, l = 0;
for (; Math.abs(c) > i && l < 10; )
c = -(2 * t + Math.sin(2 * t) - Math.PI * Math.sin(h)) / (2 + 2 * Math.cos(2 * t)), t += c, l++;
isFinite(t) || (t = h > 0 ? Math.PI / 2 : -Math.PI / 2);
const e = o / (2 * Math.SQRT2), P = (e * (2 * Math.SQRT2 / Math.PI) * m * Math.cos(t) + 2 * e) / (4.6 * e) * o, u = -e * Math.SQRT2 * Math.sin(t), x = -e * Math.SQRT2, I = e * Math.SQRT2, f = (u - x) / (I - x) * s;
return [
isFinite(P) ? P : 0,
isFinite(f) ? f : 0
];
},
winkelTripel([M, n], o, s, r) {
const [a, m] = r, h = (M - a) * Math.PI / 180, t = n * Math.PI / 180, i = h * Math.cos(Math.PI / 6), c = t, l = Math.acos(Math.cos(t) * Math.cos(h / 2)), e = 2 * Math.cos(t) * Math.sin(h / 2) / (Math.sin(l) / l || 1), y = Math.sin(t) / (Math.sin(l) / l || 1), P = o / 2 + o / (2 * Math.PI) * (i + e) / 2, u = s / 2 - s / (2 * Math.PI) * (c + y) / 2;
return [P, u];
},
aitoff([M, n], o, s, r) {
Math.sinc || (Math.sinc = function(e) {
return e === 0 ? 1 : Math.sin(Math.PI * e) / (Math.PI * e);
});
const [a, m] = r, h = (M - a) * Math.PI / 180, t = n * Math.PI / 180, i = Math.acos(Math.cos(t) * Math.cos(h / 2));
let c = 0, l = 0;
return i !== 0 && (c = 2 * Math.cos(t) * Math.sin(h / 2) / Math.sinc(i / Math.PI), l = Math.sin(t) / Math.sinc(i / Math.PI)), c = o / 2 + c * (o / 4), l = s / 2 - l * (s / 2), [c, l];
},
hammer([M, n], o, s, r) {
const [a, m] = r, h = (M - a) * Math.PI / 180, t = n * Math.PI / 180, i = Math.sqrt(1 + Math.cos(t) * Math.cos(h / 2)), c = o / 2 + o / 2 * (2 * Math.SQRT2 * Math.cos(t) * Math.sin(h / 2) / i) / 2.8284271247461903, l = s / 2 - s / 2 * (Math.SQRT2 * Math.sin(t) / i) / 1.4142135623730951;
return [c, l];
},
bonne([M, n], o, s, r = [0, 0]) {
const a = 45 * Math.PI / 180, [m, h] = r, t = (M - m) * Math.PI / 180, c = Math.max(Math.min(n, 89.9), -89.9) * Math.PI / 180, l = 1 / Math.tan(a), e = l + a - c, y = e === 0 ? 0 : t * Math.cos(c) / e, P = Math.min(o, s) / 2.6, u = o / 2 + P * e * Math.sin(y), x = s / 2 - P * (l - e * Math.cos(y));
return [u, x];
},
sinusoidal([M, n], o, s, r) {
const [a, m] = r, h = (M - a) * Math.PI / 180, t = n * Math.PI / 180, i = o / 2 + o / (2 * Math.PI) * h * Math.cos(t), c = s / 2 - s / Math.PI * t;
return [i, c];
},
gallPeters([M, n], o, s, r) {
const [a, m] = r;
n = Math.max(Math.min(n, 89.9999), -89.9999);
const h = (M - a + 180) / 360 * o, t = s / 2 - s / Math.PI * Math.sin(n * Math.PI / 180);
return [h, t];
},
vanDerGrinten([M, n], o, s, r = [0, 0]) {
const a = Math.PI, m = Math.PI / 2, h = 1e-6;
let t = (M - (r[0] || 0)) * a / 180, i = (n - (r[1] || 0)) * a / 180, c = Math.abs(t), l = Math.abs(i), e, y;
if (l < h)
e = t, y = 0;
else if (c < h || Math.abs(l - m) < h)
e = 0, y = Math.sign(i) * a * Math.tan(Math.asin(l / m) / 2);
else {
let I = l / m, f = Math.asin(I), p = Math.cos(f), d = 0.5 * Math.abs(a / t - t / a), g = d * d, b = p / (I + p - 1), R = b * (2 / I - 1), L = R * R, T = L + g, _ = b - L, F = g + b;
e = Math.sign(t) * a * (d * _ + Math.sqrt(Math.max(0, g * _ * _ - T * (b * b - L)))) / T, y = Math.sign(i) * a * (R * F - d * Math.sqrt(Math.max(0, (g + 1) * T - F * F))) / T;
}
const P = o / 2 / a * 0.98, u = o / 2, x = s / 2;
return [u + e * P, x - y * P];
},
globe([M, n], o, s, r) {
const [a, m] = r, h = (M - a) * Math.PI / 180, t = n * Math.PI / 180, i = m * Math.PI / 180, c = Math.min(o, s) / 2 * 0.95, l = o / 2, e = s / 2, y = c * Math.cos(t) * Math.sin(h) + l, P = -c * (Math.cos(i) * Math.sin(t) - Math.sin(i) * Math.cos(t) * Math.cos(h)) + e;
return Math.sin(i) * Math.sin(t) + Math.cos(i) * Math.cos(t) * Math.cos(h) < 0 ? [NaN, NaN] : [y, P];
},
azimuthalEquidistant([M, n], o, s, r = [0, 0]) {
const a = (p) => p * Math.PI / 180, [m, h] = r, t = a(M - m), i = a(n), c = a(h), l = Math.sin(c) * Math.sin(i) + Math.cos(c) * Math.cos(i) * Math.cos(t), e = Math.acos(Math.max(-1, Math.min(1, l)));
let y = e === 0 ? 1 : e / Math.sin(e);
const P = y * Math.cos(i) * Math.sin(t), u = y * (Math.cos(c) * Math.sin(i) - Math.sin(c) * Math.cos(i) * Math.cos(t)), x = Math.min(o, s) / 2 / Math.PI, I = o / 2, f = s / 2;
return [
I + P * x,
f - u * x
];
}
};
function N(M, n, o, s, r = [0, 0]) {
let a = 1 / 0, m = 1 / 0, h = -1 / 0, t = -1 / 0;
for (const i of n) {
const c = i.geometry, l = c.type === "Polygon" ? [c.coordinates] : c.type === "MultiPolygon" ? c.coordinates : [];
for (const e of l)
for (const y of e)
for (const [P, u] of y) {
const [x, I] = M([P, u], o, s, r);
!Number.isFinite(x) || !Number.isFinite(I) || (a = Math.min(a, x), h = Math.max(h, x), m = Math.min(m, I), t = Math.max(t, I));
}
}
return {
minX: Math.floor(a) - 10,
minY: Math.floor(m) - 10,
width: Math.ceil(h - a) + 20,
height: Math.ceil(t - m) + 20
};
}
function Q(M, n, o) {
const s = M.find(
(a) => a.properties.admin === n || a.properties.name === n
), r = M.find(
(a) => a.properties.admin === o || a.properties.name === o
);
return s && r && (s.geometry.type === "Polygon" && (s.geometry = {
type: "MultiPolygon",
coordinates: [s.geometry.coordinates]
}), r.geometry.type === "Polygon" ? s.geometry.coordinates.push(r.geometry.coordinates) : r.geometry.type === "MultiPolygon" && s.geometry.coordinates.push(...r.geometry.coordinates), M = M.filter(
(a) => !(a.properties.admin === o || a.properties.name === o)
)), M;
}
function C(M, n) {
let o = Array.isArray(n) ? n.map((s) => ({ ...s })) : n.features ? n.features.map((s) => ({ ...s })) : [];
return M.style.chart.territory.showTaiwanAsPartOfChina && (o = Q(o, "China", "Taiwan")), n.type === "FeatureCollection" ? { ...n, features: o } : o;
}
const S = {
projections: A,
getProjectedBounds: N,
setupTerritories: C
};
export {
S as g
};