[英]Google charts add tooltip
I would like to add some data on tooltips in my chart but I can't find a way to do it. 我想在我的图表中添加一些关于工具提示的数据,但我找不到办法。 I tried to add role: tooltip but it didn't work
我试图添加角色:工具提示,但它没有用
this is my code: 这是我的代码:
google.charts.load('current', {
callback: function () {
var cont = 1;
var rowtbl = document.getElementById("tabella").rows.length;
rowtbl = rowtbl - 1;
// use object notation for column headings
var data = new google.visualization.arrayToDataTable([
[{type: 'string', label: ''}, {type: 'number', label: 'Tempo in minuti: '}, {type: 'string', role: 'tooltip'}]
]);
//number rows table
while(cont <= rowtbl){
var nomi;
var qnt;
var time;
nomi = document.getElementById("tabella").rows[cont].cells[0].innerHTML;
time = document.getElementById("tabella").rows[cont].cells[3].innerHTML;
qnt = document.getElementById("tabella").rows[cont].cells[1].innerHTML;
var info = {
name: nomi,
tempo: time,
qn: qnt,
};
// add row to google data
data.addRow([
info.name,
parseFloat(info.tempo),
info.qn
]);
cont = cont +1;
}
var options = {
tooltip: {isHtml: true},
legend: { position: 'none' },
bar: { groupWidth: "80%" }
};
var chart = new google.charts.Bar(document.getElementById('top_x_div'));
chart.draw(data, google.charts.Bar.convertOptions(options));
},
packages: ['bar']
});
I want qnt = document.getElementById("tabella").rows[cont].cells[1].innerHTML;
我想要
qnt = document.getElementById("tabella").rows[cont].cells[1].innerHTML;
to be show in the tooltip. 要在工具提示中显示。 How do I need to change my code?
我如何更改代码?
Thank you very much guys! 非常感谢你们!
EDIT 编辑
I would like my tooltip to look like this, is this possible? 我希望我的工具提示看起来像这样,这可能吗?
need to change column defintion to allow html 需要更改列定义以允许html
from: {type: 'string', role: 'tooltip'}
from:
{type: 'string', role: 'tooltip'}
to: {type: 'string', role: 'tooltip', p: {html: true}}
to:
{type: 'string', role: 'tooltip', p: {html: true}}
EDIT 编辑
Columns Roles such as 'tooltip'
and 'style'
do not work on Material charts. 列
'tooltip'
和'style'
等字体在“ 材质”图表上不起作用。
you can use a Core chart, with the option -- theme = 'material'
-- to get the look and feel close 您可以使用核心图表,选项 -
theme = 'material'
- 以获得外观和感觉
the following snippet draws both charts, the tooltips work on the bottom, Core chart 下面的代码片段绘制了两个图表,工具提示工作在底部, 核心图表
google.charts.load('current', { callback: function () { var cont = 1; var rowtbl = document.getElementById("tabella").rows.length; rowtbl = rowtbl - 1; var data = new google.visualization.arrayToDataTable([ [{type: 'string', label: ''}, {type: 'number', label: 'Tempo in minuti: '}, {type: 'string', role: 'tooltip', p: {html: true}}] ]); //number rows table while(cont <= rowtbl){ var nomi; var qnt; var time; nomi = document.getElementById("tabella").rows[cont].cells[0].innerHTML; time = document.getElementById("tabella").rows[cont].cells[3].innerHTML; qnt = document.getElementById("tabella").rows[cont].cells[1].innerHTML; var tooltip; tooltip = '<div class="ggl-tooltip">'; tooltip += '<div><span class="ggl-tooltip-title">' + nomi + '</span></div>'; tooltip += '<div><span class="ggl-tooltip-category">Tempo in minuti:</span></div>'; tooltip += '<div><span class="ggl-tooltip-time">' + time + '</span></div>'; tooltip += '<div><span class="ggl-tooltip-note">' + qnt + '</span></div>'; tooltip += '</div>'; data.addRow([ nomi, parseFloat(time), tooltip ]); cont = cont +1; } var options = { bar: {groupWidth: '80%'}, chartArea: {width: '90%'}, height: 400, legend: {position: 'none'}, theme: 'material', tooltip: {isHtml: true}, width: '100%' }; var chart = new google.visualization.ColumnChart(document.getElementById('top_x_div_core')); chart.draw(data, options); }, packages: ['corechart'] });
.ggl-tooltip { border: 1px solid #E0E0E0; font-family: Arial, Helvetica; font-size: 12pt; padding: 12px 12px 12px 12px; } .ggl-tooltip div { padding: 6px 6px 6px 6px; } .ggl-tooltip-title { color: #000000; font-weight: bold; } .ggl-tooltip-category { color: #676767; } .ggl-tooltip-time { color: #1565C0; font-size: 20pt; } .ggl-tooltip-note { color: #F44336; }
<script src="https://www.gstatic.com/charts/loader.js"></script> <table id="tabella"> <tr> <th> nomi </th> <th> qnt </th> <th> </th> <th> time </th> </tr> <tr> <td> utente </td> <td> Here value of qnt </td> <td> </td> <td> 40 </td> </tr> <tr> <td> nomi 2 </td> <td> tooltip for nomi 2 </td> <td> </td> <td> 2 </td> </tr> <tr> <td> nomi 3 </td> <td> tooltip for nomi 3 </td> <td> </td> <td> 3 </td> </tr> </table> <div id="top_x_div_core"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.