GeneratorTemperature.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <!--
  2. * @Author: your name
  3. * @Date: 2025-01-21 11:18:49
  4. * @LastEditTime: 2025-02-19 10:06:39
  5. * @LastEditors: bogon
  6. * @Description: In User Settings Edit
  7. * @FilePath: /performance-test/src/views/performance/components/chartsCom/GeneratorTemperature.vue
  8. -->
  9. <template>
  10. <div>
  11. <!-- 图表控制面板 总图-->
  12. <div style="display: flex; align-items: center">
  13. <div style="margin-right: 20px; display: flex; align-items: center">
  14. <el-select
  15. size="small"
  16. v-model="color1"
  17. @change="updateChartColor"
  18. placeholder="选择配色方案"
  19. style="width: 200px"
  20. >
  21. <el-option
  22. v-for="(scheme, index) in colorSchemes"
  23. :key="index"
  24. :label="scheme.label"
  25. :value="scheme.colors"
  26. :style="getOptionStyle(scheme.colors)"
  27. ></el-option>
  28. </el-select>
  29. <!-- <span style="margin-left: 10px">自定义颜色</span> -->
  30. </div>
  31. <div>
  32. <el-button size="small" @click="toggleChartType">
  33. 切换为{{ chartType === "line" ? "面积图" : "折线图" }}
  34. </el-button>
  35. </div>
  36. </div>
  37. <!-- 图表容器 -->
  38. <div
  39. v-loading="loading"
  40. :id="`bar-chart${index}`"
  41. style="width: 100%; height: 400px"
  42. >
  43. <el-empty v-if="isError" description="请求失败"></el-empty>
  44. </div>
  45. </div>
  46. </template>
  47. <script>
  48. import { nextTick } from "vue"; // 导入 nextTick
  49. import Plotly from "plotly.js-dist";
  50. import axios from "axios";
  51. import { colorSchemesLine } from "@/views/overview/js/colors";
  52. import { myMixin } from "@/mixins/chartRequestMixin"; // 假设你需要的 mixin
  53. export default {
  54. props: {
  55. fileAddr: {
  56. type: String,
  57. default: "",
  58. },
  59. index: {
  60. type: String,
  61. default() {
  62. return "";
  63. },
  64. },
  65. },
  66. mixins: [myMixin],
  67. data() {
  68. return {
  69. chartData: {},
  70. color1: [], // 默认颜色
  71. // 配色方案列表(每个方案是一个颜色数组)
  72. colorSchemes: [...colorSchemesLine],
  73. chartType: "line", // 默认图表类型是折线图
  74. loading: false,
  75. isError: false,
  76. colors: [...colorSchemesLine[0].colors],
  77. typeLine: ["solid", "dot", "dash", "longdash", "dashdot", "longdashdot"],
  78. };
  79. },
  80. mounted() {
  81. if (this.fileAddr) {
  82. this.getData();
  83. }
  84. },
  85. methods: {
  86. // 获取数据
  87. async getData() {
  88. if (this.fileAddr !== "") {
  89. try {
  90. this.loading = true;
  91. this.cancelToken = axios.CancelToken.source();
  92. const resultChartsData = await axios.get(this.fileAddr, {
  93. cancelToken: this.cancelToken.token,
  94. });
  95. this.chartData = resultChartsData.data;
  96. // 使用 nextTick 来确保 DOM 渲染完成后绘制图表
  97. nextTick(() => {
  98. this.drawChart();
  99. this.isError = false;
  100. this.loading = false;
  101. });
  102. } catch (error) {
  103. console.error("Error loading data:", error);
  104. this.isError = true;
  105. this.loading = false;
  106. }
  107. }
  108. },
  109. // 绘制图表
  110. drawChart() {
  111. const data = [];
  112. this.chartData.data.forEach((turbine, index) => {
  113. // 判断图表类型,根据类型调整绘制方式
  114. const chartConfig = {
  115. x: turbine.xData, // X 数据
  116. y: turbine.yData, // Y 数据
  117. name: turbine.Name, // 使用机组名称
  118. line: {
  119. dash: this.typeLine[index % this.colors.length],
  120. color:
  121. this.color1.length > 0
  122. ? this.color1[index % this.color1.length]
  123. : this.colors[index % this.colors.length], // 为每个机组分配不同的颜色
  124. },
  125. marker: {
  126. color:
  127. this.color1.length > 0
  128. ? this.color1[index % this.color1.length]
  129. : this.colors[index % this.colors.length], // 为每个机组分配不同的颜色
  130. },
  131. };
  132. if (this.chartType === "line") {
  133. chartConfig.fill = "none";
  134. chartConfig.mode = "lines"; // 如果是折线图
  135. } else if (this.chartType === "bar") {
  136. chartConfig.fill = "tonexty";
  137. // chartConfig.type = "bar"; // 如果是柱状图
  138. }
  139. data.push(chartConfig);
  140. });
  141. const layout = {
  142. title: this.chartData.title,
  143. xaxis: {
  144. title: this.chartData.xaixs || "X轴", // 横坐标标题
  145. },
  146. yaxis: {
  147. title: this.chartData.yaixs || "Y轴", // 纵坐标标题
  148. },
  149. margin: {
  150. l: 50,
  151. r: 50,
  152. t: 50,
  153. b: 50,
  154. },
  155. autosize: true, // 开启自适应
  156. barmode: this.chartType === "bar" ? "stack" : "group", // 如果是柱状图则启用堆叠
  157. };
  158. // 使用 Plotly.react 来更新图表
  159. Plotly.react(`bar-chart${this.index}`, data, layout, {
  160. responsive: true,
  161. });
  162. },
  163. // 切换图表类型
  164. toggleChartType() {
  165. this.chartType = this.chartType === "line" ? "bar" : "line"; // 切换图表类型
  166. this.drawChart(); // 重新绘制图表
  167. },
  168. // 更新图表颜色
  169. updateChartColor() {
  170. this.drawChart(); // 更新颜色后重新绘制图表
  171. },
  172. // 根据配色方案设置每个选项的样式
  173. getOptionStyle(scheme) {
  174. return {
  175. background: `linear-gradient(to right, ${scheme.join(", ")})`,
  176. color: "#fff",
  177. height: "30px",
  178. lineHeight: "30px",
  179. borderRadius: "4px",
  180. };
  181. },
  182. },
  183. };
  184. </script>
  185. <style scoped>
  186. /* 样式可以根据需求自定义 */
  187. </style>