lineAndChildLine.vue 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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() {
  91. this.color1 = this.themeColor;
  92. this.updateChartColor();
  93. },
  94. deep: true,
  95. },
  96. setUpImgData: {
  97. handler(newType) {
  98. this.drawChart();
  99. },
  100. deep: true,
  101. },
  102. },
  103. mounted() {
  104. if (this.fileAddr) {
  105. this.color1 = this.themeColor;
  106. this.getData();
  107. }
  108. },
  109. methods: {
  110. // 获取数据
  111. async getData() {
  112. if (this.fileAddr !== "") {
  113. try {
  114. this.loading = true;
  115. this.cancelToken = axios.CancelToken.source();
  116. const resultChartsData = await axios.get(this.fileAddr, {
  117. cancelToken: this.cancelToken.token,
  118. });
  119. this.chartData = resultChartsData.data;
  120. // 使用 nextTick 来确保 DOM 渲染完成后绘制图表
  121. nextTick(() => {
  122. this.drawChart();
  123. this.isError = false;
  124. this.loading = false;
  125. });
  126. } catch (error) {
  127. console.error("Error loading data:", error);
  128. this.isError = true;
  129. this.loading = false;
  130. }
  131. }
  132. },
  133. // 绘制图表
  134. drawChart() {
  135. if (!this.$refs[`bar-chart${this.index}`]) {
  136. return false;
  137. }
  138. const data = [];
  139. this.chartData &&
  140. this.chartData.data &&
  141. this.chartData.data.forEach((turbine, index) => {
  142. // 判断图表类型,根据类型调整绘制方式
  143. const chartConfig = {
  144. x: turbine.xData, // X 数据
  145. y: turbine.yData, // Y 数据
  146. name: turbine.engineName, // 使用机组名称
  147. line: {
  148. color:
  149. this.color1.length > 0
  150. ? this.color1[index % this.color1.length]
  151. : this.colors[index % this.colors.length], // 为每个机组分配不同的颜色
  152. },
  153. marker: {
  154. color:
  155. this.color1.length > 0
  156. ? this.color1[index % this.color1.length]
  157. : this.colors[index % this.colors.length], // 为每个机组分配不同的颜色
  158. },
  159. hovertemplate:
  160. `${this.chartData.xaixs}:` +
  161. ` %{x} <br> ` +
  162. `${this.chartData.yaixs}:` +
  163. "%{y} <br>",
  164. };
  165. if (this.chartData.yaixs === "概率密度函数") {
  166. chartConfig.line.color = this.color1[12];
  167. }
  168. if (this.chartType === "line") {
  169. chartConfig.mode = "lines"; // 如果是折线图
  170. chartConfig.fill = "none";
  171. } else if (this.chartType === "bar") {
  172. // chartConfig.type = "bar"; // 如果是柱状图
  173. chartConfig.fill = "tonexty";
  174. }
  175. data.push(chartConfig);
  176. });
  177. console.log(this.chartData, "图表title");
  178. const layout = {
  179. title: {
  180. text: this.chartData.title || this.chartData.data[0].title,
  181. font: {
  182. size: 16, // 设置标题字体大小(默认 16)
  183. weight: "bold",
  184. },
  185. },
  186. xaxis: {
  187. title: this.chartData.xaixs || "X轴", // 横坐标标题
  188. gridcolor: "rgb(255,255,255)",
  189. tickcolor: "rgb(255,255,255)",
  190. backgroundcolor: "#e5ecf6",
  191. },
  192. yaxis: {
  193. title: this.chartData.yaixs || "Y轴", // 纵坐标标题
  194. gridcolor: "rgb(255,255,255)",
  195. tickcolor: "rgb(255,255,255)",
  196. backgroundcolor: "#e5ecf6",
  197. },
  198. margin: {
  199. l: 50,
  200. r: 50,
  201. t: 50,
  202. b: 50,
  203. },
  204. plot_bgcolor: "#e5ecf6",
  205. gridcolor: "#fff",
  206. bgcolor: "#e5ecf6", // 设置背景颜色
  207. autosize: true, // 开启自适应
  208. barmode: this.chartType === "bar" ? "stack" : "group", // 如果是柱状图则启用堆叠
  209. };
  210. const getChartSetUp = (axisTitle) => {
  211. return this.setUpImgData.find((item) => item.text.includes(axisTitle));
  212. };
  213. const xChartSetUp = getChartSetUp(layout.xaxis.title);
  214. if (xChartSetUp) {
  215. layout.xaxis.dtick = xChartSetUp.dtick;
  216. layout.xaxis.range = [xChartSetUp.min, xChartSetUp.max];
  217. }
  218. const yChartSetUp = getChartSetUp(layout.yaxis.title);
  219. if (yChartSetUp) {
  220. layout.yaxis.dtick = yChartSetUp.dtick;
  221. layout.yaxis.range = [yChartSetUp.min, yChartSetUp.max];
  222. }
  223. if (
  224. this.chartData.contract_Cp_curve_yData &&
  225. this.chartData.contract_Cp_curve_yData.length > 0
  226. ) {
  227. data.push({
  228. x: this.chartData.contract_Cp_curve_xData,
  229. y: this.chartData.contract_Cp_curve_yData,
  230. mode: "lines+markers",
  231. name: "合同功率曲线",
  232. line: {
  233. color: "red",
  234. width: 1, // 设置线条的宽度为1
  235. },
  236. marker: { color: "red", size: 4 },
  237. });
  238. }
  239. // 使用 Plotly.react 来更新图表
  240. Plotly.react(`bar-chart${this.index}`, data, layout, {
  241. responsive: true,
  242. });
  243. },
  244. // 切换图表类型
  245. toggleChartType() {
  246. this.chartType = this.chartType === "line" ? "bar" : "line"; // 切换图表类型
  247. this.drawChart(); // 重新绘制图表
  248. },
  249. // 更新图表颜色
  250. updateChartColor() {
  251. this.drawChart(); // 更新颜色后重新绘制图表
  252. },
  253. // 根据配色方案设置每个选项的样式
  254. getOptionStyle(scheme) {
  255. return {
  256. background: `linear-gradient(to right, ${scheme
  257. .slice(0, 8)
  258. .join(", ")})`,
  259. color: "#fff",
  260. height: "30px",
  261. lineHeight: "30px",
  262. borderRadius: "0px",
  263. };
  264. },
  265. },
  266. };
  267. </script>
  268. <style scoped>
  269. /* 样式可以根据需求自定义 */
  270. </style>