simplepivot
数据透视
Last updated 4 months ago by bplok20010 .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install simplepivot 
SYNC missed versions from official npm registry.

simplePivot

数据透视

不进行数据的汇总计算

Install

npm install --save simplepivot

Usage

declare function simplePivot(
	data: PData,
	options?: PivotOptions
): {
	columns: string[];
	list: any[][];
};

types

interface PData {
	columns: Array<string>;
	list: any[][];
	[x: string]: any;
}

interface PivotOptions {
	columns: Array<string>;
	values: string[];
	delimiter?: string;
}

Options

columns

透视列

import { simplePivot, E_VALUE } from "simplepivot";

simplePivot(data, {
    ...
	columns: ["道具名称", E_VALUE, "道具类型"],
});

注:常量E_VALUE的值为%E_VALUE%,data.columns 中避免出现%E_VALUE%,否则将出现不可预测问题。

values

透视值

delimiter

默认: _ 表头分隔符

使用


import { simplePivot, E_VALUE } from "simplepivot";

const dataset = {
    columns: ['性别', '人数'],
    list: [
        ['男', '1584'],
        ['女', '1514'],
    ]
};

const pivotData = simplePivot(dataset, {
    columns: ['性别'],
    values: ['人数']
});

console.log(pivotData);

const dataset1 = {
    columns: ['日期', '类型', '数值'],
    list: [
        ['20170809', 'A', 1],
        ['20170809', 'B', 2],
        ['20170808', 'A', 3],
        ['20170808', 'B', 4],
        ['20170808', 'C', 5],
        ['20170807', 'A', 6],
        ['20170807', 'B', 7],
    ]
};

console.log('====================')

const pivotData1 = simplePivot(dataset1, {
    columns: ['类型'],
    values: ['数值']
});

console.log(pivotData1);


其他

/**
 * 数据透视效果
 * ============================================================================================
 * metadata
 *		columns
 *		 		[日期, 游戏, 渠道, 注册数, 付费数]
 *		list
 *			[
 *				//20170809
 * 				['20170809', '游戏A', '小米', 100, 100],
 *				['20170809', '游戏A', '华为', 100, 100],
 *				['20170809', '游戏B', '华为', 100, 100],
 *				//20170808
 *				['20170808', '游戏A', '华为', 100, 100],
 *				['20170808', '游戏B', '小米', 100, 100],
 *				['20170808', '游戏B', '华为', 100, 100],
 *				//20170807
 *				['20170807', '游戏A', '小米', 100, 100],
 *			]
 *		一,透视参数:
 *			行: 日期, 渠道
 *          列: 游戏
 *          值: 注册数, 付费数
 *
 * 		效果
 *			日期      渠道   游戏A_注册数  游戏A_付费数 	游戏B_注册数    游戏B_付费数
 * 			-----------------------------------------------------------------------------------
 *			20170809   小米   100             100
 *			-----------------------------------------------------------------------------------
 *			20170809   华为    100			   100              100	              100
 *			-----------------------------------------------------------------------------------
 *			20170808   华为    100			   100              100	              100
 *			-----------------------------------------------------------------------------------
 *			20170808   小米   			                        100	              100
 *			-----------------------------------------------------------------------------------
 *			20170807   小米   100			   100
 *			-----------------------------------------------------------------------------------
 *
  *		二,透视参数:
 *			行: 日期
 *          列: 游戏, 渠道
 *          值: 注册数, 付费数
 *
 * 		效果
 *			日期      游戏A_小米_注册数  游戏A_小米_付费数  游戏A_华为_注册数  游戏A_华为_付费数  游戏B_小米_注册数  游戏B_小米_付费数  游戏B_华为_注册数  华为B_小米_付费数
 * 			---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 *			20170809   100                   100                  100                  100                                                            100                  100
 *			---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 *			20170808                                              100                  100                  100                   100                 100                  100
 *			---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 *			20170807   100			         100
 *			---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 *
 * ============================================================================================
 *
 */

Current Tags

  • 3.0.0                                ...           latest (4 months ago)

6 Versions

  • 3.0.0                                ...           4 months ago
  • 2.1.0                                ...           10 months ago
  • 2.0.0                                ...           a year ago
  • 1.0.2                                ...           2 years ago
  • 1.0.1                                ...           2 years ago
  • 1.0.0                                ...           2 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 7
Last Day 0
Last Week 0
Last Month 7
Dependencies (3)
Dev Dependencies (8)
Dependents (0)
None

Copyright 2014 - 2017 © taobao.org |