FaultAll.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <!--
  2. * @Author: your name
  3. * @Date: 2025-01-15 14:24:59
  4. * @LastEditTime: 2025-01-15 15:55:10
  5. * @LastEditors: bogon
  6. * @Description: In User Settings Edit
  7. * @FilePath: /performance-test/src/views/performance/components/chartsCom/FaultAll.vue
  8. -->
  9. <template>
  10. <div>
  11. <div ref="chart" style="width: 100%; height: 400px"></div>
  12. </div>
  13. </template>
  14. <script>
  15. import Plotly from "plotly.js-dist";
  16. export default {
  17. name: "faultAll",
  18. props: {
  19. faultTypes: {
  20. type: Array,
  21. default: [],
  22. }, // 故障类型
  23. faultCounts: {
  24. type: Array,
  25. default: [],
  26. }, // 故障次数
  27. faultDurations: {
  28. type: Array,
  29. default: [],
  30. }, // 故障时长
  31. zongFaultCsvData: {
  32. type: Array,
  33. default: [],
  34. },
  35. },
  36. watch: {
  37. zongFaultCsvData: {
  38. deep: true,
  39. handler(v) {
  40. console.log("zongFaultCsvData 更新:", v);
  41. if (v && v[0] && v[0].data) {
  42. this.faultTypes = v[0].data.map((item) => item.fault_detail);
  43. this.faultCounts = v[0].data.map((item) => item.count);
  44. this.faultDurations = v[0].data.map((item) => item.fault_time_sum);
  45. this.renderChart();
  46. }
  47. },
  48. },
  49. faultCounts: {
  50. deep: true,
  51. handler(v) {
  52. console.log("V 更新:", v);
  53. console.log(this.faultDurations, this.faultTypes);
  54. this.renderChart();
  55. },
  56. },
  57. },
  58. mounted() {
  59. this.renderChart();
  60. },
  61. methods: {
  62. renderChart() {
  63. // 故障次数的柱状图数据(左侧 Y 轴)
  64. const barTrace = {
  65. x: this.faultTypes.slice(0, 10),
  66. y: this.faultCounts.slice(0, 10),
  67. type: "bar",
  68. marker: { color: "#64ADC2" }, // 蓝色柱状图
  69. name: "故障次数",
  70. };
  71. // 故障时长的折线图数据(右侧 Y 轴)
  72. const lineTrace = {
  73. x: this.faultTypes.slice(0, 10),
  74. y: this.faultDurations.slice(0, 10),
  75. type: "scatter",
  76. mode: "lines+markers", // 线性图 + 点标记
  77. line: { color: "#1A295D" }, // 红色折线
  78. name: "故障时长",
  79. yaxis: "y2", // 使用第二个 Y 轴(右侧)
  80. };
  81. // 布局配置,设置双 Y 轴
  82. const layout = {
  83. title: "全场故障次数与时长分析Top10",
  84. xaxis: {
  85. title: "故障类型",
  86. tickangle: 30,
  87. tickmode: "array",
  88. tickvals: this.faultTypes.slice(0, 10),
  89. tickfont: { size: 12 },
  90. },
  91. yaxis: {
  92. title: "故障次数",
  93. titlefont: { color: "#64ADC2" },
  94. tickfont: { color: "#64ADC2" },
  95. side: "left", // 左侧的 Y 轴
  96. showline: true,
  97. linecolor: "#64ADC2",
  98. },
  99. yaxis2: {
  100. title: "故障时长 (分钟)",
  101. titlefont: { color: "#1A295D" },
  102. tickfont: { color: "#1A295D" },
  103. overlaying: "y", // 在第一个 Y 轴上方绘制
  104. side: "right", // 右侧的 Y 轴
  105. position: 1, // 调整右侧轴的位置
  106. showline: true,
  107. linecolor: "#1A295D", // 设置右侧轴线颜色
  108. },
  109. barmode: "group", // 柱状图分组
  110. showlegend: false,
  111. // legend: {
  112. // // x: 1, // 设置 legend 水平位置
  113. // // y: 1, // 设置 legend 垂直位置
  114. // xanchor: "left", // 使图例与 x 轴对齐
  115. // yanchor: "top", // 使图例与 y 轴对齐
  116. // pad: {
  117. // r: 100, // 设置图表与 legend 之间的上间距
  118. // },
  119. // },
  120. margin: {
  121. t: 80, // 上边距
  122. b: 150, // 下边距,给 X 轴标签更多空间
  123. // r: 200, // 增加右边距,避免图例遮挡右侧 Y 轴
  124. },
  125. };
  126. // 渲染图表
  127. Plotly.newPlot(this.$refs.chart, [barTrace, lineTrace], layout);
  128. },
  129. // renderChart() {
  130. // var trace1 = {
  131. // x: [
  132. // "Jan",
  133. // "Feb",
  134. // "Mar",
  135. // "Apr",
  136. // "May",
  137. // "Jun",
  138. // "Jul",
  139. // "Aug",
  140. // "Sep",
  141. // "Oct",
  142. // "Nov",
  143. // "Dec",
  144. // ],
  145. // y: [
  146. // 2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3,
  147. // ],
  148. // type: "bar",
  149. // name: "Evaporation",
  150. // marker: { color: "#5470C6" },
  151. // };
  152. // var trace2 = {
  153. // x: [
  154. // "Jan",
  155. // "Feb",
  156. // "Mar",
  157. // "Apr",
  158. // "May",
  159. // "Jun",
  160. // "Jul",
  161. // "Aug",
  162. // "Sep",
  163. // "Oct",
  164. // "Nov",
  165. // "Dec",
  166. // ],
  167. // y: [
  168. // 2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3,
  169. // ],
  170. // type: "bar",
  171. // name: "Precipitation",
  172. // yaxis: "y2", // 使用第二个 Y 轴
  173. // marker: { color: "#91CC75" },
  174. // };
  175. // var trace3 = {
  176. // x: [
  177. // "Jan",
  178. // "Feb",
  179. // "Mar",
  180. // "Apr",
  181. // "May",
  182. // "Jun",
  183. // "Jul",
  184. // "Aug",
  185. // "Sep",
  186. // "Oct",
  187. // "Nov",
  188. // "Dec",
  189. // ],
  190. // y: [2.0, 2.2, 3.3, 4.5, 6.3, 10.2, 20.3, 23.4, 23.0, 16.5, 12.0, 6.2],
  191. // type: "scatter",
  192. // mode: "lines+markers",
  193. // name: "Temperature",
  194. // line: { color: "#EE6666" },
  195. // yaxis: "y3", // 使用第三个 Y 轴
  196. // };
  197. // var layout = {
  198. // title: "Evaporation, Precipitation, and Temperature",
  199. // xaxis: {
  200. // title: "Month",
  201. // },
  202. // yaxis: {
  203. // title: "Evaporation",
  204. // titlefont: { color: "#5470C6" },
  205. // tickfont: { color: "#5470C6" },
  206. // side: "left",
  207. // showline: true,
  208. // linecolor: "#5470C6",
  209. // },
  210. // yaxis2: {
  211. // title: "Precipitation",
  212. // titlefont: { color: "#91CC75" },
  213. // tickfont: { color: "#91CC75" },
  214. // overlaying: "y",
  215. // side: "right",
  216. // showline: true,
  217. // linecolor: "#91CC75",
  218. // },
  219. // yaxis3: {
  220. // title: "Temperature (°C)",
  221. // titlefont: { color: "#EE6666" },
  222. // tickfont: { color: "#EE6666" },
  223. // overlaying: "y",
  224. // side: "right",
  225. // position: 0.85, // 调整右侧 Y 轴的位置
  226. // showline: true,
  227. // linecolor: "#EE6666",
  228. // },
  229. // showlegend: true,
  230. // margin: {
  231. // t: 50,
  232. // b: 50,
  233. // r: 100,
  234. // },
  235. // };
  236. // Plotly.newPlot(this.$refs.chart, [trace1, trace2, trace3], layout);
  237. // },
  238. },
  239. };
  240. </script>
  241. <style scoped>
  242. /* 你可以根据需要添加样式 */
  243. </style>