Display and create charts

Learn how to display and create charts using Charts Components and Charts Model.

In this tutorial, you will:

  • Load and display a chart from a webmap and feature layer stored in ArcGIS Online.
  • Create a new chart using the charts model and display it in your app.
Display and create charts

Steps

Create a new pen

  1. Go to CodePen to create a new pen for your application.

Add basic HTML

Define a basic HTML page.

  1. In CodePen > HTML, add HTML to create a basic page.
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<html>

<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no" />
  <title>ArcGIS Maps SDK for JavaScript Tutorials: Display and create charts with charts-components & charts-model</title>

  <style>
  </style>
</head>

<body>
</body>

</html>

Display a chart with @arcgis/charts-components

Add references

  1. In the <head> tag, add references to the ArcGIS core library and CSS, and the @arcgis/charts-components package.
Use dark colors for code blocksCopy
1
2
3
<link rel="stylesheet" href="https://js.arcgis.com/4.29/esri/themes/dark/main.css" />
<script src="https://js.arcgis.com/4.29"></script>
<script type="module" src="https://js.arcgis.com/charts-components/4.29/arcgis-charts-components.esm.js"></script>

Add a chart component

  1. In the <body> tag, create a <div> element with a class set to chart-container. Add the <arcgis-charts-scatter-plot> component inside the <div> with an id set to scatterplot.
Use dark colors for code blocksCopy
1
2
3
<div class="chart-container">
  <arcgis-charts-scatter-plot id="scatterplot"></arcgis-charts-scatter-plot>
</div>

Add styling

  1. In the <style> tag, add CSS to style the chart container and the scatterplot.
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
.chart-container {
    display: flex;
    height: 80vh;
}

#scatterplot {
    flex: 1;
}

Add script logic for displaying a chart

  1. Add a <script type="module"> section in the <body>.
  2. Define a function named loadFeatureLayer() for loading a feature layer from a webmap by passing in the webmapId and layerTitle.
  3. Load the feature layer item by calling the asynchronous function loadFeatureLayer(webmapId, layerTitle).
  4. Use document.getElementById() to refer the arcgis-charts-scatter-plot Component.
  5. Get the first chart's configuration from the feature layer.
  6. Assign the scatterplot configuration and the feature layer to the scatterplot element.
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<!-- Step 1 -->
<script type="module">
  // Step 2
  async function loadFeatureLayer(webmapId, layerTitle) {
    const WebMap = await $arcgis.import("esri/WebMap");
    const webmap = new WebMap({
        portalItem: {
            id: webmapId
        }
    });
    await webmap.loadAll();
    const featureLayer = webmap.layers.find((layer) => layer.title === layerTitle);

    return featureLayer;
  }

  // Step 3
  const featureLayer = await loadFeatureLayer("96cb2d2825dc459abadcabc941958125", "College Scorecard");

  // Step 4
  const scatterplotElement = document.getElementById("scatterplot");

  // Step 5
  const scatterplotConfig = await featureLayer.charts[0];

  // Step 6
  scatterplotElement.config = scatterplotConfig;
  scatterplotElement.layer = featureLayer;
</script>

Create a chart with @arcgis/charts-model

Add a chart component

  1. Add the <arcgis-charts-pie-chart> component inside the <div class="chart-container"> with an id set to pie-chart.
Use dark colors for code blocksCopy
1
2
3
4
<div class="chart-container">
  ...
  <arcgis-charts-pie-chart id="pie-chart"></arcgis-charts-pie-chart>
</div>

Add styling

  1. In the <style> tag, add CSS to style the pie chart.
Use dark colors for code blocksCopy
1
2
3
4
#pie-chart
#scatterplot {
    flex: 1;
}

Add script logic for creating a chart

  1. Import the PieChartModel from @arcgis/charts-model CDN.
  2. Use document.getElementById() to refer the arcgis-charts-pie-chart component.
  3. Create a new instance of pie chart model by passing in the necessary parameters (Layer and mode).
  4. Modify some of the properties of the pie chart model.
  5. Get the chart's configuration from the pie chart model.
  6. Assign the pie chart config and the feature layer to the pie chart element.
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<script type="module">
  // Step 1
  import { PieChartModel } from "https://js.arcgis.com/charts-model/4.29/index.js";

  // Step 2
  const pieChart = document.getElementById("pie-chart");

  // Step 3
  const pieChartModel = new PieChartModel({
    layer: featureLayer,
    mode: 'category'
  });

  // Step 4
  pieChartModel.category = 'Type';
  pieChartModel.showDataLabels = true;
  pieChartModel.title = "Count by School Type";
  pieChartModel.legendTitleText = "School Type";
  pieChartModel.legendPosition = "bottom";

  // Step 5
  const pieChartConfig = await pieChartModel.config;

  // Step 6
  pieChart.config = pieChartConfig;
  pieChart.layer = featureLayer;
</script>

Run the app

In CodePen, run your code to display the chart.

The app should display a chart (scatterplot) showing the relationship between education cost and income earnings with a line displaying the linear trend.

What's next?

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