2017-09-25 29 views
0

我使用谷歌的柱形图和条形图https://developers.google.com/chart/interactive/docs/gallery/添加小,自定义图标,弹出窗口,让谷歌图表的标题

一切都很好,除了一两件事:我要新增一个小的“i”图标图表的标题。当我点击将鼠标移到它上面时,会出现一个自定义弹出窗口并显示一些信息。

让我们这个例子中https://developers.google.com/chart/interactive/docs/gallery/columnchart

var options = { 
    title: 'Motivation and Energy Level Throughout the Day', 

我想,我图标和弹出式将出现标题为“...日”

我结束之后要正确放置已经通过文档,但没有发现任何东西。有没有办法来实现这个?

P.S. 或者也许有一种方法可以得到类似的结果,也就是说,将i图标放在图表的其他位置?但它应该与整个图表有关。

+0

@WhiteHat,那对我来说不起作用,叠加是为了一些其他的目标 - >它总是可见的,就像一个水印 - >不是我所需要的。 – Jori

+0

@WhiteHat,并弹出? – Jori

+0

@WhiteHat,好的,但问题是:如何在鼠标悬停在i图标上时弹出窗口,无论它是何种类型的弹出窗口? – Jori

回答

0

您可以使用overlay放置图表

上的“信息”图标,然后使用CSS来显示一个“工具提示”当图标悬停

见下工作片断,
的叠加层放置图表的'ready'事件内

google.charts.load('current', { 
 
    packages: ['controls', 'corechart', 'table'] 
 
}).then(function() { 
 
    var data = new google.visualization.DataTable(); 
 
    data.addColumn('timeofday', 'Time of Day'); 
 
    data.addColumn('number', 'Motivation Level'); 
 
    data.addRows([ 
 
    [{v: [8, 0, 0], f: '8 am'}, 1], 
 
    [{v: [9, 0, 0], f: '9 am'}, 2], 
 
    [{v: [10, 0, 0], f:'10 am'}, 3], 
 
    [{v: [11, 0, 0], f: '11 am'}, 4], 
 
    [{v: [12, 0, 0], f: '12 pm'}, 5], 
 
    [{v: [13, 0, 0], f: '1 pm'}, 6], 
 
    [{v: [14, 0, 0], f: '2 pm'}, 7], 
 
    [{v: [15, 0, 0], f: '3 pm'}, 8], 
 
    [{v: [16, 0, 0], f: '4 pm'}, 9], 
 
    [{v: [17, 0, 0], f: '5 pm'}, 10], 
 
    ]); 
 

 
    var options = { 
 
    hAxis: { 
 
     title: 'Time of Day', 
 
     format: 'h:mm a', 
 
     viewWindow: { 
 
     min: [7, 30, 0], 
 
     max: [17, 30, 0] 
 
     } 
 
    }, 
 
    legend: { 
 
     alignment: 'end', 
 
     position: 'bottom' 
 
    }, 
 
    title: 'Motivation and Energy Level Throughout the Day', 
 
    vAxis: { 
 
     title: 'Rating (scale of 1-10)', 
 
     ticks: [ 
 
     {v: 8.5, f: 'A'}, 
 
     {v: 4.5, f: 'B'}, 
 
     {v: 2, f: 'C'} 
 
     ] 
 
    } 
 
    }; 
 

 
    var container = document.getElementById('chart'); 
 
    var chart = new google.visualization.ColumnChart(container); 
 

 
    google.visualization.events.addListener(chart, 'ready', function() { 
 
    var chartLayout = chart.getChartLayoutInterface(); 
 
    var chartPosition = $('#chart').position(); 
 
    var titleLeft = 24; 
 
    var titlePosition = chartLayout.getBoundingBox('title'); 
 
    $('#chart-overlay').css({ 
 
     left: (chartPosition.left + titlePosition.left - titleLeft) + 'px', 
 
     top: (chartPosition.top + titlePosition.top) + 'px' 
 
    }).removeClass('hidden'); 
 
    }); 
 

 
    chart.draw(data, options); 
 
});
.hidden { 
 
    display: none; 
 
    visibility: hidden; 
 
} 
 

 
.overlay { 
 
    position: absolute; 
 
    z-index: 1; 
 
} 
 

 
.overlay:hover .popup { 
 
    display: inline-block; 
 
} 
 

 
.popup { 
 
    background-color: #fff9c4; 
 
    border: 1px solid #ffd54f; 
 
    display: none; 
 
    padding: 6px; 
 
} 
 

 
.popup span { 
 
    font-weight: bold; 
 
}
<script src="https://www.gstatic.com/charts/loader.js"></script> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
 

 
<div class="hidden overlay" id="chart-overlay"> 
 
    <div> 
 
    <img alt="Information" src="http://findicons.com/files/icons/2166/oxygen/16/dialog_information.png" /> 
 
    </div> 
 
    <div class="popup"> 
 
    <div> 
 
     <span>Information:</span> 
 
    </div> 
 
    <div> 
 
     This is the information for the tooltip... 
 
    </div> 
 
    </div> 
 
</div> 
 

 
<div id="chart"></div>