Skip to content

Commit bce6ad6

Browse files
author
Savina Shen (Manpower Services Taiwan Co Ltd)
committed
add cluster aggregates example
1 parent c2a9ade commit bce6ad6

File tree

9 files changed

+265
-202
lines changed

9 files changed

+265
-202
lines changed

src/stories/DataVisualization/BubbleLayer/Test.tsx renamed to src/Test.tsx

File renamed without changes.

src/stories/DataVisualization/BubbleLayer/AdvanceBubbleLayer.stories.ts

Lines changed: 0 additions & 32 deletions
This file was deleted.

src/stories/DataVisualization/BubbleLayer/AdvanceBubbleLayer.tsx

Lines changed: 0 additions & 114 deletions
This file was deleted.

src/stories/DataVisualization/BubbleLayer/BubbleLayer.mdx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { Canvas, Meta, Source } from '@storybook/blocks';
22

3-
import { code } from './code';
4-
53
import * as BubbleLayerStories from './BubbleLayer.stories';
64

75
import BubbleLayer from './BubbleLayer';

src/stories/DataVisualization/BubbleLayer/BubbleLayer.stories.ts

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,61 @@
11
import type { Meta, StoryObj } from '@storybook/react';
22
import BubbleLayer from './BubbleLayer';
3-
import { code } from './code';
43

