Coder Social home page Coder Social logo

pivotjs's Introduction

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

pivotjs's People

Contributors

jonatan-alama avatar tsubasatmd avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.