warnGradeEchart.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <template>
  2. <div class="echartBar">
  3. <div class="bar" ref="bar"></div>
  4. </div>
  5. </template>
  6. <script lang="ts" setup>
  7. import { defineProps, ref, nextTick, reactive, watch } from 'vue';
  8. import * as echarts from 'echarts';
  9. let props = defineProps({
  10. echartData: {
  11. type: Object,
  12. default: () => {
  13. return {}
  14. }
  15. }
  16. });
  17. //获取dom元素节点
  18. let bar = ref<any>();
  19. let echartDatas = ref<any[]>([])
  20. watch(
  21. () => props.echartData,
  22. (newV, oldV) => {
  23. let data:any[]=[]
  24. Object.keys(newV).forEach(el => {
  25. if (el == 'dust') {
  26. data.push({ name: '粉尘', value: newV[el] })
  27. } else if (el == 'fire') {
  28. data.push({ name: '火灾', value: newV[el] })
  29. } else if (el == 'gas') {
  30. data.push({ name: '瓦斯', value: newV[el] })
  31. } else if (el == 'vent') {
  32. data.push({ name: '通风', value: newV[el] })
  33. }
  34. })
  35. echartDatas.value=data
  36. getOption();
  37. },
  38. { deep : true, }
  39. );
  40. function getOption() {
  41. nextTick(() => {
  42. let myChart = echarts.init(bar.value);
  43. let option = {
  44. title: {
  45. text: '风险比例分析',
  46. left: '20%',
  47. top: 0,
  48. textStyle: {
  49. color: '#fff',
  50. fontSize: 14,
  51. }
  52. },
  53. tooltip: {
  54. trigger: 'item',
  55. formatter: "{b} : {c} ({d}%)",
  56. backgroundColor: 'rgba(0, 0, 0, .6)',
  57. textStyle: {
  58. color: '#fff',
  59. fontSize: 12,
  60. },
  61. },
  62. series: [{
  63. name: '',
  64. type: 'pie',
  65. radius: '50%',
  66. center: ['30%', '54%'],
  67. color: ['rgb(131,249,103)', '#FBFE27', '#FE5050', '#1DB7E5'], //'#FBFE27','rgb(11,228,96)','#FE5050'
  68. data: echartDatas.value.sort(function (a, b) {
  69. return a.value - b.value
  70. }),
  71. roseType: 'radius',
  72. label: {
  73. normal: {
  74. formatter: ['{c|{c}}', '{b|{b}}'].join('\n'),
  75. rich: {
  76. c: {
  77. color: '#00d8ff',
  78. fontSize: 14,
  79. fontWeight: 'bold',
  80. lineHeight: 5
  81. },
  82. b: {
  83. color: '#fff',
  84. fontSize: 12,
  85. height: 40
  86. },
  87. },
  88. }
  89. },
  90. labelLine: {
  91. normal: {
  92. lineStyle: {
  93. color: 'rgb(98,137,169)',
  94. },
  95. smooth: 0.2,
  96. length: 10,
  97. length2: 10,
  98. }
  99. },
  100. itemStyle: {
  101. normal: {
  102. shadowColor: 'rgba(0, 0, 0, 0.8)',
  103. shadowBlur: 50,
  104. }
  105. }
  106. }]
  107. };
  108. myChart.setOption(option);
  109. window.onresize = function () {
  110. myChart.resize();
  111. };
  112. });
  113. }
  114. </script>
  115. <style scoped lang="less">
  116. .echartBar {
  117. width: 100%;
  118. height: 100%;
  119. .bar {
  120. width: 100%;
  121. height: 100%;
  122. }
  123. }
  124. </style>