echarts icon indicating copy to clipboard operation
echarts copied to clipboard

移动端 tooltip 手指抬起后自动消失这个有方法实现了吗

Open arssam opened this issue 9 years ago • 8 comments

One-line summary [问题简述]

移动端 tooltip 手指抬起后不会自动消失

Version & Environment [版本及环境]

  • ECharts version [ECharts 版本]: 3.6.2

Expected behaviour [期望结果]

移动端中,当手指抬起后,tooltip消失,另外用showTip手指触摸空白处不出tooltip,要点击到具体的图形才会出现tooltip,而且x,y的辅助线也没有出现。 default

default

ECharts option [ECharts配置项]

option = {
  animation: false,
        tooltip: {
          // trigger: 'axis',
          transitionDuration: 0,
          triggerOn: 'none',
          confine: true,
          textStyle: {
            fontSize: 10,
          },
          formatter(params) {
            // console.log(params);
            if (params instanceof Array &&
            ((params.filter(v => v.seriesName === 'K线').length === 0) ||
            (params.filter(v => v.seriesName === 'K线')[0].data.length !== 8))) return '';
            const today = new Date();
            const weekday = ['', '一', '二', '三', '四', '五', ''];
            let day = '';
            let aopen = 0;
            let ahigh = 0;
            let alow = 0;
            let aclose = 0;
            let achange = 0;
            let avolum = 0;
            if (params instanceof Array) {
              day = params[0].name;
              aopen = params.filter(v => v.seriesName === 'K线')[0].data[1].toFixed(2) || '-';
              ahigh = params.filter(v => v.seriesName === 'K线')[0].data[2].toFixed(2) || '-';
              alow = params.filter(v => v.seriesName === 'K线')[0].data[3].toFixed(2) || '-';
              aclose = params.filter(v => v.seriesName === 'K线')[0].data[4].toFixed(2) || '-';
              achange = params.filter(v => v.seriesName === 'K线')[0].data[6].toFixed(2) || '-';
              const tmp = params.filter(v => v.seriesName === 'K线')[0].data[7];
              avolum = volumeUnit(tmp) || '-';
            } else {
              day = params.name;
              let tmp = 0;
              if (params.data instanceof Array) {
                aopen = params.data[1].toFixed(2) || '-';
                ahigh = params.data[2].toFixed(2) || '-';
                alow = params.data[3].toFixed(2) || '-';
                aclose = params.data[4].toFixed(2) || '-';
                achange = params.data[6].toFixed(2) || '-';
                tmp = params.data[7];
                avolum = volumeUnit(tmp) || '-';
              }
              if (typeof params.data === 'number') {
                tmp = params.data;
                avolum = volumeUnit(tmp) || '-';
                return `<span>${day}/星期${weekday[today.getDay()]}<br />
                成交量: <span>${avolum}</span></span>`;
              }
            }
            function getMA(val) {
              if (params.dataIndex || params.dataIndex === 0) {
                return val[params.dataIndex];
              }
              return val[params[0].dataIndex];
            }
            that.MA5 = getMA(dataMA5);
            that.MA10 = getMA(dataMA10);
            that.MA20 = getMA(dataMA20);
            const res = `<span>${day}/星期${weekday[(new Date(day)).getDay()]}<br />
            开盘: <span>${aopen}</span><br />
            收盘: <span>${ahigh}</span><br />
            最高: <span>${alow}</span><br />
            最低: <span>${aclose}</span><br />
            涨跌幅: <span>${achange}%</span><br />
            成交量: <span>${avolum}</span><br />
            </span>`;
            return res;
          }
        },
        axisPointer: {
          link: { xAxisIndex: 'all' },
          label: {
            margin: 0,
            backgroundColor: '#777'
          }
        },
        dataZoom: [{
          type: 'inside',
          xAxisIndex: [0, 1, 2],
          startValue: startVal,
          endValue: endVal,
          minValueSpan: large, // 限制窗口大小的最小值
        }],
        xAxis: [{
          type: 'category',
          data: dates,
          scale: true,
          boundaryGap: ['20%', '20%'],
          axisLine: {
            lineStyle: {
              color: '#e0e1e6'
            }
          },
          axisLabel: {
            show: false
          },
          axisTick: {
            show: false
          },
          min: 'dataMin',
          max: 'dataMax',
          axisPointer: {
            show: true,
            label: {
              show: false,
            },
          }
        }, {
          // type: 'category',
          gridIndex: 0,
          boundaryGap: ['20%', '20%'],
          splitLine: { show: false },
          axisLabel: { show: false },
          axisTick: { show: false },
          axisLine: {
            lineStyle: {
              color: '#e0e1e6'
            }
          },
        }, {
          type: 'category',
          gridIndex: 1,
          data: dates,
          scale: true,
          boundaryGap: ['20%', '20%'],
          splitLine: {
            show: false
          },
          axisLabel: {
            show: false
          },
          axisTick: {
            show: false
          },
          axisLine: {
            lineStyle: {
              color: '#e0e1e6'
            }
          },
          min: 'dataMin',
          max: 'dataMax',
          axisPointer: {
            show: true,
            label: {
              show: false,
            },
          }
        }],
        yAxis: [{
          scale: true,
          splitNumber: 1,
          min: 'dataMin',
          max: 'dataMax',
          axisLine: {
            lineStyle: {
              color: '#e0e1e6'
            }
          },
          splitLine: {
            show: false,
            lineStyle: {
              color: '#e0e1e6'
            }
          },
          axisTick: {
            show: false
          },
          axisLabel: {
            interval: 0,
            margin: 0,
            inside: true,
            showMinLabel: true,
            showMaxLabel: true,
            formatter(val, index) {
              if (index === 1) {
                return '';
              } else if (index === 2) {
                return `\n${val}`;
              }
              return `${val}\n`;
            },
            textStyle: {
              color: '#505050',
            }
          },
          axisPointer: { // 显示坐标轴的数值
            show: true,
            label: {
              formatter(params) {
                return params.value.toFixed(2);
              }
            }
          },
        }, {
          gridIndex: 0,
          scale: true,
          splitLine: { show: false },
          axisLabel: { show: false },
          axisTick: { show: false },
          axisLine: {
            lineStyle: {
              color: '#e0e1e6'
            }
          },
        }, {
          scale: true,
          gridIndex: 1,
          splitNumber: 1,
          max: 'dataMax',
          axisLabel: {
            margin: 0,
            inside: true,
            formatter(val, index) {
              if (index === 2 || val >= volumnsMax) {
                return volumeUnit(val);
              }
              return '';
            },
            textStyle: {
              color: '#505050',
            }
          },
          axisLine: {
            lineStyle: {
              color: '#e0e1e6'
            },
          },
          axisTick: {
            show: false
          },
          splitLine: {
            show: false
          },
          axisPointer: { // 显示坐标轴的数值
            show: true,
            label: {
              formatter(params) {
                return volumeUnit(params.value) || '-';
              }
            }
          },
        }],
        grid: [{
          left: 16,
          right: 16,
          top: 0,
          height: 172
        }, {
          left: 16,
          right: 16,
          height: 44,
          top: 196
        }],
        series: [{
          type: 'candlestick',
          name: 'K线',
          data,
          // barMaxWidth: '30%',
          itemStyle: {
            normal: {
              color: '#eb333b',
              color0: '#1dad51',
              borderColor: '#eb333b',
              borderColor0: '#1dad51'
            },
          }
        }, {
          name: 'MA5',
          z: 10,
          type: 'line',
          symbol: 'none',
          data: dataMA5,
          smooth: true,
          showSymbol: false,
          lineStyle: {
            normal: {
              width: 1,
              color: '#fc5c00'
            }
          }
        }, {
          name: 'MA10',
          z: 10,
          type: 'line',
          symbol: 'none',
          data: dataMA10,
          smooth: true,
          showSymbol: false,
          lineStyle: {
            normal: {
              width: 1,
              color: '#ece013'
            }
          }
        }, {
          name: 'MA20',
          z: 10,
          type: 'line',
          symbol: 'none',
          data: dataMA20,
          smooth: true,
          showSymbol: false,
          lineStyle: {
            normal: {
              width: 1,
              color: '#346fd6'
            }
          }
        }, {
          name: 'Volumn',
          type: 'bar',
          barWidth: '52%',
          // barMaxWidth: '30%',
          xAxisIndex: 2,
          yAxisIndex: 2,
          itemStyle: {
            normal: {
              color(params) {
                return barColor[params.dataIndex];
              },
            }
          },
          data: volumns
        }]
}
 // 绘制图表
      const chart = echarts.init(this.$refs.charts);
      chart.setOption(option);
      chart.on('mousemove', (params) => {
        const index = params.dataIndex;
        console.log(params);
        chart.dispatchAction({
          type: 'showTip',
          seriesIndex: 0,
          dataIndex: index
          // x: 270,
          // y: 170,
        });
      });
      chart.on('mouseout', () => {
        chart.dispatchAction({
          type: 'hideTip',
        });
      });

