Skip to content

Box

Creates SVG box-and-whisker plots based on an array of data, one per group of values.

Points are grouped by their shared x category (“A” and “B”), which the x accessor maps to a horizontal pixel position, and each group’s raw y values are reduced to a box-and-whisker summary of quartiles and whiskers; orient switches between vertical and horizontal boxes.

const config = {
  data: [
    {x: "A", y: 40},
    {x: "A", y: 55},
    {x: "A", y: 70},
    {x: "A", y: 85},
    {x: "A", y: 110},
    {x: "A", y: 140},
    {x: "B", y: 90},
    {x: "B", y: 120},
    {x: "B", y: 150},
    {x: "B", y: 175},
    {x: "B", y: 210},
    {x: "B", y: 240},
  ],
  x: d => (d.x === "A" ? 130 : 270),
  y: "y",
};

With orient: "horizontal" the roles swap: x carries the values that are summarized and y places each group’s box, so the quartile boxes and whiskers run left to right.

const config = {
  data: [
    {group: "A", v: 40},
    {group: "A", v: 55},
    {group: "A", v: 70},
    {group: "A", v: 85},
    {group: "A", v: 110},
    {group: "A", v: 140},
    {group: "B", v: 90},
    {group: "B", v: 120},
    {group: "B", v: 150},
    {group: "B", v: 175},
    {group: "B", v: 210},
    {group: "B", v: 240},
  ],
  orient: "horizontal",
  x: "v",
  y: d => (d.group === "A" ? 110 : 230),
};

Each group has one value far outside its interquartile range. Under the default whiskerMode (Tukey’s 1.5×IQR rule) those points are drawn as outliers beyond the whiskers, here as Rect markers instead of the default Circle; rectWidth widens the boxes.

const config = {
  data: [
    {x: "A", y: 90},
    {x: "A", y: 100},
    {x: "A", y: 110},
    {x: "A", y: 115},
    {x: "A", y: 125},
    {x: "A", y: 135},
    {x: "A", y: 20},
    {x: "B", y: 150},
    {x: "B", y: 160},
    {x: "B", y: 170},
    {x: "B", y: 180},
    {x: "B", y: 195},
    {x: "B", y: 205},
    {x: "B", y: 300},
  ],
  outlier: "Rect",
  rectWidth: 70,
  x: d => (d.x === "A" ? 130 : 270),
  y: "y",
};

Configuration

import {Box} from "@d3plus/core";

Extends BaseClass.

activefunction | null

The active highlight state for all sub-shapes in this Box.

backgroundImageconstoraccessor

An image drawn inside each box (see ShapeConfig.backgroundImage); rectConfig.backgroundImage overrides it.

backgroundImageFitconstoraccessor

How the box's backgroundImage fits it: "cover" (default) or "contain".

boxMaxstring | function

Precomputed boxes: each row's upper whisker end, a data key or accessor in the same units as the value axis's x or y. See boxMedian.

boxMedianstring | function

Precomputed boxes: each row's median, a data key or accessor in the same units as the value axis's x or y. Setting it, boxMin, boxQ1, boxQ3, or boxMax makes each row one box drawn from those values, with nothing computed and whiskerMode unused. All five are needed (a warning names any left unset), and a row whose values aren't all numbers draws no box.

boxMinstring | function

Precomputed boxes: each row's lower whisker end. See boxMedian.

boxOutliersstring | function

Precomputed boxes: each row's outliers, a data key or accessor returning an array of values drawn past the whiskers. See boxMedian.

boxQ1string | function

Precomputed boxes: each row's lower quartile, where the box starts. See boxMedian.

boxQ3string | function

Precomputed boxes: each row's upper quartile, where the box ends. See boxMedian.

classNamestring | array.<string> | undefined

CSS 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 own tooltipConfig.className.

colorDefaultscolordefaultsconfig

Overrides the default colors used when assigning fills from data and choosing legible text colors: dark and light (the text colors picked for contrast against a background), missing (null/undefined values), on/off (true/false values), sequential (the anchor hue for magnitude ramps), and scale (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.

configpartial

Narrowed .config() for Box. Inherited surface from BaseClass.config(); the override exists only to surface per-shape keys (e.g. width/height for Rect) in autocomplete + type checks.

dataarray.&lt;datapoint&gt;

The data array used to create shapes.

fillconstoraccessor

Each box's fill, resolved against its merged datum (white when unset); rectConfig.fill overrides it. Outliers take it too, resolved against each observation, and the median line defaults to black or white, whichever contrasts with it.

hoverfunction | null

The hover highlight state for all sub-shapes in this Box.

idconstoraccessor

Each box's id, read from its first row; keys every part the box draws. Defaults to the box's x (vertical) or y (horizontal) value.

localestring | object

The 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.

medianConfigrecord

Configuration object for the median line.

onstring

Event listener for the specified event typenames. Mirrors the core d3-selection behavior.

orientconstoraccessor

Orientation: "vertical" or "horizontal".

outlierconstoraccessor

The shape each outlier draws as: "Circle" (default) or "Rect", or false to draw no point. An accessor receives each outlier's observation (a precomputed box's row). In BoxWhisker, the value axis leaves out the outliers it hides and reaches only as far as the whiskers.

outlierConfigrecord

Configuration object for each outlier point.

parentunknown

Parent config used by the wrapper.

rectConfigrecord

Configuration object for the rect shape.

rectWidthconstoraccessor
selectstring | htmlelement | svgelement | null

The SVG container element for this visualization. 3 selector or DOM element.

shapeConfigd3plusconfig

Configuration object with key/value pairs applied as method calls on each shape.

strokeconstoraccessor

Each box's outline color, resolved against its merged datum (black when unset); rectConfig.stroke overrides it.

textureconstoraccessor

Texture (per textures.js) filling each box; rectConfig.texture overrides it.

textureDefaultrecord

Default texture config merged into the box's texture.

translatefunction

Defines 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.

whiskerConfigrecord

Configuration object for the whisker.

whiskerModestring | number | array.&lt;string | number&gt;Default ["tukey","tukey"]

Whisker mode: single mode string/number or [low, high] pair.

xconstoraccessor
yconstoraccessor