1 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
2 |
|
3 | function _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 |
|
5 | function _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 |
|
7 | var each = require('@antv/util/lib/each');
|
8 | var maxBy = require('@antv/util/lib/math/maxBy');
|
9 | var isArray = require('@antv/util/lib/type/isArray');
|
10 | var ArrayUtil = {
|
11 | merge: require('@antv/util/lib/array/merge'),
|
12 | values: require('@antv/util/lib/array/values')
|
13 | };
|
14 | var Adjust = require('./base');
|
15 |
|
16 | var 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;
|
27 | this.yField = null;
|
28 | this.cacheMax = null;
|
29 | this.adjustNames = ['y'];
|
30 | this.groupFields = null;
|
31 | };
|
32 |
|
33 |
|
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 |
|
107 | each(value, function (subVal) {
|
108 |
|
109 | tmp.push(offset + subVal);
|
110 | });
|
111 |
|
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 |
|
124 | Adjust.Symmetric = Symmetric;
|
125 | module.exports = Symmetric; |
\ | No newline at end of file |