statusStayChart.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <template>
  2. <section>
  3. <div class="chart-contain">
  4. <normal-echart v-if="echartsOption" :chart-id="id" :option="echartsOption" @onClick="getCode" />
  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. dayList: []
  28. }
  29. },
  30. watch: {
  31. chartData: {
  32. handler(newV) {
  33. this.setChart()
  34. },
  35. deep: true,
  36. immediate: true
  37. }
  38. },
  39. methods: {
  40. getCode(val) {
  41. this.$emit('code', val, this.dayList, this.chartData)
  42. },
  43. setChart() {
  44. if (!this.chartData) return
  45. this.dayList = this.chartData.yaxis.map(item => { return item.name })
  46. const newArr = this.chartData.yaxis.filter(item => { return item.name !== '全部' })
  47. const colorArr = ['#409EFF', '#F8CE5C', '#F2904F', '#5EE2BE', '#D873F5', '#7479F5']
  48. this.echartsOption = {
  49. color: colorArr,
  50. tooltip: {
  51. trigger: 'axis',
  52. axisPointer: { type: 'line' },
  53. formatter: params => {
  54. let total = 0
  55. let backString = ``
  56. params.map((item, index) => {
  57. total = total + item.value
  58. backString = backString + `<span style="color: ${colorArr[index] || ''}">${item.seriesName}</span>:${item.value}个</br>`
  59. })
  60. return backString + `<span style="color: #F04864">总和</span>:${total}个`
  61. }
  62. },
  63. legend: { data: newArr.map(item => { return item.name }), left: 0, top: 0 },
  64. grid: { left: '0', right: '0', top: '8%', bottom: '0', containLabel: true },
  65. xAxis: { type: 'category', data: this.chartData.xaxis, axisTick: { alignWithLabel: true }, axisLabel: { interval: 0, rotate: 40 }},
  66. yAxis: { type: 'value', axisLine: { show: false }, splitLine: { lineStyle: { type: 'dashed' }}, axisLabel: { formatter: '{value}个' }},
  67. series: newArr.map(item => ({ ...item, type: 'bar', stack: '总和', barWidth: '20px' }))
  68. }
  69. }
  70. }
  71. }
  72. </script>
  73. <style lang="scss" scoped>
  74. .chart-contain {
  75. position: relative;
  76. height: 400px;
  77. width: 84%;
  78. margin: 20px auto;
  79. }
  80. </style>