Skip to main content
100%

Barchart as an input device

✓ Published0🌍 Public
AAlainRo
Last edited Oct 4, 2017
Created on Oct 31, 2016

This example demonstrates how D3's brush component can be repurposed as a direct manipulation input device for editing bar chart values. Two synchronized bar charts, one horizontal and one vertical, display the same dataset of eight values, and dragging any bar updates its value in both views. The code uses `d3.brushX` and `d3.brushY` with `brushmove` and `brushend` event handlers to capture mouse interaction and update the shared data array. SVG selections and the `selection.call` method apply the brushes and propagate value changes between the two linked charts.

AI-generated description

Barchart as an input device

Brushes can be used to draw a barchart. This gives an easy way to input or adjust values with the mouse !!!

The d3 brush is a complex piece of logic taking care of events and user feed-backs. Among other characterics it exposes a nice selection rectangle which can be used as a bar !!!

2D-brushes come in two flavors see official API v4 doc

  • d3.brushX()
  • d3.brushY()

Let's try both of them on this example.

Code comments

Code is made of:

  • CSS
  • a data definition in the form of: [{index:2, value: 23.34}, ...]
  • two almost identical barchart generators (horizontal/vertical) allowing to drag a bar to a new value
  • a refresh function which propagates data updates into the two barcharts

Enjoy !!

Similar vizzes