Skip to content

Bar

Creates SVG bars based on an array of data.

In its default vertical mode each bar spans from the baseline y (250) up to its per-datum y1 value, so the bar height is |y1 - y|. Bars are placed horizontally by x, sized by a data-bound width, and colored from a per-datum fill.

const config = {
  data: [
    {id: "a", x: 90, top: 90, width: 70, fill: "#5d6d7e"},
    {id: "b", x: 210, top: 40, width: 70, fill: "#cc4b4b"},
    {id: "c", x: 330, top: 150, width: 70, fill: "#3a7ca5"},
  ],
  x: "x",
  y: 250,
  y1: d => d.top,
  width: "width",
  fill: d => d.fill,
};

Giving each datum an x1 instead of a y1 switches the bar to horizontal: it runs from the shared x baseline (80) to its own x1, and height replaces width as the bar’s thickness.

const config = {
  data: [
    {id: "a", y: 70, right: 420, fill: "#5d6d7e"},
    {id: "b", y: 160, right: 260, fill: "#cc4b4b"},
    {id: "c", y: 250, right: 560, fill: "#3a7ca5"},
  ],
  fill: d => d.fill,
  x: 80,
  x1: d => d.right,
  y: "y",
};

Configuration

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

Extends Shape → BaseClass.

activefunction | null

The active callback function for highlighting shapes.

activeStylerecord

The style to apply to active shapes.

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.

configd3plusconfig

Methods that correspond to the key/value pairs and returns this class.

dataarray.&lt;datapoint&gt;

The data array used to create shapes. A shape will be drawn for each object in the array.

hoverfunction | null

The hover callback function for highlighting shapes on mouseover.

hoverStylerecord

The style to apply to hovered shapes.

labelConfigrecord

A pass-through to the config method of the TextBox class used to create a shape's labels.

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.

onstring

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

parentunknown

Parent config used by the wrapper.

selectstring | htmlelement | svgelement | null

The SVG container element as a d3 selector or DOM element.

shapeConfigd3plusconfig

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

sortfunction | null

A comparator function used to sort shapes for layering order.

textureDefaultrecord

A series of global texture methods to be used for all textures (ie. {stroke: "darkorange", strokeWidth: 2}).

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.

x0number | accessorfn

The x0 (left edge) position accessor for each bar.

x1number | accessorfn | null

The x1 (right edge) position accessor for each bar.

y0number | accessorfn

The y0 (top edge) position accessor for each bar.

y1number | accessorfn | null

The y1 (bottom edge) position accessor for each bar.