Skip to content

Commit f8de3b6

Browse files
committed
feedback
1 parent 5047f3d commit f8de3b6

3 files changed

Lines changed: 60 additions & 42 deletions

File tree

packages/x-charts-premium/src/internals/plugins/useGeoProjection/useGeoProjection.ts

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,52 @@
11
'use client';
22
import * as React from 'react';
3-
import { type ChartPlugin } from '@mui/x-charts/internals';
3+
import type { useGeoProjectionTypes, ChartPlugin } from '@mui/x-charts/internals';
4+
import {
5+
geoAlbers,
6+
geoAlbersUsa,
7+
geoAzimuthalEqualArea,
8+
geoAzimuthalEquidistant,
9+
geoConicConformal,
10+
geoConicEqualArea,
11+
geoConicEquidistant,
12+
geoEqualEarth,
13+
geoEquirectangular,
14+
geoGnomonic,
15+
geoMercator,
16+
geoNaturalEarth1,
17+
geoOrthographic,
18+
geoStereographic,
19+
geoTransverseMercator,
20+
type GeoProjection,
21+
} from '@mui/x-charts-vendor/d3-geo';
422
import { type UseGeoProjectionSignature } from './useGeoProjection.types';
523

24+
const PROJECTION_FACTORIES: Record<
25+
useGeoProjectionTypes.D3NamedProjection,
26+
(() => GeoProjection) | undefined
27+
> = {
28+
// Azimuthal projections (https://d3js.org/d3-geo/azimuthal)
29+
azimuthalEqualArea: geoAzimuthalEqualArea,
30+
azimuthalEquidistant: geoAzimuthalEquidistant,
31+
gnomonic: geoGnomonic,
32+
orthographic: geoOrthographic,
33+
stereographic: geoStereographic,
34+
35+
// Conic projections (https://d3js.org/d3-geo/conic)
36+
conicConformal: geoConicConformal,
37+
conicEqualArea: geoConicEqualArea,
38+
conicEquidistant: geoConicEquidistant,
39+
albers: geoAlbers,
40+
albersUsa: geoAlbersUsa, // Special composition for the USA with an edge case for Alaska and Hawaii.
41+
42+
// Cylindrical projections (https://d3js.org/d3-geo/cylindrical)
43+
equirectangular: geoEquirectangular,
44+
mercator: geoMercator,
45+
transverseMercator: geoTransverseMercator,
46+
equalEarth: geoEqualEarth,
47+
naturalEarth1: geoNaturalEarth1,
48+
};
49+
650
export const useGeoProjection: ChartPlugin<UseGeoProjectionSignature> = ({ params, store }) => {
751
const { geoData, projection, translate, rotate, scale } = params;
852

@@ -19,6 +63,7 @@ export const useGeoProjection: ChartPlugin<UseGeoProjectionSignature> = ({ param
1963
translate: translate ?? null,
2064
rotate: rotate ?? null,
2165
scale: scale ?? null,
66+
factories: PROJECTION_FACTORIES,
2267
});
2368
}, [geoData, projection, translate, rotate, scale, store]);
2469

@@ -42,5 +87,6 @@ useGeoProjection.getInitialState = (params) => ({
4287
translate: params.translate ?? null,
4388
rotate: params.rotate ?? null,
4489
scale: params.scale ?? null,
90+
factories: PROJECTION_FACTORIES,
4591
},
4692
});

packages/x-charts/src/internals/plugins/featurePlugins/useGeoProjection/useGeoProjection.selectors.ts

