Skip to content

Commit

Permalink
fix: pivot table support not number type #718
Browse files Browse the repository at this point in the history
  • Loading branch information
fangsmile committed Dec 13, 2023
1 parent 1664e3c commit 6c6bcf7
Show file tree
Hide file tree
Showing 5 changed files with 362 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/vtable/examples/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,10 @@ export const menus = [
{
path: 'pivot-analysis',
name: 'pivot-analysis-field'
},
{
path: 'pivot-analysis',
name: 'pivot-analysis-str'
}
]
},
Expand Down
334 changes: 334 additions & 0 deletions packages/vtable/examples/pivot-analysis/pivot-analysis-str.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,334 @@
import * as VTable from '../../src';
const PivotTable = VTable.PivotTable;
const CONTAINER_ID = 'vTable';
import { animalImageUrl, flowerVideoUrl } from '../resource-url';
const sumNumberFormat = VTable.DataStatistics.numberFormat({
suffix: '元'
});
const countNumberFormat = VTable.DataStatistics.numberFormat({
digitsAfterDecimal: 0,
thousandsSep: '',
suffix: '单'
});
export function createTable() {
const option: VTable.PivotTableConstructorOptions = {
rows: ['province', 'city'],
columns: ['category', 'sub_category'],
indicators: ['销售总额', '订单数', '订单均价'],
enableDataAnalysis: true,
indicatorTitle: '指标名称',
indicatorsAsCol: false,
dataConfig: {
aggregationRules: [
//做聚合计算的依据,如销售额如果没有配置则默认按聚合sum计算结果显示单元格内容
{
indicatorKey: '销售总额', //指标名称
field: 'sales', //指标依据字段
aggregationType: VTable.TYPES.AggregationType.NONE //计算类型
},
{
indicatorKey: '订单数', //指标名称
field: 'sales', //指标依据字段
aggregationType: VTable.TYPES.AggregationType.COUNT, //计算类型
formatFun: countNumberFormat
},
{
indicatorKey: '订单均价', //指标名称
field: 'sales', //指标依据字段
aggregationType: VTable.TYPES.AggregationType.AVG, //计算类型
formatFun: sumNumberFormat
}
]
},
corner: { titleOnDimension: 'row' },
records: [
{
sales: 'rrr',
number: 7789,
province: '浙江省',
city: '杭州市',
category: '家具',
sub_category: '桌子'
},
{
sales: 1891,
number: 7789,
province: '浙江省',
city: '杭州市',
category: '家具',
sub_category: '桌子'
},
{
sales: 792,
number: 2367,
province: '浙江省',
city: '绍兴市',
category: '家具',
sub_category: '桌子'
},
{
sales: 893,
number: 3877,
province: '浙江省',
city: '宁波市',
category: '家具',
sub_category: '桌子'
},
{
sales: 1094,
number: 4342,
province: '浙江省',
city: '舟山市',
category: '家具',
sub_category: '桌子'
},
{
sales: 1295,
number: 5343,
province: '浙江省',
city: '杭州市',
category: '家具',
sub_category: '沙发'
},
{
sales: 496,
number: 632,
province: '浙江省',
city: '绍兴市',
category: '家具',
sub_category: '沙发'
},
{
sales: 1097,
number: 7234,
province: '浙江省',
city: '宁波市',
category: '家具',
sub_category: '沙发'
},
{
sales: 998,
number: 834,
province: '浙江省',
city: '舟山市',
category: '家具',
sub_category: '沙发'
},
{
sales: 766,
number: 945,
province: '浙江省',
city: '杭州市',
category: '办公用品',
sub_category: '笔'
},
{
sales: 990,
number: 1304,
province: '浙江省',
city: '绍兴市',
category: '办公用品',
sub_category: '笔'
},
{
sales: 891,
number: 1145,
province: '浙江省',
city: '宁波市',
category: '办公用品',
sub_category: '笔'
},
{
sales: 792,
number: 1432,
province: '浙江省',
city: '舟山市',
category: '办公用品',
sub_category: '笔'
},
{
sales: 745,
number: 1343,
province: '浙江省',
city: '杭州市',
category: '办公用品',
sub_category: '纸张'
},
{
sales: 843,
number: 1354,
province: '浙江省',
city: '绍兴市',
category: '办公用品',
sub_category: '纸张'
},
{
sales: 895,
number: 1523,
province: '浙江省',
city: '宁波市',
category: '办公用品',
sub_category: '纸张'
},
{
sales: 965,
number: 1634,
province: '浙江省',
city: '舟山市',
category: '办公用品',
sub_category: '纸张'
},
{
sales: 776,
number: 1723,
province: '四川省',
city: '成都市',
category: '家具',
sub_category: '桌子'
},
{
sales: 634,
number: 1822,
province: '四川省',
city: '绵阳市',
category: '家具',
sub_category: '桌子'
},
{
sales: 909,
number: 1943,
province: '四川省',
city: '南充市',
category: '家具',
sub_category: '桌子'
},
{
sales: 399,
number: 2330,
province: '四川省',
city: '乐山市',
category: '家具',
sub_category: '桌子'
},
{
sales: 478,
number: 1900,
province: '四川省',
city: '乐山市',
category: '家具',
sub_category: '桌子'
},
{
sales: 700,
number: 2451,
province: '四川省',
city: '成都市',
category: '家具',
sub_category: '沙发'
},
{
sales: 689,
number: 2244,
province: '四川省',
city: '绵阳市',
category: '家具',
sub_category: '沙发'
},
{
sales: 500,
number: 2333,
province: '四川省',
city: '南充市',
category: '家具',
sub_category: '沙发'
},
{
sales: 800,
number: 2445,
province: '四川省',
city: '乐山市',
category: '家具',
sub_category: '沙发'
},
{
sales: 1044,
number: 2335,
province: '四川省',
city: '成都市',
category: '办公用品',
sub_category: '笔'
},
{
sales: 689,
number: 245,
province: '四川省',
city: '绵阳市',
category: '办公用品',
sub_category: '笔'
},
{
sales: 794,
number: 2457,
province: '四川省',
city: '南充市',
category: '办公用品',
sub_category: '笔'
},
{
sales: 566,
number: 2458,
province: '四川省',
city: '乐山市',
category: '办公用品',
sub_category: '笔'
},
{
sales: animalImageUrl,
number: 1458,
province: '四川省',
city: '乐山市',
category: '办公用品',
sub_category: '笔'
},
{
sales: 865,
number: 4004,
province: '四川省',
city: '成都市',
category: '办公用品',
sub_category: '纸张'
},
{
sales: animalImageUrl,
number: 3077,
province: '四川省',
city: '绵阳市',
category: '办公用品',
sub_category: '纸张'
},
{
sales: animalImageUrl,
number: 3551,
province: '四川省',
city: '南充市',
category: '办公用品',
sub_category: '纸张'
},
{
sales: animalImageUrl,
number: 352,
province: '四川省',
city: '乐山市',
category: '办公用品',
sub_category: '纸张'
}
],
widthMode: 'autoWidth' // 宽度模式:standard 标准模式; adaptive 自动填满容器
};

const instance = new PivotTable(document.getElementById(CONTAINER_ID)!, option);
window.tableInstance = instance;

// 只为了方便控制太调试用,不要拷贝
window.tableInstance = instance;
}
2 changes: 2 additions & 0 deletions packages/vtable/src/dataset/dataset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
CountAggregator,
MaxAggregator,
MinAggregator,
NoneAggregator,
RecordAggregator,
SumAggregator,
naturalSort,
Expand Down Expand Up @@ -291,6 +292,7 @@ export class Dataset {
this.registerAggregator(AggregationType.MAX, MaxAggregator);
this.registerAggregator(AggregationType.MIN, MinAggregator);
this.registerAggregator(AggregationType.AVG, AvgAggregator);
this.registerAggregator(AggregationType.NONE, NoneAggregator);
}
/**processRecord中按照collectValuesBy 收集了维度值。现在需要对有聚合需求的 处理收集维度值范围 */
private processCollectedValuesWithSumBy() {
Expand Down
22 changes: 21 additions & 1 deletion packages/vtable/src/dataset/statistics-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { BaseTableAPI } from '../ts-types/base-table';

export abstract class Aggregator {
className = 'Aggregator';
isRecord?: boolean = true;
isRecord?: boolean = true; //是否需要维护records 将数据源都记录下来
records?: any[] = [];
type?: string;
field?: string | string[];
Expand Down Expand Up @@ -66,6 +66,26 @@ export class RecordAggregator extends Aggregator {
this.records = [];
}
}

export class NoneAggregator extends Aggregator {
type: string = AggregationType.NONE; //仅获取其中一条数据 不做聚合 其fieldValue可以是number或者string类型
isRecord?: boolean = true;
declare field?: string;
fieldValue?: any;
push(record: any): void {
if (this.isRecord) {
this.records = [record];
}
this.fieldValue = record[this.field];
}
value() {
return this.fieldValue;
}
reset() {
this.records = [];
this.fieldValue = undefined;
}
}
export class SumAggregator extends Aggregator {
type: string = AggregationType.SUM;
sum = 0;
Expand Down
Loading

0 comments on commit 6c6bcf7

Please sign in to comment.