symmetric.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
  2. var each = require('@antv/util/lib/each');
  3. var maxBy = require('@antv/util/lib/math/max-by');
  4. var isArray = require('@antv/util/lib/type/is-array');
  5. var ArrayUtil = {
  6. merge: require('@antv/util/lib/array/merge')
  7. };
  8. var Adjust = require('./base');
  9. var Symmetric =
  10. /*#__PURE__*/
  11. function (_Adjust) {
  12. _inheritsLoose(Symmetric, _Adjust);
  13. function Symmetric() {
  14. return _Adjust.apply(this, arguments) || this;
  15. }
  16. var _proto = Symmetric.prototype;
  17. _proto._initDefaultCfg = function _initDefaultCfg() {
  18. this.xField = null; // 调整对应的 x 方向对应的字段名称
  19. this.yField = null; // 调整对应的 y 方向对应的字段名称
  20. this.cacheMax = null; // 缓存的最大值
  21. this.adjustNames = ['y']; // Only support stack y
  22. this.groupFields = null; // 参与分组的数据维度
  23. }; // 获取最大的y值
  24. _proto._getMax = function _getMax(dim) {
  25. var self = this;
  26. var mergeData = self.mergeData;
  27. var maxRecord = maxBy(mergeData, function (obj) {
  28. var value = obj[dim];
  29. if (isArray(value)) {
  30. return Math.max.apply(null, value);
  31. }
  32. return value;
  33. });
  34. var maxValue = maxRecord[dim];
  35. var max = isArray(maxValue) ? Math.max.apply(null, maxValue) : maxValue;
  36. return max;
  37. }; // 获取每个字段最大的值
  38. _proto._getXValuesMax = function _getXValuesMax() {
  39. var self = this;
  40. var yField = self.yField;
  41. var xField = self.xField;
  42. var cache = {};
  43. var mergeData = self.mergeData;
  44. each(mergeData, function (obj) {
  45. var xValue = obj[xField];
  46. var yValue = obj[yField];
  47. var max = isArray(yValue) ? Math.max.apply(null, yValue) : yValue;
  48. cache[xValue] = cache[xValue] || 0;
  49. if (cache[xValue] < max) {
  50. cache[xValue] = max;
  51. }
  52. });
  53. return cache;
  54. }; // 入口函数
  55. _proto.processAdjust = function processAdjust(dataArray) {
  56. var self = this;
  57. var mergeData = ArrayUtil.merge(dataArray);
  58. self.mergeData = mergeData;
  59. self._processSymmetric(dataArray);
  60. self.mergeData = null;
  61. }; // 处理对称
  62. _proto._processSymmetric = function _processSymmetric(dataArray) {
  63. var self = this;
  64. var xField = self.xField;
  65. var yField = self.yField;
  66. var max = self._getMax(yField);
  67. var first = dataArray[0][0];
  68. var cache;
  69. if (first && isArray(first[yField])) {
  70. cache = self._getXValuesMax();
  71. }
  72. each(dataArray, function (data) {
  73. each(data, function (obj) {
  74. var value = obj[yField];
  75. var offset;
  76. if (isArray(value)) {
  77. var xValue = obj[xField];
  78. var valueMax = cache[xValue];
  79. offset = (max - valueMax) / 2;
  80. var tmp = [];
  81. /* eslint-disable no-loop-func */
  82. each(value, function (subVal) {
  83. // 多个字段
  84. tmp.push(offset + subVal);
  85. });
  86. /* eslint-enable no-loop-func */
  87. obj[yField] = tmp;
  88. } else {
  89. offset = (max - value) / 2;
  90. obj[yField] = [offset, value + offset];
  91. }
  92. });
  93. });
  94. };
  95. return Symmetric;
  96. }(Adjust);
  97. Adjust.Symmetric = Symmetric;
  98. module.exports = Symmetric;