f092c9452547b702ff5c93635dcb230c2cc4e024ec09f1e2c4068ebe6e4c252bf7f1deb87c3d4cdd35847544679803245e190e9b9be723eb396c242496e825 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. export class Updater {
  2. constructor (config, series) {
  3. const { chart, key, getGraphConfig } = config
  4. if (typeof getGraphConfig !== 'function') {
  5. console.warn('Updater need function getGraphConfig!')
  6. return
  7. }
  8. if (!chart[key]) this.graphs = chart[key] = []
  9. Object.assign(this, config)
  10. this.update(series)
  11. }
  12. }
  13. Updater.prototype.update = function (series) {
  14. const { graphs, beforeUpdate } = this
  15. delRedundanceGraph(this, series)
  16. if (!series.length) return
  17. const beforeUpdateType = typeof beforeUpdate
  18. series.forEach((seriesItem, i) => {
  19. if (beforeUpdateType === 'function') beforeUpdate(graphs, seriesItem, i, this)
  20. const cache = graphs[i]
  21. if (cache) {
  22. changeGraphs(cache, seriesItem, i, this)
  23. } else {
  24. addGraphs(graphs, seriesItem, i, this)
  25. }
  26. })
  27. }
  28. function delRedundanceGraph (updater, series) {
  29. const { graphs, chart: { render } } = updater
  30. const cacheGraphNum = graphs.length
  31. const needGraphNum = series.length
  32. if (cacheGraphNum > needGraphNum) {
  33. const needDelGraphs = graphs.splice(needGraphNum)
  34. needDelGraphs.forEach(item => item.forEach(g => render.delGraph(g)))
  35. }
  36. }
  37. function changeGraphs (cache, seriesItem, i, updater) {
  38. const { getGraphConfig, chart: { render }, beforeChange } = updater
  39. const configs = getGraphConfig(seriesItem, updater)
  40. balanceGraphsNum(cache, configs, render)
  41. cache.forEach((graph, j) => {
  42. const config = configs[j]
  43. if (typeof beforeChange === 'function') beforeChange(graph, config)
  44. updateGraphConfigByKey(graph, config)
  45. })
  46. }
  47. function balanceGraphsNum (graphs, graphConfig, render) {
  48. const cacheGraphNum = graphs.length
  49. const needGraphNum = graphConfig.length
  50. if (needGraphNum > cacheGraphNum) {
  51. const lastCacheGraph = graphs.slice(-1)[0]
  52. const needAddGraphNum = needGraphNum - cacheGraphNum
  53. const needAddGraphs = new Array(needAddGraphNum).fill(0).map(foo => render.clone(lastCacheGraph))
  54. graphs.push(...needAddGraphs)
  55. } else if (needGraphNum < cacheGraphNum) {
  56. const needDelCache = graphs.splice(needGraphNum)
  57. needDelCache.forEach(g => render.delGraph(g))
  58. }
  59. }
  60. function addGraphs (graphs, seriesItem, i, updater) {
  61. const { getGraphConfig, getStartGraphConfig, chart } = updater
  62. const { render } = chart
  63. let startConfigs = null
  64. if (typeof getStartGraphConfig === 'function') startConfigs = getStartGraphConfig(seriesItem, updater)
  65. const configs = getGraphConfig(seriesItem, updater)
  66. if (!configs.length) return
  67. if (startConfigs) {
  68. graphs[i] = startConfigs.map(config => render.add(config))
  69. graphs[i].forEach((graph, i) => {
  70. const config = configs[i]
  71. updateGraphConfigByKey(graph, config)
  72. })
  73. } else {
  74. graphs[i] = configs.map(config => render.add(config))
  75. }
  76. const { afterAddGraph } = updater
  77. if (typeof afterAddGraph === 'function') afterAddGraph(graphs[i])
  78. }
  79. function updateGraphConfigByKey (graph, config) {
  80. const keys = Object.keys(config)
  81. keys.forEach(key => {
  82. if (key === 'shape' || key === 'style') {
  83. graph.animation(key, config[key], true)
  84. } else {
  85. graph[key] = config[key]
  86. }
  87. })
  88. }
  89. export function doUpdate ({
  90. chart,
  91. series,
  92. key,
  93. getGraphConfig,
  94. getStartGraphConfig,
  95. beforeChange,
  96. beforeUpdate,
  97. afterAddGraph
  98. } = {}) {
  99. if (chart[key]) {
  100. chart[key].update(series)
  101. } else {
  102. chart[key] = new Updater({
  103. chart,
  104. key,
  105. getGraphConfig,
  106. getStartGraphConfig,
  107. beforeChange,
  108. beforeUpdate,
  109. afterAddGraph
  110. }, series)
  111. }
  112. }