Learn how to display a chart from a web map stored in ArcGIS and how to create a new chart using charts model.
In this tutorial, you will:
For help with this tutorial, please post on the Esri community site.
Prerequisites ArcGIS Accounts:You need an ArcGIS Location Platform or ArcGIS Online account.
Steps Create a new penDefine a basic HTML page.
Use dark colors for code blocks Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
<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 arcgis/charts-components</title>
<style></style>
</head>
<body>
</body>
</html>
<head>
tag, add references to the JavaScript SDK core library and CSS, calcite components and charts components.
Use dark colors for code blocks Copy
1
2
3
4
5
6
7
8
9
<!-- Load Calcite components from CDN -->
<script type="module" src="https://js.arcgis.com/calcite-components/3.2.1/calcite.esm.js"></script>
<!-- Load the ArcGIS Maps SDK for JavaScript from CDN -->
<link rel="stylesheet" href="https://js.arcgis.com/4.33/esri/themes/light/main.css" />
<script src="https://js.arcgis.com/4.33/"></script>
<!-- Load Charts components from CDN-->
<script type="module" src="https://js.arcgis.com/4.33/charts-components/"></script>
<body>
tag, create a <div>
element with a class set to chart-container
. Add the <arcgis-chart>
component inside the <div>
with an id set to scatterplot
.
Use dark colors for code blocks Copy
1
2
3
<div class="chart-container">
<arcgis-chart id="scatterplot"></arcgis-chart>
</div>
<style>
tag, add CSS to style the chart container and the scatterplot.
Use dark colors for code blocks Copy
1
2
3
4
5
6
7
8
.chart-container {
display: flex;
height: 80vh;
}
#scatterplot {
flex: 1;
}
<script>
at the bottom of the <body>
, use $arcgis.import()
to add the WebMap
module.96cb2d2825dc459abadcabc941958125
.document.querySelector()
to select the <arcgis-chart>
component with the id of scatterplot
.layer
property to the feature layer, then set the model
property to the scatterplot's model.
Use dark colors for code blocks Copy
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
<!-- Step 1 -->
<script type="module">
const WebMap = await $arcgis.import("@arcgis/core/WebMap.js");
// Step 2
const webmap = new WebMap({
portalItem: {
id: "96cb2d2825dc459abadcabc941958125",
},
});
await webmap.loadAll();
// Step 3
const featureLayer = webmap.layers.find(
(layer) => layer.title === "College Scorecard"
);
// Step 4
const scatterplotElement = document.querySelector("#scatterplot");
// Step 5
const scatterplotModel = featureLayer.charts[0];
// Step 6
scatterplotElement.layer = featureLayer;
scatterplotElement.model = scatterplotModel;
</script>
<arcgis-chart>
component inside the <div class="chart-container">
with an id set to pie-chart
.
Use dark colors for code blocks Copy
1
2
3
4
<div class="chart-container">
...
<arcgis-chart id="pie-chart"></arcgis-chart>
</div>
<style>
tag, add CSS to style the pie chart.
Use dark colors for code blocks Copy
1
2
3
4
#pie-chart,
#scatterplot {
flex: 1;
}
createModel
from the charts components CDN.document.querySelector()
to select the <arcgis-chart>
component with the id of pie-chart
.createModel()
to create a new pie chart model with the feature layer from the webmap. The chart type is set to pieChart
.model
property to the pieChartModel
.
Use dark colors for code blocks Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<script type="module">
// Step 1
const { createModel } = await $arcgis.import("@arcgis/charts-components/model.js");
// Step 2
const pieChartElement = document.querySelector("#pie-chart");
// Step 3
const pieChartModel = await createModel({ layer: featureLayer, chartType: "pieChart" });
// Step 4
await pieChartModel.setCategory("Type");
pieChartModel.setDataLabelsVisibility(true);
pieChartModel.setTitleText("Count by School Type");
pieChartModel.setLegendTitleText("School Type");
pieChartModel.setLegendPosition("bottom");
// Step 5
pieChartElement.model = pieChartModel;
</script>
In CodePen, run your code to display the charts.
You should see a scatterplot showing the relationship between education cost and income earnings with a line displaying the linear trend. It should also display a pie chart showing the count of schools by type.
What's next?Learn how to use additional API features and ArcGIS services in these tutorials:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4