Bdgb.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. export default {
  2. data() {
  3. return {
  4. // 边带光标
  5. sidebandCursorVisible: false,
  6. sidebandCursorPoints: [],
  7. sidebandCursorSeries: null,
  8. };
  9. },
  10. mounted() {},
  11. beforeDestroy() {},
  12. methods: {
  13. // 边带光标
  14. enableSidebandCursor() {
  15. this.sidebandCursorVisible = true;
  16. // 添加鼠标移动事件监听
  17. this.chartInstance.getZr().on("mousemove", this.handleSidebandCursorMove);
  18. },
  19. disableSidebandCursor() {
  20. this.sidebandCursorVisible = false;
  21. // 移除鼠标移动事件监听
  22. this.chartInstance
  23. .getZr()
  24. .off("mousemove", this.handleSidebandCursorMove);
  25. // 移除边带光标
  26. this.removeSidebandCursor();
  27. },
  28. handleSidebandCursorMove(event) {
  29. if (!this.sidebandCursorVisible) return;
  30. // 获取鼠标位置对应的X值
  31. const pointInGrid = this.chartInstance.convertFromPixel(
  32. { seriesIndex: 0 },
  33. [event.offsetX, event.offsetY]
  34. );
  35. const xValue = pointInGrid[0];
  36. // 生成3个光标点(中心、左、右)
  37. this.sidebandCursorPoints = [
  38. { xAxis: xValue - 5, val: (xValue - 5).toFixed(2) }, // 左边带
  39. { xAxis: xValue, val: xValue.toFixed(2) }, // 中心
  40. { xAxis: xValue + 5, val: (xValue + 5).toFixed(2) }, // 右边带
  41. ];
  42. this.updateSidebandCursor();
  43. },
  44. updateSidebandCursor() {
  45. if (this.sidebandCursorPoints.length < 3) return;
  46. // 获取当前图表选项
  47. const option = this.chartInstance.getOption();
  48. // 创建新的系列配置
  49. this.sidebandCursorSeries = {
  50. type: "line",
  51. id: "SIDEBAND_CURSOR",
  52. silent: true, // 禁止系列响应事件
  53. markLine: {
  54. silent: true,
  55. lineStyle: {
  56. color: "red",
  57. type: "dashed",
  58. width: 1,
  59. },
  60. symbol: ["none", "none"],
  61. label: {
  62. show: true,
  63. position: "start",
  64. formatter: (params) => params.data.val,
  65. color: "red",
  66. },
  67. data: this.sidebandCursorPoints.map((point) => ({
  68. xAxis: point.xAxis,
  69. val: point.val,
  70. })),
  71. },
  72. markArea: {
  73. silent: true,
  74. itemStyle: {
  75. color: "rgba(255, 0, 0, 0.2)", // 红色半透明背景
  76. },
  77. data: [
  78. // 左侧区域(左线到中线)
  79. [
  80. {
  81. xAxis: this.sidebandCursorPoints[0].xAxis,
  82. yAxis: "min", // 从Y轴最小值开始
  83. },
  84. {
  85. xAxis: this.sidebandCursorPoints[1].xAxis,
  86. yAxis: "max", // 到Y轴最大值结束
  87. },
  88. ],
  89. // 右侧区域(中线到右线)
  90. [
  91. {
  92. xAxis: this.sidebandCursorPoints[1].xAxis,
  93. yAxis: "min",
  94. },
  95. {
  96. xAxis: this.sidebandCursorPoints[2].xAxis,
  97. yAxis: "max",
  98. },
  99. ],
  100. ],
  101. },
  102. };
  103. // 更新图表(保留原有系列)
  104. const otherSeries = option.series.filter(
  105. (s) => s.id !== "SIDEBAND_CURSOR"
  106. );
  107. this.chartInstance.setOption(
  108. {
  109. series: [...otherSeries, this.sidebandCursorSeries],
  110. },
  111. {
  112. replaceMerge: ["series"], // 关键配置:只替换series
  113. notMerge: false,
  114. }
  115. );
  116. },
  117. removeSidebandCursor() {
  118. // 获取当前选项
  119. const option = this.chartInstance.getOption();
  120. // 过滤掉边带光标系列
  121. const series = option.series.filter((s) => s.id !== "SIDEBAND_CURSOR");
  122. // 更新图表
  123. this.chartInstance.setOption(
  124. {
  125. series: series,
  126. },
  127. { replaceMerge: "series" }
  128. );
  129. this.sidebandCursorPoints = [];
  130. },
  131. },
  132. };