Skip to content

Network

Creates a network visualization based on a defined set of nodes and edges.

Each node supplies its own x/y coordinates, so d3plus places nodes exactly rather than solving a layout for them.

const config = {
  groupBy: "id",
  nodes: [
    {id: "alpha", x: 1, y: 1},
    {id: "beta", x: 2, y: 1},
    {id: "gamma", x: 1, y: 2},
    {id: "epsilon", x: 3, y: 2},
    {id: "zeta", x: 2.5, y: 1.5},
    {id: "theta", x: 2, y: 2},
  ],
  links: [
    {source: 0, target: 1},
    {source: 0, target: 2},
    {source: 3, target: 4},
    {source: 3, target: 5},
    {source: 5, target: 0},
  ],
};

Omitting x/y from the nodes lets a force simulation position them automatically—reach for this when connections matter but there’s no meaningful spatial layout.

const config = {
  nodes: [
    {id: "alpha"},
    {id: "beta"},
    {id: "gamma"},
    {id: "epsilon"},
    {id: "zeta"},
    {id: "theta"},
  ],
  links: [
    {source: 0, target: 1, weight: 10},
    {source: 0, target: 2, weight: 10},
    {source: 3, target: 4, weight: 10},
    {source: 3, target: 5, weight: 5},
    {source: 5, target: 0, weight: 20},
    {source: 2, target: 1, weight: 12},
    {source: 4, target: 5, weight: 12},
  ],
  linkSize: d => d.weight,
};

linkSize takes an accessor returning each link’s weight, which d3plus runs through a linear scale to set stroke thickness—heavier links render thicker.

linkSize

The thickness of each link: a pixel number used for every link, or an accessor function whose values are scaled between linkSizeMin and the smallest node radius (see linkSizeScale).

ƒ (function)

arrows draws an arrowhead at the node boundary to show edge direction. Use "target", "source", "both", or a per-link accessor for bi-directional edges. arrowSize overrides the automatic size.

Draws arrowheads on the links: true (or "target") at each link's target end, "source" at its source end, "both", or false for none. Also accepts an accessor function returning one of those per link.

The arrowhead size in pixels, or an accessor function returning one per link. Defaults to three times the link's stroke width (at least 6).

Sizing nodes with size adds a size legend. When there’s room it is drawn in the open space around the nodes (see legendInset), otherwise in the bottom-right corner. It is drawn with the chart’s own radius scale, which Network fits to the space between nodes. Zoom in and the circles keep their size while the labels change to the values those circles now represent on screen.

The maximum pixel radius used when sizing nodes. Defaults to half the distance between the two closest nodes.

A network gathered in the middle of a wide chart leaves its corners empty, so the legend is drawn in one of them, over a translucent box, instead of taking a margin. Zoom in and the nodes slide under the box while the legend stays readable. Set legendInset to false to keep the legend in its margin.

const config = {
  data: [
    {id: "Hub", region: "Core"},
    {id: "Alpha", region: "North"},
    {id: "Beta", region: "North"},
    {id: "Gamma", region: "East"},
    {id: "Delta", region: "East"},
    {id: "Epsilon", region: "South"},
    {id: "Zeta", region: "South"},
    {id: "Eta", region: "West"},
  ],
  groupBy: ["region", "id"],
  nodes: [
    {id: "Hub", x: 0, y: 0},
    {id: "Alpha", x: -1, y: -1.2},
    {id: "Beta", x: 0.4, y: -1.4},
    {id: "Gamma", x: 1.3, y: -0.3},
    {id: "Delta", x: 1.2, y: 0.6},
    {id: "Epsilon", x: 0.2, y: 1.3},
    {id: "Zeta", x: -0.8, y: 1},
    {id: "Eta", x: -1.4, y: 0.1},
  ],
  links: [
    {source: "Hub", target: "Alpha"},
    {source: "Hub", target: "Beta"},
    {source: "Hub", target: "Gamma"},
    {source: "Hub", target: "Delta"},
    {source: "Hub", target: "Epsilon"},
    {source: "Hub", target: "Zeta"},
    {source: "Hub", target: "Eta"},
    {source: "Alpha", target: "Beta"},
    {source: "Gamma", target: "Delta"},
  ],
};

