ff09e619fde21f005ccaa118900851280ecaf47211048df889eb3e022bccea355e48c6d82092abf9fc13d2edfcbd952d602c0dd1bc973247738610d551e0cf 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.doUpdate = doUpdate;
  7. exports.Updater = void 0;
  8. var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
  9. var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
  10. var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
  11. var Updater = function Updater(config, series) {
  12. (0, _classCallCheck2["default"])(this, Updater);
  13. var chart = config.chart,
  14. key = config.key,
  15. getGraphConfig = config.getGraphConfig;
  16. if (typeof getGraphConfig !== 'function') {
  17. console.warn('Updater need function getGraphConfig!');
  18. return;
  19. }
  20. if (!chart[key]) this.graphs = chart[key] = [];
  21. Object.assign(this, config);
  22. this.update(series);
  23. };
  24. exports.Updater = Updater;
  25. Updater.prototype.update = function (series) {
  26. var _this = this;
  27. var graphs = this.graphs,
  28. beforeUpdate = this.beforeUpdate;
  29. delRedundanceGraph(this, series);
  30. if (!series.length) return;
  31. var beforeUpdateType = (0, _typeof2["default"])(beforeUpdate);
  32. series.forEach(function (seriesItem, i) {
  33. if (beforeUpdateType === 'function') beforeUpdate(graphs, seriesItem, i, _this);
  34. var cache = graphs[i];
  35. if (cache) {
  36. changeGraphs(cache, seriesItem, i, _this);
  37. } else {
  38. addGraphs(graphs, seriesItem, i, _this);
  39. }
  40. });
  41. };
  42. function delRedundanceGraph(updater, series) {
  43. var graphs = updater.graphs,
  44. render = updater.chart.render;
  45. var cacheGraphNum = graphs.length;
  46. var needGraphNum = series.length;
  47. if (cacheGraphNum > needGraphNum) {
  48. var needDelGraphs = graphs.splice(needGraphNum);
  49. needDelGraphs.forEach(function (item) {
  50. return item.forEach(function (g) {
  51. return render.delGraph(g);
  52. });
  53. });
  54. }
  55. }
  56. function changeGraphs(cache, seriesItem, i, updater) {
  57. var getGraphConfig = updater.getGraphConfig,
  58. render = updater.chart.render,
  59. beforeChange = updater.beforeChange;
  60. var configs = getGraphConfig(seriesItem, updater);
  61. balanceGraphsNum(cache, configs, render);
  62. cache.forEach(function (graph, j) {
  63. var config = configs[j];
  64. if (typeof beforeChange === 'function') beforeChange(graph, config);
  65. updateGraphConfigByKey(graph, config);
  66. });
  67. }
  68. function balanceGraphsNum(graphs, graphConfig, render) {
  69. var cacheGraphNum = graphs.length;
  70. var needGraphNum = graphConfig.length;
  71. if (needGraphNum > cacheGraphNum) {
  72. var lastCacheGraph = graphs.slice(-1)[0];
  73. var needAddGraphNum = needGraphNum - cacheGraphNum;
  74. var needAddGraphs = new Array(needAddGraphNum).fill(0).map(function (foo) {
  75. return render.clone(lastCacheGraph);
  76. });
  77. graphs.push.apply(graphs, (0, _toConsumableArray2["default"])(needAddGraphs));
  78. } else if (needGraphNum < cacheGraphNum) {
  79. var needDelCache = graphs.splice(needGraphNum);
  80. needDelCache.forEach(function (g) {
  81. return render.delGraph(g);
  82. });
  83. }
  84. }
  85. function addGraphs(graphs, seriesItem, i, updater) {
  86. var getGraphConfig = updater.getGraphConfig,
  87. getStartGraphConfig = updater.getStartGraphConfig,
  88. chart = updater.chart;
  89. var render = chart.render;
  90. var startConfigs = null;
  91. if (typeof getStartGraphConfig === 'function') startConfigs = getStartGraphConfig(seriesItem, updater);
  92. var configs = getGraphConfig(seriesItem, updater);
  93. if (!configs.length) return;
  94. if (startConfigs) {
  95. graphs[i] = startConfigs.map(function (config) {
  96. return render.add(config);
  97. });
  98. graphs[i].forEach(function (graph, i) {
  99. var config = configs[i];
  100. updateGraphConfigByKey(graph, config);
  101. });
  102. } else {
  103. graphs[i] = configs.map(function (config) {
  104. return render.add(config);
  105. });
  106. }
  107. var afterAddGraph = updater.afterAddGraph;
  108. if (typeof afterAddGraph === 'function') afterAddGraph(graphs[i]);
  109. }
  110. function updateGraphConfigByKey(graph, config) {
  111. var keys = Object.keys(config);
  112. keys.forEach(function (key) {
  113. if (key === 'shape' || key === 'style') {
  114. graph.animation(key, config[key], true);
  115. } else {
  116. graph[key] = config[key];
  117. }
  118. });
  119. }
  120. function doUpdate() {
  121. var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
  122. chart = _ref.chart,
  123. series = _ref.series,
  124. key = _ref.key,
  125. getGraphConfig = _ref.getGraphConfig,
  126. getStartGraphConfig = _ref.getStartGraphConfig,
  127. beforeChange = _ref.beforeChange,
  128. beforeUpdate = _ref.beforeUpdate,
  129. afterAddGraph = _ref.afterAddGraph;
  130. if (chart[key]) {
  131. chart[key].update(series);
  132. } else {
  133. chart[key] = new Updater({
  134. chart: chart,
  135. key: key,
  136. getGraphConfig: getGraphConfig,
  137. getStartGraphConfig: getStartGraphConfig,
  138. beforeChange: beforeChange,
  139. beforeUpdate: beforeUpdate,
  140. afterAddGraph: afterAddGraph
  141. }, series);
  142. }
  143. }