Programmatically open and close Chart.js tooltip

后端 未结 2 893
伪装坚强ぢ
伪装坚强ぢ 2021-02-09 10:41

Chart.js 2.2.1

Any idea how to trigger the code that runs when I hover over a datapoint, and that runs when I move the mouse off? I need to programmaticall

2条回答
  •  余生分开走
    2021-02-09 11:01

    I would be careful accessing/modifying private variables that begin with _. You may find yourself with unexpected behavior. Why not trigger the canvas mousemove event

      openToolTip: function(myChart, index) {
    
        var mouseMoveEvent, meta, point, rectangle, value;
    
        meta = myChart.getDatasetMeta(0);
        rectangle = myChart.canvas.getBoundingClientRect();
        point = meta.data[index].getCenterPoint();
    
        mouseMoveEvent = new MouseEvent('mousemove', {
          clientX: rectangle.left + point.x,
          clientY: rectangle.top + point.y
        });
    
        myChart.canvas.dispatchEvent(mouseMoveEvent);
    
      },
    

    To close the tooltip just trigger the mouseout event

      closeToolTip: function(myChart) {
    
        var mouseOutEvent = new MouseEvent('mouseout');
        return myChart.canvas.dispatchEvent(mouseOutEvent);
    
      }
    

提交回复
热议问题