-
Notifications
You must be signed in to change notification settings - Fork 0
Home
David Mesquita-Morris edited this page Mar 30, 2023
·
11 revisions
Pivot is a minimalist library designed to summarise tabular data (an array of objects with properties) into a cube. This process simply slices the table firstly by the y dimension then each slice by the x dimension.
Here is a simple example:
import { criteria, distinct, pivot, map } from '..';
interface Data {
a: number;
b: number;
c: string;
}
// a simple data set comprising 4 rows with some dimensional data
const data: Data[] = [
{ a: 1, b: 1, c: 'Row 1' },
{ a: 2, b: 1, c: 'Row 2' },
{ a: 1, b: 2, c: 'Row 3' },
{ a: 2, b: 2, c: 'Row 4' },
{ a: 2, b: 2, c: 'Row 5' }
];
// create a dimension with pre-defined values for the property 'a' in the data.
const x = [1, 2, 3].map(criteria('a'));
// create a dimension with derived values for the property 'b' in the data.
const y = data.map(row => row.b).filter(distinct).map(criteria('b'));
// create a cube from the data using the x and y dimensions
const cube = pivot(data, x, y);
// Display the values of 'c' seen in the data
console.log(map(cube, records => records.map(t => t.c)));```
The output of this is:
[ [ [ 'Row 1' ], [ 'Row 2' ], [] ], [ [ 'Row 3' ], [ 'Row 4', 'Row 5' ], [] ] ]
From this output, we see that 'Row 1' and 'Row 2' both have a value for 'b' of 1 with the others having 2. This shows the _slice_ by the y dimension. The same stands for the x dimension, with 'Row 1' and 'Row 3' both having a value for 'a' of 1.
## Main concepts
[Dimensions](Dimensions)