lineAndChildLine.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 === "line" ? "柱状图" : "折线图" }}
  17. </el-button>
  18. </div>
  19. </div>
  20. <!-- 图表容器 -->
  21. <div
  22. v-loading="loading"
  23. :id="`bar-chart${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"; // 假设你需要的 mixin
  35. export default {
  36. props: {
  37. fileAddr: {
  38. type: String,
  39. default: "",
  40. },
  41. index: {
  42. type: Number,
  43. default() {
  44. return 0;
  45. },
  46. },
  47. },
  48. mixins: [myMixin],
  49. data() {
  50. return {
  51. chartData: {},
  52. chartType: "line", // 默认图表类型是折线图
  53. color1: "#406DAB", // 默认颜色
  54. loading: false,
  55. isError: false,
  56. colors: [
  57. "#DFEDC1",
  58. "#DBEEBC",
  59. "#A8D7BE",
  60. "#8ECAC1",
  61. "#77BDC2",
  62. "#64ADC2",
  63. "#559ABE",
  64. "#4884B7",
  65. "#406DAB",
  66. "#3856A0",
  67. "#314291",
  68. "#28357A",
  69. "#1A285E",
  70. ],
  71. };
  72. },
  73. mounted() {
  74. if (this.fileAddr) {
  75. this.getData();
  76. }
  77. },
  78. methods: {
  79. // 获取数据
  80. async getData() {
  81. if (this.fileAddr !== "") {
  82. try {
  83. this.loading = true;
  84. this.cancelToken = axios.CancelToken.source();
  85. const resultChartsData = await axios.get(this.fileAddr, {
  86. cancelToken: this.cancelToken.token,
  87. });
  88. console.log("resultChartsData.data", resultChartsData);
  89. this.chartData = resultChartsData.data;
  90. // 使用 nextTick 来确保 DOM 渲染完成后绘制图表
  91. nextTick(() => {
  92. this.drawChart();
  93. this.isError = false;
  94. this.loading = false;
  95. });
  96. } catch (error) {
  97. console.error("Error loading data:", error);
  98. this.isError = true;
  99. this.loading = false;
  100. }
  101. }
  102. },
  103. // 绘制图表
  104. drawChart() {
  105. const data = [];
  106. console.log(this.chartData, "this.chartData");
  107. this.chartData &&
  108. this.chartData.data &&
  109. this.chartData.data.forEach((turbine, index) => {
  110. // 判断图表类型,根据类型调整绘制方式
  111. const chartConfig = {
  112. x: turbine.xData, // X 数据
  113. y: turbine.yData, // Y 数据
  114. name: turbine.engineName, // 使用机组名称
  115. line: {
  116. color: this.colors[index % this.colors.length], // 为每个机组分配不同的颜色
  117. },
  118. marker: {
  119. color: this.colors[index % this.colors.length], // 为每个机组分配不同的颜色
  120. },
  121. };
  122. if (this.chartType === "line") {
  123. chartConfig.mode = "lines"; // 如果是折线图
  124. } else if (this.chartType === "bar") {
  125. chartConfig.type = "bar"; // 如果是柱状图
  126. }
  127. data.push(chartConfig);
  128. });
  129. const layout = {
  130. title: this.chartData.title,
  131. xaxis: {
  132. title: this.chartData.xaixs || "X轴", // 横坐标标题
  133. },
  134. yaxis: {
  135. title: this.chartData.yaixs || "Y轴", // 纵坐标标题
  136. },
  137. margin: {
  138. l: 50,
  139. r: 50,
  140. t: 50,
  141. b: 50,
  142. },
  143. autosize: true, // 开启自适应
  144. barmode: this.chartType === "bar" ? "stack" : "group", // 如果是柱状图则启用堆叠
  145. };
  146. if (
  147. this.chartData.contract_Cp_curve_yData &&
  148. this.chartData.contract_Cp_curve_yData.length > 0
  149. ) {
  150. data.push({
  151. x: this.chartData.contract_Cp_curve_xData,
  152. y: this.chartData.contract_Cp_curve_yData,
  153. mode: "lines+markers",
  154. name: "合同功率曲线",
  155. line: {
  156. color: "red",
  157. width: 1, // 设置线条的宽度为1
  158. },
  159. marker: { color: "red", size: 4 },
  160. });
  161. }
  162. // 使用 Plotly.react 来更新图表
  163. Plotly.react(`bar-chart${this.index}`, data, layout, {
  164. responsive: true,
  165. });
  166. },
  167. // 切换图表类型
  168. toggleChartType() {
  169. this.chartType = this.chartType === "line" ? "bar" : "line"; // 切换图表类型
  170. this.drawChart(); // 重新绘制图表
  171. },
  172. // 更新图表颜色
  173. updateChartColor() {
  174. this.drawChart(); // 更新颜色后重新绘制图表
  175. },
  176. },
  177. };
  178. </script>
  179. <style scoped>
  180. /* 样式可以根据需求自定义 */
  181. </style>