FaultAll.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <!--
  2. * @Author: your name
  3. * @Date: 2025-01-15 14:24:59
  4. * @LastEditTime: 2025-02-21 17:22:22
  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. hovertemplate:
  71. `故障类型:` + ` %{x} <br> ` + `故障时长:` + "%{y} 分钟<br>",
  72. };
  73. // 故障时长的折线图数据(右侧 Y 轴)
  74. const lineTrace = {
  75. x: this.faultTypes.slice(0, 10),
  76. y: this.faultDurations.slice(0, 10),
  77. type: "scatter",
  78. mode: "lines+markers", // 线性图 + 点标记
  79. line: { color: "#1A295D" }, // 红色折线
  80. name: "故障时长",
  81. yaxis: "y2", // 使用第二个 Y 轴(右侧)
  82. hovertemplate:
  83. `故障类型:` + ` %{x} <br> ` + `故障时长:` + "%{y} 分钟 <br>",
  84. };
  85. // 布局配置,设置双 Y 轴
  86. const layout = {
  87. title: "全场故障次数与时长分析Top10",
  88. xaxis: {
  89. title: "故障类型",
  90. tickangle: 30,
  91. tickmode: "array",
  92. tickvals: this.faultTypes.slice(0, 10),
  93. tickfont: { size: 12 },
  94. },
  95. yaxis: {
  96. title: "故障次数",
  97. titlefont: { color: "#64ADC2" },
  98. tickfont: { color: "#64ADC2" },
  99. side: "left", // 左侧的 Y 轴
  100. showline: true,
  101. linecolor: "#64ADC2",
  102. },
  103. yaxis2: {
  104. title: "故障时长 (分钟)",
  105. titlefont: { color: "#1A295D" },
  106. tickfont: { color: "#1A295D" },
  107. overlaying: "y", // 在第一个 Y 轴上方绘制
  108. side: "right", // 右侧的 Y 轴
  109. position: 1, // 调整右侧轴的位置
  110. showline: true,
  111. linecolor: "#1A295D", // 设置右侧轴线颜色
  112. },
  113. barmode: "group", // 柱状图分组
  114. showlegend: false,
  115. // legend: {
  116. // // x: 1, // 设置 legend 水平位置
  117. // // y: 1, // 设置 legend 垂直位置
  118. // xanchor: "left", // 使图例与 x 轴对齐
  119. // yanchor: "top", // 使图例与 y 轴对齐
  120. // pad: {
  121. // r: 100, // 设置图表与 legend 之间的上间距
  122. // },
  123. // },
  124. margin: {
  125. t: 80, // 上边距
  126. b: 150, // 下边距,给 X 轴标签更多空间
  127. // r: 200, // 增加右边距,避免图例遮挡右侧 Y 轴
  128. },
  129. };
  130. // 渲染图表
  131. Plotly.newPlot(this.$refs.chart, [barTrace, lineTrace], layout);
  132. },
  133. // renderChart() {
  134. // var trace1 = {
  135. // x: [
  136. // "Jan",
  137. // "Feb",
  138. // "Mar",
  139. // "Apr",
  140. // "May",
  141. // "Jun",
  142. // "Jul",
  143. // "Aug",
  144. // "Sep",
  145. // "Oct",
  146. // "Nov",
  147. // "Dec",
  148. // ],
  149. // y: [
  150. // 2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3,
  151. // ],
  152. // type: "bar",
  153. // name: "Evaporation",
  154. // marker: { color: "#5470C6" },
  155. // };
  156. // var trace2 = {
  157. // x: [
  158. // "Jan",
  159. // "Feb",
  160. // "Mar",
  161. // "Apr",
  162. // "May",
  163. // "Jun",
  164. // "Jul",
  165. // "Aug",
  166. // "Sep",
  167. // "Oct",
  168. // "Nov",
  169. // "Dec",
  170. // ],
  171. // y: [
  172. // 2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3,
  173. // ],
  174. // type: "bar",
  175. // name: "Precipitation",
  176. // yaxis: "y2", // 使用第二个 Y 轴
  177. // marker: { color: "#91CC75" },
  178. // };
  179. // var trace3 = {
  180. // x: [
  181. // "Jan",
  182. // "Feb",
  183. // "Mar",
  184. // "Apr",
  185. // "May",
  186. // "Jun",
  187. // "Jul",
  188. // "Aug",
  189. // "Sep",
  190. // "Oct",
  191. // "Nov",
  192. // "Dec",
  193. // ],
  194. // 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],
  195. // type: "scatter",
  196. // mode: "lines+markers",
  197. // name: "Temperature",
  198. // line: { color: "#EE6666" },
  199. // yaxis: "y3", // 使用第三个 Y 轴
  200. // };
  201. // var layout = {
  202. // title: "Evaporation, Precipitation, and Temperature",
  203. // xaxis: {
  204. // title: "Month",
  205. // },
  206. // yaxis: {
  207. // title: "Evaporation",
  208. // titlefont: { color: "#5470C6" },
  209. // tickfont: { color: "#5470C6" },
  210. // side: "left",
  211. // showline: true,
  212. // linecolor: "#5470C6",
  213. // },
  214. // yaxis2: {
  215. // title: "Precipitation",
  216. // titlefont: { color: "#91CC75" },
  217. // tickfont: { color: "#91CC75" },
  218. // overlaying: "y",
  219. // side: "right",
  220. // showline: true,
  221. // linecolor: "#91CC75",
  222. // },
  223. // yaxis3: {
  224. // title: "Temperature (°C)",
  225. // titlefont: { color: "#EE6666" },
  226. // tickfont: { color: "#EE6666" },
  227. // overlaying: "y",
  228. // side: "right",
  229. // position: 0.85, // 调整右侧 Y 轴的位置
  230. // showline: true,
  231. // linecolor: "#EE6666",
  232. // },
  233. // showlegend: true,
  234. // margin: {
  235. // t: 50,
  236. // b: 50,
  237. // r: 100,
  238. // },
  239. // };
  240. // Plotly.newPlot(this.$refs.chart, [trace1, trace2, trace3], layout);
  241. // },
  242. },
  243. };
  244. </script>
  245. <style scoped>
  246. /* 你可以根据需要添加样式 */
  247. </style>