Skip to content

Commit 14effd5

Browse files
author
Savina Shen (Manpower Services Taiwan Co Ltd)
committed
add layer events example
1 parent 3d25a03 commit 14effd5

File tree

6 files changed

+130
-5
lines changed

6 files changed

+130
-5
lines changed

src/stories/DataVisualization/ImageLayer/ImageLayer.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Source, Meta } from '@storybook/blocks';
2-
2+
import * as ImageLayerStories from './ImageLayer.stories.ts';
33
import ImageLayer from './ImageLayer';
44

5-
<Meta title="Image Layer"/>
5+
<Meta of={ImageLayerStories} />
66

77
# Image Layer
88

src/stories/DataVisualization/ImageLayer/ImageLayer.stories.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { Meta, StoryObj } from '@storybook/react';
22
import ImageLayer from './ImageLayer';
33

44
const meta: Meta<typeof ImageLayer> = {
5-
title: 'Image Layer',
5+
title: 'Data Visualization/Image Layer',
66
component: ImageLayer,
77
parameters: {
88
storySource: {

src/stories/DataVisualization/TileLayer/TileLayer.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Source, Meta } from '@storybook/blocks';
2+
import * as TileLayerStories from './TileLayer.stories.ts';
23

3-
<Meta title="Tile Layer"/>
4+
<Meta of={TileLayerStories}/>
45

56
# Tile Layer
67

src/stories/DataVisualization/TileLayer/TileLayer.stories.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { Meta, StoryObj } from '@storybook/react';
22
import TileLayer from './TileLayer';
33

44
const meta: Meta<typeof TileLayer> = {
5-
title: 'Tile Layer',
5+
title: 'Data Visualization/Tile Layer',
66
component: TileLayer,
77
parameters: {
88
storySource: {
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { Meta, Source } from '@storybook/addon-docs/blocks';
2+
import LayerEvents from './LayerEvents';
3+
4+
<Meta title="Events/Layer Events"/>
5+
6+
# Layer Events
7+
The layer triggers events whenever the user interacts with it.<br/>
8+
For a complete list of available events, refer to the [Layer Events Sample](https://samples.azuremaps.com/symbol-layer/symbol-layer-events).
9+
10+
> The symbol, bubble, line, and polygon layer all support the same set of events. <br/>
11+
> The heat map and tile layers don't support any of these events.
12+
13+
The example below shows how to listen for different mouse events on a bubble layer.<br/>
14+
**Try moving the mouse back and forth over the map**.<br/>
15+
You'll see the bubbles' color change as the mouse enters and leaves.
16+
17+
<LayerEvents/>
18+
19+
<Source code={`
20+
import { AzureMap, AzureMapsProvider, AzureMapDataSourceProvider, AzureMapLayerProvider } from 'react-azure-maps';
21+
import atlas from 'azure-maps-control';
22+
import { useState } from 'react';
23+
24+
// Generate data for the bubble layer
25+
const collection = generateRandomPoints();
26+
27+
const LayerEvents = () => {
28+
const [color, setColor] = useState('red');
29+
30+
const setRandomColor = () => {
31+
// Generate a random color
32+
const randomHex = Math.floor(Math.random() * 16777215).toString(16);
33+
setColor(\`#\${randomHex.padStart(6, '0')}\`);
34+
};
35+
36+
return (
37+
<AzureMapsProvider>
38+
<AzureMap options={mapOptions}>
39+
<AzureMapDataSourceProvider id="BubbleLayer DataSourceProvider" collection={collection}>
40+
<AzureMapLayerProvider
41+
type="BubbleLayer"
42+
options={{
43+
radius: 16,
44+
color,
45+
}}
46+
events={{
47+
mouseenter: setRandomColor,
48+
mouseleave: setRandomColor,
49+
}}
50+
/>
51+
</AzureMapDataSourceProvider>
52+
</AzureMap>
53+
</AzureMapsProvider>
54+
);
55+
};
56+
57+
function generateRandomPoints() {
58+
var layerData = [];
59+
60+
for (var i = 0; i < 50; i++) {
61+
layerData.push(
62+
new atlas.data.Feature(new atlas.data.Point([Math.random() * 360 - 180, Math.random() * 170 - 85]), {
63+
title: 'Pin_' + i,
64+
}),
65+
);
66+
}
67+
68+
return layerData;
69+
}
70+
`}/>
71+
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { mapOptions } from '../../../key';
2+
import { AzureMap, AzureMapsProvider, AzureMapDataSourceProvider, AzureMapLayerProvider } from 'react-azure-maps';
3+
import atlas from 'azure-maps-control';
4+
import { useState } from 'react';
5+
6+
const collection = generateRandomPoints();
7+
8+
const LayerEvents = () => {
9+
const [color, setColor] = useState('red');
10+
11+
const setRandomColor = () => {
12+
const randomHex = Math.floor(Math.random() * 16777215).toString(16);
13+
setColor(`#${randomHex.padStart(6, '0')}`);
14+
};
15+
16+
return (
17+
<div className="defaultMap sb-unstyled">
18+
<AzureMapsProvider>
19+
<AzureMap options={mapOptions}>
20+
<AzureMapDataSourceProvider id="BubbleLayer DataSourceProvider" collection={collection}>
21+
<AzureMapLayerProvider
22+
type="BubbleLayer"
23+
options={{
24+
radius: 16,
25+
color,
26+
}}
27+
events={{
28+
mouseenter: setRandomColor,
29+
mouseleave: setRandomColor,
30+
}}
31+
/>
32+
</AzureMapDataSourceProvider>
33+
</AzureMap>
34+
</AzureMapsProvider>
35+
</div>
36+
);
37+
};
38+
39+
function generateRandomPoints() {
40+
var layerData = [];
41+
42+
for (var i = 0; i < 50; i++) {
43+
layerData.push(
44+
new atlas.data.Feature(new atlas.data.Point([Math.random() * 360 - 180, Math.random() * 170 - 85]), {
45+
title: 'Pin_' + i,
46+
}),
47+
);
48+
}
49+
50+
return layerData;
51+
}
52+
53+
export default LayerEvents;

0 commit comments

Comments
 (0)