stack.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
  2. var isArray = require('@antv/util/lib/type/is-array');
  3. var isNil = require('@antv/util/lib/type/is-nil');
  4. var Adjust = require('./base');
  5. var Stack =
  6. /*#__PURE__*/
  7. function (_Adjust) {
  8. _inheritsLoose(Stack, _Adjust);
  9. function Stack() {
  10. return _Adjust.apply(this, arguments) || this;
  11. }
  12. var _proto = Stack.prototype;
  13. _proto._initDefaultCfg = function _initDefaultCfg() {
  14. this.xField = null; // 调整对应的 x 方向对应的字段名称
  15. this.yField = null; // 调整对应的 y 方向对应的字段名称
  16. };
  17. _proto.processAdjust = function processAdjust(dataArray) {
  18. this.processStack(dataArray);
  19. };
  20. _proto.processStack = function processStack(dataArray) {
  21. var self = this;
  22. var xField = self.xField;
  23. var yField = self.yField;
  24. var count = dataArray.length;
  25. var stackCache = {
  26. positive: {},
  27. negative: {}
  28. }; // 层叠顺序翻转
  29. if (self.reverseOrder) {
  30. dataArray = dataArray.slice(0).reverse();
  31. }
  32. for (var i = 0; i < count; i++) {
  33. var data = dataArray[i];
  34. for (var j = 0, len = data.length; j < len; j++) {
  35. var item = data[j];
  36. var x = item[xField] || 0;
  37. var y = item[yField];
  38. var xkey = x.toString();
  39. y = isArray(y) ? y[1] : y;
  40. if (!isNil(y)) {
  41. var direction = y >= 0 ? 'positive' : 'negative';
  42. if (!stackCache[direction][xkey]) {
  43. stackCache[direction][xkey] = 0;
  44. }
  45. item[yField] = [stackCache[direction][xkey], y + stackCache[direction][xkey]];
  46. stackCache[direction][xkey] += y;
  47. }
  48. }
  49. }
  50. };
  51. return Stack;
  52. }(Adjust);
  53. Adjust.Stack = Stack;
  54. module.exports = Stack;