bing.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <template>
  2. <div ref="chartDom" style="width: 100%; height: 100%"></div>
  3. </template>
  4. <script>
  5. import * as echarts from "echarts";
  6. export default {
  7. name: "EChartsComponent",
  8. props: {
  9. result: {
  10. type: Object, // 使用 Object 作为类型
  11. default: () => ({}),
  12. },
  13. },
  14. data() {
  15. return {
  16. myChart: null,
  17. };
  18. },
  19. mounted() {
  20. console.log(this.result, "this.result");
  21. this.initChart(); // 初始化图表
  22. },
  23. watch: {
  24. // 监听 result 的变化
  25. result(newValue) {
  26. console.log("result changed", newValue);
  27. this.updateChart(newValue); // 更新图表
  28. },
  29. },
  30. methods: {
  31. initChart() {
  32. const chartDom = this.$refs.chartDom;
  33. this.myChart = echarts.init(chartDom);
  34. const option = {
  35. title: {
  36. text: "状态统计图",
  37. left: "center",
  38. textStyle: {
  39. fontSize: 14, // 设置字体大小
  40. fontWeight: "600", // 可选,设置字体粗细
  41. color: "#333", // 可选,设置字体颜色
  42. },
  43. },
  44. tooltip: {
  45. trigger: "item",
  46. formatter: "{b}: {c} 次 ({d}%)", // 显示次数和百分比
  47. },
  48. series: [
  49. {
  50. name: "状态次数",
  51. type: "pie",
  52. radius: ["40%", "60%"], // 设置内外半径来形成圆环
  53. data: [
  54. { value: 0, name: "正常", itemStyle: { color: "#8AE359" } },
  55. { value: 0, name: "报警", itemStyle: { color: "#EECB5F" } },
  56. { value: 0, name: "危险", itemStyle: { color: "#F7715F" } },
  57. ],
  58. emphasis: {
  59. itemStyle: {
  60. shadowBlur: 10,
  61. shadowOffsetX: 0,
  62. shadowColor: "rgba(0, 0, 0, 0.5)",
  63. },
  64. },
  65. label: {
  66. // formatter: "{b}: {c} 次", // 在图表中显示次数
  67. },
  68. },
  69. ],
  70. };
  71. this.myChart.setOption(option);
  72. },
  73. // 更新图表方法
  74. updateChart(newResult) {
  75. console.log(newResult, "111111111111111111");
  76. const option = {
  77. title: {
  78. text: "状态统计图",
  79. left: "center",
  80. textStyle: {
  81. fontSize: 14, // 设置字体大小
  82. fontWeight: "600", // 可选,设置字体粗细
  83. color: "#333", // 可选,设置字体颜色
  84. },
  85. },
  86. tooltip: {
  87. trigger: "item",
  88. // formatter: "{b}: {c} 次 ({d}%)", // 显示次数和百分比
  89. },
  90. series: [
  91. {
  92. name: "状态次数",
  93. type: "pie",
  94. radius: ["40%", "60%"], // 设置内外半径来形成圆环
  95. data: [
  96. {
  97. value: newResult[1] || 0,
  98. name: "正常",
  99. itemStyle: { color: "#8AE359" },
  100. },
  101. {
  102. value: newResult[2] || 0,
  103. name: "报警",
  104. itemStyle: { color: "#EECB5F" },
  105. },
  106. {
  107. value: newResult[3] || 0,
  108. name: "危险",
  109. itemStyle: { color: "#F7715F" },
  110. },
  111. ],
  112. emphasis: {
  113. itemStyle: {
  114. shadowBlur: 10,
  115. shadowOffsetX: 0,
  116. shadowColor: "rgba(0, 0, 0, 0.5)",
  117. },
  118. },
  119. label: {
  120. // formatter: "{b}: {c} 次", // 在图表中显示次数
  121. },
  122. },
  123. ],
  124. };
  125. // 更新图表
  126. this.myChart.setOption(option);
  127. },
  128. },
  129. };
  130. </script>
  131. <style scoped>
  132. /* 可根据需求添加样式 */
  133. </style>