UNPKG

4.22 kBJavaScriptView Raw
1function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
2
3function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
4
5function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
6
7var each = require('@antv/util/lib/each');
8var maxBy = require('@antv/util/lib/math/maxBy');
9var isArray = require('@antv/util/lib/type/isArray');
10var ArrayUtil = {
11 merge: require('@antv/util/lib/array/merge'),
12 values: require('@antv/util/lib/array/values')
13};
14var Adjust = require('./base');
15
16var Symmetric = function (_Adjust) {
17 _inherits(Symmetric, _Adjust);
18
19 function Symmetric() {
20 _classCallCheck(this, Symmetric);
21
22 return _possibleConstructorReturn(this, _Adjust.apply(this, arguments));
23 }
24
25 Symmetric.prototype._initDefaultCfg = function _initDefaultCfg() {
26 this.xField = null; // 调整对应的 x 方向对应的字段名称
27 this.yField = null; // 调整对应的 y 方向对应的字段名称
28 this.cacheMax = null; // 缓存的最大值
29 this.adjustNames = ['y']; // Only support stack y
30 this.groupFields = null; // 参与分组的数据维度
31 };
32
33 // 获取最大的y值
34
35
36 Symmetric.prototype._getMax = function _getMax(dim) {
37 var self = this;
38 var mergeData = self.mergeData;
39 var maxRecord = maxBy(mergeData, function (obj) {
40 var value = obj[dim];
41 if (isArray(value)) {
42 return Math.max.apply(null, value);
43 }
44 return value;
45 });
46 var maxValue = maxRecord[dim];
47 var max = isArray(maxValue) ? Math.max.apply(null, maxValue) : maxValue;
48 return max;
49 };
50
51 // 获取每个字段最大的值
52
53
54 Symmetric.prototype._getXValuesMax = function _getXValuesMax() {
55 var self = this;
56 var yField = self.yField;
57 var xField = self.xField;
58 var cache = {};
59 var mergeData = self.mergeData;
60 each(mergeData, function (obj) {
61 var xValue = obj[xField];
62 var yValue = obj[yField];
63 var max = isArray(yValue) ? Math.max.apply(null, yValue) : yValue;
64 cache[xValue] = cache[xValue] || 0;
65 if (cache[xValue] < max) {
66 cache[xValue] = max;
67 }
68 });
69 return cache;
70 };
71
72 // 入口函数
73
74
75 Symmetric.prototype.processAdjust = function processAdjust(dataArray) {
76 var self = this;
77 var mergeData = ArrayUtil.merge(dataArray);
78 self.mergeData = mergeData;
79 self._processSymmetric(dataArray);
80 self.mergeData = null;
81 };
82
83 // 处理对称
84
85
86 Symmetric.prototype._processSymmetric = function _processSymmetric(dataArray) {
87 var self = this;
88 var xField = self.xField;
89 var yField = self.yField;
90 var max = self._getMax(yField);
91 var first = dataArray[0][0];
92
93 var cache = void 0;
94 if (first && isArray(first[yField])) {
95 cache = self._getXValuesMax();
96 }
97 each(dataArray, function (data) {
98 each(data, function (obj) {
99 var value = obj[yField];
100 var offset = void 0;
101 if (isArray(value)) {
102 var xValue = obj[xField];
103 var valueMax = cache[xValue];
104 offset = (max - valueMax) / 2;
105 var tmp = [];
106 /* eslint-disable no-loop-func */
107 each(value, function (subVal) {
108 // 多个字段
109 tmp.push(offset + subVal);
110 });
111 /* eslint-enable no-loop-func */
112 obj[yField] = tmp;
113 } else {
114 offset = (max - value) / 2;
115 obj[yField] = [offset, value + offset];
116 }
117 });
118 });
119 };
120
121 return Symmetric;
122}(Adjust);
123
124Adjust.Symmetric = Symmetric;
125module.exports = Symmetric;
\No newline at end of file