GoodsRankingChart.vue 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <template>
  2. <div class="goods-ranking-chart" ref="goodsRankingChartRef"></div>
  3. </template>
  4. <script>
  5. import echartsMixin from '../mixin/echarts'
  6. export default {
  7. mixins: [echartsMixin('goodsRankingChartRef')],
  8. data() {
  9. return {
  10. staticOptions: {
  11. grid: { left: '30%', right: '3%', top: '0%', bottom: '1%' },
  12. xAxis: { type: 'value', show: false },
  13. yAxis: {
  14. type: 'category',
  15. axisLine: { show: false },
  16. axisTick: { show: false },
  17. data: [],
  18. axisLabel: {
  19. color: '#A4A4A4',
  20. fontSize: 12,
  21. formatter(value) {
  22. const MAX_LENGTH = 7
  23. const SPLIT_LENGTH = 5
  24. if (value.length <= MAX_LENGTH) {
  25. return value
  26. }
  27. const pattern = new RegExp(`(.{${SPLIT_LENGTH}})(.{1,${SPLIT_LENGTH}})`)
  28. const replacement = '$1\n$2'
  29. return value.replace(pattern, replacement) + (value.length > SPLIT_LENGTH * 2 ? '...' : '')
  30. }
  31. }
  32. },
  33. series: {
  34. zlevel: 100,
  35. name: '销量',
  36. type: 'bar',
  37. data: [],
  38. barWidth: '16px',
  39. itemStyle: { borderRadius: 10, color: '#FFC117' },
  40. label: { show: true, precision: 1, position: 'right', valueAnimation: true, fontFamily: 'monospace', color: '#3d3d3d' }
  41. }
  42. },
  43. timer: null
  44. }
  45. },
  46. methods: {
  47. update(data) {
  48. if (!data || (Array.isArray(data) && !data.length)) return
  49. if (this.timer) this.clearTimer()
  50. let index = 0
  51. this.setChartData(data, index)
  52. this.timer = setInterval(() => {
  53. if (data.length - 9 === index) {
  54. index = 0
  55. }
  56. this.setChartData(data, index)
  57. index++
  58. }, 1000)
  59. },
  60. clearTimer() {
  61. clearInterval(this.timer)
  62. this.timer = null
  63. },
  64. setChartData(data, index) {
  65. const d = data.slice(index, index + 9)
  66. const goodsName = []
  67. const goodsCount = []
  68. d.forEach((item) => {
  69. goodsName.push(item.productName)
  70. goodsCount.push(item.productTransactionTotal)
  71. })
  72. this.instance.setOption({
  73. yAxis: { data: goodsName.reverse() },
  74. series: { data: goodsCount.reverse() }
  75. })
  76. }
  77. },
  78. beforeDestroy() {
  79. this.clearTimer()
  80. }
  81. }
  82. </script>
  83. <style lang="scss" scoped>
  84. .goods-ranking-chart {
  85. height: 370px;
  86. }
  87. </style>