UNPKG

1.44 kBJavaScriptView Raw
1const isArray = require('@antv/util/lib/type/isArray');
2const isNil = require('@antv/util/lib/type/isNil');
3const Adjust = require('./base');
4
5class Stack extends Adjust {
6
7 _initDefaultCfg() {
8 this.xField = null; // 调整对应的 x 方向对应的字段名称
9 this.yField = null; // 调整对应的 y 方向对应的字段名称
10 }
11
12 processAdjust(dataArray) {
13 this.processStack(dataArray);
14 }
15
16 processStack(dataArray) {
17 const self = this;
18 const xField = self.xField;
19 const yField = self.yField;
20 const count = dataArray.length;
21 const stackCache = {
22 positive: {},
23 negative: {}
24 };
25 // 层叠顺序翻转
26 if (self.reverseOrder) {
27 dataArray = dataArray.slice(0).reverse();
28 }
29 for (let i = 0; i < count; i++) {
30 const data = dataArray[i];
31 for (let j = 0, len = data.length; j < len; j++) {
32 const item = data[j];
33 const x = item[xField] || 0;
34 let y = item[yField];
35 const xkey = x.toString();
36 y = isArray(y) ? y[1] : y;
37 if (!isNil(y)) {
38 const direction = y >= 0 ? 'positive' : 'negative';
39 if (!stackCache[direction][xkey]) {
40 stackCache[direction][xkey] = 0;
41 }
42 item[yField] = [ stackCache[direction][xkey], y + stackCache[direction][xkey] ];
43 stackCache[direction][xkey] += y;
44 }
45 }
46 }
47 }
48}
49
50Adjust.Stack = Stack;
51module.exports = Stack;