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

pivotjs

v0.0.5

Published

Pivot table with multiple measure implementation for Javascript

Downloads

5

Readme

pivotjs

pivotjs is a Javascript library which calculate pivot table data for multiple rows and columns with multiple measure.

How to use

Install

pivotjs is avaliable as npm library, so just call npm install command to install this.

npm install pivotjs[ --save]

Prepare data

pivotjs manipulate Array typed data with Objects as following.

var records = [
  { category: 'A', data: 'a1', date: '2016-01-01', month: '2016-01', day: '01', value: 100},
  { category: 'A', data: 'a1', date: '2016-01-01', month: '2016-01', day: '01', value: 50},
  { category: 'A', data: 'a2', date: '2016-01-01', month: '2016-01', day: '01', value: 20},
  { category: 'B', data: 'b1', date: '2016-01-02', month: '2016-01', day: '02', value: 150},
  { category: 'B', data: 'b2', date: '2016-01-03', month: '2016-01', day: '03', value: 200},
  { category: 'C', data: 'c1', date: '2016-01-01', month: '2016-01', day: '01', value: 100},
  { category: 'C', data: 'c2', date: '2016-02-01', month: '2016-02', day: '01', value: 10}
]

Params for pivot

measures

measure param is Array with measure objects which has key, format and aggregation attributes. pivotjs provides some aggregation function as following

sum
count
counta
unique
average
median
mode
max
min

Example for measure param is following.

var measures = [
  {
    key: 'value',
    name: 'SUM',
    format: 'int',
    aggregation: 'sum'
  },
  {
    key: 'value',
    name: 'AVERAGE',
    format: 'float',
    aggregation: 'average'
  }
];

Dimension(rows and cols)

Params for rows and cols are same object structure which has id(key) and sort attribute, but sort is object and has 2 types('self' and 'measure') like following. For sort by self value (i.e.: using value A, B, C in this case)

var rows = [
  {
    id: 'category',
    sort: {
      type: 'self',
      ascending: true
    }
  }
];

Or for sort by measure value (i.e.: using value 170, 350, 10 in this case), key#sort is the columns key which is used by as sort value.

var rows = [
  {
    id: 'category',
    sort: {
      type: 'measure',
      key['2016-01']
      measureIndex: 0,
      ascending: false
    }
  }
];

If you want to populate pivot data with multiple rows, you just put another dimension object into rows array.

var rows = [
  {
    id: 'category',
    sort: {
      type: 'measure',
      measureIndex: 0,
      ascending: false
    }
  },
  {
    id: 'data',
    sort: {
      type: 'self',
      ascending: true
    }
  }
];

This is same for cols.

var cols = [
  {
    id: 'month',
    sort: {
      type: 'self',
      ascending: true
    }
  },
  {
    id: 'day',
    sort: {
      type: 'self',
      ascending: true
    }
  }
];

Populate data

Just create Pivot class instance with argument object which has records, measures, rows, cols param object And call populate method.

var params = {
  records: records,
  measures: measures,
  rows: rows,
  cols: cols
};
var pivot = new Pivot(params);
pivot.populate();

Get populated data

Get row keys and col keys

getSortedRowKeys(or getSortedColKeys)#pivot returns Each possible keys for rows or cols.

var rowKeys = pivot.getSortedRowKeys();
console.log(rowKeys);
-----
[['A', 'a1'], ['A', 'a2'], ['B', 'b1'], ['B', 'b2'], ['C', 'c1'],['C', 'c2']]
-----

var colKeys = pivot.getSortedColKeys();
console.log(colKeys);
-----
[['2016-01', '01'], ['2016-01', '02'], ['2016-01', '03'], ['2016-02', '01']]
-----

Get data

values(rowKey, colKey)#pivot returns Array of measures which has populated data for specified row and col key. For example, if you want to get data for row:['A', 'a1'] and col:['2016-01', '01'] do like following.

var values = pivot.values(rowKeys[0], colKeys[0]);
console.log(values[0].value());
-----
150 // value for measure[0] i.e. summed value
-----

console.log(values[1].value());
-----
75 // value for measure[1] i.e. averaged value
-----

Build

npm run build

Test

npm run test