statusStayChart.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <template>
  2. <section>
  3. <div class="chart-contain">
  4. <normal-echart v-if="echartsOption" :chart-id="id" :option="echartsOption" @onClick="chartClick" />
  5. </div>
  6. </section>
  7. </template>
  8. <script>
  9. import normalEchart from '@/components/chart/normalEchart'
  10. export default {
  11. components: { normalEchart },
  12. props: {
  13. id: {
  14. type: String,
  15. default: 'status-stay-chart',
  16. required: false
  17. },
  18. chartData: {
  19. type: Object,
  20. default: () => null,
  21. required: false
  22. }
  23. },
  24. data() {
  25. return {
  26. echartsOption: null
  27. }
  28. },
  29. watch: {
  30. chartData: {
  31. handler(newV) {
  32. this.setChart()
  33. },
  34. deep: true,
  35. immediate: true
  36. }
  37. },
  38. methods: {
  39. setChart() {
  40. if (!this.chartData) return
  41. const newArr = this.chartData.yaxis.filter(item => { return item && (item.name !== '全部') })
  42. const colorArr = ['#409EFF', '#F8CE5C', '#F2904F', '#5EE2BE', '#D873F5', '#7479F5']
  43. let series = null
  44. if (this.chartData.type === '0') {
  45. const childArr = this.chartData.xaxis.map((item, index) => {
  46. return {
  47. value: this.chartData.yaxis[0] && this.chartData.yaxis[0].data[index] || 0,
  48. idList: this.chartData.yaxis[0] && this.chartData.yaxis[0].idList[index] || []
  49. }
  50. })
  51. series = [{
  52. name: '数量', type: 'bar', barWidth: '20px', data: childArr,
  53. itemStyle: { normal: { label: { show: true, formatter: '{c}', position: 'top' }}}
  54. }]
  55. } else {
  56. series = newArr.map(item => (
  57. { ...item, type: 'bar', stack: '总和', barWidth: '20px' }
  58. ))
  59. }
  60. this.echartsOption = {
  61. color: colorArr,
  62. tooltip: {
  63. trigger: 'axis',
  64. axisPointer: { type: 'line' },
  65. formatter: params => {
  66. let total = 0
  67. let backString = ``
  68. params.map((item, index) => {
  69. total = total + item.value
  70. backString = backString + `<span style="color: ${colorArr[index] || ''}">${item.seriesName}</span>:${item.value}个</br>`
  71. })
  72. return backString + `<span style="color: #F04864">总和</span>:${total}个`
  73. }
  74. },
  75. legend: { data: newArr.map(item => { return item && (item.name !== '全部') }), left: 0, top: 0 },
  76. grid: { left: '0', right: '0', top: '8%', bottom: '0', containLabel: true },
  77. xAxis: { type: 'category', data: this.chartData.xaxis, axisTick: { alignWithLabel: true }, axisLabel: { interval: 0, rotate: 40 }},
  78. yAxis: { type: 'value', axisLine: { show: false }, splitLine: { lineStyle: { type: 'dashed' }}, axisLabel: { formatter: '{value}个' }},
  79. series
  80. }
  81. },
  82. chartClick(param) {
  83. this.$emit('onClick', param)
  84. }
  85. }
  86. }
  87. </script>
  88. <style lang="scss" scoped>
  89. .chart-contain {
  90. position: relative;
  91. height: 320px;
  92. width: 84%;
  93. margin: 20px auto;
  94. }
  95. </style>