statusStayChart.vue 2.1 KB

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