1 | const isArray = require('@antv/util/lib/type/isArray');
|
2 | const isNil = require('@antv/util/lib/type/isNil');
|
3 | const Adjust = require('./base');
|
4 |
|
5 | class Stack extends Adjust {
|
6 |
|
7 | _initDefaultCfg() {
|
8 | this.xField = null;
|
9 | this.yField = null;
|
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 |
|
50 | Adjust.Stack = Stack;
|
51 | module.exports = Stack;
|