lineAndChildLine.vue 11 KB

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