lineAndChildLine.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. console.log(
  173. index,
  174. this.color1.length > 0,
  175. index % this.colors.length,
  176. "this.color1.length > 0",
  177. );
  178. // 判断图表类型,根据类型调整绘制方式
  179. const chartConfig = {
  180. x: turbine.xData, // X 数据
  181. y: turbine.yData, // Y 数据
  182. name: turbine.engineName, // 使用机组名称
  183. line: {
  184. color:
  185. this.color1.length > 0
  186. ? this.color1[(index % this.color1.length) + 3]
  187. : this.colors[(index % this.colors.length) + 3], // 为每个机组分配不同的颜色
  188. },
  189. marker: {
  190. color:
  191. this.color1.length > 0
  192. ? this.color1[index % this.color1.length]
  193. : this.colors[index % this.colors.length], // 为每个机组分配不同的颜色
  194. },
  195. hovertemplate:
  196. `${this.chartData.xaixs}:` +
  197. ` %{x} <br> ` +
  198. `${this.chartData.yaixs}:` +
  199. "%{y} <br>",
  200. };
  201. if (this.chartData.yaixs === "概率密度函数") {
  202. chartConfig.line.color =
  203. this.color1.length > 0 ? this.color1[7] : this.colors[7]; // 为每个机组分配不同的颜色
  204. }
  205. if (this.chartType === "line") {
  206. chartConfig.mode = "lines"; // 如果是折线图
  207. chartConfig.fill = "none";
  208. } else if (this.chartType === "bar") {
  209. // chartConfig.type = "bar"; // 如果是柱状图
  210. chartConfig.fill = "tonexty";
  211. }
  212. data.push(chartConfig);
  213. });
  214. const layout = {
  215. title: {
  216. text: this.chartData.title || this.chartData.data[0].title,
  217. font: {
  218. size: 16, // 设置标题字体大小(默认 16)
  219. weight: "bold",
  220. },
  221. },
  222. xaxis: {
  223. title: this.chartData.xaixs || "X轴", // 横坐标标题
  224. gridcolor: "rgb(255,255,255)",
  225. tickcolor: "rgb(255,255,255)",
  226. backgroundcolor: "#e5ecf6",
  227. dtick: this.chartData.xaixs === "风速" ? 1 : undefined,
  228. range:
  229. this.chartData.analysisTypeCode === "风电机组风能利用系数分析" &&
  230. this.chartData.contract_Cp_curve_xData
  231. ? [
  232. 0,
  233. Math.max(
  234. ...this.chartData.contract_Cp_curve_xData
  235. .map(Number)
  236. .filter((val) => !isNaN(val)),
  237. ) * 0.9,
  238. ]
  239. : undefined,
  240. },
  241. yaxis: {
  242. title: this.chartData.yaixs || "Y轴", // 纵坐标标题
  243. gridcolor: "rgb(255,255,255)",
  244. tickcolor: "rgb(255,255,255)",
  245. backgroundcolor: "#e5ecf6",
  246. range:
  247. this.chartData.analysisTypeCode === "风电机组风能利用系数分析"
  248. ? [0, 1.5]
  249. : undefined,
  250. },
  251. margin: {
  252. l: 50,
  253. r: 50,
  254. t: 50,
  255. b: 50,
  256. },
  257. plot_bgcolor: "#e5ecf6",
  258. gridcolor: "#fff",
  259. bgcolor: "#e5ecf6", // 设置背景颜色
  260. autosize: true, // 开启自适应
  261. barmode: this.chartType === "bar" ? "stack" : "group", // 如果是柱状图则启用堆叠
  262. };
  263. const getChartSetUp = (axisTitle) => {
  264. return this.setUpImgData.find((item) => item.text.includes(axisTitle));
  265. };
  266. const xChartSetUp = getChartSetUp(layout.xaxis.title);
  267. if (xChartSetUp) {
  268. layout.xaxis.dtick = xChartSetUp.dtick;
  269. layout.xaxis.range = [Number(xChartSetUp.min), Number(xChartSetUp.max)];
  270. }
  271. const yChartSetUp = getChartSetUp(layout.yaxis.title);
  272. if (yChartSetUp) {
  273. layout.yaxis.dtick = yChartSetUp.dtick;
  274. layout.yaxis.range = [Number(yChartSetUp.min), Number(yChartSetUp.max)];
  275. }
  276. if (
  277. this.chartData.contract_Cp_curve_yData &&
  278. this.chartData.contract_Cp_curve_yData.length > 0
  279. ) {
  280. data.push({
  281. x: this.chartData.contract_Cp_curve_xData,
  282. y: this.chartData.contract_Cp_curve_yData,
  283. mode: "lines+markers",
  284. name: "合同功率曲线",
  285. line: {
  286. color: "red",
  287. width: 1, // 设置线条的宽度为1
  288. },
  289. marker: { color: "red", size: 4 },
  290. });
  291. }
  292. // 使用 Plotly.react 来更新图表
  293. Plotly.react(`bar-chart${this.index}`, data, layout, {
  294. responsive: true,
  295. modeBarButtonsToRemove: [
  296. // 移除不需要的工具按钮
  297. "lasso2d",
  298. "sendDataToCloud",
  299. "resetCameraLastSave3d",
  300. "resetCameraDefault3d",
  301. "resetCameraLastSave",
  302. "sendDataToCloud",
  303. "zoom2d", // 缩放按钮
  304. "zoom3d",
  305. "plotlylogo2D",
  306. "plotlylogo3D",
  307. ],
  308. displaylogo: false,
  309. }).then(function (gd) {
  310. // 获取工具栏按钮
  311. const toolbar = gd.querySelector(".modebar");
  312. const buttons = toolbar.querySelectorAll(".modebar-btn");
  313. // 定义一个映射对象,方便修改按钮提示
  314. const titleMap = {
  315. "Download plot as a png": "保存图片",
  316. Autoscale: "缩放",
  317. Pan: "平移",
  318. "Zoom out": "缩小",
  319. "Zoom in": "放大",
  320. "Box Select": "选择框操作",
  321. "Lasso Select": "套索选择操作",
  322. "Reset axes": "重置操作",
  323. "Reset camera to default": "重置相机视角",
  324. "Turntable rotation": "转台式旋转",
  325. "Orbital rotation": "轨道式旋转",
  326. };
  327. // 遍历所有按钮,修改它们的 title
  328. buttons.forEach(function (button) {
  329. const dataTitle = button.getAttribute("data-title");
  330. // 如果标题匹配,修改属性值
  331. if (titleMap[dataTitle]) {
  332. button.setAttribute("data-title", titleMap[dataTitle]);
  333. }
  334. });
  335. });
  336. },
  337. // 切换图表类型
  338. toggleChartType() {
  339. this.chartType = this.chartType === "line" ? "bar" : "line"; // 切换图表类型
  340. this.drawChart(); // 重新绘制图表
  341. },
  342. // 更新图表颜色
  343. updateChartColor() {
  344. this.drawChart(); // 更新颜色后重新绘制图表
  345. },
  346. // 根据配色方案设置每个选项的样式
  347. getOptionStyle(scheme) {
  348. return {
  349. background: `linear-gradient(to right, ${scheme
  350. .slice(0, 8)
  351. .join(", ")})`,
  352. color: "#fff",
  353. height: "30px",
  354. lineHeight: "30px",
  355. borderRadius: "0px",
  356. };
  357. },
  358. },
  359. beforeUnmount() {
  360. if (this.cancelToken) {
  361. this.cancelToken.cancel("组件卸载,取消请求");
  362. }
  363. },
  364. };
  365. </script>
  366. <style scoped>
  367. /* 样式可以根据需求自定义 */
  368. </style>