symmetric.js 2.8 KB

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