This sample demonstrates how to generate a data-driven continuous color visualization based on statistics returned from a numeric field in a SceneLayer.
This is accomplished with the createContinuousRenderer() in the color renderer creator helper object. All that is required for generating a renderer is a SceneLayer and a field name. However, you can set other parameters including a theme. In this case we use an above-and-below
theme, which returns a scheme with two colors diverging from a midpoint.
const colorParams = {
layer: layer,
view: view,
field: "CNSTRCT_YR", // construction year
theme: "above-and-below",
minValue: 1800,
maxValue: 2020
};
colorRendererCreator.createContinuousRenderer(colorParams)
.then((response) => {
// set the renderer to the layer
layer.renderer = response.renderer;
// create the color slider
const colorSlider = new ColorSlider({
primaryHandleEnabled: true,
container: "slider",
min: response.statistics.min,
max: response.statistics.max,
stops: response.visualVariable.stops,
labelFormatFunction: (value) => {
return value.toFixed(0);
}
});
// Since data represents years, we don't
// want values to show decimal places
colorSlider.viewModel.precision = 0;
});
After the ColorSlider is constructed with the statistics of the SceneLayer, you can listen to its events to update the renderer of the layer with the output visual variable in the event object
function changeEventHandler () {
const renderer = layer.renderer.clone();
const colorVariable = renderer.visualVariables[0].clone();
colorVariable.stops = colorSlider.stops;
renderer.visualVariables = [ colorVariable ];
layer.renderer = renderer;
}
colorSlider.on(["thumb-change", "thumb-drag", "min-change", "max-change"], changeEventHandler);
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.