| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- export default {
- data() {
- return {
- // 添加光标
- cursorPoints: [], // 存储参考点数据
- };
- },
- mounted() {},
- beforeDestroy() {},
- methods: {
- handleDoubleClick(event) {
- if (!this.checkedGB.includes("添加光标")) return;
- if (!this.chartInstance) return;
- const xData = this.spectrumList?.x || this.timeList?.x;
- const yData = this.spectrumList?.y || this.timeList?.y;
- if (!xData || !yData) return;
- const pointInGrid = this.chartInstance.convertFromPixel(
- { seriesIndex: 0 },
- [event.offsetX, event.offsetY],
- );
- const xValue = pointInGrid?.[0];
- if (xValue == null) return;
- const closestIndex = this.findClosestIndex(xValue, xData);
- const yValue = yData[closestIndex];
- this.cursorPoints.push({
- xAxis: xData[closestIndex],
- yAxis: Number(yValue).toFixed(2),
- val: Number(yValue).toFixed(2),
- });
- this.updateCursorElements();
- },
- // 找到最接近的X轴索引
- findClosestIndex(xValue) {
- if (!this.envelopeList.x) return 0;
- let minDiff = Infinity;
- let closestIndex = 0;
- this.envelopeList.x.forEach((x, index) => {
- const diff = Math.abs(x - xValue);
- if (diff < minDiff) {
- minDiff = diff;
- closestIndex = index;
- }
- });
- return closestIndex;
- },
- updateCursorElements() {
- if (!this.chartInstance) return;
- // 获取当前图表选项
- const currentOption = this.chartInstance.getOption();
- // 主数据系列
- const mainSeries = currentOption.series[0];
- // 特征值系列(过滤掉已有的光标系列)
- const featureSeries = currentOption.series.filter(
- (s) => s.markLine && !s.id?.startsWith("CURSOR_"),
- );
- // 参考线配置(垂直线)
- const cursorLineSeries = {
- id: "CURSOR_LINE_SERIES",
- type: "line",
- markLine: {
- silent: true,
- lineStyle: {
- color: "#FF0000",
- type: "dashed",
- width: 1,
- },
- symbol: ["none", "none"],
- label: {
- show: true,
- formatter: (params) => params.data.val || "",
- },
- data: this.cursorPoints.map((point) => ({
- xAxis: point.xAxis,
- val: point.val,
- })),
- },
- };
- // 参考点配置(实际数据点位置)
- const cursorPointSeries = {
- id: "CURSOR_POINT_SERIES",
- type: "scatter",
- symbol: "circle",
- symbolSize: 8,
- itemStyle: { color: "#FF0000" },
- data: this.cursorPoints.map((point) => ({
- value: [point.xAxis, point.yAxis],
- name: point.val,
- })),
- label: {
- show: true,
- formatter: "{@[1]}",
- position: "top",
- color: "#FF0000",
- fontSize: 12,
- backgroundColor: "rgba(255,255,255,0.7)",
- padding: [2, 4],
- borderRadius: 3,
- },
- };
- // 设置新选项
- this.chartInstance.setOption(
- {
- series: [
- mainSeries,
- ...featureSeries,
- {
- id: "CURSOR_LINE_SERIES",
- ...cursorLineSeries,
- },
- {
- id: "CURSOR_POINT_SERIES",
- ...cursorPointSeries,
- },
- ],
- },
- { replaceMerge: ["series"] },
- );
- },
- },
- };
|