|
| 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