UnsafeItemsChart.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <template>
  2. <div class="unsafe-items-chart">
  3. <div class="chart-container" ref="itemsChart"></div>
  4. <div class="items-list">
  5. <div v-for="(item, index) in chartsData.items" :key="index" class="list-item">
  6. <div class="item-name">{{ item.name }}</div>
  7. <div class="item-value">{{ item.value }}</div>
  8. </div>
  9. </div>
  10. </div>
  11. </template>
  12. <script>
  13. import * as echarts from 'echarts'
  14. export default {
  15. name: 'UnsafeItemsChart',
  16. props: {
  17. chartsData: {
  18. type: Object,
  19. default: () => ({
  20. items: [
  21. { name: '打火机', value: 57 },
  22. { name: '固体酒精', value: 55 },
  23. { name: '手机', value: 56 },
  24. { name: '不合格充电宝', value: 56 },
  25. { name: '登山杖', value: 55 }
  26. ]
  27. })
  28. }
  29. },
  30. data() {
  31. return {
  32. chart: null
  33. }
  34. },
  35. mounted() {
  36. this.$nextTick(() => {
  37. this.initChart()
  38. })
  39. },
  40. watch: {
  41. chartsData: {
  42. deep: true,
  43. handler() {
  44. this.$nextTick(() => {
  45. if (this.chart) this.chart.dispose()
  46. this.initChart()
  47. })
  48. }
  49. }
  50. },
  51. methods: {
  52. initChart() {
  53. if (!this.$refs.itemsChart) return
  54. this.chart = echarts.init(this.$refs.itemsChart)
  55. const option = {
  56. responsive: true,
  57. maintainAspectRatio: false,
  58. series: [{
  59. type: 'pie',
  60. radius: ['50%', '70%'],
  61. data: this.chartsData.items.map(item => ({
  62. name: item.name,
  63. value: item.value
  64. })),
  65. itemStyle: {
  66. color: function(params) {
  67. const colors = ['#60A5FA', '#A78BFA', '#34D399', '#FBBF24', '#F472B6']
  68. return colors[params.dataIndex]
  69. }
  70. },
  71. label: {
  72. show: true,
  73. color: 'rgba(255, 255, 255, 0.7)',
  74. fontSize: 10,
  75. formatter: '{b}\n{d}%'
  76. }
  77. }]
  78. }
  79. this.chart.setOption(option)
  80. }
  81. },
  82. beforeDestroy() {
  83. if (this.chart) this.chart.dispose()
  84. }
  85. }
  86. </script>
  87. <style lang="scss" scoped>
  88. .unsafe-items-chart {
  89. .chart-container {
  90. height: 220rpx;
  91. }
  92. .items-list {
  93. margin-top: 16rpx;
  94. }
  95. .list-item {
  96. display: flex;
  97. justify-content: space-between;
  98. font-size: 24rpx;
  99. padding: 8rpx 0;
  100. .item-name {
  101. color: rgba(255, 255, 255, 0.7);
  102. }
  103. .item-value {
  104. color: rgba(255, 255, 255, 0.9);
  105. }
  106. }
  107. }
  108. </style>