Charts components with Map components

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-map component and create a chart with an arcgis-chart component.
  • Add an arcgis-charts-action-bar component 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.
Charts components with map components

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

  1. Download the initial Vite vanilla JavaScript project to your local machine.
  2. Unzip the downloaded file.
  3. Open the unzipped folder's files in your text editor.
  4. Navigate to the unzipped folder in your terminal.

Install dependencies

  1. Install Charts Components and Map Components in the terminal as dependencies.

    Use dark colors for code blocksCopy
    1
    npm install @arcgis/charts-components @arcgis/map-components
  2. Start the Vite development server.

    Use dark colors for code blocksCopy
    1
    npm run dev
  3. 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.

  4. Import the <arcgis-map> component and the <arcgis-placement> component from the @arcgis/map-components package in main.js.

    Use dark colors for code blocksCopy
    1
    2
    import "@arcgis/map-components/components/arcgis-map";
    import "@arcgis/map-components/components/arcgis-placement";
  5. Import the <arcgis-chart> component and the <arcgis-charts-action-bar> component from the @arcgis/charts-components package in main.js.

    Use dark colors for code blocksCopy
    1
    2
    import "@arcgis/charts-components/components/arcgis-chart";
    import "@arcgis/charts-components/components/arcgis-charts-action-bar";

Add styles

  1. In style.css, import the @arcgis/core light theme CSS stylesheet.

    Use dark colors for code blocksCopy
    1
    @import "https://js.arcgis.com/4.33/@arcgis/core/assets/esri/themes/light/main.css";
  2. Add CSS styles to make the html, body and arcgis-map elements the full height of their parent containers.

    Use dark colors for code blocksCopy
    1
    2
    3
    4
    5
    6
    html,
    body,
    arcgis-map {
       height: 100%;
       margin: 0;
    }
  3. Add CSS styles to the arcgis-chart element.

    Use dark colors for code blocksCopy
    1
    2
    3
    4
    arcgis-chart {
       height: 100%;
       width: 200%;
    }
  4. Import the CSS file in main.js.

    Use dark colors for code blocksCopy
    1
    import "./style.css";

Add components

  1. Add the <arcgis-map> component to index.html under <body>.

    Use dark colors for code blocksCopy
    1
    <arcgis-map item-id="f2481ef191924872be8897179f73d55c"></arcgis-map>
  2. Add the <arcgis-placement> component inside the <arcgis-map> component to position the chart on the map.

    Use dark colors for code blocksCopy
    1
    2
    3
    4
    <arcgis-map item-id="f2481ef191924872be8897179f73d55c">
      <arcgis-placement position="bottom-left">
      </arcgis-placement>
    </arcgis-map>
  3. Add the <arcgis-chart> component under the placement component, and slot the <arcgis-charts-action-bar> component into it using slot="action-bar" to enable chart actions like filtering and exporting.

    Use dark colors for code blocksCopy
    1
    2
    3
    4
    5
    6
    7
    <arcgis-map item-id="f2481ef191924872be8897179f73d55c">
       <arcgis-placement position="bottom-left">
         <arcgis-chart>
           <arcgis-charts-action-bar slot="action-bar"></arcgis-charts-action-bar>
         </arcgis-chart>
       </arcgis-placement>
     </arcgis-map>

Create a reference to components

  1. In main.js, use document.querySelector() to get references for the <arcgis-map>, <arcgis-chart> and <arcgis-charts-action-bar> components.
    Use dark colors for code blocksCopy
    1
    2
    3
    const mapElement = document.querySelector("arcgis-map");
    const chartElement = document.querySelector("arcgis-chart");
    const actionBarElement = document.querySelector("arcgis-charts-action-bar");

Load the bar chart

  1. Since we are going to use property values from the map component, we'll use the arcgisViewReadyChange event to determine when the map is ready.

    Use dark colors for code blocksCopy
    1
    2
    3
    mapElement.addEventListener("arcgisViewReadyChange", async (event) => {
      // to be implemented
    });
  2. Inside the event listener, get the map and view from the event target.

    Use dark colors for code blocksCopy
    1
    const { map, view } = event.target;
  3. Use find to get the CollegeScorecard layer from the map.

    Use dark colors for code blocksCopy
    1
    const layer = map.layers.find((layer) => layer.title === "CollegeScorecard");
  4. First, set the chart element’s layer property to the feature layer. Then, set its model property using the first existing chart model from the layer. Setting the model last ensures the chart renders correctly.

    Use dark colors for code blocksCopy
    1
    2
    chartElement.layer = layer;
    chartElement.model = layer.charts[0];

Filter chart data by map extent

  1. In the callback function of the arcgisViewReadyChange event listener, set the chart element’s view property to the map’s view.

    Use dark colors for code blocksCopy
    1
    chartElement.view = view;
  2. This will enable the chart to filter it's data based on the current map extent. To use this, you can click on the Filter by extent action 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

  1. In the callback function of the arcgisViewReadyChange event listener, get the layer views from the view.

    Use dark colors for code blocksCopy
    1
    const featureLayerViews = view.layerViews;
  2. Add a listener for the arcgisSelectionComplete event on the chart element to clear the previous selection and highlight the new features on the map.

    Use dark colors for code blocksCopy
    1
    2
    3
    4
    chartElement.addEventListener("arcgisSelectionComplete", (event) => {
      map.highlightSelect?.remove();
      map.highlightSelect = featureLayerViews.items[0].highlight(event.detail.selectionData.selectionOIDs);
    });

Highlight chart data based on map selections

  1. Outside of the arcgisViewReadyChange event listener, add a second event listener to the mapElement: arcgisViewClick.

    Use dark colors for code blocksCopy
    1
    2
    3
    viewElement.addEventListener("arcgisViewClick", (event) => {
      // to be implemented
    });
  2. In the arcgisViewClick event listener's callback, get the view from the event target.

    Use dark colors for code blocksCopy
    1
    const { view } = event.target;
  3. Get the screen points from the event's detail property.

    Use dark colors for code blocksCopy
    1
    2
    let screenPoints = event.detail.screenPoint;
    view.hitTest(screenPoints).then(getFeatures);
  4. Lastly, get the features from the hit test. Within the function, get the selected feature's OID (in this case, the attribute is ObjectId) and assign it to the chart element's selection data.

    Use dark colors for code blocksCopy
    1
    2
    3
    4
    5
    6
    7
    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.

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.