spectrogramcharts.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <template>
  2. <div>
  3. <div class="FD">
  4. <div v-if="TZshow" class="eigenvalue">
  5. <h5>特征值</h5>
  6. <p>有效值:{{ this.spectrumList.Xrms }}</p>
  7. <p>平均值:{{ this.spectrumList.mean_value }}</p>
  8. <p>最大值:{{ this.spectrumList.max_value }}</p>
  9. <p>最小值:{{ this.spectrumList.min_value }}</p>
  10. <p>峰值:{{ this.spectrumList.Xp }}</p>
  11. <p>峰峰值:{{ this.spectrumList.Xpp }}</p>
  12. <p>波形因子:{{ this.spectrumList.Sf }}</p>
  13. <p>脉冲指标:{{ this.spectrumList.If }}</p>
  14. <p>裕度指标:{{ this.spectrumList.Ce }}</p>
  15. <p>偏度指标:{{ this.spectrumList.Cw }}</p>
  16. <p>峭度指标:{{ this.spectrumList.Cq }}</p>
  17. </div>
  18. </div>
  19. <!-- ECharts 图表容器 -->
  20. <div class="line-chart" ref="chart"></div>
  21. </div>
  22. </template>
  23. <script>
  24. import axios from "axios";
  25. import * as echarts from "echarts"; // 导入 echarts 库
  26. export default {
  27. name: "TimedomainCharts", // 组件名称
  28. props: {
  29. currentIndex: {
  30. type: Number,
  31. default: 0,
  32. },
  33. activeIndex: {
  34. type: Number,
  35. default: 0,
  36. },
  37. ids: {
  38. type: Array,
  39. default: () => [],
  40. },
  41. spectrumListTwo: {
  42. type: Object,
  43. default: () => ({}),
  44. },
  45. currentRow: {
  46. type: Object,
  47. default: () => ({}),
  48. },
  49. windCode: {
  50. type: String,
  51. default: "",
  52. },
  53. },
  54. data() {
  55. return {
  56. chartInstance: null,
  57. option: null,
  58. TZshow: false,
  59. spectrumList: {},
  60. };
  61. },
  62. watch: {
  63. // 监听 chartData 和 chartLabels 的变化,重新绘制图表
  64. chartData(newData) {
  65. this.updateChart(newData, this.chartLabels);
  66. },
  67. chartLabels(newLabels) {
  68. this.updateChart(this.chartData, newLabels);
  69. },
  70. spectrumListTwo(newValue) {
  71. this.spectrumList = newValue; // 将 spectrumListTwo 的新值赋给 spectrumList
  72. if (this.chartInstance) {
  73. this.updateChart(this.spectrumList.y, this.spectrumList.x); // 更新图表
  74. }
  75. },
  76. // 监听 spectrumList 的变化
  77. spectrumList: {
  78. handler(newValue) {
  79. if (this.chartInstance) {
  80. this.updateChart(newValue.y, newValue.x); // 只在 chartInstance 初始化后更新图表
  81. }
  82. },
  83. deep: true, // 深度监听
  84. },
  85. },
  86. mounted() {
  87. this.$nextTick(() => {
  88. setTimeout(() => {
  89. this.initializeChart(); // 延迟2秒后调用
  90. this.getTime();
  91. }, 2000); // 2000毫秒,即2秒
  92. });
  93. },
  94. methods: {
  95. initializeChart() {
  96. const chartDom = this.$refs.chart;
  97. if (chartDom && !this.chartInstance) {
  98. this.chartInstance = echarts.init(chartDom); // Initialize chart only once
  99. }
  100. // Update the chart with the initial data if available
  101. this.$nextTick(() => {
  102. if (this.chartInstance) {
  103. this.updateChart(this.spectrumList.y, this.spectrumList.x); // 更新图表
  104. }
  105. });
  106. },
  107. // 更新图表数据
  108. updateChart(data, labels) {
  109. if (!this.chartInstance || !labels || !data) return; // Check if chartInstance, labels, or data are not available
  110. // Ensure labels is an array
  111. if (!Array.isArray(labels)) {
  112. console.error("Labels are not an array");
  113. return;
  114. }
  115. // Ensure data and labels have the same length
  116. if (labels.length !== data.length) {
  117. console.error("Data and labels length mismatch");
  118. return;
  119. }
  120. const maxX = Math.max(...labels);
  121. const maxAxisValue = Math.ceil(maxX / 5) * 5;
  122. const ticks = [];
  123. for (let i = 0; i <= maxAxisValue; i += 5) {
  124. ticks.push(i);
  125. }
  126. const option = {
  127. title: {
  128. text: this.spectrumList.title,
  129. left: "center",
  130. },
  131. toolbox: {
  132. feature: {
  133. dataZoom: { yAxisIndex: "none" },
  134. restore: {},
  135. saveAsImage: {},
  136. myCustomTool: {
  137. show: true,
  138. title: "上一条",
  139. icon: `image://${require("@/assets/analyse/08.png")}`,
  140. onclick: () => this.previousRow(),
  141. },
  142. myCustomTool2: {
  143. show: true,
  144. title: "下一条",
  145. icon: `image://${require("@/assets/analyse/09.png")}`,
  146. onclick: () => this.nextRow(),
  147. },
  148. myCustomTool3: {
  149. show: true,
  150. title: "特征值",
  151. icon: `image://${require("@/assets/analyse/10.png")}`,
  152. onclick: () => this.Show(),
  153. },
  154. },
  155. },
  156. xAxis: {
  157. type: "value", // Use numeric x-axis
  158. name: this.spectrumList.xaxis, // Set X-axis title
  159. nameLocation: "center", // Title position
  160. nameTextStyle: {
  161. fontSize: 14,
  162. color: "#333", // Title color
  163. padding: [10, 0, 0, 0], // Padding for title
  164. },
  165. axisLabel: {
  166. formatter: (value) => {
  167. return value; // Show values without formatting
  168. },
  169. },
  170. axisTick: {
  171. show: true, // Show tick marks
  172. },
  173. axisLine: {
  174. show: true, // Show axis line
  175. },
  176. data: ticks, // Set tick values
  177. },
  178. yAxis: {
  179. type: "value", // Y-axis type as numeric
  180. name: this.spectrumList.yaxis, // Set Y-axis title
  181. nameTextStyle: {
  182. fontSize: 14,
  183. color: "#333", // Title color
  184. padding: [10, 0, 0, 0], // Padding for title
  185. },
  186. axisLabel: {
  187. formatter: (value) => {
  188. return value; // Show values without formatting
  189. },
  190. },
  191. axisTick: {
  192. show: true, // Show tick marks
  193. },
  194. axisLine: {
  195. show: true, // Show axis line
  196. },
  197. },
  198. tooltip: {
  199. trigger: "axis",
  200. formatter: (params) => {
  201. const xValue = params[0].value[0]; // Get x value
  202. const yValue = params[0].value[1]; // Get y value
  203. return `X: ${xValue}<br/>Y: ${yValue}`;
  204. },
  205. axisPointer: {
  206. type: "line", // Crosshair line
  207. },
  208. },
  209. series: [
  210. {
  211. name: "数据系列",
  212. type: "line", // Line chart
  213. data: labels.map((item, index) => [item, data[index]]), // Map x, y values
  214. symbol: "none", // No symbols at data points
  215. symbolSize: 8, // Set data point size
  216. lineStyle: {
  217. color: "rgb(75, 192, 192)", // Line color
  218. width: 2, // Line width
  219. },
  220. itemStyle: {
  221. color: "rgb(75, 192, 192)", // Data point color
  222. borderColor: "#fff",
  223. borderWidth: 2,
  224. },
  225. },
  226. ],
  227. };
  228. this.chartInstance.setOption(option); // Update the chart with the new option
  229. },
  230. getTime() {
  231. this.$emit("handleLoading", null, true, this.activeIndex);
  232. const params = {
  233. ids: this.ids,
  234. windCode: "SKF001",
  235. // windCode: this.windCode,
  236. analysisType: "frequency",
  237. };
  238. axios
  239. .post("/WJapi/analysis/frequency", params)
  240. .then((res) => {
  241. const parsedData = JSON.parse(res.data);
  242. // 使用 $set 或者扩展运算符确保 Vue 检测到变化
  243. this.spectrumList = { ...parsedData }; // 或者 this.$set(this, 'spectrumList', parsedData);
  244. this.$nextTick(() => {
  245. if (this.chartInstance) {
  246. this.updateChart(this.spectrumList.y, this.spectrumList.x); // 更新图表
  247. }
  248. });
  249. })
  250. .catch((error) => {
  251. console.error(error);
  252. })
  253. .finally(() => {
  254. this.$emit("handleLoading", this.currentRow, false, this.activeIndex);
  255. });
  256. },
  257. previousRow() {
  258. this.$emit("update-previous-row", 2, this.activeIndex);
  259. },
  260. // 触发下一条
  261. nextRow() {
  262. this.$emit("update-next-row", 2, this.activeIndex);
  263. },
  264. Show() {
  265. this.TZshow = !this.TZshow;
  266. },
  267. },
  268. };
  269. </script>
  270. <style lang="scss" scoped>
  271. .line-chart {
  272. width: 100%;
  273. height: 300px;
  274. }
  275. .FD {
  276. width: 100%;
  277. height: 1px;
  278. position: relative;
  279. }
  280. .eigenvalue {
  281. position: absolute;
  282. top: 30px;
  283. right: 0;
  284. font-size: 10px;
  285. width: 100px;
  286. border: 1px solid black;
  287. padding: 5px;
  288. background: #fff;
  289. z-index: 99;
  290. h5 {
  291. line-height: 16px;
  292. height: 16px;
  293. }
  294. }
  295. </style>