# pivot-table-core

> A data analsis tool for viewing and editing aggregated data.

Latest version **1.0.1** (published 2018-08-14) · ISC license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install pivot-table-core
pnpm add pivot-table-core
yarn add pivot-table-core
bun add pivot-table-core
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2018-08-14 |
| First published | 2018-08-14 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 58.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Marketing Evolution |
| Maintainers | mevo_jdavancens |

## Links

- npm: https://www.npmjs.com/package/pivot-table-core
- npm.io page: https://npm.io/package/pivot-table-core

## Dependencies (3)

- [npm](https://npm.io/package/npm.md) ^6.1.0
- [node-csv](https://npm.io/package/node-csv.md) ^0.1.2
- [babel-plugin-transform-inline-environment-variables](https://npm.io/package/babel-plugin-transform-inline-environment-variables.md) ^0.4.3

## Recent versions

- 1.0.1 (latest) — 2018-08-14

## README

# Table of Contents

1.  [Install](#markdown-header-install)
2.  [Test](#markdown-header-test)
3.  [Build](#markdown-header-build)
4.  [Usage](#markdown-header-usage)
5.  [API](#markdown-header-api)

# Install

Make sure you have Node 9.4.0, NPM 5.6.0 and git.
Clone the repository:

```bash
git clone git@bitbucket.org:goodcarrot/pivot-table.git
```

Install dependencies:

```bash
npm install
```

# Test

Run the test suite:

```bash
npm test
```

# Build

Build everything:

```bash
npm run build-all
```

Build for development (node and web):

```bash
npm run build-dev
```

Build for production (node and web):

```bash
npm run build-prod
```

Build for development (node only):

```bash
npm run build-dev-node
```

Build for development (web only):

```bash
npm run build-dev-web
```

Build for production (node only):

```bash
npm run build-prod-node
```

Build for production (web only):

```bash
npm run build-prod-web
```

# Usage

## Configuration file

Here's an example:

```javascript
module.exports = {
  columns: [
    // pivots
    {
      data: 'rollup_name',
      export: true,
      id: 'rollup_id',
      kind: 'pivot',
    },
    {
      data: 'geography_name',
      export: true,
      id: 'geography_id',
      kind: 'pivot',
    },
    {
      base: true,
      data: 'name',
      export: true,
      id: 'media_id',
      kind: 'pivot',
    },
    // values
    {
      data: 'plan_spend',
      export: true,
      kind: 'value',
      reducer: 'sum',
    },
    {
      data: 'opt_spend',
      export: true,
      kind: 'value',
      reducer: 'sum',
    },
    {
      data: 'override_impact',
      export: true,
      kind: 'value',
      reducer: 'mean',
    },
    // derived
    // first order
    {
      data: 'spend_difference',
      derive: aggregate => aggregate.opt_spend - aggregate.plan_spend,
      export: true,
      kind: 'derived',
      order: 1,
    },
    // second order
    {
      data: 'spend_difference_per_impact',
      derive: aggregate => aggregate.spend_difference / aggregate.override_impact,
      export: true,
      kind: 'derived',
      order: 2,
    },
  ],
  postProcessing: {
    createRowData(x) {
      x.postProcessing = true;
    },
    initialize(x) {
      x.postProcessing = true;
    },
    summarizeAggregate(x) {
      x.postProcessing = true;
    },
  },
  subTable: {
    data: 'creative',
    columns: [
      {
        data: 'mct_name',
        kind: 'pivot',
      },
      {
        data: 'opt_spend',
        kind: 'value',
        reducer: 'sum',
      },
      {
        data: 'mct_name_opt_spend',
        derive: aggregate => `${aggregate.mct_name}_${aggregate.opt_spend}`,
        kind: 'derived',
        order: 1,
        reducer: 'sum',
      },
    ],
  },
  options: {
    paging: {},
  },
};
```

## Node

Example data JSON

```JSON
{
  "1": {
    "geography_id": 1,
    "geography_name": "National",
    "media_id": 1,
    "name": "Network TV - Spanish Language Network - Non-Prime",
    "opt_spend": 90,
    "override_impact": 100,
    "plan_spend": 100,
    "rollup_id": 17,
    "rollup_name": "Network TV"
  },
  "2": {
    "geography_id": 1,
    "geography_name": "National",
    "media_id": 2,
    "name": "Network TV - Spanish Language Network - Prime",
    "opt_spend": 50,
    "override_impact": 200,
    "plan_spend": 60,
    "rollup_id": 17,
    "rollup_name": "Network TV"
  },
  "3": {
    "creative": {
      "704": {
        "mct_name":  "color",
        "opt_spend": 10
      }
    },
    "geography_id": 1,
    "geography_name": "National",
    "media_id": 3,
    "name": "INVERSE»SWTO_Mid Spot 8/22»1 x 1»NA»RMB»DT»CPM»CTX»W»NA»SS»NA»NULLSG»PJ3ZWP",
    "opt_spend": 30,
    "override_impact": 100,
    "plan_spend": 25,
    "rollup_id": 51,
    "rollup_name": "Rich Media"
  },
  "4": {
    "creative": {
      "704": {
        "mct_name":  "color",
        "opt_spend": 10
      }
    },
    "geography_id": 1,
    "geography_name": "National",
    "media_id": 4,
    "name": "INVERSE»SPON_Innovation Section Takeover_Mid Spot»300 x 250»NA»BAN»MO»CPM»CTX»W»NA»TP»NA»NULLSG»PJ7DKL",
    "opt_spend": 30,
    "override_impact": 200,
    "plan_spend": 25,
    "rollup_id": 51,
    "rollup_name": "Rich Media"
  }
}
```

Import the PivotTableCore package, configuration file and data, and define metaData:

```javascript
const PivotTableCore = require('./PivotTableCore');
const config = require('./config');
const data = require('./data.json')
const metaData = { initialPivots: ['rollup_name'] };
```

Instantiate and initialize:

```javascript
const pivotTable = new PivotTableCore();
pivotTable.initialize(data, config, metaData);
```

Pivot on some columns and inspect:

```javascript
// set the active pivot
pivotTable.setActivePivot(['rollup_name', 'geography_name']);
console.log(pivotTable.getActivePivotKey());
// "rollup_name,geography_name"

// aggregate and summarize rows
pivotTable.pivot();

// get aggregates for active pivot
let aggregates = pivotTable.getAggregates();
console.log(Object.keys(aggregates));
// ["17,1", ["51,1"]]

// get a specific aggregate
let agg = pivotTable.getAggregate(['17,1'])

// keys are composites made from IDs
console.log(agg);
// { geography_name: "National", name: "Mixed", opt_spend: 140, ... }
```

# API

<!-- Generated by documentation.js. Update this documentation by updating the source code. -->

### Table of Contents

-   [PivotTableCore](#pivottablecore)
    -   [initialize](#initialize)
        -   [Parameters](#parameters)
    -   [getAggregate](#getaggregate)
        -   [Parameters](#parameters-1)
    -   [getAggregates](#getaggregates)
    -   [getActivePivotColumns](#getactivepivotcolumns)
    -   [getActivePivotKey](#getactivepivotkey)
    -   [getPivot](#getpivot)
        -   [Parameters](#parameters-2)
    -   [pivot](#pivot)
    -   [query](#query)
    -   [setActivePivots](#setactivepivots)
        -   [Parameters](#parameters-3)
    -   [setFilter](#setfilter)
        -   [Parameters](#parameters-4)
    -   [setPagination](#setpagination)
        -   [Parameters](#parameters-5)
    -   [setSorting](#setsorting)
        -   [Parameters](#parameters-6)

## PivotTableCore

Class representing a PivotTable.

### initialize

Initialize the pivot table.

#### Parameters

-   `data` **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** Row data.
-   `config` **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** PivotTableCore configuration.
-   `metaData` **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** Meta data.

### getAggregate

Returns an aggregate from the active pivot.

#### Parameters

-   `key` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Aggregate key (i.e. it's id).

Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** 

### getAggregates

Returns all aggregates from the active pivot.

Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** 

### getActivePivotColumns

Returns the active pivot's columns names.

Returns **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)** 

### getActivePivotKey

Returns a composite key formed by the active pivot's columns names.

Returns **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** 

### getPivot

Returns a pivot (an object containing aggregates)

#### Parameters

-   `key` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Pivot key (column names separated by comma)

Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** A pivot.

### pivot

Aggregates row data by active pivots, summarizes data and runs optional
post processing specified in config.

### query

Returns the aggregate data filtered by the query set, with pagination info.

### setActivePivots

Sets the active pivot. (Doesn't perform the pivot logic though)

#### Parameters

-   `activePivots` **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)** An array of column names.

### setFilter

Sets filter options for the query set

#### Parameters

-   `conjunction` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Determines if all or any of the predicates
    should be matched ('&&' or '||') (optional, default `'&&'`)
-   `predicates` **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)** A list of predicate objects: column name (string)
    , value (any), and a comparison (string). Supported comparisons:
    '==', '!=', '&lt;', '&lt;=', '>', '>=', and 'in'. (optional, default `[]`)

### setPagination

Sets pagination options for the query set

#### Parameters

-   `isActive`   (optional, default `true`)
-   `currentPage` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)**  (optional, default `0`)
-   `rowsPerPage` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)**  (optional, default `15`)

### setSorting

Sets sorting options for the query set

#### Parameters

-   `columnName` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Column to sort by.
-   `direction` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Sorting direction ('ASC' or 'DESC'). (optional, default `Directions.ASC`)

---
_Source: https://npm.io/package/pivot-table-core · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
