9951 explained code solutions for 126 technologies


javascript-d3How can I use the d3.js brush to manipulate data?


The d3.js brush is a powerful tool that can be used to manipulate data. It allows users to select a range of values from a given dataset.

For example, to create a brush that selects values between 0 and 10, you can use the following code:

var brush = d3.brushX()
    .extent([[0, 0], [10, 10]])
    .on("brush end", brushed);

function brushed() {
    var selection = d3.event.selection;
    // selection is the range of values selected by the brush
    // use selection to manipulate data
}

This code creates a brush that can select values between 0 and 10. The brushed function is called whenever the brush is used, and the selection variable contains the range of values selected. This selection can then be used to manipulate data.

The following list contains the parts of the code and their functions:

  • var brush = d3.brushX() creates a brush object
  • .extent([[0, 0], [10, 10]]) sets the range of values the brush can select
  • .on("brush end", brushed) calls the brushed function whenever the brush is used
  • function brushed() contains the code that will be executed whenever the brush is used
  • var selection = d3.event.selection stores the range of values the brush has selected

For more information about the d3.js brush, please see the following links:

Edit this code on GitHub