Google Charts/Visualization - dismiss tooltip on click away

后端 未结 3 706
暗喜
暗喜 2020-12-21 13:23

Using Google Charts (haven\'t migrated to the Material ones yet), one can make tooltips require a click by using the {trigger: \'selection\'} option. However, u

3条回答
  •  無奈伤痛
    2020-12-21 14:09

    You could attach a click event handler for the body element to clear the chart's selection as demonstrated below:

    Example

    google.setOnLoadCallback(drawChart);
    
    var chart;
    function drawChart() {
    
        var data = google.visualization.arrayToDataTable([
            ['Year', 'Fixations'],
            ['2015', 80],
            ['2016', 90],
            ['2017', 100],
            ['2018', 90],
            ['2019', 80], ]);
    
        var options = {
            tooltip: {
                isHtml: true,
                trigger: 'selection'
            },
            legend: {
                position: 'none'
            },
            bar: {
                groupWidth: '90%'
            },
            colors: ['#A61D4C'],
            enableInteractivity: true
        };
    
        chart = new google.visualization.ColumnChart(document.getElementById('tooltip_rotated'));
    
        chart.draw(data, options);
        addEvent(document.querySelector('body'),'click',clearSelection);
    }
    
    
    function clearSelection (e) {
        if (!document.querySelector('#tooltip_rotated').contains(e.srcElement)) {   
           chart.setSelection();
        }
    }
    
    
    function addEvent(element, evnt, funct){
      if (element.attachEvent)
       return element.attachEvent('on'+evnt, funct);
      else
       return element.addEventListener(evnt, funct, false);
    }
    
    

提交回复
热议问题