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

d3-state-handler

v0.4.6

Published

A module for managing states allowing users to toggle between d3 charts.

Downloads

19

Readme

d3-state-handler

v0.3.x

A thin data-layer for creating kick-ass d3 stories.

How does it work?

d3-state-handler won't make charts for you, nor will it tell you how to display them. Instead, it allows you to register different states (read charts) and call hooks as you transfer between states. This allows you to act on your data between charts making it easy to transition between them. It also supports hooks for jumping to specific charts via urls. For a demonstration, see the demo.

Installation

d3-state-handler can be installed with npm. npm install --save d3-state-handler

Usage

d3-state-handler is no longer an amd module. ~~To use it, you'll need requirejs.~~ But you can still use requirejs if you want to.

API

constructor(options)

This is the method first called on d3-state-handler. It takes an options object with the following properties (shown with defaults):

{
  /* 
   * Data is shared between states. Every state method recieves a data parameter, this 
   * specifies the the data passed in to stateHandler.start() when it is first called.
  */
  data: {},
  /*
   * Specifies whether lastChart.next() goes to first chart and whether
   * firstChart.prev() calls the last chart.
  */
  loop: true, // Currently impossible to turn off.
  /*
   * Calling jumpTo() instead of next() or previous() allows you to go to any chart
   * from any other chart. Doing this is tricky if you don't take special care to clean
   * up your state before you jump. Setting this control object allows you to create a
   * contract that all states must adhere to when calling jumpOut on the state.
  */
  jumpState: {} // Currently unimplemented.
}

StateHandler.add(state)

Adds a new state. See states section below.

StateHandler.next()

Transitions to the next chart. Calls nextOut(data) on the current chart, nextIn(data) on the next chart, and then render(data) on the new current chart.

StateHandler.prev()

Transitions to the previous chart. Calls prevOut(data) on the current chart, prevIn(data) on the next chart, and then render(data) on the new current chart.

StateHandler.remove(name)

Removes a state by name.

StateHandler.resize()

Calls resize(data) on the current state. Defaults to render(data) if resize is undefined.

StateHandler.jumpTo(name)

Jumps from current state to a different state by name. Calls jumpOut(data) on current state, checks that data === jumpState (not yet implemented), calls jumpIn(data) on the target state, and then calls render(data) on the new current state.

StateHandler.start()

Calls render on the current state.

StateHandler.currentState()

Returns the current state object.

States

States are the building blocks for d3 stories created with d3-state-handler. States must have a render method but all other methods are optional. All methods receive a data object as the first parameter and must return a data object. See the example state below.

{
  name: [String], // Name of the state. Defaults to the index of the state
  render: function(data) {
    // Create chart based on data here.
    return data;
  },
  nextOut: function(data) {
    // Clean up data here before passing it to the next state.
    return data;
  },
  prevOut: function(data) {
    // Clean up data here before passing it to the previous state.
    return data;
  },
  nextIn: function(data) {
    // Manipulate data to prepare it for this.render(data). Called when 
    // StateHandler.next() is called on the state before this one.
    return data;
  },
  prevIn: function(data) {
    // Manipulate data to prepare it for this.render(data). Called when
    // StateHandler.prev() is called on the state after this one.
    return data;
  },
  jumpOut: function(data) {
    // Clean up data to match the jumpState contract specified in 
    // the constructor.
    return data;
  },
  jumpIn: function(data) {
    // Manipulate data to prepare it for this.render(data). It is safe
    // to assume data here is exactly what was specified in the 
    // jumpState contract. 
    // (Actually, I lied. That's not a safe assumption to make.) 
    // (It will be by version 1.0.0, however.)
    return data;
  },
  resize: function(data) {
    // Do some checks about screen size here and manipulate the state if you
    // need to. This is called when StateHandler.resize() is called.
    // StateHandler.resize() defaults to calling render(data) if this
    // function is unimplemented.
    return data;
  }
}