UnsafeTypesChart.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. series: [{
  63. type: 'pie',
  64. radius: ['50%', '70%'],
  65. data: this.chartsData.types.map(item => ({
  66. name: item.name,
  67. value: item.value
  68. })),
  69. itemStyle: {
  70. color: function(params) {
  71. const colors = ['#A78BFA', '#6366F1', '#8B5CF6', '#7C3AED', '#6D28D9']
  72. return colors[params.dataIndex]
  73. }
  74. },
  75. label: {
  76. show: true,
  77. color: '#333',
  78. fontSize: 10,
  79. formatter: '{b}\n{d}%'
  80. }
  81. }]
  82. }
  83. this.chart.setOption(option)
  84. }
  85. },
  86. beforeDestroy() {
  87. if (this.chart) this.chart.dispose()
  88. }
  89. }
  90. </script>
  91. <style lang="scss" scoped>
  92. .unsafe-types-chart {
  93. .chart-container {
  94. height: 220rpx;
  95. }
  96. .types-list {
  97. margin-top: 16rpx;
  98. }
  99. .list-item {
  100. display: flex;
  101. justify-content: space-between;
  102. font-size: 24rpx;
  103. padding: 8rpx 0;
  104. .item-name {
  105. color: #666;
  106. }
  107. .item-value {
  108. color: #333;
  109. }
  110. }
  111. }
  112. </style>