yawErrorBarSum.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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-sums` + 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. // 为每个数据点返回对应颜色
  71. getColor(value) {
  72. if (value <= 3) {
  73. return "#8AC8BE"; // (0, 3] 蓝色
  74. } else if (value <= 5) {
  75. return "#407DB3"; // (3, 5] 绿色
  76. } else {
  77. return "#1B2973"; // (5, ∞] 红色
  78. }
  79. },
  80. drawChart() {
  81. let a = [];
  82. let b = [];
  83. let c = [];
  84. this.zongFaultCsvData[0].data.map((item) => {
  85. if (item["[0,3]"] != "0.0") {
  86. a.push(item["[0,3]"]);
  87. }
  88. if (item["(3,5]"] != "0.0") {
  89. b.push(item["(3,5]"]);
  90. }
  91. if (item["(5, )"] != "0.0") {
  92. c.push(item["(5, )"]);
  93. }
  94. });
  95. const chartDataset = this.zongFaultCsvData[0].data;
  96. const xData = ["(0,3]", "(3,5]", "(>5]"]; // 机组编号
  97. const yData = [a.length, b.length, c.length]; // 分别为"(0,3]", "(3,5]", "(>5]的机组有几个
  98. // 每个柱子的颜色
  99. const colors = [
  100. "#8AC8BE", // (0, 3] 蓝色
  101. "#407DB3", // (3, 5] 绿色
  102. "#1B2973", // (5, ∞] 红色
  103. ];
  104. const trace = {
  105. x: xData, // 横坐标数据
  106. y: yData, // 纵坐标数据
  107. type: this.chartType, // 当前图表类型 ('bar' 或 'scatter')
  108. marker: {
  109. color: colors, // 为每个柱子分配不同的颜色
  110. },
  111. line: {
  112. color: this.color1, // 折线图颜色(如果切换到折线图)
  113. },
  114. hovertemplate:
  115. `偏航误差值:` + ` %{x} <br> ` + `台数:` + "%{y} <br> <extra></extra>",
  116. };
  117. const layout = {
  118. title: {
  119. text: "静态偏航误差的绝对值机组台数分布情况",
  120. font: {
  121. size: 16,
  122. weight: "bold",
  123. },
  124. }, // 图表标题
  125. xaxis: {
  126. title: "静态偏航误差值(度)", // 横坐标标题
  127. gridcolor: "rgb(255,255,255)",
  128. tickcolor: "rgb(255,255,255)",
  129. backgroundcolor: "#e5ecf6",
  130. },
  131. yaxis: {
  132. title: "台数", // 纵坐标标题
  133. gridcolor: "rgb(255,255,255)",
  134. tickcolor: "rgb(255,255,255)",
  135. backgroundcolor: "#e5ecf6",
  136. },
  137. margin: {
  138. l: 50,
  139. r: 50,
  140. t: 50,
  141. b: 50,
  142. },
  143. autosize: true, // 开启自适应
  144. // showlegend: true, // 显示图例
  145. plot_bgcolor: "#e5ecf6",
  146. gridcolor: "#fff",
  147. bgcolor: "#e5ecf6", // 设置背景颜色
  148. };
  149. // 渲染图表
  150. Plotly.newPlot(`bar-chart-sums` + this.index, [trace], layout, {
  151. responsive: true,
  152. displaylogo: false,
  153. modeBarButtonsToRemove: [
  154. "lasso2d", // 移除不需要的工具按钮
  155. // 移除不需要的工具按钮
  156. "lasso2d",
  157. "sendDataToCloud",
  158. "resetCameraLastSave3d",
  159. "resetCameraDefault3d",
  160. "resetCameraLastSave",
  161. "sendDataToCloud",
  162. "zoom2d", // 缩放按钮
  163. "zoom3d",
  164. "plotlylogo2D",
  165. "plotlylogo3D",
  166. ],
  167. }).then(function (gd) {
  168. // 获取工具栏按钮
  169. const toolbar = gd.querySelector(".modebar");
  170. const buttons = toolbar.querySelectorAll(".modebar-btn");
  171. // 定义一个映射对象,方便修改按钮提示
  172. const titleMap = {
  173. "Download plot as a png": "保存图片",
  174. Autoscale: "缩放",
  175. Pan: "平移",
  176. "Zoom out": "放大",
  177. "Zoom in": "缩小",
  178. "Box Select": "选择框操作",
  179. "Lasso Select": "套索选择操作",
  180. "Reset axes": "重置操作",
  181. "Reset camera to default": "重置相机视角",
  182. "Turntable rotation": "转台式旋转",
  183. "Orbital rotation": "轨道式旋转",
  184. };
  185. // 遍历所有按钮,修改它们的 title
  186. buttons.forEach(function (button) {
  187. const dataTitle = button.getAttribute("data-title");
  188. // 如果标题匹配,修改属性值
  189. if (titleMap[dataTitle]) {
  190. button.setAttribute("data-title", titleMap[dataTitle]);
  191. }
  192. });
  193. });
  194. },
  195. // 切换图表类型
  196. toggleChartType() {
  197. this.chartType = this.chartType === "bar" ? "scatter" : "bar";
  198. this.drawChart();
  199. },
  200. // 更新图表颜色
  201. updateChartColor() {
  202. this.drawChart();
  203. },
  204. },
  205. };
  206. </script>
  207. <style scoped>
  208. /* 样式可以根据需求自定义 */
  209. </style>