lineAndChildLine.vue 11 KB

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