Lines changed: 11 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,5 @@
11
import { createSelector, createSelectorMemoized } from '@mui/x-internals/store';
22
import {
3-
geoAlbers,
4-
geoAlbersUsa,
5-
geoAzimuthalEqualArea,
6-
geoAzimuthalEquidistant,
7-
geoConicConformal,
8-
geoConicEqualArea,
9-
geoConicEquidistant,
10-
geoEqualEarth,
11-
geoEquirectangular,
12-
geoGnomonic,
13-
geoMercator,
14-
geoNaturalEarth1,
15-
geoOrthographic,
16-
geoStereographic,
17-
geoTransverseMercator,
183
geoPath,
194
type ExtendedFeatureCollection,
205
type GeoProjection,
@@ -31,29 +16,6 @@ import { selectorChartDrawingArea } from '../../corePlugins/useChartDimensions/u
3116
import { type ChartState } from '../../models/chart';
3217
import { type GeoTooltipPosition } from '../../corePlugins/useChartSeriesConfig';
3318

34-
const PROJECTION_FACTORIES: Record<D3NamedProjection, (() => GeoProjection) | undefined> = {
35-
// Azimuthal projections (https://d3js.org/d3-geo/azimuthal)
36-
azimuthalEqualArea: geoAzimuthalEqualArea,
37-
azimuthalEquidistant: geoAzimuthalEquidistant,
38-
gnomonic: geoGnomonic,
39-
orthographic: geoOrthographic,
40-
stereographic: geoStereographic,
41-
42-
// Conic projections (https://d3js.org/d3-geo/conic)
43-
conicConformal: geoConicConformal,
44-
conicEqualArea: geoConicEqualArea,
45-
conicEquidistant: geoConicEquidistant,
46-
albers: geoAlbers,
47-
albersUsa: geoAlbersUsa, // Special composition for the USA with an edge case for Alaska and Hawaii.
48-
49-
// Cylindrical projections (https://d3js.org/d3-geo/cylindrical)
50-
equirectangular: geoEquirectangular,
51-
mercator: geoMercator,
52-
transverseMercator: geoTransverseMercator,
53-
equalEarth: geoEqualEarth,
54-
naturalEarth1: geoNaturalEarth1,
55-
};
56-
5719
const isConicProjection = (projection: GeoProjection): projection is GeoConicProjection => {
5820
return 'parallels' in projection && typeof projection.parallels === 'function';
5921
};
@@ -73,6 +35,12 @@ export const selectorChartRawProjection = createSelector(
7335
(geoProjection): GeoProjectionInput | null => geoProjection?.projection ?? null,
7436
);
7537

38+
export const selectorChartProjectionFactory = createSelector(
39+
selectorChartGeoProjectionState,
40+
(geoProjection): Record<D3NamedProjection, (() => GeoProjection) | undefined> | null =>
41+
geoProjection?.factories ?? null,
42+
);
43+
7644
export const selectorChartRawScale = createSelector(
7745
selectorChartGeoProjectionState,
7846
(geoProjection): number | null => geoProjection?.scale ?? null,
@@ -133,6 +101,7 @@ export const selectorChartGeoFeatureIndexesByName = createSelectorMemoized(
133101
*/
134102
export const selectorChartProjection = createSelectorMemoized(
135103
selectorChartRawProjection,
104+
selectorChartProjectionFactory,
136105
selectorChartGeoData,
137106
selectorChartParallels,
138107
selectorChartRotate,
@@ -141,24 +110,25 @@ export const selectorChartProjection = createSelectorMemoized(
141110
selectorChartDrawingArea,
142111
(
143112
projectionInput,
113+
projectionFactory,
144114
geoData,
145115
parallels,
146116
rotate,
147117
translate,
148118
scale,
149119
drawingArea,
150120
): GeoProjection | null => {
151-
if (!projectionInput) {
121+
if (!projectionInput || !projectionFactory) {
152122
return null;
153123
}
154124
let projection: GeoProjection;
155125
if (typeof projectionInput === 'string') {
156-
const factory = PROJECTION_FACTORIES[projectionInput];
126+
const factory = projectionFactory[projectionInput];
157127
if (!factory) {
158128
if (process.env.NODE_ENV !== 'production') {
159129
console.error(
160130
`MUI X Charts: Unknown projection name '${projectionInput}'. ` +
161-
`Expected one of: ${Object.keys(PROJECTION_FACTORIES).join(', ')}.`,
131+
`Expected one of: ${Object.keys(projectionFactory).join(', ')}.`,
162132
);
163133
}
164134
return null;

packages/x-charts/src/internals/plugins/featurePlugins/useGeoProjection/useGeoProjection.types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ export interface UseGeoProjectionState {
6161
translate: [number, number] | null;
6262
rotate: [number, number] | null;
6363
scale: number | null;
64+
// TODO: Remove factories from the state when some maps move to MIT package.
65+
factories: Record<D3NamedProjection, (() => GeoProjection) | undefined>;
6466
/**
6567
* The two standard parallels used by conic projections, if applicable.
6668
* Used for projection 'conicConformal', 'conicEqualArea', 'conicEquidistant'.

0 commit comments

Comments
 (0)