YewErrorBarChart.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. },
  45. mixins: [myMixin],
  46. data() {
  47. return {
  48. chartData: {
  49. analysisTypeCode: "",
  50. engineCode: "",
  51. engineTypeName: "",
  52. xaixs: "",
  53. yaixs: "",
  54. data: [],
  55. },
  56. chartType: "bar", // 当前图表类型 ('bar' 或 'scatter')
  57. color1: "#406DAB", // 默认颜色
  58. isError: false, // 错误标志
  59. loading: true, // 加载标志
  60. };
  61. },
  62. mounted() {
  63. nextTick(() => {
  64. this.drawChart();
  65. this.isError = false;
  66. this.loading = false;
  67. });
  68. },
  69. methods: {
  70. getData() {
  71. console.log(this.zongFaultCsvData[0].data);
  72. },
  73. // 为每个数据点返回对应颜色
  74. getColor(value) {
  75. if (value <= 3) {
  76. return "#8AC8BE"; // (0, 3]
  77. } else if (value <= 5) {
  78. return "#407DB3"; // (3, 5]
  79. } else {
  80. return "#1B2973"; // (5, ∞]
  81. }
  82. },
  83. drawChart() {
  84. const chartDataset = this.zongFaultCsvData[0].data;
  85. const xData = chartDataset.map((item) => item.engine_name); // 机组编号
  86. const yData = chartDataset.map((item) => item.yaw_error1); // 偏航误差值
  87. // 为每个数据点分配颜色
  88. const colors = yData.map(this.getColor);
  89. const trace = {
  90. x: xData, // 横坐标数据
  91. y: yData, // 纵坐标数据
  92. type: this.chartType, // 当前图表类型 ('bar' 或 'scatter')
  93. marker: {
  94. color: colors, // 每个点的颜色
  95. },
  96. line: {
  97. color: this.color1, // 折线图颜色
  98. },
  99. name: "偏航误差值", // 图例名称
  100. };
  101. // 创建虚拟的 trace 以便显示图例
  102. const legendTrace1 = {
  103. x: [null],
  104. y: [null],
  105. name: "(0, 3]",
  106. mode: "markers",
  107. marker: { color: "#8AC8BE", size: 10 },
  108. };
  109. const legendTrace2 = {
  110. x: [null],
  111. y: [null],
  112. name: "(3, 5]",
  113. mode: "markers",
  114. marker: { color: "#407DB3", size: 10 },
  115. };
  116. const legendTrace3 = {
  117. x: [null],
  118. y: [null],
  119. name: "(5, ∞]",
  120. mode: "markers",
  121. marker: { color: "#1B2973", size: 10 },
  122. };
  123. const layout = {
  124. title: "机组静态偏航误差值", // 图表标题
  125. xaxis: {
  126. title: "机组编号", // 横坐标标题
  127. },
  128. yaxis: {
  129. title: "静态偏航误差值(度)", // 纵坐标标题
  130. },
  131. margin: {
  132. l: 50,
  133. r: 50,
  134. t: 50,
  135. b: 50,
  136. },
  137. autosize: true, // 开启自适应
  138. showlegend: true, // 显示图例
  139. legend: {
  140. x: 1.05,
  141. y: 1,
  142. },
  143. annotations: [
  144. {
  145. x: 1.05,
  146. y: 4,
  147. xref: "paper",
  148. yref: "paper",
  149. text: "(0, 3] ",
  150. showarrow: false,
  151. font: { size: 12, color: "#8AC8BE" },
  152. },
  153. {
  154. x: 1.05,
  155. y: 3,
  156. xref: "paper",
  157. yref: "paper",
  158. text: "(3, 5] ",
  159. showarrow: false,
  160. font: { size: 12, color: "#407DB3" },
  161. },
  162. {
  163. x: 1.05,
  164. y: 2,
  165. xref: "paper",
  166. yref: "paper",
  167. text: "(5, ∞] ",
  168. showarrow: false,
  169. font: { size: 12, color: "#1B2973" },
  170. },
  171. ],
  172. };
  173. // 渲染图表
  174. Plotly.newPlot(
  175. `bar-chart-sum` + this.index,
  176. [trace, legendTrace1, legendTrace2, legendTrace3],
  177. layout,
  178. {
  179. responsive: true,
  180. }
  181. );
  182. },
  183. // 切换图表类型
  184. toggleChartType() {
  185. this.chartType = this.chartType === "bar" ? "scatter" : "bar";
  186. this.drawChart();
  187. },
  188. // 更新图表颜色
  189. updateChartColor() {
  190. this.drawChart();
  191. },
  192. },
  193. };
  194. </script>
  195. <style scoped>
  196. /* 样式可以根据需求自定义 */
  197. </style>