Xdgb.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. export default {
  2. data() {
  3. return {
  4. // 谐波光标
  5. harmonicCursorVisible: false,
  6. harmonicCursorPoints: [],
  7. harmonicCursorSeries: null,
  8. };
  9. },
  10. mounted() {},
  11. beforeDestroy() {},
  12. methods: {
  13. enableHarmonicCursor() {
  14. this.harmonicCursorVisible = true;
  15. this.chartInstance.getZr().on("click", this.handleHarmonicCursorClick);
  16. },
  17. // 禁用谐波光标
  18. disableHarmonicCursor() {
  19. this.harmonicCursorVisible = false;
  20. this.chartInstance.getZr().off("click", this.handleHarmonicCursorClick);
  21. this.removeHarmonicCursor();
  22. },
  23. handleHarmonicCursorClick(event) {
  24. if (!this.harmonicCursorVisible) return;
  25. // 获取点击位置对应的X值
  26. const pointInGrid = this.chartInstance.convertFromPixel(
  27. { seriesIndex: 0 },
  28. [event.offsetX, event.offsetY]
  29. );
  30. const baseX = pointInGrid[0];
  31. // 生成1-6倍频的X坐标
  32. this.harmonicCursorPoints = [];
  33. for (let i = 1; i <= 6; i++) {
  34. const xValue = baseX * i;
  35. const closestIndex = this.findClosestIndex(xValue);
  36. this.harmonicCursorPoints.push({
  37. xAxis: xValue,
  38. multiple: i,
  39. yValue: this.envelopeList.y[closestIndex]?.toFixed(2) || "0",
  40. });
  41. }
  42. this.updateHarmonicCursor();
  43. },
  44. // 更新谐波光标显示
  45. updateHarmonicCursor() {
  46. // 创建谐波光标系列
  47. this.harmonicCursorSeries = {
  48. type: "line",
  49. id: "HARMONIC_CURSOR",
  50. silent: true,
  51. markLine: {
  52. silent: true,
  53. lineStyle: {
  54. color: "#FFA500", // 橙色表示谐波光标
  55. type: "dashed",
  56. width: 1,
  57. },
  58. symbol: ["none", "none"],
  59. label: {
  60. show: true,
  61. position: "start",
  62. formatter: (params) =>
  63. `${params.data.multiple}X\n${params.data.yValue}`,
  64. color: "#FFA500",
  65. backgroundColor: "rgba(255,255,255,0.8)",
  66. padding: [2, 4],
  67. borderRadius: 3,
  68. },
  69. data: this.harmonicCursorPoints.map((point) => ({
  70. xAxis: point.xAxis,
  71. multiple: point.multiple,
  72. yValue: point.yValue,
  73. })),
  74. },
  75. };
  76. // 更新图表
  77. const option = this.chartInstance.getOption();
  78. const otherSeries = option.series.filter(
  79. (s) => s.id !== "HARMONIC_CURSOR"
  80. );
  81. this.chartInstance.setOption(
  82. {
  83. series: [...otherSeries, this.harmonicCursorSeries],
  84. },
  85. {
  86. replaceMerge: ["series"],
  87. notMerge: false,
  88. }
  89. );
  90. },
  91. // 移除谐波光标
  92. removeHarmonicCursor() {
  93. const option = this.chartInstance.getOption();
  94. const series = option.series.filter((s) => s.id !== "HARMONIC_CURSOR");
  95. this.chartInstance.setOption({ series }, { replaceMerge: ["series"] });
  96. this.harmonicCursorPoints = [];
  97. },
  98. },
  99. };