npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

d3fc-brush

v1.0.1

Published

Adapts the D3 brush, making it easier to create data-driven brushed charts.

Downloads

13

Readme

d3fc-brush

Adapts the D3 brush, making it easier to create data-driven, responsive brushed charts.

Main d3fc package

Installing

npm install d3fc-brush

API Reference

General API

The d3fc-brush component adapts the d3-brush, to achieve the following:

  1. The d3fc-brush is data-driven, with the location of the brush determined by the data supplied via a data join. This makes it easier to create charts that are responsive (i.e, support re-sizing), integrate with other interactive components such as zoom, and create chart rendering logic that is idempotent.
  2. The d3fc-brush exposes xScale and yScale properties for integration with the multi series component and cartesian chart component. As a result the extent of the brush is automatically set based on the range of each axis.
  3. The d3fc-brush selection (which defines the brushed range), is represented as a percentage, whereas d3-brush uses pixels. The use of a percentage selection makes it easier to resize a brushed chart.
  4. The brush events expose utility functions for computing the axis domain based on the current selection, making it easier to handle brush events and update the chart.

Creating a navigator chart

A common application of a brush is to create two linked charts, one which acts as a 'navigator', allowing the user to zoom in on a particular data region. The complete code for this example is found in the examples folder.

The d3fc brush is data-driven, so the first step is to create data object that combines the standard series data with a range that specifies the brush location:

var chartData = {
    series: data,
    brushedRange: [0, 1]
};

The d3fc brush uses the same data format as the d3 brush:

For a two-dimensional brush, it is [[x0, y0], [x1, y1]], where x0 is the minimum x-value, y0 is the minimum y-value, x1 is the maximum x-value, and y1 is the maximum y-value. For an x-brush, it is [x0, x1]; for a y-brush, it is [y0, y1].

A multi series can be used to combine the brush with another series, for example an area series:

var brush = fc.brushX();

var multi = fc.seriesSvgMulti()
    .series([area, brush])
    .mapping((data, index, series) => {
        switch (series[index]) {
        case area:
            return data.series;
        case brush:
            return data.brushedRange;
        }
    });

The main and navigator charts are rendered using the cartesian chart component, with the navigator using the multi series defined above:

var mainChart = fc.chartSvgCartesian(x, y)
    .plotArea(area);

var navigatorChart = fc.chartSvgCartesian(x.copy(), y.copy())
    .plotArea(multi);

function render() {
    d3.select('#main-chart')
        .datum(chartData.series)
        .call(mainChart);

    d3.select('#navigator-chart')
        .datum(chartData)
        .call(navigatorChart);
}

render();

The final step is to add event handlers to the brush. The event returns the updated selection together with xDomain and yDomain properties that are set to the domain represented by brushed region.

The event handler simply updates the data 'bound' to the chart to reflect the new selection and updates the domain of the main chart. Because the charts are idempotent and data-drive, all that remains is to invoke the render function.

var brush = fc.brushX()
    .on('brush end', function(evt) {
        chartData.brushedRange = evt.selection;
        x.domain(evt.xDomain);
        render();
    });

If you want to initialise the brush so that it does not occupy the entire range [0, 1], you'll need to set the initial domain for the main chart. This can be achieved by using a scale with the same domain and a unit range:

var scale = d3.scaleLinear().domain(x.domain());
x.domain(chartData.brushedRange.map(scale.invert));

Brush

# fc.brush()

Creates a new two-dimensional brush.

# fc.brushX()

Creates a new one-dimensional brush along the x-dimension.

# fc.brushY()

Creates a new one-dimensional brush along the y-dimension.

# brush.on(typenames, [listener])

Adds an event listener for the given event types, see dispatch.on for details. The type must be one of the following:

  • start - at the start of a brush gesture, such as on mousedown.
  • brush - when the brush moves, such as on mousemove.
  • end - at the end of a brush gesture, such as on mouseup.

The event object exposes the following fields:

  • selection - the updated selection.
  • xDomain - the x-domain represented by the given selection.
  • yDomain - the y-domain represented by the given selection.

# brush.xScale(scale)
# brush.yScale(scale)

If scale is specified, sets the scale and returns this brush. If scale is not specified, returns the current scale.

# brush.filter([filter])

See d3-brush filter.

# brush.handleSize([size])

See d3-brush handleSize.