GeneratorTemperature.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <!--
  2. * @Author: your name
  3. * @Date: 2025-01-21 11:18:49
  4. * @LastEditTime: 2025-03-13 16:43:59
  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 { colorSchemes } from "@/views/overview/js/colors";
  52. import { myMixin } from "@/mixins/chartRequestMixin"; // 假设你需要的 mixin
  53. import { mapState } from "vuex";
  54. export default {
  55. props: {
  56. fileAddr: {
  57. type: String,
  58. default: "",
  59. },
  60. index: {
  61. type: String,
  62. default() {
  63. return "";
  64. },
  65. },
  66. setUpImgData: {
  67. default: () => [],
  68. type: Array,
  69. },
  70. },
  71. mixins: [myMixin],
  72. data() {
  73. return {
  74. chartData: {},
  75. color1: [], // 默认颜色
  76. // 配色方案列表(每个方案是一个颜色数组)
  77. colorSchemes: [...colorSchemes],
  78. chartType: "line", // 默认图表类型是折线图
  79. loading: false,
  80. isError: false,
  81. colors: [...colorSchemes[0].colors],
  82. typeLine: ["solid", "dot", "dash", "longdash", "dashdot", "longdashdot"],
  83. };
  84. },
  85. computed: {
  86. ...mapState("themes", {
  87. themeColor: "themeColor",
  88. }),
  89. },
  90. watch: {
  91. themeColor: {
  92. handler() {
  93. this.color1 = this.themeColor;
  94. this.updateChartColor();
  95. },
  96. deep: true,
  97. },
  98. setUpImgData: {
  99. handler(newType) {
  100. this.drawChart();
  101. },
  102. deep: true,
  103. },
  104. },
  105. mounted() {
  106. if (this.fileAddr) {
  107. this.getData();
  108. this.color1 = this.themeColor;
  109. }
  110. },
  111. methods: {
  112. // 获取数据
  113. async getData() {
  114. if (this.fileAddr !== "") {
  115. try {
  116. this.loading = true;
  117. this.cancelToken = axios.CancelToken.source();
  118. const resultChartsData = await axios.get(this.fileAddr, {
  119. cancelToken: this.cancelToken.token,
  120. });
  121. this.chartData = resultChartsData.data;
  122. // 使用 nextTick 来确保 DOM 渲染完成后绘制图表
  123. nextTick(() => {
  124. this.drawChart();
  125. this.isError = false;
  126. this.loading = false;
  127. });
  128. } catch (error) {
  129. console.error("Error loading data:", error);
  130. this.isError = true;
  131. this.loading = false;
  132. }
  133. }
  134. },
  135. // 绘制图表
  136. drawChart() {
  137. const data = [];
  138. this.chartData.data.forEach((turbine, index) => {
  139. // 判断图表类型,根据类型调整绘制方式
  140. const chartConfig = {
  141. x: turbine.xData, // X 数据
  142. y: turbine.yData, // Y 数据
  143. name: turbine.Name, // 使用机组名称
  144. line: {
  145. dash: this.typeLine[index % this.colors.length],
  146. color:
  147. this.color1.length > 0
  148. ? this.color1[index % this.color1.length]
  149. : this.colors[index % this.colors.length], // 为每个机组分配不同的颜色
  150. },
  151. marker: {
  152. color:
  153. this.color1.length > 0
  154. ? this.color1[index % this.color1.length]
  155. : this.colors[index % this.colors.length], // 为每个机组分配不同的颜色
  156. },
  157. hovertemplate:
  158. `${this.chartData.xaixs}:` +
  159. ` %{x} <br> ` +
  160. `${this.chartData.yaixs}:` +
  161. "%{y} <br>",
  162. };
  163. if (this.chartType === "line") {
  164. chartConfig.fill = "none";
  165. chartConfig.mode = "lines"; // 如果是折线图
  166. } else if (this.chartType === "bar") {
  167. chartConfig.fill = "tonexty";
  168. // chartConfig.type = "bar"; // 如果是柱状图
  169. }
  170. data.push(chartConfig);
  171. });
  172. const layout = {
  173. title: {
  174. text: this.chartData.title,
  175. font: {
  176. size: 16, // 设置标题字体大小(默认 16)
  177. weight: "bold",
  178. },
  179. },
  180. xaxis: {
  181. title: this.chartData.xaixs || "X轴", // 横坐标标题
  182. gridcolor: "rgb(255,255,255)",
  183. tickcolor: "rgb(255,255,255)",
  184. backgroundcolor: "#e5ecf6",
  185. },
  186. yaxis: {
  187. title: this.chartData.yaixs || "Y轴", // 纵坐标标题
  188. gridcolor: "rgb(255,255,255)",
  189. tickcolor: "rgb(255,255,255)",
  190. backgroundcolor: "#e5ecf6",
  191. },
  192. margin: {
  193. l: 50,
  194. r: 50,
  195. t: 50,
  196. b: 50,
  197. },
  198. plot_bgcolor: "#e5ecf6",
  199. gridcolor: "#fff",
  200. bgcolor: "#e5ecf6", // 设置背景颜色
  201. autosize: true, // 开启自适应
  202. barmode: this.chartType === "bar" ? "stack" : "group", // 如果是柱状图则启用堆叠
  203. };
  204. const getChartSetUp = (axisTitle) => {
  205. return this.setUpImgData.find((item) => item.text.includes(axisTitle));
  206. };
  207. const xChartSetUp = getChartSetUp(layout.xaxis.title);
  208. if (xChartSetUp) {
  209. layout.xaxis.dtick = xChartSetUp.dtick;
  210. layout.xaxis.range = [xChartSetUp.min, xChartSetUp.max];
  211. }
  212. const yChartSetUp = getChartSetUp(layout.yaxis.title);
  213. if (yChartSetUp) {
  214. layout.yaxis.dtick = yChartSetUp.dtick;
  215. layout.yaxis.range = [yChartSetUp.min, yChartSetUp.max];
  216. }
  217. this.$nextTick(() => {
  218. // 使用 Plotly.react 来更新图表
  219. Plotly.react(`bar-chart${this.index}`, data, layout, {
  220. responsive: true,
  221. });
  222. });
  223. },
  224. // 切换图表类型
  225. toggleChartType() {
  226. this.chartType = this.chartType === "line" ? "bar" : "line"; // 切换图表类型
  227. this.drawChart(); // 重新绘制图表
  228. },
  229. // 更新图表颜色
  230. updateChartColor() {
  231. this.drawChart(); // 更新颜色后重新绘制图表
  232. },
  233. // 根据配色方案设置每个选项的样式
  234. getOptionStyle(scheme) {
  235. return {
  236. background: `linear-gradient(to right, ${scheme
  237. .slice(0, 8)
  238. .join(", ")})`,
  239. color: "#fff",
  240. height: "30px",
  241. lineHeight: "30px",
  242. borderRadius: "0px",
  243. };
  244. },
  245. },
  246. };
  247. </script>
  248. <style scoped>
  249. /* 样式可以根据需求自定义 */
  250. </style>