YewErrorBarChart.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <template>
  2. <div>
  3. <!-- 图表控制面板 -->
  4. <div style="display: flex; align-items: center">
  5. <!-- <div style="margin-right: 20px; display: flex; align-items: center">
  6. <el-color-picker
  7. size="small"
  8. v-model="color1"
  9. show-alpha
  10. @change="updateChartColor"
  11. ></el-color-picker>
  12. <span style="margin-left: 10px">自定义颜色</span>
  13. </div> -->
  14. <div>
  15. <el-button size="small" @click="toggleChartType">
  16. 切换为{{ chartType === "bar" ? "折线图" : "柱状图" }}
  17. </el-button>
  18. </div>
  19. </div>
  20. <!-- 图表容器 -->
  21. <div
  22. v-loading="loading"
  23. :id="`bar-chart-sum` + index"
  24. style="width: 100%; height: 400px"
  25. >
  26. <el-empty v-if="isError" description="请求失败"></el-empty>
  27. </div>
  28. </div>
  29. </template>
  30. <script>
  31. import { nextTick } from "vue"; // 导入 nextTick
  32. import Plotly from "plotly.js-dist";
  33. import axios from "axios";
  34. import { myMixin } from "@/mixins/chartRequestMixin";
  35. export default {
  36. props: {
  37. zongFaultCsvData: {
  38. default: [],
  39. type: Array,
  40. },
  41. index: {
  42. type: String,
  43. },
  44. setUpImgData: {
  45. default: () => [],
  46. type: Array,
  47. },
  48. },
  49. mixins: [myMixin],
  50. data() {
  51. return {
  52. chartData: {
  53. analysisTypeCode: "",
  54. engineCode: "",
  55. engineTypeName: "",
  56. xaixs: "",
  57. yaixs: "",
  58. data: [],
  59. },
  60. chartType: "bar", // 当前图表类型 ('bar' 或 'scatter')
  61. color1: "#406DAB", // 默认颜色
  62. isError: false, // 错误标志
  63. loading: true, // 加载标志
  64. };
  65. },
  66. mounted() {
  67. nextTick(() => {
  68. this.drawChart();
  69. this.isError = false;
  70. this.loading = false;
  71. });
  72. },
  73. methods: {
  74. getData() {
  75. console.log(this.zongFaultCsvData[0].data);
  76. },
  77. // 为每个数据点返回对应颜色
  78. getColor(value) {
  79. if (value <= 3) {
  80. return "#8AC8BE"; // (0, 3]
  81. } else if (value <= 5) {
  82. return "#407DB3"; // (3, 5]
  83. } else {
  84. return "#1B2973"; // (5, ∞]
  85. }
  86. },
  87. drawChart() {
  88. const chartDataset = this.zongFaultCsvData[0].data;
  89. const xData = chartDataset.map((item) => item.engine_name); // 机组编号
  90. const yData = chartDataset.map((item) => item.yaw_error1); // 偏航误差值
  91. // 为每个数据点分配颜色
  92. const colors = yData.map(this.getColor);
  93. const trace = {
  94. x: xData, // 横坐标数据
  95. y: yData, // 纵坐标数据
  96. type: this.chartType, // 当前图表类型 ('bar' 或 'scatter')
  97. marker: {
  98. color: colors, // 每个点的颜色
  99. },
  100. line: {
  101. color: this.color1, // 折线图颜色
  102. },
  103. name: "偏航误差值", // 图例名称
  104. hovertemplate:
  105. `机组名称:` +
  106. ` %{x} <br> ` +
  107. `偏航误差值:` +
  108. "%{y} <br> <extra></extra>",
  109. };
  110. // 创建虚拟的 trace 以便显示图例
  111. const legendTrace1 = {
  112. x: [null],
  113. y: [null],
  114. name: "(0, 3]",
  115. mode: "markers",
  116. marker: { color: "#8AC8BE", size: 10 },
  117. };
  118. const legendTrace2 = {
  119. x: [null],
  120. y: [null],
  121. name: "(3, 5]",
  122. mode: "markers",
  123. marker: { color: "#407DB3", size: 10 },
  124. };
  125. const legendTrace3 = {
  126. x: [null],
  127. y: [null],
  128. name: "(5, ∞]",
  129. mode: "markers",
  130. marker: { color: "#1B2973", size: 10 },
  131. };
  132. const layout = {
  133. title: "机组静态偏航误差值", // 图表标题
  134. xaxis: {
  135. title: "机组编号", // 横坐标标题
  136. tickmode: "array",
  137. tickvals: xData, // 设置刻度值(机组编号)
  138. ticktext: xData, // 设置刻度文本(机组编号)
  139. gridcolor: "rgb(255,255,255)",
  140. tickcolor: "rgb(255,255,255)",
  141. backgroundcolor: "#e5ecf6",
  142. },
  143. yaxis: {
  144. title: "静态偏航误差值(度)", // 纵坐标标题
  145. gridcolor: "rgb(255,255,255)",
  146. tickcolor: "rgb(255,255,255)",
  147. backgroundcolor: "#e5ecf6",
  148. },
  149. margin: {
  150. l: 50,
  151. r: 50,
  152. t: 50,
  153. b: 50,
  154. },
  155. plot_bgcolor: "#e5ecf6",
  156. gridcolor: "#fff",
  157. bgcolor: "#e5ecf6", // 设置背景颜色
  158. autosize: true, // 开启自适应
  159. showlegend: true, // 显示图例
  160. legend: {
  161. x: 1.05,
  162. y: 1,
  163. },
  164. annotations: [
  165. {
  166. x: 1.05,
  167. y: 4,
  168. xref: "paper",
  169. yref: "paper",
  170. text: "(0, 3] ",
  171. showarrow: false,
  172. font: { size: 12, color: "#8AC8BE" },
  173. },
  174. {
  175. x: 1.05,
  176. y: 3,
  177. xref: "paper",
  178. yref: "paper",
  179. text: "(3, 5] ",
  180. showarrow: false,
  181. font: { size: 12, color: "#407DB3" },
  182. },
  183. {
  184. x: 1.05,
  185. y: 2,
  186. xref: "paper",
  187. yref: "paper",
  188. text: "(5, ∞] ",
  189. showarrow: false,
  190. font: { size: 12, color: "#1B2973" },
  191. },
  192. ],
  193. };
  194. // 渲染图表
  195. Plotly.newPlot(
  196. `bar-chart-sum` + this.index,
  197. [trace, legendTrace1, legendTrace2, legendTrace3],
  198. layout,
  199. {
  200. responsive: true,
  201. }
  202. );
  203. },
  204. // 切换图表类型
  205. toggleChartType() {
  206. this.chartType = this.chartType === "bar" ? "scatter" : "bar";
  207. this.drawChart();
  208. },
  209. // 更新图表颜色
  210. updateChartColor() {
  211. this.drawChart();
  212. },
  213. },
  214. };
  215. </script>
  216. <style scoped>
  217. /* 样式可以根据需求自定义 */
  218. </style>