warnGradeEchart.vue 3.2 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. {
  64. name: '',
  65. type: 'pie',
  66. radius: '50%',
  67. center: ['30%', '54%'],
  68. color: ['rgb(131,249,103)', '#FBFE27', '#FE5050', '#1DB7E5'], //'#FBFE27','rgb(11,228,96)','#FE5050'
  69. data: echartDatas.value.sort(function (a, b) {
  70. return a.value - b.value;
  71. }),
  72. roseType: 'radius',
  73. label: {
  74. normal: {
  75. formatter: ['{c|{c}}', '{b|{b}}'].join('\n'),
  76. rich: {
  77. c: {
  78. color: '#00d8ff',
  79. fontSize: 14,
  80. fontWeight: 'bold',
  81. lineHeight: 5,
  82. },
  83. b: {
  84. color: '#fff',
  85. fontSize: 12,
  86. height: 40,
  87. },
  88. },
  89. },
  90. },
  91. labelLine: {
  92. normal: {
  93. lineStyle: {
  94. color: 'rgb(98,137,169)',
  95. },
  96. smooth: 0.2,
  97. length: 2,
  98. length2: 2,
  99. },
  100. },
  101. itemStyle: {
  102. normal: {
  103. shadowColor: 'rgba(0, 0, 0, 0.8)',
  104. shadowBlur: 50,
  105. },
  106. },
  107. },
  108. ],
  109. };
  110. myChart.setOption(option);
  111. window.onresize = function () {
  112. myChart.resize();
  113. };
  114. });
  115. }
  116. </script>
  117. <style scoped lang="less">
  118. .echartBar {
  119. width: 100%;
  120. height: 100%;
  121. .bar {
  122. width: 100%;
  123. height: 100%;
  124. }
  125. }
  126. </style>