Skip to content

Create a pie chart

In this tutorial, you will create a pie chart programmatically using charts model and a feature layer.

Prerequisites

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 blocks
    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
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    <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: Create a pie chart with arcgis/charts-components</title>
    
      <style>
        html,
        body,
        arcgis-chart {
          height: 100%;
          margin: 0;
        }
      </style>
    
    </head>
    
    
    <body>
    
    
    
    </body>
    
    
    </html>

Add references

  1. In the <head> tag, add references to the JavaScript SDK core library, calcite components and charts components.
    Expand
    Use dark colors for code blocks
    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
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
      <style>
        html,
        body,
        arcgis-chart {
          height: 100%;
          margin: 0;
        }
      </style>
    
      <!-- Load Calcite components from CDN -->
      <script type="module" src="https://js.arcgis.com/calcite-components/3.3.3/calcite.esm.js"></script>
    
      <!-- Load the ArcGIS Maps SDK for JavaScript from CDN -->
      <script src="https://js.arcgis.com/4.34/"></script>
    
      <!-- Load Charts components from CDN-->
      <script type="module" src="https://js.arcgis.com/4.34/charts-components/"></script>
    
    Expand

Add chart component

  1. In the <body> tag, add the <arcgis-chart> component and specify the layer item ID and chart index.
    Expand
    Use dark colors for code blocks
    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
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    
    <body>
    
      <arcgis-chart>
    
      </arcgis-chart>
    
    
    
    </body>
    

Add charts action bar component

  1. Inside the <arcgis-chart> component, add the <arcgis-charts-action-bar> component and specify its slot.
    Expand
    Use dark colors for code blocks
    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
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    
    <body>
    
      <arcgis-chart>
    
        <arcgis-charts-action-bar slot="action-bar"></arcgis-charts-action-bar>
    
      </arcgis-chart>
    
    
    
    </body>
    

Add logic to create a chart

  1. Within the <body> tag, add a <script> tag with type="module" to write JavaScript code to create a chart with charts model.

    Expand
    Use dark colors for code blocks
    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
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
      <script type="module">
    
      </script>
    
    Expand
  2. Within the <script> tag, use @arcgis.import() to import the createModel function from @arcgis/charts-components and FeatureLayer from @arcgis/core/layers/FeatureLayer.js.

    Expand
    Use dark colors for code blocks
    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
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
      <script type="module">
    
        const { createModel } = await $arcgis.import("@arcgis/charts-components");
        const FeatureLayer = await $arcgis.import("@arcgis/core/layers/FeatureLayer.js");
    
      </script>
    
    Expand
  3. Load a feature layer from a portalItem that contains data to be visualized in the pie chart.

    Expand
    Use dark colors for code blocks
    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
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
      <script type="module">
    
        const { createModel } = await $arcgis.import("@arcgis/charts-components");
        const FeatureLayer = await $arcgis.import("@arcgis/core/layers/FeatureLayer.js");
    
        const featureLayer = new FeatureLayer({
          portalItem: {
            id: "8871626e970a4f3e9d6113ec63a92f2f"
          }
        });
    
      </script>
    
    Expand
  4. Use document.querySelector() to select the <arcgis-chart> component.

    Expand
    Use dark colors for code blocks
    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
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
      <script type="module">
    
        const { createModel } = await $arcgis.import("@arcgis/charts-components");
        const FeatureLayer = await $arcgis.import("@arcgis/core/layers/FeatureLayer.js");
    
        const featureLayer = new FeatureLayer({
          portalItem: {
            id: "8871626e970a4f3e9d6113ec63a92f2f"
          }
        });
    
        const pieChartElement = document.querySelector("arcgis-chart");
    
      </script>
    
    Expand
  5. Use createModel() to create a new pie chart model with the feature layer. The chart type is set to pieChart.

    Expand
    Use dark colors for code blocks
    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
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
      <script type="module">
    
        const { createModel } = await $arcgis.import("@arcgis/charts-components");
        const FeatureLayer = await $arcgis.import("@arcgis/core/layers/FeatureLayer.js");
    
        const featureLayer = new FeatureLayer({
          portalItem: {
            id: "8871626e970a4f3e9d6113ec63a92f2f"
          }
        });
    
        const pieChartElement = document.querySelector("arcgis-chart");
    
        const pieChartModel = await createModel({ layer: featureLayer, chartType: "pieChart" });
    
      </script>
    
    Expand
  6. Modify properties of the pie chart model. Refer to the full list of available properties and methods in the pie chart model reference.

    Expand
    Use dark colors for code blocks
    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
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
      <script type="module">
    
        const { createModel } = await $arcgis.import("@arcgis/charts-components");
        const FeatureLayer = await $arcgis.import("@arcgis/core/layers/FeatureLayer.js");
    
        const featureLayer = new FeatureLayer({
          portalItem: {
            id: "8871626e970a4f3e9d6113ec63a92f2f"
          }
        });
    
        const pieChartElement = document.querySelector("arcgis-chart");
    
        const pieChartModel = await createModel({ layer: featureLayer, chartType: "pieChart" });
    
        pieChartModel.category = "Type";
        pieChartModel.dataLabelsVisibility = true;
        pieChartModel.labelCharacterLimit = 20;
        pieChartModel.titleText = "Count by School Type";
        pieChartModel.legendTitleText = "School Type";
        pieChartModel.legendPosition = "bottom";
    
      </script>
    
    Expand
  7. Set the model property to the pieChartModel.

    Expand
    Use dark colors for code blocks
    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
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
      <script type="module">
    
        const { createModel } = await $arcgis.import("@arcgis/charts-components");
        const FeatureLayer = await $arcgis.import("@arcgis/core/layers/FeatureLayer.js");
    
        const featureLayer = new FeatureLayer({
          portalItem: {
            id: "8871626e970a4f3e9d6113ec63a92f2f"
          }
        });
    
        const pieChartElement = document.querySelector("arcgis-chart");
    
        const pieChartModel = await createModel({ layer: featureLayer, chartType: "pieChart" });
    
        pieChartModel.category = "Type";
        pieChartModel.dataLabelsVisibility = true;
        pieChartModel.labelCharacterLimit = 20;
        pieChartModel.titleText = "Count by School Type";
        pieChartModel.legendTitleText = "School Type";
        pieChartModel.legendPosition = "bottom";
    
        pieChartElement.model = pieChartModel;
    
      </script>
    
    Expand

Run the app

In CodePen, run your code to display the chart.

You should see a pie chart showing the count by different school types, along with an action bar to interact with the chart.

What's next?

Learn how to use additional SDK features and ArcGIS services in these tutorials:

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