Skip to content

Commit 2f94a35

Browse files
committed
[charts] Add seriesId prop to MapShapePlot
Allows rendering a subset of `mapShape` series, so multiple `MapShapePlot` can style separate layers (for example filled shapes below and outlines on top).
1 parent a57d297 commit 2f94a35

3 files changed

Lines changed: 28 additions & 2 deletions

File tree

docs/pages/x/api/charts/map-shape-plot.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
{
22
"props": {
33
"fill": { "type": { "name": "string" } },
4+
"seriesId": {
5+
"type": { "name": "union", "description": "Array&lt;string&gt;<br>&#124;&nbsp;string" }
6+
},
47
"stroke": { "type": { "name": "string" }, "default": "'none'" },
58
"strokeWidth": { "type": { "name": "number" }, "default": "1" }
69
},

docs/translations/api-docs/charts/map-shape-plot/map-shape-plot.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
"fill": {
55
"description": "Fill color applied to every feature path. Overrides item and series colors."
66
},
7+
"seriesId": {
8+
"description": "The id, or ids, of the series to render. If not provided, every <code>mapShape</code> series is rendered. Use it to render layers with different styles through multiple <code>MapShapePlot</code>."
9+
},
710
"stroke": { "description": "Stroke color applied to every feature path." },
811
"strokeWidth": { "description": "Stroke width applied to every feature path." }
912
},

packages/x-charts-premium/src/Map/MapShapePlot.tsx

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use client';
22
import * as React from 'react';
33
import PropTypes from 'prop-types';
4+
import type { SeriesId } from '@mui/x-charts/models';
45
import { useZAxes } from '@mui/x-charts/hooks';
56
import { useGeoData } from '../hooks/useGeoData';
67
import { useGeoPath } from '../hooks/useGeoPath';
@@ -26,16 +27,29 @@ export interface MapShapePlotProps {
2627
* @default 1
2728
*/
2829
strokeWidth?: number;
30+
/**
31+
* The id, or ids, of the series to render.
32+
* If not provided, every `mapShape` series is rendered.
33+
* Use it to render layers with different styles through multiple `MapShapePlot`.
34+
*/
35+
seriesId?: SeriesId | SeriesId[];
2936
}
3037

3138
/**
3239
* Renders series mapShape items.
3340
*/
3441
function MapShapePlot(props: MapShapePlotProps) {
35-
const { className, fill, stroke = 'none', strokeWidth = 1 } = props;
42+
const { className, fill, stroke = 'none', strokeWidth = 1, seriesId } = props;
3643
const geoData = useGeoData();
3744
const path = useGeoPath();
38-
const series = useMapShapeSeries();
45+
const allSeries = useMapShapeSeries();
46+
const series = React.useMemo(() => {
47+
if (seriesId === undefined) {
48+
return allSeries;
49+
}
50+
const ids = Array.isArray(seriesId) ? seriesId : [seriesId];
51+
return allSeries.filter((seriesItem) => ids.includes(seriesItem.id));
52+
}, [allSeries, seriesId]);
3953
const featureIndexesByName = useGeoFeatureIndexesByName();
4054
const { zAxis, zAxisIds } = useZAxes();
4155

@@ -111,6 +125,12 @@ MapShapePlot.propTypes /* remove-proptypes */ = {
111125
* Fill color applied to every feature path. Overrides item and series colors.
112126
*/
113127
fill: PropTypes.string,
128+
/**
129+
* The id, or ids, of the series to render.
130+
* If not provided, every `mapShape` series is rendered.
131+
* Use it to render layers with different styles through multiple `MapShapePlot`.
132+
*/
133+
seriesId: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.string), PropTypes.string]),
114134
/**
115135
* Stroke color applied to every feature path.
116136
* @default 'none'

0 commit comments

Comments
 (0)