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

jsheatmap

v1.3.1

Published

Generates heat map data

Downloads

62

Readme

jsheatmap

A heat map package for Node. You provide the column headings, row labels and row values, and it will return corresponding rgb colors that can be used to populate a table or other representation of your choosing.

What's a heat map?

A heat map (or heatmap) is a graphical representation of data where the individual values contained in a matrix are represented as colors.

A common use of a heat map is to visualize frequency of occurrence of one variable in correlation with another. For example: the number of traffic accidents that occur along each mile of a highway, or the number of jellybeans in several sample packages.

About the implementation

This is largely a JavaScript transliteration of Andrew Noske's C# sample code.

Release history

1.0.1 Initial release 1.1.2 Added logarithmic scaling 1.2.0 Allow extra information as pass-thru data

Usage

npm install jsheatmap

In your Node application, import the HeatMap class from the jsheatmap module

import HeatMap from 'jsheatmap'

Next, construct an HeatMap instance with heading and row data.

// Days of rain in summer summer months, by year
const headings = ["June", "July", "August", "September"]  // the months
const rows = [
  ["2015", [9, 5, 6, 8]],   // the years and rainy days by month
  ["2016", [7, 5, 10, 7]],
  ["2017", [7, 4, 3, 9]],
  ["2018", [10, 5, 6, 8]],
  ["2019", [8, 9, 3, 1]],
]


const heatmap = new HeatMap(headings, rows)
const data = heatmap.getData();

The HeatMap will scale all values of the data (all rainy day values in this case), and to fit within the range 0 to 1.0. The scaled values are then converted to rgb colors based on a color gradient, with blue at the lowest scale and red at the highest.

Data is returned in the following format:

{
  "headings": [
    "Jun",
    "Jul",
    "Aug",
    "Sep"
  ],
  "high": 10,
  "low": 1,
  "rows": [
    {
      "label": "2015",
      "cells": {
        "values": [
          7,
          5,
          6,
          8
        ],
        "colors": [
          {
            "red": 0.6249999999999998,
            "green": 1,
            "blue": 0
          },
          {
            "red": 0,
            "green": 0.588235294117647,
            "blue": 1
          },
          {
            "red": 0,
            "green": 1,
            "blue": 0.625
          },
          {
            "red": 1,
            "green": 0.588235294117647,
            "blue": 0
          }
        ],
        "scales": [
          0.6,
          0.2,
          0.4,
          0.8
        ]
      }
    },
    ...  
  ]
}

options

The getData() method takes an optional options parameter.

Logarithmic Scaling

heatmap.getData({logn: true})

Oftentimes, the distribution of values is skewed toward the high end, making the heatmap look mostly greenish-blue. An example of this is a map of the winning odds of hole cards in Texas Hold'em. By applying logarithmic scaling, the range of high and low scale values is decreased, making the resulting HeatMap colors less skewed.

Extra info

As a convenience, it is possible to construct the HeatMap where each row of data has a third parameter. This is an array containing objects of the same length as the value array. These objects are then passed-thru as an array called extra in the returned cell data structure.

 // the years and rainy days by month; extra info indicating record rainfall
const rows = [
  ["2015", [9, 5, 12, 8], [{record: false}, {record: false}, {record: true}, {record: false}]],  
  ["2016", [7, 5, 10, 7], [{record: false}, {record: false}, {record: false}, {record: false}]],
  ["2017", [7, 4, 3, 9], [{record: false}, {record: false}, {record: false}, {record: true}]],
  ["2018", [10, 5, 6, 8], [{record: true}, {record: false}, {record: false}, {record: false}]],
  ["2019", [8, 9, 3, 1], [{record: false}, {record: false}, {record: false}, {record: false}]],
]

The returned data structure would look like:

```json
{
  "headings": [
    "Jun",
    "Jul",
    "Aug",
    "Sep"
  ],
  "high": 12,
  "low": 1,
  "rows": [
    {
      "label": "2015",
      "cells": {
        "values": [
          7,
          5,
          6,
          8
        ],
        "colors": [
          ...
        ],
        "scales": [
          ...
        ],
        "extra": [
          {"record": false}, 
          {"record": false}, 
          {"record": true}, 
          {"record": false}
        ], ...
      }
    },...
  ]
}

A visualization using React

Once the HeatMap data is returned, it is possible to write some React components that visualizes the result.

test

In this visualization, blue indicates fewer rainy days, and red more. Inside the color boxes are the scaled values, where 0.0 is the lowest (fewest rainy days) and 1.0 is the highest (most rainy days).