LineAndScatter.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <!--
  2. * @Author: your name
  3. * @Date: 2025-07-24 17:30:10
  4. * @LastEditTime: 2025-07-25 09:20:20
  5. * @LastEditors: bogon
  6. * @Description: In User Settings Edit
  7. * @FilePath: /performance-test/src/views/performance/components/chartsCom/LineAndScatter.vue
  8. -->
  9. <template>
  10. <div>
  11. <!-- 图表控制面板 总图-->
  12. <div style="display: flex; align-items: center">
  13. <el-select
  14. size="small"
  15. v-model="color1"
  16. @change="updateChartColor"
  17. placeholder="选择配色方案"
  18. style="width: 200px"
  19. >
  20. <el-option
  21. v-for="(scheme, index) in colorSchemes"
  22. :key="index"
  23. :label="scheme.label"
  24. :value="scheme.colors"
  25. >
  26. <span
  27. v-for="color in scheme.colors.slice(0, 8)"
  28. :style="{
  29. background: color,
  30. width: '20px',
  31. height: '20px',
  32. display: 'inline-block',
  33. }"
  34. ></span>
  35. </el-option>
  36. </el-select>
  37. <div>
  38. <el-button size="small" @click="toggleChartType">
  39. 切换为{{ chartType === "line" ? "面积图" : "折线图" }}
  40. </el-button>
  41. </div>
  42. </div>
  43. <!-- 图表容器 -->
  44. <div
  45. v-loading="loading"
  46. :id="`bar-chart${index}`"
  47. :ref="`bar-chart${index}`"
  48. style="width: 100%; height: 400px"
  49. >
  50. <el-empty v-if="isError" description="请求失败"></el-empty>
  51. </div>
  52. </div>
  53. </template>
  54. <script>
  55. import { nextTick } from "vue"; // 导入 nextTick
  56. import Plotly from "plotly.js-dist";
  57. import axios from "axios";
  58. import { colorSchemes } from "@/views/overview/js/colors";
  59. import { myMixin } from "@/mixins/chartRequestMixin"; // 假设你需要的 mixin
  60. import { mapState } from "vuex";
  61. export default {
  62. props: {
  63. fileAddr: {
  64. type: String,
  65. default: "",
  66. },
  67. index: {
  68. type: String,
  69. default() {
  70. return "0";
  71. },
  72. },
  73. setUpImgData: {
  74. default: () => [],
  75. type: Array,
  76. },
  77. },
  78. mixins: [myMixin],
  79. data() {
  80. return {
  81. chartData: {},
  82. chartType: "line", // 默认图表类型是折线图
  83. color1: [], // 默认颜色
  84. // 配色方案列表(每个方案是一个颜色数组)
  85. colorSchemes: colorSchemes,
  86. loading: false,
  87. isError: false,
  88. colors: [...colorSchemes[0].colors],
  89. };
  90. },
  91. computed: {
  92. ...mapState("themes", {
  93. themeColor: "themeColor",
  94. }),
  95. },
  96. watch: {
  97. themeColor: {
  98. handler(newVal, oldVal) {
  99. if (JSON.stringify(newVal) !== JSON.stringify(oldVal)) {
  100. this.color1 = newVal;
  101. this.updateChartColor();
  102. }
  103. },
  104. deep: true,
  105. },
  106. setUpImgData: {
  107. handler(newVal, oldVal) {
  108. if (JSON.stringify(newVal) !== JSON.stringify(oldVal)) {
  109. this.drawChart();
  110. }
  111. },
  112. deep: true,
  113. },
  114. },
  115. mounted() {
  116. if (this.fileAddr) {
  117. this.$nextTick(() => {
  118. this.color1 = this.colorSchemes[0].colors;
  119. this.getData();
  120. });
  121. }
  122. },
  123. methods: {
  124. // 获取数据
  125. async getData() {
  126. if (this.fileAddr !== "") {
  127. try {
  128. this.loading = true;
  129. this.cancelToken = axios.CancelToken.source();
  130. const resultChartsData = await axios.get(this.fileAddr, {
  131. cancelToken: this.cancelToken.token,
  132. });
  133. this.chartData = resultChartsData.data;
  134. // 使用 nextTick 来确保 DOM 渲染完成后绘制图表
  135. nextTick(() => {
  136. this.drawChart();
  137. });
  138. this.isError = false;
  139. this.loading = false;
  140. } catch (error) {
  141. console.error("Error loading data:", error);
  142. this.isError = true;
  143. this.loading = false;
  144. }
  145. }
  146. },
  147. // 绘制图表
  148. drawChart() {
  149. if (!this.$refs[`bar-chart${this.index}`]) {
  150. return false;
  151. }
  152. const data = [];
  153. const newData =
  154. this.chartData.analysisTypeCode === "风电机组叶尖速比和风速分析"
  155. ? this.chartData &&
  156. this.chartData.data &&
  157. JSON.parse(JSON.stringify(this.chartData.data)).sort((a, b) => {
  158. return a.engineName.localeCompare(b.engineName);
  159. })
  160. : JSON.parse(JSON.stringify(this.chartData.data));
  161. newData.forEach((turbine, index) => {
  162. // 判断图表类型,根据类型调整绘制方式
  163. const chartConfig = {
  164. x: turbine.xData, // X 数据
  165. y: turbine.yData, // Y 数据
  166. name: turbine.engineName, // 使用机组名称
  167. type: "scattergl", // 使用散点图
  168. // line: {
  169. // color:
  170. // this.color1.length > 0
  171. // ? this.color1[index % this.color1.length]
  172. // : this.colors[index % this.colors.length], // 为每个机组分配不同的颜色
  173. // },
  174. marker: {
  175. color:
  176. this.color1.length > 0
  177. ? this.color1[index % this.color1.length]
  178. : this.colors[index % this.colors.length], // 为每个机组分配不同的颜色
  179. size: 12,
  180. symbol: "x", // 也可以是 "circle", "diamond", "x", etc.
  181. },
  182. hovertemplate:
  183. `${this.chartData.xaixs}:` +
  184. ` %{x} <br> ` +
  185. `${this.chartData.yaixs}:` +
  186. "%{y} <br>",
  187. };
  188. if (this.chartType === "line") {
  189. // chartConfig.mode = "lines"; // 如果是折线图
  190. chartConfig.fill = "none";
  191. } else if (this.chartType === "bar") {
  192. // chartConfig.type = "bar"; // 如果是柱状图
  193. chartConfig.fill = "tonexty";
  194. }
  195. data.push(chartConfig);
  196. });
  197. const layout = {
  198. title: {
  199. text: this.chartData.title || this.chartData.data[0].title,
  200. font: {
  201. size: 16, // 设置标题字体大小(默认 16)
  202. weight: "bold",
  203. },
  204. },
  205. xaxis: {
  206. title: this.chartData.xaixs || "X轴", // 横坐标标题
  207. gridcolor: "rgb(255,255,255)",
  208. tickcolor: "rgb(255,255,255)",
  209. backgroundcolor: "#e5ecf6",
  210. dtick: this.chartData.xaixs === "风速" ? 1 : undefined,
  211. range:
  212. this.chartData.analysisTypeCode === "风电机组风能利用系数分析" &&
  213. this.chartData.contract_Cp_curve_xData
  214. ? [
  215. 0,
  216. Math.max(
  217. ...this.chartData.contract_Cp_curve_xData
  218. .map(Number)
  219. .filter((val) => !isNaN(val))
  220. ) * 0.9,
  221. ]
  222. : undefined,
  223. },
  224. yaxis: {
  225. title: this.chartData.yaixs || "Y轴", // 纵坐标标题
  226. gridcolor: "rgb(255,255,255)",
  227. tickcolor: "rgb(255,255,255)",
  228. backgroundcolor: "#e5ecf6",
  229. range:
  230. this.chartData.analysisTypeCode === "风电机组风能利用系数分析"
  231. ? [0, 1.5]
  232. : undefined,
  233. },
  234. margin: {
  235. l: 50,
  236. r: 50,
  237. t: 50,
  238. b: 50,
  239. },
  240. plot_bgcolor: "#e5ecf6",
  241. gridcolor: "#fff",
  242. bgcolor: "#e5ecf6", // 设置背景颜色
  243. autosize: true, // 开启自适应
  244. // barmode: this.chartType === "bar" ? "stack" : "group", // 如果是柱状图则启用堆叠
  245. };
  246. const getChartSetUp = (axisTitle) => {
  247. return this.setUpImgData.find((item) => item.text.includes(axisTitle));
  248. };
  249. const xChartSetUp = getChartSetUp(layout.xaxis.title);
  250. if (xChartSetUp) {
  251. layout.xaxis.dtick = xChartSetUp.dtick;
  252. layout.xaxis.range = [xChartSetUp.min, xChartSetUp.max];
  253. }
  254. const yChartSetUp = getChartSetUp(layout.yaxis.title);
  255. if (yChartSetUp) {
  256. layout.yaxis.dtick = yChartSetUp.dtick;
  257. layout.yaxis.range = [yChartSetUp.min, yChartSetUp.max];
  258. }
  259. // 使用 Plotly.react 来更新图表
  260. Plotly.react(`bar-chart${this.index}`, data, layout, {
  261. responsive: true,
  262. modeBarButtonsToRemove: [
  263. // 移除不需要的工具按钮
  264. "lasso2d",
  265. "sendDataToCloud",
  266. "resetCameraLastSave3d",
  267. "resetCameraDefault3d",
  268. "resetCameraLastSave",
  269. "sendDataToCloud",
  270. "zoom2d", // 缩放按钮
  271. "zoom3d",
  272. "plotlylogo2D",
  273. "plotlylogo3D",
  274. ],
  275. displaylogo: false,
  276. }).then(function (gd) {
  277. // 获取工具栏按钮
  278. const toolbar = gd.querySelector(".modebar");
  279. const buttons = toolbar.querySelectorAll(".modebar-btn");
  280. // 定义一个映射对象,方便修改按钮提示
  281. const titleMap = {
  282. "Download plot as a png": "保存图片",
  283. Autoscale: "缩放",
  284. Pan: "平移",
  285. "Zoom out": "缩小",
  286. "Zoom in": "放大",
  287. "Box Select": "选择框操作",
  288. "Lasso Select": "套索选择操作",
  289. "Reset axes": "重置操作",
  290. "Reset camera to default": "重置相机视角",
  291. "Turntable rotation": "转台式旋转",
  292. "Orbital rotation": "轨道式旋转",
  293. };
  294. // 遍历所有按钮,修改它们的 title
  295. buttons.forEach(function (button) {
  296. const dataTitle = button.getAttribute("data-title");
  297. // 如果标题匹配,修改属性值
  298. if (titleMap[dataTitle]) {
  299. button.setAttribute("data-title", titleMap[dataTitle]);
  300. }
  301. });
  302. });
  303. },
  304. // 切换图表类型
  305. toggleChartType() {
  306. this.chartType = this.chartType === "line" ? "bar" : "line"; // 切换图表类型
  307. this.drawChart(); // 重新绘制图表
  308. },
  309. // 更新图表颜色
  310. updateChartColor() {
  311. this.drawChart(); // 更新颜色后重新绘制图表
  312. },
  313. // 根据配色方案设置每个选项的样式
  314. getOptionStyle(scheme) {
  315. return {
  316. background: `linear-gradient(to right, ${scheme
  317. .slice(0, 8)
  318. .join(", ")})`,
  319. color: "#fff",
  320. height: "30px",
  321. lineHeight: "30px",
  322. borderRadius: "0px",
  323. };
  324. },
  325. },
  326. beforeUnmount() {
  327. if (this.cancelToken) {
  328. this.cancelToken.cancel("组件卸载,取消请求");
  329. }
  330. },
  331. };
  332. </script>
  333. <style scoped>
  334. /* 样式可以根据需求自定义 */
  335. </style>