UnsafeTypesChart.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <template>
  2. <div class="unsafe-types-chart">
  3. <div class="chart-container" ref="typesChart"></div>
  4. <div class="types-list">
  5. <div v-for="(item, index) in chartsData.types" :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: 'UnsafeTypesChart',
  16. props: {
  17. chartsData: {
  18. type: Object,
  19. default: () => ({
  20. types: [
  21. { name: '一类', value: 56 },
  22. { name: '二类', value: 57 },
  23. { name: '三类', value: 56 },
  24. { name: '四类', value: 55 },
  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.typesChart) return
  54. this.chart = echarts.init(this.$refs.typesChart)
  55. const option = {
  56. responsive: true,
  57. maintainAspectRatio: false,
  58. tooltip: {
  59. trigger: 'item',
  60. formatter: '{b}: {c} ({d}%)'
  61. },
  62. legend: {
  63. data: this.chartsData.types.map(item => item.name),
  64. textStyle: { color: '#666', fontSize: 10 },
  65. top: 0
  66. },
  67. series: [{
  68. type: 'pie',
  69. radius: ['50%', '70%'],
  70. data: this.chartsData.types.map(item => ({
  71. name: item.name,
  72. value: item.value
  73. })),
  74. itemStyle: {
  75. color: function(params) {
  76. const colors = ['#A78BFA', '#6366F1', '#8B5CF6', '#7C3AED', '#6D28D9']
  77. return colors[params.dataIndex]
  78. }
  79. },
  80. label: {
  81. show: true,
  82. color: '#333',
  83. fontSize: 10,
  84. formatter: '{b}\n{d}%'
  85. }
  86. }]
  87. }
  88. this.chart.setOption(option)
  89. }
  90. },
  91. beforeDestroy() {
  92. if (this.chart) this.chart.dispose()
  93. }
  94. }
  95. </script>
  96. <style lang="scss" scoped>
  97. .unsafe-types-chart {
  98. .chart-container {
  99. height: 400rpx;
  100. }
  101. .types-list {
  102. margin-top: 16rpx;
  103. }
  104. .list-item {
  105. display: flex;
  106. justify-content: space-between;
  107. font-size: 24rpx;
  108. padding: 8rpx 0;
  109. .item-name {
  110. color: #666;
  111. }
  112. .item-value {
  113. color: #333;
  114. }
  115. }
  116. }
  117. </style>