Giter Site home page Giter Site logo

steelbreeze / pivot Goto Github PK

View Code? Open in Web Editor NEW
7.0 3.0 0.0 1.19 MB

A minimalist library for pivoting data by 1-n dimensions

Home Page: https://steelbreeze.net/pivot

License: MIT License

JavaScript 37.36% TypeScript 62.64%
pivot-tables pivot dimension dimensional-analysis typescript javascript

pivot's Introduction

pivot

Maintainability

A minimalist pivot table library for TypeScript/JavaScript. While small (a mere 485 bytes when minified), this library is large in capability, supporting derived and custom dimensions, derived fields for dimensions and calculations, composite dimensions, filtering.

The library also provides a modest set of numerical selectors. Suggestions for additions, or better still contributions, are welcome.

Why create another pivot table library?

There are plenty of pivot table libraries in existence, so why create another one? Well, this is a spin-off from the steelbreeze/landscape project, where instead of aggregating numerical data from the pivot cube, non-numerical data is needed.

It also focuses just on dimension and cube creation, without any layout considerations keeping it small and unopinionated.

n-cubes

The libary allows 1-n dimensions to be passed into the pivot function allowing n-cube (or hypercube) generation.

Installation

NPM

For installation via the node package manager:

npm i @steelbreeze/pivot

Web

For web via a CDN:

import * as pivot from 'https://cdn.skypack.dev/@steelbreeze/pivot';

Documentation

The documentation can be found here, and more discussion in the Wiki.

Example

The following is the result of pivoting publicly available information about the Fulham Football Club men's squad at the end of the 2020/21 season, calculating the average age of players by position and country.

import { distinct, criteria, pivot, map, average } from '@steelbreeze/pivot';
import { squad } from './fulham';

// the position dimension we want in a custom order
const positions = ['Goalkeeper', 'Defender', 'Midfielder', 'Forward'];

// the countries dimension we derive from the data and order alphabetically
const countries = squad.map(player => player.country).filter(distinct).sort();

// we then create dimensions which also reference a property in the source data 
const x = positions.map(criteria('position'));
const y = countries.map(criteria('country'));

// create the pivot cube from the squad data using position and country for x and y axes
let cube = pivot(squad, y, x);

// find the average age of players by position by country as at 2021-05-23
const result = map(cube, average(age(new Date('2021-05-23'))));

The full example can be found here.

The selection is the average age of the players grouped by position and country:

        Goalke… Defend… Midfie… Forward
Belgium         32
Camero…                 25
Denmark         24
England         25      23.25   23
France  28      27
Gabon                   27
Jamaica         28              28
Nether…         25
Nigeria         24              22
Portug…                         27
Scotla…                 31
Serbia                          26
Slovak… 24
Spain   33
USA             28

The full example code can be found here.

Alternatively, as can be seen in the web example, non-numerical content can also be queried, mapping the source data to an arbitrary selection:

const result = pivot.map(cube, pivot.select(player => `${player.givenName} ${player.familyName}`));

Resulting in this sort of output:

Goalkeeper Defender Midfielder Forward
Belgium Denis Odoi
Cameroon Andre-Frank Zambo Anguissa
Denmark Joachim Anderson
England Tosin Abarabioyo, Joe Bryan Ruben Loftus-Cheek, Harrison Reed, Josh Onomah, Fabio Carvalho Ademola Lookman
France Alphonse Areola Terence Kongolo
Gabon Mario Lemina
Jamaica Michael Hector Bobby De Cordova-Reid
Netherlands Kenny Tete
Nigeria Ola Aina Josh Maja
Portugal Ivan Cavaleiro
Scotland Kevin McDonald, Tom Cairney
Serbia Aleksander Mitrovic
Slovakia Marek Rodak
Spain Fabrico Agosto Ramirez
USA Tim Ream, Antonee Robinson

Data and calculations correct as of: 2021-05-23.

pivot's People

Contributors

mesmo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

pivot's Issues

Allow multiple axes to be used on x or y

The last commit removed this capability to allow simple code for simple scenarios; now need to reintroduce the ability to merge axes allowing the result to be used in pivot operation.

Derived field names

Derived fields must currently have the same name as a column in the source data due to the type constraints. It would be good to loosen this constraint to allow custom names.

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.