Skip to content

accessor

Wraps an object key in a simple accessor function.

Turns a key into the function d => d[key]. Every chart config that accepts “a key or an accessor” (groupBy, x, y, sum, label, …) runs string values through this, so the rest of the pipeline only ever deals with functions.

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

accessor("id")({id: "alpha", value: 3});
accessor("value")({id: "alpha", value: 3});
[{id: "alpha"}, {id: "beta"}, {id: "gamma"}].map(accessor("id"));

A second argument is returned whenever the key is undefined on a row, which is how optional config such as x or y fall back to 0 for rows missing the field.

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

accessor("missing")({id: "alpha"});
accessor("missing", "n/a")({id: "alpha"});
accessor("value", 0)({id: "alpha", value: 3});

Parameters

import {accessor} from "@d3plus/core";
keyrequiredstring

The key to be returned from each Object passed to the function.

defstring | number | boolean | DataPoint

A default value to be returned if the key is not present.