-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathCityStatisticMap.tsx
More file actions
63 lines (52 loc) · 1.68 KB
/
CityStatisticMap.tsx
File metadata and controls
63 lines (52 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { computed } from 'mobx';
import { observer } from 'mobx-react';
import { ObservedComponent } from 'mobx-react-helper';
import dynamic from 'next/dynamic';
import { MarkerMeta, OpenReactMapProps } from 'open-react-map';
import { OrganizationStatistic } from '../../models/Organization';
import systemStore from '../../models/System';
const ChinaMap = dynamic(() => import('./ChinaMap'), { ssr: false });
export interface CityStatisticMapProps {
data: OrganizationStatistic['city'];
onChange?: (city: string) => any;
}
@observer
export class CityStatisticMap extends ObservedComponent<CityStatisticMapProps> {
componentDidMount() {
systemStore.getCityCoordinate();
}
@computed
get markers() {
const { cityCoordinate } = systemStore,
{ data } = this.observedProps;
return Object.entries(data)
.map(([city, count]) => {
const point = cityCoordinate[city.replace(/(市|自治州|特别行政区)$/, '')];
if (point)
return {
tooltip: `${city} ${count}`,
position: [point[1], point[0]],
};
})
.filter(Boolean) as MarkerMeta[];
}
handleChange: OpenReactMapProps['onMarkerClick'] = ({ latlng: { lat, lng } }) => {
const { markers } = this;
const { tooltip } =
markers.find(({ position: p }) => p instanceof Array && lat === p[0] && lng === p[1]) || {};
const [city] = tooltip?.split(/\s+/) || [];
this.props.onChange?.(city);
};
render() {
const { markers } = this;
return (
<ChinaMap
style={{ height: '70vh' }}
center={[34.32, 108.55]}
zoom={4}
markers={markers}
onMarkerClick={this.handleChange}
/>
);
}
}