echarts 鼠标悬停事件 点强调显示 折线图 showTip

这篇博客介绍了如何使用Echarts库,在鼠标悬停到横坐标7时,突出显示该点两个图标的纵坐标数据。通过监听'showTip'事件,动态更新markPoint数据并重新渲染图表,实现数据高亮效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1,效果图

在这里插入图片描述

2,描述

当鼠标停留在横坐标7的时候,着重显示横坐标7的两个图标的纵坐标数据

3,代码


  <div class="layui-col-md6 bl">
      <div id="main1" class="biao"></div>
  </div>
                    

var option1 = {
          color: ["#3fb1e3", "#6be6c1", "#626c91", "#a0a7e6", "#c4ebad", "#96dee8"],
          tooltip: {
              trigger: 'axis',
              backgroundColor: '#fff',
              textStyle: {
                  color: '#333',
                  align: 'left',
              },
              axisPointer: {
                  lineStyle: {
                      color: {
                          type: 'linear',
                          x: 0,
                          y: 0,
                          x2: 0,
                          y2: 1,
                          colorStops: [{
                              offset: 0,
                              color: '#333'
                          }, {
                              offset: 0.5,
                              color: 'rgba(255, 255, 255,1)',
                          }, {
                              offset: 1,
                              color: 'rgba(0, 255, 233,0)'
                          }],
                          global: false
                      }
                  },
              },
              formatter: function(arg) {

              }
          },
          title: {
              text: '月度数据上报',
              x: 'center',
              textStyle: {
                  fontSize: 16,
                  fontWeight: 'bolder',
                  color: '#333'          // 主标题文字颜色
              },
          },
          grid: {
              left: '4%',
              right: '8%',
              bottom: '10%',
              top: '20%',
              containLabel: true,
          },
          legend: {
              show: true,
              x: 'right',
              top: '7%',
              y: '35',
              textStyle: {
                  color: '#1bb4f6',
              },
              data: ['上报数', '填报数'],
          },
          xAxis: [
              {
                  name: '日',
                  type: 'category',
                  boundaryGap: false,
                  axisLabel: {
                      color: '#666',
                  },
                  axisLine: {
                      show: true,
                      lineStyle: {
                          color: '#efefef',
                      },
                  },
                  axisTick: {
                      show: false,
                  },
                  splitLine: {
                      show: true,
                      lineStyle: {
                          color: '#efefef',
                      },
                  },
                  data: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12','13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31'],
              },
          ],
          yAxis: [
              {
                  type: 'value',
                  min: 0,
                  max: 1000,
                  axisLabel: {
                      formatter: '{value}',
                      textStyle: {
                          color: '#666',
                      },
                  },
                  axisLine: {
                      lineStyle: {
                          color: '#efefef',
                      },
                  },
                  axisTick: {
                      show: false,
                  },
                  splitLine: {
                      show: true,
                      lineStyle: {
                          color: '#efefef',
                      },
                  },
              },
          ],
          series: [
              {
                  name: '上报数',
                  type: 'line',
                  stack: 'Total',
                  areaStyle: {},
                  symbolSize: 8,
                  itemStyle: {
                      normal: {
                          lineStyle: {
                              color: '#0092f6',
                              width: 1,
                          },
                          areaStyle: {
                              color: '#3fb1e3'

                          },
                      },
                  },
                  markPoint: {
                      data: [
                          // { xAxis: '1', yAxis: '100',name: 'num',value:100},
                      ]
                  },
                  data:sbmdata,
              },

              {
                  name: '填报数',
                  type: 'line',
                  stack: 'Total',
                  areaStyle: {},
                  symbolSize: 8,
                  itemStyle: {
                      normal: {
                          lineStyle: {
                              color: '#6FE81A',
                              width: 1,
                          },
                          areaStyle: {
                              color: '#6be6c1'
                          },
                      },
                  },
                  markPoint: {
                      data: [
                              // { xAxis: '1', yAxis: '0',name: 'num',value:0},
                          ]
                  },
                  data:tbmdata,
              },
          ],
     };


//初始化渲染加载数据
   var myChart1 = echarts.init(document.getElementById('main1'));
    myChart1.setOption(option1);
    

//操作实现的核心代码
//showTip显示提示框操作
    myChart1.on('showTip',function (params) {
        // console.log(JSON.stringify(params))
        option1.series[0].markPoint.data.splice(0,option1.series[0].markPoint.data.length);//清空数组
        option1.series[1].markPoint.data.splice(0,option1.series[1].markPoint.data.length);//清空数组
        xindex=params.dataIndex
        var sbdata=sbmdata[params.dataIndex]
        option1.series[0].markPoint.data.push({ xAxis: xindex, yAxis: sbdata.value, name: 'num',value: sbdata.value});
        var tbdata=tbmdata[params.dataIndex]
        option1.series[1].markPoint.data.push({ xAxis: xindex, yAxis: tbdata.value, name: 'num',value: tbdata.value});

        //重新渲染
        echarts.init(document.getElementById('main1')).setOption(option1);
    });
