Skip to content

Interaction ​

Interaction in Charming is data-driven, like Vega-Lite's selections. An interactor, such as a hover, a click or a brush, sets a clause in a selection; marks read the selection, with the filter option or with selection.match, or the data of a flow tests it. A selection keeps its clauses in a signal, so whatever reads it is drawn again when it changes.

js
const penguins = await d3.csv("/data/penguins.csv", d3.autoType);
const brush = cm.selection();

const renderer = cm.svg({width: 640, height: 400});
const root = cm.flow().renderer(renderer);

root
  .data(penguins.filter((d) => Number.isFinite(d.body_mass_g)))
  .transform(cm.encode, {x: "culmen_length_mm", y: "culmen_depth_mm", fill: "species"})
  .transform(cm.plane, {fill: {legend: true}})
  .add(cm.circle, {r: 3, fill: brush.match((d) => d.fill, "#ccc", (d) => d.fill)})
  .add(cm.brush, {as: brush});

root.render();
document.body.append(renderer.node());

Drag to brush; click to clear.

cm.selection([options]) ​

Returns a new, empty selection: the rows chosen by its sources, one clause per source.

  • resolve: how the clauses combine: "single", the latest only, by default; "union"; or "intersect".

A selection given as the filter of a mark draws the rows it chooses, and an empty selection chooses none:

js
flow.add(cm.circle, {filter: hover, r: 4});

selection.test(row, [options]) ​

Returns whether the selection chooses a row, {datum, key}, such as {datum: d} for a datum of the data. A clause chooses rows by their key, else their datum; by the value of a channel in the data, such as every row at the date hovered; or by ranges of channels, such as the rows under a brush, so that rows of other charts can be tested.

  • except: a source whose clause is ignored, such as the brush of the chart filtered by the others in a crossfilter.
  • empty: what an empty selection returns, false by default.

Testing in the data function of a flow filters the data itself, so the scales and transforms see only the rows chosen:

js
root.data(() => days.filter((d) => brush.test({datum: d}, {except: field, empty: true})));

See Crossfilter and Legend filter.

selection.match(selected, other, [empty]) ​

Returns a channel by selection, for the options of a mark: selected for the rows chosen, else other, and empty for every row while the selection is empty, selected by default. Each may be a function of the row, such as (d) => d.fill to keep the color of the rows chosen.

js
flow.add(cm.rect, {fillOpacity: hover.match(1, 0.3)});

selection.set(source, clause) ​

Sets the clause of a source, any value, or removes it with null, and returns the selection. Interactors call it; call it to select from outside a chart. A clause is one of {keys}, {datums}, or {value, values}, an accessor of the datum and the values it chooses.

js
selection.set("search", {value: (d) => d.species, values: ["Adelie"]});

cm.nearestX([options]) ​

An interactor that draws nothing: it listens to the pointer and sets the clause of the row nearest it, along x then y like Plot's pointerX, or removes it when the pointer leaves or is too far. It searches the rows of its flow, by their boxes, such as bars, or in cm.polar their wedges; in facets, those of the cell under the pointer.

  • as: the selection to set; a value that isn't a selection throws "invalid as".
  • maxDistance: how far a row can be from the pointer, in pixels, 40 by default.
  • shared: the name of a channel, to choose every row that shares its value in the data with the row picked, such as "x" for every series at the date hovered; true for the position it's nearest along.
  • source: the name of its clause, the interactor by default, such as for except.

See Radial lines with tips and cm.tip.

cm.nearestY([options]) ​

Like cm.nearestX, along y then x.

cm.nearest([options]) ​

Like cm.nearestX, by distance in both directions.

cm.toggle([options]) ​

An interactor that selects the rows clicked: a click selects a row alone, shift-click adds it or removes it, and a click on the row selected alone, on nothing, or a double-click clears. Its options are those of cm.nearestX, with maxDistance 4 by default.

js
const data = await d3.csv("/data/population-state-age.csv", d3.autoType);
const ages = Array.from(new Set(data.map((d) => d.age)));
const chosen = cm.selection();

const renderer = cm.svg({width: 640, height: 300});
const root = cm.flow().renderer(renderer);

root
  .data(data.filter((d) => ["CA", "TX", "NY", "FL"].includes(d.state)))
  .transform(cm.encode, {x: "state", y: "population", fill: "age"})
  .transform(cm.spreadX)
  .transform(cm.stackX, {order: ages})
  .transform(cm.plane, {y: {tickFormat: "s"}, fill: {legend: true}})
  .add(cm.rect, {fillOpacity: chosen.match(1, 0.25)})
  .add(cm.toggle, {as: chosen, shared: "fill"});

root.render();
document.body.append(renderer.node());

Click a bar to select its age group across states.

cm.brushX([options]) ​

An interactor that selects by dragging along x in the frame of a chart. Dragging inside the extent moves it, and a click without a drag clears it. In facets, a brush is in one cell at a time. The extent is drawn over the chart.

  • as: the selection to set.
  • select: "range", the default of a one-dimensional brush, sets the ranges of the positions brushed, so that rows of other charts can be tested by their data; "rows" sets the rows under the extent.
  • by: "y", to brush each axis of a position by the categories of the other, {x: {by: "y"}}, such as the dimensions of parallel coordinates; each axis has its own extent, and a row is chosen if it's within every axis brushed.
  • width: with by, the width of the column around each axis that a drag starts in; by default, a drag anywhere brushes the nearest axis.
  • source: the name of its clause, the brush by default.

See Focus + context, Brushing parallel coordinates and Brushable scatterplot matrix.

cm.brushY([options]) ​

Like cm.brushX, along y; its by is "x".

cm.brush([options]) ​

Like cm.brushX, along x and y, with select "rows" by default.

Legends ​

A legend of categories given a selection as as, {fill: {as: selection}} on the scale or on cm.legend, selects by its items: a click chooses a category alone, shift-click adds or removes one, and a click on the category chosen alone, or a double-click on the chart, clears. The clause is the values of the channel in the data, so every mark of those categories reacts.

  • multiple: if true, every click adds or removes, like checkboxes.
  • fade: which items fade: those not chosen, by default, or "chosen", such as series hidden.
  • source: the name of its clause, the legend by default.
js
const chosen = cm.selection();

root
  .data(() => data.filter((d) => chosen.test({datum: d}, {empty: true})))
  .transform(cm.encode, {x: "state", y: "population", fill: "age"})
  .transform(cm.spreadX)
  .transform(cm.stackY)
  .transform(cm.plane, {fill: {domain: ages, legend: true}})
  .add(cm.rect)
  .add(cm.legend, {fill: {as: chosen}});

See Legend filter.