Generate a class breaks visualization

This sample demonstrates how to generate a data-driven class breaks visualization based on a classification method used to classify data from a numeric field in a FeatureLayer.

This is accomplished with the createClassBreaksRenderer() method in the color renderer creator helper object. All that is required for generating a renderer is a Feature Layer and a field name.

Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const params = {
  layer: layer,
  field: "COL_DEG",
  normalizationField: "EDUCBASECY",
  classificationMethod: "quantile",
  numClasses: 4,
  legendOptions: {
    title: "% population with a college degree"
  }
};

// generate the renderer and set it on the layer
const rendererResponse = await colorRendererCreator.createClassBreaksRenderer(params);
layer.renderer = rendererResponse.renderer;
updateColorSlider(rendererResponse);

You can also create a ClassedColorSlider to allow the user to create manual class breaks.

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
30
31
32
const updateColorSlider = async (rendererResult) => {
  const histogramResult = await histogram({
    layer: layer,
    valueExpression: getValueExpression(fieldSelect.value),
    view: view,
    numBins: 100
  });

  if (!slider) {
    const sliderContainer = document.createElement("div");
    const container = document.createElement("div");
    container.id = "containerDiv";
    container.appendChild(sliderContainer);
    view.ui.add(container, "top-right");

    slider = ClassedColorSlider.fromRendererResult(rendererResult, histogramResult);
    slider.container = container;
    slider.viewModel.precision = 1;

    // update the renderer based on the user's input
    const changeEventHandler = () => {
      const renderer = layer.renderer.clone();
      renderer.classBreakInfos = slider.updateClassBreakInfos(renderer.classBreakInfos);
      layer.renderer = renderer;
    };

    slider.on(["thumb-change", "thumb-drag", "min-change", "max-change"], changeEventHandler);
  } else {
    // update the slider if another variable is selected
    slider.updateFromRendererResult(rendererResult, histogramResult);
  }
};

A word of caution

Keep in mind that generating renderers should be avoided in most applications because of the performance cost affecting the end user. As stated in the Smart Mapping guide topic, the Smart Mapping APIs were designed for two types of applications: data exploration apps and visualization authoring apps similar to ArcGIS Online. In all other cases, renderers should be saved to the layer or manually created using any of the renderer classes.

Additional visualization samples and resources

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