<think>好的,我现在需要解决用户的问题:在ECharts中让指示器的十字准心固定在折线的数据上。首先,我得回忆一下ECharts的相关配置。根据用户提供的引用内容,特别是引用[3]提到了axisPointer的配置,这可能与坐标轴指示器有关。 用户希望十字准心始终固定在数据上,而不是鼠标滑过时才显示。通常,ECharts的tooltip触发方式有'item'和'axis'两种,但默认都是鼠标悬停触发。要实现固定显示,可能需要手动触发tooltip并保持其状态,或者寻找其他配置项。 查阅ECharts文档,axisPointer的show属性可以控制是否显示坐标轴指示器。但是默认情况下,它只在触发时显示。有没有办法让它一直显示?可能需要设置triggerOn为某个值,比如引用[3]提到alwaysShowContent,但默认是false。或许可以设置triggerOn为 'none',然后手动触发? 另外,用户提到固定到数据上,可能需要获取数据的位置,然后设置axisPointer的坐标。但ECharts是否有动态更新的接口呢?比如使用dispatchAction来触发highlight和showTip。 可能需要结合多个配置:设置axisPointer的show为true,同时使用dispatchAction在图表初始化后自动触发tooltip显示,并定时更新位置,跟随数据。不过这样可能会有性能问题,或者不够精确。 再考虑数据吸附功能,引用[1]提到鼠标滑过数据自动吸附,或许可以结合snap到数据,然后通过事件监听获取当前数据位置,再用axisPointer的坐标来固定。不过如何保持常显呢? 可能需要组合使用tooltip的alwaysShowContent为true,并设置triggerOn为'click'或者其他方式,然后通过JavaScript代码控制其显示。或者,使用自定义的十字准心,通过图形元素绘制,但这样可能不够灵活。 总结可能的步骤:1. 配置axisPointer显示;2. 设置tooltip的触发方式和吸附;3. 使用dispatchAction在加载后触发显示,并可能使用定时器更新位置。但如何固定在某个数据?比如固定在最后一个,或者跟随某个交互事件? 用户的需求是“始终固定”,可能需要指定具体的数据,比如最新数据。假设用户想实时更新十字线到最新,可能需要结合setOption和dispatchAction动态更新。例如,每次数据更新时,调用myChart.dispatchAction来移动axisPointer的位置。 但ECharts是否支持直接设置axisPointer的坐标?查看文档,axisPointer的value属性可以设置坐标轴上的值,比如xAxis的value。因此,可以设置xAxis.axisPointer.value为数据的x值,并确保snap开启,这样十字线会吸附到最近的数据。 最后,需要编写代码示例,配置xAxis和yAxis的axisPointer,设置snap为true,triggerOn为'none',并通过dispatchAction触发显示。同时,可能需要监听数据变化,更新value的值,保持十字线固定。</think>### 实现方法分步说明 在 ECharts 中固定十字准心到折线数据,需结合 `axisPointer` 配置和交互事件控制。以下是具体步骤: --- #### 1. **基础配置:启用坐标轴指示器** 在 `xAxis` 和 `yAxis` 中配置 `axisPointer`,并启用吸附功能(`snap: true`),使指示器自动吸附到最近的数据: ```javascript xAxis: { type: 'category', axisPointer: { show: true, type: 'line', snap: true, // 自动吸附到数据 label: { show: false } } }, yAxis: { type: 'value', axisPointer: { show: true, type: 'line', snap: true, // 自动吸附到数据 label: { show: false } } } ``` --- #### 2. **固定显示十字准心** 通过 `tooltip.triggerOn: 'none'` 禁用默认的鼠标悬停触发,再手动调用 `dispatchAction` 触发指示器显示: ```javascript tooltip: { trigger: 'axis', triggerOn: 'none', // 关闭自动触发 axisPointer: { type: 'cross' } }, // 初始化后触发十字准心 myChart.dispatchAction({ type: 'showTip', x: dataX[0], // 初始位置(如第一个数据) y: dataY[0] }); ``` --- #### 3. **动态更新位置(可选)** 若需跟随数据变化(如实时数据),通过定时器或事件监听更新十字线位置: ```javascript // 示例:每2秒移动到下一个数据 let index = 0; setInterval(() => { index = (index + 1) % data.length; myChart.dispatchAction({ type: 'showTip', x: dataX[index], y: dataY[index] }); }, 2000); ``` --- #### 完整代码示例 ```javascript const dataX = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri']; const dataY = [150, 230, 224, 218, 135]; const option = { xAxis: { type: 'category', data: dataX, axisPointer: { show: true, type: 'line', snap: true } }, yAxis: { type: 'value', axisPointer: { show: true, type: 'line', snap: true } }, series: [{ type: 'line', data: dataY }], tooltip: { trigger: 'axis', triggerOn: 'none', axisPointer: { type: 'cross' } } }; const myChart = echarts.init(document.getElementById('chart')); myChart.setOption(option); // 初始触发 myChart.dispatchAction({ type: 'showTip', x: dataX[0], y: dataY[0] }); ``` --- ### § 1. **如何让 ECharts 十字准心跟随鼠标移动?** > 设置 `tooltip.triggerOn: 'mousemove'` 并启用 `axisPointer.snap` 实现动态吸附[^3]。 2. **如何自定义十字准心的样式?** > 通过 `axisPointer.lineStyle` 修改颜色、宽度等属性,例如: > ```javascript > axisPointer: { lineStyle: { color: '#FF0000', width: 2 } } > ``` 3. **ECharts 如何实现多折线图的十字准心同步?** > 在多个 `xAxis` 和 `yAxis` 中配置相同的 `axisPointer`,并通过事件监听统一更新位置[^2]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值