Tjgb.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. export default {
  2. data() {
  3. return {
  4. // 添加光标
  5. cursorPoints: [], // 存储参考点数据
  6. };
  7. },
  8. mounted() {},
  9. beforeDestroy() {},
  10. methods: {
  11. handleDoubleClick(event) {
  12. if (!this.checkedGB.includes("添加光标")) return;
  13. if (!this.chartInstance) return;
  14. const xData = this.spectrumList?.x || this.timeList?.x;
  15. const yData = this.spectrumList?.y || this.timeList?.y;
  16. if (!xData || !yData) return;
  17. const pointInGrid = this.chartInstance.convertFromPixel(
  18. { seriesIndex: 0 },
  19. [event.offsetX, event.offsetY],
  20. );
  21. const xValue = pointInGrid?.[0];
  22. if (xValue == null) return;
  23. const closestIndex = this.findClosestIndex(xValue, xData);
  24. const yValue = yData[closestIndex];
  25. this.cursorPoints.push({
  26. xAxis: xData[closestIndex],
  27. yAxis: Number(yValue).toFixed(2),
  28. val: Number(yValue).toFixed(2),
  29. });
  30. this.updateCursorElements();
  31. },
  32. // 找到最接近的X轴索引
  33. findClosestIndex(xValue) {
  34. if (!this.envelopeList.x) return 0;
  35. let minDiff = Infinity;
  36. let closestIndex = 0;
  37. this.envelopeList.x.forEach((x, index) => {
  38. const diff = Math.abs(x - xValue);
  39. if (diff < minDiff) {
  40. minDiff = diff;
  41. closestIndex = index;
  42. }
  43. });
  44. return closestIndex;
  45. },
  46. updateCursorElements() {
  47. if (!this.chartInstance) return;
  48. // 获取当前图表选项
  49. const currentOption = this.chartInstance.getOption();
  50. // 主数据系列
  51. const mainSeries = currentOption.series[0];
  52. // 特征值系列(过滤掉已有的光标系列)
  53. const featureSeries = currentOption.series.filter(
  54. (s) => s.markLine && !s.id?.startsWith("CURSOR_"),
  55. );
  56. // 参考线配置(垂直线)
  57. const cursorLineSeries = {
  58. id: "CURSOR_LINE_SERIES",
  59. type: "line",
  60. markLine: {
  61. silent: true,
  62. lineStyle: {
  63. color: "#FF0000",
  64. type: "dashed",
  65. width: 1,
  66. },
  67. symbol: ["none", "none"],
  68. label: {
  69. show: true,
  70. formatter: (params) => params.data.val || "",
  71. },
  72. data: this.cursorPoints.map((point) => ({
  73. xAxis: point.xAxis,
  74. val: point.val,
  75. })),
  76. },
  77. };
  78. // 参考点配置(实际数据点位置)
  79. const cursorPointSeries = {
  80. id: "CURSOR_POINT_SERIES",
  81. type: "scatter",
  82. symbol: "circle",
  83. symbolSize: 8,
  84. itemStyle: { color: "#FF0000" },
  85. data: this.cursorPoints.map((point) => ({
  86. value: [point.xAxis, point.yAxis],
  87. name: point.val,
  88. })),
  89. label: {
  90. show: true,
  91. formatter: "{@[1]}",
  92. position: "top",
  93. color: "#FF0000",
  94. fontSize: 12,
  95. backgroundColor: "rgba(255,255,255,0.7)",
  96. padding: [2, 4],
  97. borderRadius: 3,
  98. },
  99. };
  100. // 设置新选项
  101. this.chartInstance.setOption(
  102. {
  103. series: [
  104. mainSeries,
  105. ...featureSeries,
  106. {
  107. id: "CURSOR_LINE_SERIES",
  108. ...cursorLineSeries,
  109. },
  110. {
  111. id: "CURSOR_POINT_SERIES",
  112. ...cursorPointSeries,
  113. },
  114. ],
  115. },
  116. { replaceMerge: ["series"] },
  117. );
  118. },
  119. },
  120. };