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/d3fc-discontinuous-scale

v4.1.1

Published

A D3 scale that supports domains that are not continuous, allowing 'breaks' in the scale, for example skipping weekends for a date scale.

Downloads

32,191

Readme

d3fc-discontinuous-scale

A D3 scale that supports domains that are not continuous, creating gaps, or discontinuities. For example, this can be used to create a linear scale that has certain ranges removed, creating discontinuities in the rendered axis, or it can be used in conjunction with a time scale, in order to 'skip' weekends.

Main D3FC package

Ticks that are located within a discontinuity are 'clamped' to the upper boundary, as a result, if there are multiple ticks within a single discontinuity these will overlap. In this case it is recommended that the scale.ticks property is used to specify the ticks that make most sense for the given use case.

Installing

npm install @d3fc/d3fc-discontinuous-scale

API Reference

The discontinuous scale adapts a D3 scale, with an associated discontinuity provider supplying the information relating to any 'gaps' in the scale. The following example shows an adapted linear scale, with a discontinuity which removes the domain range [50, 75]:

import { scaleDiscontinuous, discontinuityRange } from 'd3fc-discontinuous-scale';
import { scaleLinear, axisBottom } from 'd3-scale';

var scale = scaleDiscontinuous(scaleLinear())
    .discontinuityProvider(discontinuityRange([50, 75]))
    .domain([0, 100])
    .range([0, 550]);

var axis = axisBottom()
    .scale(scale);

Which renders as follows:

There are various different discontinuity providers which can be used with the discontinuous scale. It is also possible to write your own.

scaleDiscontinuous

# fc.scaleDiscontinuous(scale)

Constructs a new discontinuous scale, adapting the given scale. If a scale is not specified, a D3 identity scale is used.

# scaleDiscontinuous(value) # scaleDiscontinuous.invert(value)

The underlying scale method, and the invert method are adapted to remove discontinuous. For example, a regular D3 linear scale performs as follows:

const linear = scaleLinear()
    .domain([10, 110])
    .range([0, 960]);

linear(20); // 96
linear(50); // 384

Whereas a discontinuous scale (that adapts a linear), with a discontinuity that removes the domain range [50, 80], gives a different output:

const discontinuous = scaleDiscontinuous(scaleLinear())
    .discontinuityProvider(discontinuityRange([50, 70]))
    .domain([10, 110])
    .range([0, 960]);

discontinuous(20); // 120
discontinuous(50); // 480

The same behaviour is observed via invert also.

All of the adapted scale methods are re-exposed by the discontinuous scale. The discontinuous scale API documentation details where the scale behaviour differs from the adapted scale. For all other methods, you can assume that their behaviour is unchanged.

# scaleDiscontinuous.domain([provider])

Adapts the underlying scale's domain method, clamping the upper and lower domain bounds to ensure that they do not fall within a discontinuity.

# scaleDiscontinuous.nice()

Adapts the underlying scale's nice method, clamping the resulting domain to ensure that the upper and lower bounds do not fall within a discontinuity.

# scaleDiscontinuous.ticks([count])

Adapts the underlying scale's ticks method, removing any ticks that fall within discontinuities.

# scaleDiscontinuous.discontinuityProvider(provider)

If provider is specified, sets the discontinuity provider for the scale. The discontinuity provider exposes an API that is used to create gaps within the domain. This package includes a number of different types of discontinuity provider, however you can also create your own.

discontinuityIdentity

# fc.discontinuityIdentity()

The identity discontinuity provider does not define any discontinuities, and as a result, when associated with a discontinuities scale, the scale will perform in exactly the same way as the adapted scale.

discontinuityRange

# fc.discontinuityRange(ranges)

This discontinuity provider defines one or more domain ranges which should be excluded from the scale. These ranges are supplied as tuples, for example, discontinuityRange([0, 10], [20, 30]). Both numeric and date ranges are supported, for example to create a range that skips a day, you can do the following:

const start = new Date(2015, 0, 9);
const end = new Date(2015, 0, 10);
const range = discontinuityRange([start, end]);

discontinuitySkipWeekends

# fc.discontinuitySkipWeekends() # fc.discontinuitySkipUtcWeekends()

This discontinuity provider is intended to be used with a time scale. This provider will remove all weekends from the domain, a feature which is particularly useful for financial time-series charts.

Discontinuity provider interface

You can create your own discontinuity provider by providing an object that exposes the following methods:

  • clampUp - When given a value, if it falls within a discontinuity (i.e. an excluded domain range) it should be shifted forwards to the discontinuity boundary. Otherwise, it should be returned unchanged.
  • clampDown - When given a value, if it falls within a discontinuity it should be shifted backwards to the discontinuity boundary. Otherwise, it should be returned unchanged.
  • distance - When given a pair of values, this function returns the distance between the, in domain units, minus any discontinuities. discontinuities.
  • offset - When given a value and an offset, the value should be advanced by the offset value, skipping any discontinuities, to return the final value.
  • copy - Creates a copy of the discontinuity provider.