简体   繁体   English

Google Charts - 工具提示中的完整html

[英]Google Charts - full html in tooltips

I am trying to make Google Charts Display custom tooltips with full HTML in them. 我正在尝试制作带有完整HTML的Google Charts Display自定义工具提示。

I know how to enable tooltips and pass appropriate data - the problem is - even when allowHTML option is enabled, the tooltips are rendered as plain text, so for example I can't show a picture in the tooltip. 我知道如何启用工具提示并传递适当的数据 - 问题是 - 即使启用了allowHTML选项,工具提示也会呈现为纯文本,因此例如我无法在工具提示中显示图片。

Here is a little example of what I am going for: 这是我想要的一个小例子:

What I have now: 我现在拥有的: 工具提示中的纯文本

What I want: 我想要的是: 工具提示中的图片

One way to solve this problem is to disable tooltips, capture onmouseover events and use another library (like cluetip) to display tooltips at cursor, but I was wondering if there is a cleaner, native way to enable this kind of functionality in Google Charts. 解决此问题的一种方法是禁用工具提示,捕获onmouseover事件并使用另一个库(如cluetip)在光标处显示工具提示,但我想知道是否有更简洁,本机的方式在Google Charts中启用此类功能。

Also please check out my other question about images as point markers in google charts. 另外,请查看我在谷歌图表中关于图像作为点标记的其他问题。

Edit: 编辑:

In the meantime I found a very good and quite inexpensive (60$ per website license) library that covers this functionality : Highcharts library 与此同时,我发现了一个非常好且相当便宜(每个网站许可证60美元)的库,它涵盖了这个功能: Highcharts库

As you can see in the example it is possible to pass a function that will format the tooltips - easily enough we could add a special property to each datapoint containig an url that could be used to dynammically load the tooltips content. 正如您在示例中所看到的,可以传递一个将格式化工具提示的函数 - 我们可以很容易地为每个数据点添加一个特殊属性,包含一个可用于动态加载工具提示内容的URL。 The tooltips can then be cached by adding an extra property to each data point in a serie. 然后可以通过向系列中的每个数据点添加额外属性来缓存工具提示。 I've implemented it this way and it works perfectly. 我已经用这种方式实现了它并且它完美地工作。

Hope the latest edit will help someone. 希望最新的编辑能帮助某人。

Google gives an example here . 谷歌给出了一个例子在这里

You will need to designate the column to be html tooltip: 您需要将列指定为html工具提示:

data.addColumn({'type': 'string', 'role': 'tooltip', 'p': {'html': true}});

You will also need to pass the correct option to your chart: 您还需要将正确的选项传递给图表:

tooltip: {isHtml: true}

I have tested this with line chart, and it works. 我用折线图对它进行了测试,它确实有效。

Edit: It also looks like (at least for line chart) you have to use the chart wrapper for this to work. 编辑:它看起来(至少对于折线图)你必须使用图表包装器才能工作。 I couldn't get it to obey the options without the wrapper. 没有包装器,我无法让它服从选项。

This feature was incredibly obscure! 这个功能令人难以置信地模糊不清! The key for me, as noted earlier, was adding ", 'p': {'html': true}" when using 'addColumn()' to define a tooltip; 如前所述,对我来说,关键是在使用'addColumn()'来定义工具提示时添加“,'p':{'html':true}”; the 'draw()' option 'tooltip: {isHtml: true}' alone is not enough. 仅仅'draw()'选项'tooltip:{isHtml:true}'是不够的。

It's very handy to use a function call to return the HTML string when creating the array of 'graphItems' that gets passed to 'addRows()': 在创建传递给'addRows()'的'graphItems'数组时,使用函数调用返回HTML字符串非常方便:

function myToolTip(row) {
    return '<div style="white-space: nowrap; padding:5px;"><b>' + row.name + '</b><br>' +
        '$' + row.worth + ' B, <b>' + _scope.sortBy + '</b>: ' + row[_scope.sortBy] + '</div>';
}

var graphItems = [];
angular.forEach(_scope.ForbesList, function(row, key) {
    graphItems.push([key, row.worth, myToolTip(row)]);
});

var data = new google.visualization.DataTable();
data.addColumn('number', 'Rank');
data.addColumn('number', 'Worth');
data.addColumn({type: 'string', role: 'tooltip', 'p': {'html': true}});
data.addRows(graphItems);

More here: https://developers.google.com/chart/interactive/docs/customizing_tooltip_content#custom_html_content 更多信息: https//developers.google.com/chart/interactive/docs/customizing_tooltip_content#custom_html_content

This is currently working for me: 目前这对我有用:

Step 1: be certain BOTH of these settings are in your chart options: 第1步:确定这些设置在您的图表选项中:

// This line makes the entire category's tooltip active.
focusTarget: 'category',
// Use an HTML tooltip.
tooltip: { isHtml: true }

Step 2: add your tooltip one of the chart columns: 第2步:添加工具提示其中一个图表列:

dataTable.addColumn({'type': 'string', 'role': 'tooltip' , 'p': {'html': true}});

Step 3: In your data, add the HTML for your tooltip at the appropriate column: 第3步:在您的数据中,在相应的列中添加工具提示的HTML:

chartReading.push(chartSensorTooltip(obsDate, reading[1]));

The method "chartSensorTooltip( date, number)" is a JS method I wrote myself to properly format the HTML for each values' tool tip. 方法“chartSensorTooltip(date,number)”是我自己编写的JS方法,用于为每个值的工具提示正确格式化HTML。 You don't need to write this sort of method, but as someone above suggested, doing so makes it very easy to format all the tooltips the same way. 您不需要编写这种方法,但正如上面提到的那样,这样做可以很容易地以相同的方式格式化所有工具提示。

The FocusTarget thing is what tripped me up. FocusTarget是让我失望的原因。 HTH someone! HTH有人!

我不得不将{p: {html: true}}到每个行数据而不是列,以及整个图表的isHtml选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM