PlotlyCharts.vue 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <template>
  2. <div>
  3. <div style="display: flex; align-items: center">
  4. <div
  5. style="margin-right: 20px; display: flex; align-items: center"
  6. v-if="comType === 'generalDrawing'"
  7. >
  8. <el-select
  9. size="small"
  10. v-model="color1"
  11. @change="updateChartColor"
  12. placeholder="选择配色方案"
  13. style="width: 200px"
  14. >
  15. <el-option
  16. v-for="(scheme, index) in colorSchemes"
  17. :key="index"
  18. :label="scheme.label"
  19. :value="scheme.colors"
  20. :style="getOptionStyle(scheme.colors)"
  21. ></el-option>
  22. </el-select>
  23. <!-- <span style="margin-left: 10px">自定义颜色</span> -->
  24. </div>
  25. <div>
  26. <el-button size="small" @click="toggleChartType">
  27. 切换为{{ chartType === "line" ? "面积图" : "折线图" }}
  28. </el-button>
  29. </div>
  30. </div>
  31. <!-- 总图组件 -->
  32. <div
  33. v-if="comType === 'generalDrawing'"
  34. :id="`power-curve-plot${inds}`"
  35. style="width: 100%; height: 550px"
  36. ></div>
  37. <!-- 分图组件 -->
  38. <template v-else-if="comType === 'graph'">
  39. <div :id="`chart-${inds}`" style="width: 100%; height: 550px"></div>
  40. </template>
  41. </div>
  42. </template>
  43. <script>
  44. import Plotly from "plotly.js-dist";
  45. import { colorSchemesLine } from "@/views/overview/js/colors";
  46. export default {
  47. props: {
  48. lineMarkerData: Object,
  49. comType: String,
  50. inds: String,
  51. },
  52. name: "PowerCurvePlot",
  53. data() {
  54. return {
  55. chartType: "line", // 默认图表类型是折线图
  56. graphData: [], //分图数据length data
  57. color1: [], // 默认颜色
  58. // 配色方案列表(每个方案是一个颜色数组)
  59. colorSchemes: [...colorSchemesLine],
  60. config: {
  61. powerConfig: {
  62. mode: "lines+markers",
  63. name: "合同功率曲线",
  64. line: {
  65. color: "red",
  66. width: 1, // 设置线条的宽度为1
  67. },
  68. marker: { color: "red", size: 4 },
  69. },
  70. lableConfig: {
  71. title: { text: "", x: 0.5 },
  72. plot_bgcolor: "#e5ecf6", // Chart background color
  73. xaxis: {
  74. title: "风速(m/s)",
  75. // range: [0, 26],
  76. tickmode: "linear",
  77. gridcolor: "rgb(255,255,255)",
  78. showgrid: true,
  79. zeroline: false,
  80. tickcolor: "rgb(255,255,255)",
  81. dtick: 1,
  82. tickangle: -45,
  83. },
  84. yaxis: {
  85. title: "功率(kW)",
  86. gridcolor: "rgb(255,255,255)",
  87. showgrid: true,
  88. zeroline: false,
  89. tickcolor: "rgb(255,255,255)",
  90. },
  91. legend: {
  92. orientation: "h",
  93. xanchor: "center",
  94. x: 0.5,
  95. y: -0.2,
  96. },
  97. },
  98. colors: [
  99. "#636EFA",
  100. "#EF553B",
  101. "#00CC96",
  102. "#AB63FA",
  103. "#FFA15A",
  104. "#19D3F3",
  105. "#FF6692",
  106. "#B6E880",
  107. "#FF97FF",
  108. "#FECB52",
  109. "#636EFB",
  110. "#EF553C",
  111. "#00CC97",
  112. "#AB63FB",
  113. "#FFA15B",
  114. "#19D3F4",
  115. "#FF6693",
  116. "#B6E881",
  117. "#FF97FE",
  118. "#FECB51",
  119. "#1F77B4",
  120. "#FF7F0E",
  121. "#2CA02C",
  122. "#D62728",
  123. "#9467BD",
  124. "#8C564B",
  125. "#E377C2",
  126. "#7F7F7F",
  127. "#BCBD22",
  128. "#17BECF",
  129. "#1A55F2",
  130. "#FF5733",
  131. "#33FF57",
  132. "#3375FF",
  133. "#FF33A6",
  134. "#57FF33",
  135. "#3380FF",
  136. "#FF8033",
  137. "#57FF80",
  138. "#8033FF",
  139. "#FF3380",
  140. "#FFD733",
  141. ],
  142. },
  143. powerCurveData: {
  144. turbines: [],
  145. contractPowerCurve: [],
  146. },
  147. };
  148. },
  149. mounted() {
  150. this.updateCharts(); // 初次渲染
  151. },
  152. watch: {
  153. lineMarkerData: {
  154. deep: true,
  155. handler() {
  156. this.updateCharts(); // 数据变化时更新
  157. },
  158. },
  159. comType(newType, oldType) {
  160. if (newType !== oldType) {
  161. this.updateCharts(); // 类型变化时更新
  162. }
  163. },
  164. },
  165. methods: {
  166. updateCharts() {
  167. // console.log(this.lineMarkerData, "this.lineMarkerData");
  168. this.powerCurveData.turbines =
  169. this.lineMarkerData.data?.filter(
  170. (item) => item.enginName !== "合同功率曲线"
  171. ) || [];
  172. this.powerCurveData.contractPowerCurve =
  173. this.lineMarkerData.data?.filter(
  174. (item) => item.enginName === "合同功率曲线"
  175. ) || [];
  176. if (this.comType === "generalDrawing" && this.lineMarkerData?.data) {
  177. this.renderPlot();
  178. } else if (this.comType === "graph" && this.lineMarkerData?.data) {
  179. this.$nextTick(() => {
  180. this.initializeEngineCharts();
  181. });
  182. }
  183. },
  184. renderPlot() {
  185. const data = [];
  186. this.powerCurveData.turbines.forEach((turbine, index) => {
  187. data.push({
  188. x: turbine.xData,
  189. y: turbine.yData.map((val) => (val !== null ? val : 0.0)),
  190. mode: "lines",
  191. name: turbine.enginName,
  192. fill: this.chartType === "line" ? "none" : "tonexty",
  193. line: {
  194. color:
  195. this.color1.length > 0
  196. ? this.color1[index % this.color1.length]
  197. : this.config.colors[index % this.config.colors.length],
  198. },
  199. });
  200. });
  201. data.push({
  202. x: this.powerCurveData.contractPowerCurve[0].xData,
  203. y: this.powerCurveData.contractPowerCurve[0].yData,
  204. mode: this.config.powerConfig.mode,
  205. name: this.config.powerConfig.name,
  206. line: this.config.powerConfig.line,
  207. marker: this.config.powerConfig.marker,
  208. });
  209. // console.log(this.powerCurveData, "this.powerCurveData");
  210. const layout = {
  211. title: "有功功率曲线分析" + this.lineMarkerData.engineTypeName,
  212. plot_bgcolor: this.config.lableConfig.plot_bgcolor,
  213. xaxis: this.config.lableConfig.xaxis,
  214. yaxis: this.config.lableConfig.yaxis,
  215. legend: this.config.lableConfig.legend,
  216. };
  217. Plotly.newPlot(`power-curve-plot${this.inds}`, data, layout);
  218. },
  219. //初始化分图
  220. initializeEngineCharts() {
  221. const fentuCharts = this.powerCurveData.turbines.filter(
  222. (item) =>
  223. item.enginCode === this.lineMarkerData?.formInfoFieldEngineCode
  224. );
  225. // console.log(this.powerCurveData.turbines, "this.powerCurveData.turbines");
  226. // console.log(fentuCharts, "fentuCharts");
  227. if (fentuCharts[0]?.enginName || fentuCharts[0]?.enginCode) {
  228. const highlightedData = [];
  229. const nonHighlightedData = [];
  230. const layout = {
  231. title:
  232. fentuCharts[0]?.enginName ||
  233. fentuCharts[0]?.enginCode + "风机有功功率曲线分析",
  234. plot_bgcolor: this.config.lableConfig.plot_bgcolor,
  235. xaxis: this.config.lableConfig.xaxis,
  236. yaxis: this.config.lableConfig.yaxis,
  237. legend: this.config.lableConfig.legend,
  238. };
  239. // 先渲染其他的风机数据
  240. this.powerCurveData.turbines.forEach((turbine, index) => {
  241. const isHighlighted =
  242. turbine?.enginCode ===
  243. this.lineMarkerData?.formInfoFieldEngineCode ||
  244. turbine?.enginName === this.lineMarkerData?.formInfoFieldEngineCode;
  245. const trace = {
  246. x: turbine.xData,
  247. y: turbine.yData.map((val) => (val !== null ? val : 0.0)),
  248. mode: "lines",
  249. name: turbine.enginName,
  250. fill: this.chartType === "line" ? "none" : "tonexty",
  251. line: {
  252. color: isHighlighted ? "#1c77b3" : "#d3d3d3",
  253. },
  254. };
  255. if (isHighlighted) {
  256. highlightedData.push(trace); // 高亮的线条放入 highlighedData
  257. } else {
  258. nonHighlightedData.push(trace); // 非高亮的线条放入 nonHighlightedData
  259. }
  260. });
  261. // 先添加非高亮的风机数据
  262. const data = [...nonHighlightedData, ...highlightedData]; // 高亮的风机数据最后添加
  263. // 添加合同功率曲线
  264. data.push({
  265. x: this.powerCurveData.contractPowerCurve[0].xData,
  266. y: this.powerCurveData.contractPowerCurve[0].yData,
  267. mode: "lines+markers",
  268. name: this.powerCurveData.contractPowerCurve[0].enginName,
  269. line: {
  270. color: "red",
  271. width: 1, // 设置线条的宽度为1
  272. },
  273. marker: { color: "red", size: 4 },
  274. });
  275. Plotly.newPlot(`chart-${this.inds}`, data, layout);
  276. }
  277. },
  278. // 切换图表类型
  279. toggleChartType() {
  280. this.chartType = this.chartType === "line" ? "bar" : "line"; // 切换图表类型
  281. // 重新绘制图表
  282. this.updateCharts();
  283. },
  284. updateChartColor(color) {
  285. // 更新图表颜色
  286. // this.color1 = color;
  287. this.updateCharts();
  288. },
  289. // 根据配色方案设置每个选项的样式
  290. getOptionStyle(scheme) {
  291. return {
  292. background: `linear-gradient(to right, ${scheme.join(", ")})`,
  293. color: "#fff",
  294. height: "30px",
  295. lineHeight: "30px",
  296. borderRadius: "4px",
  297. };
  298. },
  299. },
  300. };
  301. </script>
  302. <style scoped>
  303. /* You can add styles for your component here */
  304. </style>