Other comments [其他信息]

arssam avatar Jul 06 '17 12:07 arssam

用showTip手指触摸空白处不出tooltip,要点击到具体的图形才会出现tooltip

可监听 chart.getZr().on('mousemove', function () { ... }); 则任何地方都可以触发事件。

当手指抬起后,tooltip消失

可监听 chart.getZr().on('mouseout', function () { ... }); 则任何地方都可以触发事件。

而且x,y的辅助线也没有出现

须设置:tooltip: {trigger: 'axis'} 才能出现辅助线

100pah avatar Jul 07 '17 05:07 100pah

感谢@100pah 可是getZr(),好像获取不了dataIndex,截图网络原因传不上来。

还有个问题是dataZoom的单指左右滑动平移能取消吗,因为k线图需要单指选择触摸具体的x轴,这个时候平移会影响用户体验,所以只需要双指缩放即可。

arssam avatar Jul 07 '17 09:07 arssam

解决了,datazoom添加moveOnMouseMove: false,就行了

arssam avatar Jul 08 '17 07:07 arssam

@pissang getZr()能获取到dataIndex属性吗,我做k线的需要点击空白处获取k线的数据,或者有什么其他解决方案也可以,拜托了!

arssam avatar Jul 13 '17 03:07 arssam

@arssam getZr() 的事件,本身不会有 dataIndex 的信息。

但是可以用 chart.convertFromPixel 来得到当前点击的点对应的 x 轴的刻度,从而能计算得到最近的 dataIndex。

100pah avatar Jul 13 '17 04:07 100pah

@100pah 谢谢,这个问题解决了

arssam avatar Jul 13 '17 11:07 arssam

解决了,datazoom添加moveOnMouseMove: false,就行了

移动端 tooltip 手指抬起后不会自动消失 你好,移动端手指离开 ,tooltip 不会自动消失好像没用

hugeorange avatar Mar 11 '20 10:03 hugeorange

手指抬起后 如何将辅助线,也一起关闭呢,使用 以下方法,在tooltip隐藏后,辅助线没有隐藏

this.chart.dispatchAction({
        type: "hideTip"
      });

soeasyjx avatar Mar 25 '24 01:03 soeasyjx