Learn how to use Charts Components with Map Components using Vite developer build tools.
In this tutorial, you will:
- Add a map with an
<arcgis-mapcomponent and display a chart with an> <arcgis-chartcomponent using slots.> - Add an
<arcgis-charts-action-barcomponent that will allow more interaction actions with the chart.> - Explore interactive functionalities, such as filtering by extent of the map and synchronizing selections between the chart and the map.
Prerequisites
- The most recent LTS Node.js runtime environment.
- A text editor to edit files.
- A terminal to enter commands.
Steps
Create a new project using Vite
- Download the initial Vite vanilla JavaScript project to your local machine.
- Unzip the downloaded file.
- Open the unzipped folder's files in your text editor.
- Navigate to the unzipped folder in your terminal.
Install dependencies
-
To use charts and map components, install the
@arcgis/charts-componentsand@arcgis/map-componentspackages and its dependencies into your project:Use dark colors for code blocks Copy npm install @arcgis/charts-components @arcgis/map-components @arcgis/core @esri/calcite-components -
Start the Vite development server.
Use dark colors for code blocks Copy npm run dev -
Open a web browser and navigate to http://localhost:5173, this webpage will be empty, however, it will update as we proceed through the remaining steps.
-
Import the stylesheet, along with the
<arcgis-chartand the> <arcgis-charts-action-barcomponents from the> @arcgis/charts-componentspackage inmain.js.main.jsUse dark colors for code blocks import "./style.css"; import "@arcgis/charts-components/components/arcgis-chart"; import "@arcgis/charts-components/components/arcgis-charts-action-bar"; -
Import the
<arcgis-mapcomponent from the> @arcgis/map-componentspackage inmain.js.main.jsUse dark colors for code blocks import "./style.css"; import "@arcgis/charts-components/components/arcgis-chart"; import "@arcgis/charts-components/components/arcgis-charts-action-bar"; import "@arcgis/map-components/components/arcgis-map";
Add styles
-
Add CSS styles to make
htmlandbodyfill the entire viewport with no margin.style.cssUse dark colors for code blocks html, body { height: 100%; margin: 0; } -
Add CSS to the
arcgis-chartelement so its width is800px.style.cssUse dark colors for code blocks html, body { height: 100%; margin: 0; } arcgis-chart { width: 800px; }
Add components
-
Add the
<arcgis-mapcomponent to> index.htmlunder<bodyand set its> item-idattribute to a WebMap portal item ID.index.htmlUse dark colors for code blocks <body> <arcgis-map item-id="f2481ef191924872be8897179f73d55c"> </arcgis-map> <script type="module" src="main.js"></script> </body> -
Next, add the
<arcgis-chartcomponent to the> bottom-leftslot of the map, and set itslayer-item-idandchart-indexattributes to load an existing bar chart from a feature layer item.index.htmlUse dark colors for code blocks <body> <arcgis-map item-id="f2481ef191924872be8897179f73d55c"> <arcgis-chart layer-item-id="8c7ef8e9f0f149a4972300187dfc70c0" chart-index="0" slot="bottom-left"> </arcgis-chart> </arcgis-map> <script type="module" src="main.js"></script> </body> -
Lastly, add the
<arcgis-charts-action-barcomponent and slot it within the chart component to enable chart actions like filtering and exporting.> index.htmlUse dark colors for code blocks <body> <arcgis-map item-id="f2481ef191924872be8897179f73d55c"> <arcgis-chart layer-item-id="8c7ef8e9f0f149a4972300187dfc70c0" chart-index="0" slot="bottom-left"> <arcgis-charts-action-bar slot="action-bar"></arcgis-charts-action-bar> </arcgis-chart> </arcgis-map> <script type="module" src="main.js"></script> </body>
Interacting with the chart and map
-
In
main.js, usedocument.queryto get references for theSelector() <arcgis-mapand> <arcgis-chartcomponents.> main.jsUse dark colors for code blocks const mapElement = document.querySelector("arcgis-map"); const chartElement = document.querySelector("arcgis-chart"); let highlightSelect; -
Since we are going to use property values from the map component, we'll use the
arcgisevent to determine when the map is ready.View Ready Change main.jsUse dark colors for code blocks mapElement.addEventListener("arcgisViewReadyChange", (event) => { }); -
Inside the event listener, get the
mapandviewfrom the event target.main.jsUse dark colors for code blocks mapElement.addEventListener("arcgisViewReadyChange", (event) => { const { map, view } = event.target; });
Filter chart data by map extent
-
In the callback function of the
arcgisevent listener, set the chart element’sView Ready Change viewproperty to the map’sview.main.jsUse dark colors for code blocks mapElement.addEventListener("arcgisViewReadyChange", (event) => { const { map, view } = event.target; chartElement.view = view; }); -
This will enable the chart to filter its data based on the current map extent. To use this, you can click on the
Filter by extentaction in the chart's action bar. When you pan or zoom the map, the chart will automatically update to show only the features that are currently within the map's extent.
Synchronize chart selection with map highlights
-
Outside of the
arcgisevent listener, declare a variableView Ready Change highlightto hold the highlight handle for the map.Select main.jsUse dark colors for code blocks const mapElement = document.querySelector("arcgis-map"); const chartElement = document.querySelector("arcgis-chart"); let highlightSelect; -
Add a listener for the
arcgisevent on the chart element to clear the previous selection and highlight the new features on the map.Selection Complete main.jsUse dark colors for code blocks mapElement.addEventListener("arcgisViewReadyChange", (event) => { const { map, view } = event.target; chartElement.addEventListener("arcgisSelectionComplete", (event) => { if (highlightSelect) { highlightSelect.remove(); } highlightSelect = view.layerViews.items[0].highlight(event.detail.selectionData.selectionOIDs); }); }); -
Now when you select features on the chart by clicking or dragging, the corresponding features will be highlighted on the map.
Highlight chart data based on map selections
-
Outside of the
arcgisevent listener, add a second event listener to theView Ready Change map:Element arcgis.View Click main.jsUse dark colors for code blocks mapElement.addEventListener("arcgisViewClick", (event) => { }); -
In the
arcgisevent listener's callback, get theView Click viewfrom the event target.main.jsUse dark colors for code blocks mapElement.addEventListener("arcgisViewClick", (event) => { const { view } = event.target; }); -
Get the
screenfrom the event'sPoint detailproperty.main.jsUse dark colors for code blocks mapElement.addEventListener("arcgisViewClick", (event) => { const { view } = event.target; const { screenPoint } = event.detail; view.hitTest(screenPoint).then(getFeatures); }); -
Lastly, get the features from the
hit. Within the function, get the selected feature's OID (in this case, the attribute isTest Object) and assign it to the chart element's selection data.Id main.jsUse dark colors for code blocks mapElement.addEventListener("arcgisViewClick", (event) => { const { view } = event.target; const { screenPoint } = event.detail; view.hitTest(screenPoint).then(getFeatures); function getFeatures(response) { const selectedFeatureOID = response.results[0].graphic.attributes["ObjectId"]; chartElement.selectionData = { selectionOIDs: [selectedFeatureOID], }; } });
Run the application
The application should display a bar chart overlaying on bottom left of the map. When you select features on the chart, the corresponding features will be highlighted on the map. With the Filter By Extent action from the action bar enabled, the bar chart will only show features within the current map extent. Additionally, selecting features on the map will highlight the corresponding features on the chart.