PlotlyCharts.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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: [...colorSchemesLine[0].colors],
  99. },
  100. powerCurveData: {
  101. turbines: [],
  102. contractPowerCurve: [],
  103. },
  104. };
  105. },
  106. mounted() {
  107. this.updateCharts(); // 初次渲染
  108. },
  109. watch: {
  110. lineMarkerData: {
  111. deep: true,
  112. handler() {
  113. this.updateCharts(); // 数据变化时更新
  114. },
  115. },
  116. comType(newType, oldType) {
  117. if (newType !== oldType) {
  118. this.updateCharts(); // 类型变化时更新
  119. }
  120. },
  121. },
  122. methods: {
  123. updateCharts() {
  124. // console.log(this.lineMarkerData, "this.lineMarkerData");
  125. this.powerCurveData.turbines =
  126. this.lineMarkerData.data?.filter(
  127. (item) => item.enginName !== "合同功率曲线"
  128. ) || [];
  129. this.powerCurveData.contractPowerCurve =
  130. this.lineMarkerData.data?.filter(
  131. (item) => item.enginName === "合同功率曲线"
  132. ) || [];
  133. if (this.comType === "generalDrawing" && this.lineMarkerData?.data) {
  134. this.renderPlot();
  135. } else if (this.comType === "graph" && this.lineMarkerData?.data) {
  136. this.$nextTick(() => {
  137. this.initializeEngineCharts();
  138. });
  139. }
  140. },
  141. renderPlot() {
  142. const data = [];
  143. this.powerCurveData.turbines.forEach((turbine, index) => {
  144. data.push({
  145. x: turbine.xData,
  146. y: turbine.yData.map((val) => (val !== null ? val : 0.0)),
  147. mode: "lines",
  148. name: turbine.enginName,
  149. fill: this.chartType === "line" ? "none" : "tonexty",
  150. line: {
  151. color:
  152. this.color1.length > 0
  153. ? this.color1[index % this.color1.length]
  154. : this.config.colors[index % this.config.colors.length],
  155. },
  156. });
  157. });
  158. data.push({
  159. x: this.powerCurveData.contractPowerCurve[0].xData,
  160. y: this.powerCurveData.contractPowerCurve[0].yData,
  161. mode: this.config.powerConfig.mode,
  162. name: this.config.powerConfig.name,
  163. line: this.config.powerConfig.line,
  164. marker: this.config.powerConfig.marker,
  165. });
  166. // console.log(this.powerCurveData, "this.powerCurveData");
  167. const layout = {
  168. title: "有功功率曲线分析" + this.lineMarkerData.engineTypeName,
  169. plot_bgcolor: this.config.lableConfig.plot_bgcolor,
  170. xaxis: this.config.lableConfig.xaxis,
  171. yaxis: this.config.lableConfig.yaxis,
  172. legend: this.config.lableConfig.legend,
  173. };
  174. Plotly.newPlot(`power-curve-plot${this.inds}`, data, layout);
  175. },
  176. //初始化分图
  177. initializeEngineCharts() {
  178. const fentuCharts = this.powerCurveData.turbines.filter(
  179. (item) =>
  180. item.enginCode === this.lineMarkerData?.formInfoFieldEngineCode
  181. );
  182. // console.log(this.powerCurveData.turbines, "this.powerCurveData.turbines");
  183. // console.log(fentuCharts, "fentuCharts");
  184. if (fentuCharts[0]?.enginName || fentuCharts[0]?.enginCode) {
  185. const highlightedData = [];
  186. const nonHighlightedData = [];
  187. const layout = {
  188. title:
  189. fentuCharts[0]?.enginName ||
  190. fentuCharts[0]?.enginCode + "风机有功功率曲线分析",
  191. plot_bgcolor: this.config.lableConfig.plot_bgcolor,
  192. xaxis: this.config.lableConfig.xaxis,
  193. yaxis: this.config.lableConfig.yaxis,
  194. legend: this.config.lableConfig.legend,
  195. };
  196. // 先渲染其他的风机数据
  197. this.powerCurveData.turbines.forEach((turbine, index) => {
  198. const isHighlighted =
  199. turbine?.enginCode ===
  200. this.lineMarkerData?.formInfoFieldEngineCode ||
  201. turbine?.enginName === this.lineMarkerData?.formInfoFieldEngineCode;
  202. const trace = {
  203. x: turbine.xData,
  204. y: turbine.yData.map((val) => (val !== null ? val : 0.0)),
  205. mode: "lines",
  206. name: turbine.enginName,
  207. fill: this.chartType === "line" ? "none" : "tonexty",
  208. line: {
  209. color: isHighlighted ? "#1c77b3" : "#d3d3d3",
  210. },
  211. };
  212. if (isHighlighted) {
  213. highlightedData.push(trace); // 高亮的线条放入 highlighedData
  214. } else {
  215. nonHighlightedData.push(trace); // 非高亮的线条放入 nonHighlightedData
  216. }
  217. });
  218. // 先添加非高亮的风机数据
  219. const data = [...nonHighlightedData, ...highlightedData]; // 高亮的风机数据最后添加
  220. // 添加合同功率曲线
  221. data.push({
  222. x: this.powerCurveData.contractPowerCurve[0].xData,
  223. y: this.powerCurveData.contractPowerCurve[0].yData,
  224. mode: "lines+markers",
  225. name: this.powerCurveData.contractPowerCurve[0].enginName,
  226. line: {
  227. color: "red",
  228. width: 1, // 设置线条的宽度为1
  229. },
  230. marker: { color: "red", size: 4 },
  231. });
  232. Plotly.newPlot(`chart-${this.inds}`, data, layout);
  233. }
  234. },
  235. // 切换图表类型
  236. toggleChartType() {
  237. this.chartType = this.chartType === "line" ? "bar" : "line"; // 切换图表类型
  238. // 重新绘制图表
  239. this.updateCharts();
  240. },
  241. updateChartColor(color) {
  242. // 更新图表颜色
  243. // this.color1 = color;
  244. this.updateCharts();
  245. },
  246. // 根据配色方案设置每个选项的样式
  247. getOptionStyle(scheme) {
  248. return {
  249. background: `linear-gradient(to right, ${scheme.join(", ")})`,
  250. color: "#fff",
  251. height: "30px",
  252. lineHeight: "30px",
  253. borderRadius: "4px",
  254. };
  255. },
  256. },
  257. };
  258. </script>
  259. <style scoped>
  260. /* You can add styles for your component here */
  261. </style>