A node’s label stays inside it when it fits there at a readable size (pointLabelConfig.fontMin, 10px by default). Otherwise it moves beside the node, on the side with the most open room, as long as it overlaps no other node or label; a label with no room is hidden, and the node’s tooltip still names it. Bigger nodes are labeled first. Set labelPosition to "inside" to keep every label inside its node, shrinking it to fit, or "outside" to put every label beside its node.

const constellation = [
  ["Hub", 0, 0, 120],
  ["Aster", -1.1, -0.9, 6],
  ["Briar", -0.6, -1.3, 4],
  ["Clove", 0.3, -1.4, 8],
  ["Dahlia", 1.0, -1.0, 5],
  ["Elder", 1.4, -0.2, 3],
  ["Fennel", 1.3, 0.6, 7],
  ["Ginger", 0.7, 1.2, 4],
  ["Hazel", -0.1, 1.4, 6],
  ["Iris", -0.9, 1.1, 3],
  ["Juniper", -1.4, 0.4, 5],
  ["Kale", -1.5, -0.3, 4],
  ["Laurel", 0.45, -0.75, 3],
  ["Mallow", 0.6, -0.55, 2],
  ["Nettle", -0.5, 0.65, 3],
];

const config = {
  nodes: constellation.map(([id, x, y, value]) => ({id, x, y, value})),
  links: constellation.slice(1).map(([id], i) => ({
    source: i % 3 ? constellation[i][0] : "Hub",
    target: id,
  })),
  size: "value",
  sizeMin: 5,
  sizeMax: 50,
  labelPosition: "auto",
};

Configuration

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

Extends Viz.

activefalse | function

The active callback function for highlighting shapes.

arrowSizenumber | function

The arrowhead size in pixels, or an accessor function returning one per link. Defaults to three times the link's stroke width (at least 6).

arrowsboolean | "target" | "source" | "both" | functionDefault false

Draws arrowheads on the links: true (or "target") at each link's target end, "source" at its source end, "both", or false for none. Also accepts an accessor function returning one of those per link.

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.

datastring | array.&lt;datapoint&gt; | object

The primary data array used to draw the visualization. The value passed should be an Array of objects or a String representing a filepath or URL to be loaded. The following filetypes are supported: csv, tsv, txt, and json.

If your data URL needs specific headers to be set, an Object with a "url" and "headers" (or any other fetch request option) may also be passed. An Object with any other key is a data row, not a request.

Additionally, a custom formatting function can be passed as a second argument to this method. This custom function will be passed the data that has been loaded, as long as there are no errors. This function should return the final array of obejcts to be used as the primary data array. For example, some JSON APIs return the headers split from the data values to save bandwidth. These would need be joined using a custom formatter.

If you would like to specify certain configuration options based on the yet-to-be-loaded data, you can also return a full config object from the data formatter (including the new data array as a key in the object).

To re-fetch a URL on an interval and redraw when its data changes, set dataPollingInterval; each poll runs the same formatter.

Defaults to an empty array ([]).

facetstring | false | function

Splits the chart into small multiples: a grid of equally sized panels, one per distinct value of this data key or accessor, each drawing the chart for that value's rows. The panels share one legend, color scale, title, and timeline, color each series the same way, and hover, tooltips, and legend clicks apply across all of them. Pass false to draw a single chart. See facetConfig for the grid's layout and shared scales.

facetConfigfacetconfig

