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

ticks

v0.0.4

Published

A utility for choosing nice tick marks or histogram intervals.

Downloads

198

Readme

Ticks

A utility for choosing nice tick marks or histogram intervals.

API

ticks(min, max, n)

  • min The minimum value of the interval.
  • max The maximum value of the interval.
  • n The approximate number of desired ticks.

Returns an array of "ticks", numbers that are suitable to use as tick mark values. The first tick will be less than or equal to min, and the last tick will be greater than or equal to max.

Usage

Install via NPM:

npm install -S ticks

Example use:

var ticks = require("ticks");

ticks(0.5432, 9.543, 10); //[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
ticks(1, 10000, 5); // [ 0, 2000, 4000, 6000, 8000, 10000 ]
ticks(-1, 1, 10); // [-1,-0.8,-0.6,-0.4,-0.2,0,0.2,0.4,0.6,0.8,1]
ticks(1000, 1002, 10); //[1000,1000.2,1000.4,1000.6,1000.8,1001,1001.2,1001.4,1001.6,1001.8,1002]

API

# ticks(min, max, numTicks [,tight])

Computes and returns approximately numTicks ticks spaced nicely that include the intervaj [min, max].

If tight is specified, then the first tick is the min, and the last tick is the max. The The Graphics Gems chapter "Nice Numbers for Graph Labels" by Paul S. Heckbert introduces the notion of "loose" and "tight" tick marks.

Algorithm

The algorithm for computing ticks is based on the idea of a "nice interval". Nice intervals can be expressed as (base * 10^exp), where exp is some integer exponent, and base is either 1, 2, or 5. Examples of nice intervals are 0.1, 0.5, 10, 20, 5, 2, and 500.

The Ticks algorithm computes the exponent of the raw interval, by Math.log10((max - min) / n), then computes both the floor and ceiling of this value, which are candidate exponents for use in generating nice intervals. The algorithm then tries all 6 possible combinations of the two candidate exponents with the possible bases (1, 2, and 5) to generate a set of candidate nice intervals. From the generated set of nice intervals, the one that is closest to the raw interval ((max - min) / n) is chosen.

Related Work

The seminal approach for generating tick marks appeared in "Nice Numbers for Graph Labels" by Paul S. Heckbert in the book "Graphics Gems", originally published in 1990.

Subsequently, there have been a variety of new takes on the problem. This research paper presents an approach for this that takes more factors into account, including the actual size of label text: An Extension of Wilkinson’s Algorithm for Positioning Tick Labels on Axes, by Justin Talbot, Sharon Lin, and Pat Hanrahan, and also surveys other algorithms.

Also, a similar algorithm is implemented in D3.scale.linear.ticks.

Development Tooling

This module is published as an NPM package. The module itself is authored using ES6 module syntax in index.js, which is declared as the jsnext:main entry point in package.json so it can be included in Rollup-based builds. The module is converted to CommonJS format in the prepublish script, which runs the pretest and test scripts, making the built file ticks.jsavailable in the NPM package. The built file is excluded from the Git repository via the.gitignorefile. Usually NPM ignores files in.gitignore, so to cause NPM to include ticks.js, an [empty .npmignore` file was added.

Here's what it looks like when npm publish is run:

During development you can run npm test to run the Rollup build and run the unit tests. The Mocha unit tests are written in ES5, and consume the CommonJS package generated by Rollup via Node.js.

Curran Kelleher June 2015