54
const meta: Meta<typeof BubbleLayer> = {
65
title: 'Data Visualization/Bubble Layer',
76
component: BubbleLayer,
87
parameters: {
98
layout: 'centered',
109
storySource: {
11-
source: code,
10+
source: `import { AzureMap, AzureMapsProvider, AzureMapDataSourceProvider, AzureMapLayerProvider } from 'react-azure-maps';
11+
import atlas, { BubbleLayerOptions } from 'azure-maps-control';
12+
13+
// Generate random points to build the data source for the BubbleLayer.
14+
const collection = generateRandomPoints();
15+
16+
const BubbleLayer = () => {
17+
<AzureMapsProvider>
18+
<div className="defaultMap">
19+
<AzureMap options={your_options}>
20+
21+
<AzureMapDataSourceProvider
22+
id="BubbleLayer DataSourceProvider"
23+
collection={collection}>
24+
<AzureMapLayerProvider
25+
type="BubbleLayer"
26+
options={{
27+
radius: 10,
28+
color: 'DodgerBlue',
29+
opacity: 1,
30+
strokeColor: 'DarkBlue',
31+
strokeWidth: 2,
32+
strokeOpacity: 1,
33+
blur: 0,
34+
}}
35+
/>
36+
</AzureMapDataSourceProvider>
37+
38+
</AzureMap>
39+
</div>
40+
</AzureMapsProvider>
41+
);
42+
};
43+
44+
function generateRandomPoints() {
45+
var layerData = [];
46+
47+
for (var i = 0; i < 50; i++) {
48+
layerData.push(
49+
new atlas.data.Feature(new atlas.data.Point([Math.random() * 360 - 180, Math.random() * 170 - 85]), {
50+
title: 'Pin_' + i,
51+
}),
52+
);
53+
}
54+
55+
return layerData;
56+
}
57+
58+
`,
1259
},
1360
},
1461
args: {

src/stories/DataVisualization/BubbleLayer/BubbleLayer.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { mapOptions } from '../../../key';
22
import { AzureMap, AzureMapsProvider, AzureMapDataSourceProvider, AzureMapLayerProvider } from 'react-azure-maps';
33
import { BubbleLayerOptions } from 'azure-maps-control';
44
import atlas from 'azure-maps-control';
5-
import Test from './Test';
65

76
const collection = generateRandomPoints();
87

src/stories/DataVisualization/BubbleLayer/code.ts

Lines changed: 0 additions & 51 deletions
This file was deleted.
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import { Source, Meta } from '@storybook/addon-docs/blocks';
2+
3+
import ClusterAggregates from './ClusterAggregates';
4+
5+
<Meta title="Data Visualization/Cluster Aggregates"/>
6+
7+
# Cluster Aggregates
8+
This example shows how to enable point based clustering on a data source and render them differently from individual points on the map.
9+
Clustered points have four properties;
10+
- `cluster` - A boolean value indicating that it is a cluster.
11+
- `cluster_id` - A unique id for the cluster which can be used with the DataSource getClusterExpansionZoom, getClusterChildren, and getClusterLeaves functions.
12+
- `point_count` - The number of points the cluster contains.
13+
- `point_count_abbreviated` - A string that abbreviates the point_count value if it is long. (i.e. 4,000 becomes 4K)
14+
15+
You can observe the clusters by zooming in and out on the map.
16+
17+
<ClusterAggregates showBubbles showNumbers/>
18+
19+
In this example, we use a **Bubble Layer** to render the clusters as circles and a **Symbol Layer** to render the number of points in each cluster.
20+
For the bubble layer, we set the radius and the color to change based on the number of points in the cluster.
21+
22+
<Source code={`
23+
const bubbleLayerOptions = {
24+
//Scale the size of the clustered bubble based on the number of points inthe cluster.
25+
radius: [
26+
'step',
27+
['get', 'point_count'],
28+
20, //Default of 20 pixel radius.
29+
100,
30+
30, //If point_count >= 100, radius is 30 pixels.
31+
750,
32+
40, //If point_count >= 750, radius is 40 pixels.
33+
],
34+
35+
//Change the color of the cluster based on the value on the point_cluster property of the cluster.
36+
color: [
37+
'step',
38+
['get', 'point_count'],
39+
'rgba(0,255,0,0.8)', //Default to green.
40+
100,
41+
'rgba(255,255,0,0.8)', //If the point_count >= 100, color is yellow.
42+
750,
43+
'rgba(255,0,0,0.8)', //If the point_count >= 100, color is red.
44+
],
45+
strokeWidth: 0,
46+
filter: ['has', 'point_count'], //Only rendered data points which have a point_count property, which clusters do.
47+
};
48+
`}/>
49+
50+
For the symbol layer, we set the text to be the point_count_abbreviated property of the cluster.
51+
52+
<Source code={`
53+
const symbolLayerOptions = {
54+
iconOptions: {
55+
image: 'none', //Hide the icon image.
56+
},
57+
textOptions: {
58+
textField: ['get', 'point_count_abbreviated'],
59+
offset: [0, 0.4],
60+
},
61+
};
62+
`}/>
63+
64+
Finally, we set the options of the **AzureMapDataSourceProvider** to enable clustering.
65+
66+
<Source code={`
67+
import {
68+
AzureMap,
69+
AzureMapDataSourceProvider,
70+
AzureMapLayerProvider,
71+
AzureMapsProvider,
72+
} from 'react-azure-maps';
73+
74+
const ClusterAggregates = ({ showBubbles, showNumbers }: ClusterAggregatesProps) => {
75+
76+
return (
77+
<AzureMapsProvider>
78+
<div className="defaultMap">
79+
<AzureMap options={your_option}>
80+
<AzureMapDataSourceProvider
81+
id="ClusterAggregates DataSourceProvider"
82+
dataFromUrl="https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_month.geojson"
83+
options={{
84+
//Tell the data source to cluster point data.
85+
cluster: true,
86+
//The radius in pixels to cluster points together.
87+
clusterRadius: 45,
88+
//The maximium zoom level in which clustering occurs.
89+
//If you zoom in more than this, all points are rendered as symbols.
90+
clusterMaxZoom: 15,
91+
}}
92+
>
93+
<AzureMapLayerProvider
94+
id={'BubbleLayer LayerProvider'}
95+
options={bubbleLayerOptions}
96+
type="BubbleLayer"
97+
></AzureMapLayerProvider>
98+
<AzureMapLayerProvider
99+
id={'NumberLayer2 LayerProvider'}
100+
options={symbolLayerOptions}
101+
type="SymbolLayer"
102+
></AzureMapLayerProvider>
103+
</AzureMapDataSourceProvider>
104+
</AzureMap>
105+
</div>
106+
</AzureMapsProvider>
107+
);
108+
};
109+
`} />

0 commit comments

Comments
 (0)