ColorScale
Creates an SVG color scale based on an array of data.
Linear scale
Section titled “Linear scale”The default scale: "linear" maps values to a continuous single-hue gradient (blue). A smooth ramp is best when every value matters equally and you want to read magnitude by eye.
The kind of scale: "linear" or "log" (a continuous gradient), or "jenks", "buckets", or "quantile" (discrete buckets). A function picks one on each render: it receives the numeric values the scale is built from (sorted, without null or NaN) and their [min, max] extent, with the ColorScale as this, and returns a scale name, as in (values, [min, max]) => max - min > 5 ? "jenks" : "linear". An unknown name logs a warning and builds a linear scale. Defaults to "linear", and to "jenks" in a chart's colorScaleConfig.
Jenks scale
Section titled “Jenks scale”scale: "jenks" splits the data into natural-breaks (ckmeans) buckets — discrete steps placed where the data clusters, minimizing within-bucket variance. This is the default for charts (Geomap choropleths) because it reveals grouping in skewed data that a linear ramp flattens.
The kind of scale: "linear" or "log" (a continuous gradient), or "jenks", "buckets", or "quantile" (discrete buckets). A function picks one on each render: it receives the numeric values the scale is built from (sorted, without null or NaN) and their [min, max] extent, with the ColorScale as this, and returns a scale name, as in (values, [min, max]) => max - min > 5 ? "jenks" : "linear". An unknown name logs a warning and builds a linear scale. Defaults to "linear", and to "jenks" in a chart's colorScaleConfig.
Buckets scale
Section titled “Buckets scale”scale: "buckets" divides the domain into equal-width steps — the breakpoints are evenly spaced by value (unlike jenks, which follows the data). Set the count with buckets.
The kind of scale: "linear" or "log" (a continuous gradient), or "jenks", "buckets", or "quantile" (discrete buckets). A function picks one on each render: it receives the numeric values the scale is built from (sorted, without null or NaN) and their [min, max] extent, with the ColorScale as this, and returns a scale name, as in (values, [min, max]) => max - min > 5 ? "jenks" : "linear". An unknown name logs a warning and builds a linear scale. Defaults to "linear", and to "jenks" in a chart's colorScaleConfig.
Quantile scale
Section titled “Quantile scale”scale: "quantile" puts an equal count of data points in each bucket, so every color represents the same share of the data. Breakpoints bunch up where values are dense.
The kind of scale: "linear" or "log" (a continuous gradient), or "jenks", "buckets", or "quantile" (discrete buckets). A function picks one on each render: it receives the numeric values the scale is built from (sorted, without null or NaN) and their [min, max] extent, with the ColorScale as this, and returns a scale name, as in (values, [min, max]) => max - min > 5 ? "jenks" : "linear". An unknown name logs a warning and builds a linear scale. Defaults to "linear", and to "jenks" in a chart's colorScaleConfig.
Log scale
Section titled “Log scale”scale: "log" spaces the gradient logarithmically, so each order of magnitude gets comparable color range. Use it when values span several powers of ten (all values must share a sign).
The kind of scale: "linear" or "log" (a continuous gradient), or "jenks", "buckets", or "quantile" (discrete buckets). A function picks one on each render: it receives the numeric values the scale is built from (sorted, without null or NaN) and their [min, max] extent, with the ColorScale as this, and returns a scale name, as in (values, [min, max]) => max - min > 5 ? "jenks" : "linear". An unknown name logs a warning and builds a linear scale. Defaults to "linear", and to "jenks" in a chart's colorScaleConfig.
Diverging scale
Section titled “Diverging scale”When the data spans both sides of the midpoint (default 0), the scale becomes diverging automatically: red (low) → neutral gray → blue (high), so the two directions read as opposites around the center. Move the center with midpoint.
Scale function
Section titled “Scale function”scale also accepts a function that picks the scale on every render. It receives the numeric values the scale is built from (sorted, without null or NaN) and their [min, max] extent, and returns a scale name. Here a range wider than 5 gets jenks buckets and a narrower one gets a linear gradient, so new data (from a timeline or a filter) can switch between them.
The kind of scale: "linear" or "log" (a continuous gradient), or "jenks", "buckets", or "quantile" (discrete buckets). A function picks one on each render: it receives the numeric values the scale is built from (sorted, without null or NaN) and their [min, max] extent, with the ColorScale as this, and returns a scale name, as in (values, [min, max]) => max - min > 5 ? "jenks" : "linear". An unknown name logs a warning and builds a linear scale. Defaults to "linear", and to "jenks" in a chart's colorScaleConfig.
ƒ (function)Choosing the labels
Section titled “Choosing the labels”A gradient, or a bucketed scale drawn as one bar (bucketAxis: true), is labeled by an Axis that axisConfig configures. The axis picks round values to label, wraps long labels, and hides the ones that would crowd their neighbors, keeping the labels at its two ends. To choose the labels yourself, list their values in labels (and the values to mark in ticks); tickFormat writes their text. A bucketed scale drawn as swatches labels every bucket instead, with text from bucketFormat and bucketJoiner. In a chart, these go in colorScaleConfig, as in colorScaleConfig: {axisConfig: {labels: [200, 6400]}}.
In a chart, a scale along one of its sides takes at most a third of the chart’s width (a scale at the left or right) or a third of its height (top or bottom), its bar, ticks, labels and title together: labels that need more wrap, are cut short, or are left out. A gradient or a bucketed bar keeps to its third on a short chart too: when its labels don’t fit beside its title, the title is left out, and when even a line of labels doesn’t fit, the bar is drawn without them. At the top or bottom, that’s a chart shorter than about 230px for the title and 165px for the labels. colorScaleConfig: {axisConfig: {maxSize: …}} sets the room for the labels and title instead, as a number of pixels or a function of that width or height.
A scale drawn as swatches keeps to the same third: its labels wrap onto more lines, at the top or bottom its swatches take more rows, and labels that still don’t fit are cut short with “…”. Its title wraps within the third too, and is left out first. colorScaleConfig: {legendConfig: {width: …}} (or height, for a scale at the top or bottom) sets another limit, in pixels.
The scale’s own width (at the left or right) or height (at the top or bottom), as in colorScaleConfig: {width: 120}, replaces the third for any kind of scale, keeping it against its edge of the chart. Along the top or bottom, width is the scale’s length instead, as height is at the left or right.
A scale at the top keeps clear of the chart’s corner buttons, as a top legend does: it sits between them when it draws as well there, its swatches keeping every label and its bar at least half of its labels, and otherwise drops below them, where it takes at most a third of the chart.
The kind of scale: "linear" or "log" (a continuous gradient), or "jenks", "buckets", or "quantile" (discrete buckets). A function picks one on each render: it receives the numeric values the scale is built from (sorted, without null or NaN) and their [min, max] extent, with the ColorScale as this, and returns a scale name, as in (values, [min, max]) => max - min > 5 ? "jenks" : "linear". An unknown name logs a warning and builds a linear scale. Defaults to "linear", and to "jenks" in a chart's colorScaleConfig.
Draws a bucket-type scale ("jenks", "quantile", …) as one bar with an axis along it, labeled where the buckets start, instead of a labeled swatch for each bucket. Defaults to false.
Hover mark
Section titled “Hover mark”hover marks a value on the scale, as a chart does while one of its shapes is hovered: an extra axis tick marks the value on the bar, and a badge reads it where that tick’s label would sit. A bucketed bar (bucketAxis) also outlines the value’s bucket; a scale drawn as swatches draws no mark. hoverMark(false) turns it off; hoverMarkConfig styles the tickConfig, badgeConfig, and outlineConfig.
Whether hovering a shape marks its value on the color scale: a tick and a badge with the value (and, on a bucketAxis, an outline around its bucket). A swatch scale lights the value's swatch instead. Defaults to true.
The kind of scale: "linear" or "log" (a continuous gradient), or "jenks", "buckets", or "quantile" (discrete buckets). A function picks one on each render: it receives the numeric values the scale is built from (sorted, without null or NaN) and their [min, max] extent, with the ColorScale as this, and returns a scale name, as in (values, [min, max]) => max - min > 5 ? "jenks" : "linear". An unknown name logs a warning and builds a linear scale. Defaults to "linear", and to "jenks" in a chart's colorScaleConfig.
Bivariate scale
Section titled “Bivariate scale”bivariate: true colors by two variables at once: value returns a [first, second] pair for each row, and each variable is split into bivariateBuckets quantile bins (2 to 4, default 3), every bin holding about the same number of rows, with tied values sharing one. The pair of bins picks a color from bivariateColors, a square grid listed row by row from the low/low corner (each row one bin of the second variable, each column one of the first). It defaults to Joshua Stevens’ blue–red 3×3 scheme, the first variable rising toward red and the second toward blue, and a grid of another size is blended bilinearly to fit, so 2×2 keeps its corners and 4×4 blends between its colors. The key turns the grid 45° into a diamond, low/low at the bottom, with bivariateLabels naming each variable along its lower edge. A chart sets all but the colors and labels for you when its colorScale names two fields (see Geomap’s bivariate choropleth).
How many quantile bins a bivariate scale splits each variable into, from 2 to 4. Defaults to 3.
Configuration
import {ColorScale} from "@d3plus/core";
Extends BaseClass.
alignstringDefaultmiddleaxisConfigrecordThe ColorScale is constructed by combining an Axis for the ticks/labels and a Rect for the actual color box (or multiple boxes, as in a jenks scale). Because of this, there are separate configs for the Axis class used to display the text (axisConfig) and the Rect class used to draw the color breaks (rectConfig). This method acts as a pass-through to the config method of the Axis. A scale drawn as swatches has no Axis: its
titleandtitleTooltipapply to the swatches' title, and itstickTooltipto each swatch's label, given the value and index of the label at that bucket's start on abucketAxisbar.bivariatebooleanDefaultfalseDraws the scale as a bivariate key: each row's
valueis a pair of numbers, and a chart sets this when itscolorScalenames two fields. Defaults tofalse.bivariateBucketsnumberDefault3How many quantile bins a bivariate scale splits each variable into, from
2to4. Defaults to3.bivariateColorsarray.<string>A bivariate scale's colors: a square grid, row-major from the low/low corner, each row one bin of the second variable and each column one bin of the first. A grid of another size than
bivariateBucketsis blended to fit. Defaults to Joshua Stevens' blue–red 3×3 scheme.bivariateLabels[string, string]A bivariate key's names for its two variables, in
colorScaleorder. Defaults to the two field names.bucketAxisbooleanDefaultfalseDraws a bucket-type scale ("jenks", "quantile", …) as one bar with an axis along it, labeled where the buckets start, instead of a labeled swatch for each bucket. Defaults to
false.bucketFormatfunctionFormats the label for each bucket in a bucket-type scale ("jenks", "quantile", …). Passed the bucket's start value, its index, the full bucket array, and every data value used to build the buckets.
bucketJoinerfunctionGiven a bucket's minimum and maximum values, returns the full label.
bucketsnumberDefault5centeredbooleanDefaulttrueclassNamestring|array.<string>|undefinedCSS classes added to the outermost element this chart or component draws into, next to its fixed d3plus classes, so page CSS can target it: a chart's root
<svg class="d3plus-viz">(its<canvas>with the Canvas renderer) and a standalone component's<svg>. Takes a space-separated string or an array. A new value replaces the classes the previous one added; unset adds none. Tooltips take their owntooltipConfig.className.colorstring|array.<string>The scale's colors: one color, expanded into a light→dark ramp, or an array of colors to step through.
colorDefaultscolordefaultsconfigOverrides the default colors used when assigning fills from data and choosing legible text colors:
darkandlight(the text colors picked for contrast against a background),missing(null/undefined values),on/off(true/falsevalues),sequential(the anchor hue for magnitude ramps), andscale(the categorical palette, given as a d3 ordinal scale or an array of colors). Keys are merged into the current defaults, and a Viz passes its overrides down to the shapes and components it draws.colorMaxunknowncolorMidunknowncolorMinunknownconfigd3plusconfigMethods that correspond to the key/value pairs and returns this class.
dataarray.<datapoint>Default[ ]The data array used to create shapes. A shape key will be drawn for each object in the array.
domainarray.<number>For a linear scale, the
[min, max]values used by the color scale; values outside this range map to the nearest color.durationnumberDefault600Animation duration in ms.
heightnumberDefault200Overall height of the visualization in pixels.
hovernumber|false|[number, number]Marks a value on the scale: an extra axis tick where the value falls on the color bar and a badge reading it where that tick's label would sit, plus an outline around its bucket on a bucketed bar (
bucketAxis). A scale drawn as discrete swatches draws no mark, and a bivariate scale takes a pair of values. A chart sets this while one of its shapes is hovered. Passfalseto clear it. Turned off withhoverMark(false).hoverMarkbooleanDefaulttrueWhether hovering a shape marks its value on the color scale: a tick and a badge with the value (and, on a
bucketAxis, an outline around its bucket). A swatch scale lights the value's swatch instead. Defaults totrue.hoverMarkConfigcolorscalehovermarkconfigStyles the
hoverMark:tickConfig(stroke,strokeWidth,length),badgeConfig(fill,stroke,strokeWidth,fontColor,fontFamily,fontSize,fontWeight,padding,rx), andoutlineConfig, the bucket outline on abucketAxisbar (stroke,strokeWidth). Each is a value or a function of the hovered value.labelConfigrecordA pass-through for the TextBox class used to style the labelMin and labelMax text.
labelMaxstringDefines a text label to be displayed off of the end of the maximum point in the scale (currently only available in horizontal orientation).
labelMinstringDefines a text label to be displayed off of the end of the minimum point in the scale (currently only available in horizontal orientation).
legendConfigrecordConfiguration passed to the Legend that draws the scale when its values are rendered as discrete swatches instead of a continuous bar (for example a categorical or buckets scale), acting as a pass-through to that Legend's config method.
localestring|objectThe locale used for all text and number formatting. Supports the locales defined in d3plus-format. The locale can be a complex Object, a locale code (like "en-US"), or a 2-digit language code (like "en"). If a 2-digit code is provided, the "findLocale" function is used to identify the most approximate locale.
midpointnumberDefault0onstringEvent listener for the specified event typenames. Mirrors the core d3-selection behavior.
ordinalRampbooleanDefaultfalseHolds the palest step of a single-hue ramp darker on a light theme (
colorRamp'sordinaloption), so the lowest values still read as thin lines against the background instead of fading into it. Dark themes keep their ramps. Defaults tofalse.orientconstoraccessorDefaultbottomOrientation: "vertical" or "horizontal".
paddingcalloutvalueDefault5Space between the text and the box's edge, in pixels. Defaults to
6.parentunknownParent config used by the wrapper.
rectConfigrecordThe ColorScale is constructed by combining an Axis for the ticks/labels and a Rect for the actual color box (or multiple boxes, as in a jenks scale). Because of this, there are separate configs for the Axis class used to display the text (axisConfig) and the Rect class used to draw the color breaks (rectConfig). This method acts as a pass-through to the config method of the Rect.
renderfunctionRenders the current ColorScale to the page.
scalecolorscaletypesettingDefaultlinearThe kind of scale:
"linear"or"log"(a continuous gradient), or"jenks","buckets", or"quantile"(discrete buckets). A function picks one on each render: it receives the numeric values the scale is built from (sorted, without null or NaN) and their[min, max]extent, with the ColorScale asthis, and returns a scale name, as in(values, [min, max]) => max - min > 5 ? "jenks" : "linear". An unknown name logs a warning and builds a linear scale. Defaults to"linear", and to"jenks"in a chart'scolorScaleConfig.selectstring|htmlelementThe SVG container element for this visualization. 3 selector or DOM element.
shapeConfigd3plusconfigConfiguration object with key/value pairs applied as method calls on each shape.
sizestringDefault10Size accessor key.
translatefunctionDefines how informational text strings should be displayed. By default, this function will try to find the string in question (which is the first argument provided to this function) inside of an internally managed translation Object. If you'd like to override to use custom text, simply pass this method your own custom formatting function.
valuestring|functionValue accessor for the visualization.
widthnumberDefault400Overall width of the visualization in pixels.