Configuration for small multiples (see facet), merged into the current config: - columns / rows: the grid's shape. Chosen by default to make the panels as large as the chart's shape allows. - padding: the space between panels, in pixels (default 20). - sort: panel order — "ascending" (default), "descending", "data" (first appearance), a comparator over facet values, or an Array of values. - scales: "shared" (default) gives every panel of a chart with axes the same x/y domains; "independent" fits each panel to its own rows. Funnel defaults to "independent" (each panel sized to its own widest stage); "shared" sizes every panel against the widest stage across all panels. PolarArea and RadialBar default to "independent" (each panel's rings and ticks fit its own values); "shared" gives every panel the same value domain. - axes: with shared scales, "outer" (default) labels the axes only along the grid's left and bottom edges; "all" labels every panel. An object sets each axis on its own, as in {x: "all", y: "outer"}: every panel labels its x axis, only the left column labels its y axis, and the panels' plot areas line up. BumpChart defaults to "all", and Marimekko labels its categories on every panel and its 0–100% share axis only along the outer edge, even with independent scales. - title: a (value, data) => string function for each panel's title, or false to hide panel titles. - titleConfig: TextBox config for the panel titles (fontSize, fontWeight, fontColor, textAnchor, padding, …).

fontFamilystring | array.&lt;string&gt;

The font family used throughout the visualization.

groupBystring | function | array.&lt;string | function&gt;

Defines the mapping between data and shape. The value can be a String matching a key in each data point (default is "id"), or an accessor Function that returns a unique value for each data point. Additionally, an Array of these values may be provided if the visualization supports nested hierarchies.

highlightfalse | function

Persistently emphasizes the data points matching the given predicate: the matching marks keep their color while every other mark is de-emphasized to a neutral gray (the "emphasis" form — highlight one series, gray the rest). Unlike hover/active (transient, opacity-based), highlight is a standing state that survives pointer movement. Pass false to clear it.

hoverfalse | function

The hover callback function for highlighting shapes on mouseover.

linkSizenumber | functionDefault 1

The thickness of each link: a pixel number used for every link, or an accessor function whose values are scaled between linkSizeMin and the smallest node radius (see linkSizeScale).

linkSizeMinunknownDefault 1

The minimum pixel stroke width used when sizing links.

linkSizeScaleunknownDefault "sqrt"

The type of continuous d3-scale used to size links (e.g. "linear", "sqrt", "log").

The edges that connect the nodes. The source and target of each link refer to a node in one of three ways: its index in the nodes array, the node object itself (matched by its id), or a string matching the node's id.

Accepts an array of links, or a URL/filepath string to load them from. An optional formatting function can be passed as a second argument; it receives the loaded data and returns the final links array.

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.

noDataMessagebooleanDefault false

Toggles the visibility of the status message that is displayed when no data is supplied to the visualization.

nodeGroupBystring | function | (string | function)[]Default [accessor("id")]

The key(s) or accessor(s) that identify each object passed to nodes, one per groupBy depth, used in place of groupBy for nodes that the data doesn't cover. Defaults to each node's id.

nodesobject[] | stringDefault []

The nodes to draw. Accepts an array of node objects, or a URL/filepath string to load them from. An optional formatting function can be passed as a second argument; it receives the loaded data and returns the final nodes array.

onstring

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

parentunknown

Parent config used by the wrapper.

renderfunction

Draws the visualization given the specified configuration.

rendererboolean | "svg" | "canvas"

Selects which @d3plus/render backend paints the visible output. "svg" = SvgRenderer (default), "canvas" = CanvasRenderer. Boolean arguments both normalize to "svg".

selectstring | htmlelement

The SVG container element as a d3 selector or DOM element. Defaults to undefined.

shapestring | functionDefault "Circle"

Changes the primary shape used to represent each data point in a visualization. Not all visualizations support changing shapes, this method can be provided the String name of a D3plus shape class (for example, "Rect" or "Circle"), or an accessor Function that returns the String class name to be used for each individual data point.

shapeConfigd3plusconfig

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

sizeMaxnumber

The maximum pixel radius used when sizing nodes. Defaults to half the distance between the two closest nodes.

sizeMinunknownDefault 5

The minimum pixel radius used when sizing nodes.

sizeScaleunknownDefault "sqrt"

The type of continuous d3-scale used to size nodes (e.g. "linear", "sqrt", "log").

thresholdnumber | function

The threshold value for bucketing small data points together, as a share of the total. On a stacked Plot, the series whose summed value across every position is under that share of the whole chart's total merge into one series, stacked on top. A bucket needs at least two items, and its tooltips list them. Off (0) by default on Plot charts.

thresholdKeystring | function

Accessor for the value used in the threshold algorithm. A stacked Plot defaults to its stacked value.

timestring | false | function

Accessor function or string key for the time dimension of each data point.

timelineDefaultstring | date | array.&lt;string | date&gt;

The starting time or range for the timeline. Can be a single Date/String, or an Array of 2 values representing the min and max.

tooltipConfigtooltipconfig

Configuration for the tooltip component.

totalstring | boolean | function

Accessor function or string key for the total value displayed in the visualization.

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.