简体   繁体   English

如何在 Google Geochart 图表的工具提示文本中添加新行?

[英]How do I add a new line to the tooltip text in a Google Geochart chart?

I'm using Google's GeoChart API to generate a map of the US.我正在使用 Google 的 GeoChart API 生成美国的 map。 I want to highlight the states based on one value, but then display some extra information about each state in the tool tip that displays when you hover over a state. I'd like the finished tool tip to look like this:我想根据一个值突出显示状态,然后在工具提示中显示有关每个 state 的一些额外信息,当您在 state 上显示 hover 时。我希望完成的工具提示看起来像这样:

Nevada
Relevance: 4 ( the data driving the state highlight )
Data
More Data

The map works great, and I can add any text I want to the tooltip using the PatternFormat() function, but it strips or ignores all the new line options I've tried: map 效果很好,我可以使用PatternFormat() function 在工具提示中添加我想要的任何文本,但它会删除或忽略我尝试过的所有新行选项:

var formatter = new google.visualization.PatternFormat('some data\nmore data');
formatter.format(data, [0], 1);

I've tried all of these new line options and none of them work: <br> , <br /> , &#13;我已经尝试了所有这些换行选项,但没有一个有效: <br><br />&#13; , &#10 , &#x0A , &#x0D , , &#10 , &#x0A , &#x0D ,, ,, \n , \r , \r\n , %0A , %0D , \n , \r , \r\n , %0A , %0D

Any suggestions on things to try or what actually works?关于要尝试的事情或实际有效的方法有什么建议吗? This seems possible in some of the other Google charts.这在其他一些谷歌图表中似乎是可能的。

The rendered HTML looks like this渲染后的 HTML 看起来像这样

<script type='text/javascript' src='https://www.google.com/jsapi'></script>

<script type='text/javascript'>

    google.load( 'visualization', '1', { 'packages': ['geochart'] } );
    google.setOnLoadCallback( drawRegionsMap );

    function drawRegionsMap()
    {

        var data = google.visualization.arrayToDataTable([
            [ 'State', 'Relevance' ],
            [ 'Arizona', 2 ],
            [ 'California', 4 ],
            [ 'Colorado', 1 ],
            [ 'Florida', 1 ],
            [ 'Georgia', 1 ],
            [ 'Idaho', 1 ],
            [ 'Illinois', 1 ],
            [ 'Indiana', 1 ],
            [ 'Iowa', 1 ],
            [ 'Kansas', 1 ],
            [ 'Kentucky', 1 ],
            [ 'Louisiana', 1 ],
            [ 'Maryland', 2 ],
            [ 'Montana', 1 ],
            [ 'Nevada', 2 ],
            [ 'New Mexico', 2 ],
            [ 'North Carolina', 1 ],
            [ 'North Dakota', 1 ],
            [ 'Oklahoma', 1 ],
            [ 'Oregon', 1 ],
            [ 'Pennsylvania', 1 ],
            [ 'South Carolina', 1 ],
            [ 'Tennessee', 1 ],
            [ 'Texas', 1 ],
            [ 'Utah', 2 ],
            [ 'Washington', 1 ],
            [ 'Wyoming', 1 ]
        ]);

        data.addRows([
            ['Has Distributor', 1],
            ['Sells Direct', 1]
        ]);

        var formatter = new google.visualization.PatternFormat('data <br> <br /> &#13; &#10 &#x0A &#x0D \u000A \u000D \n \r \r\n %0A %0D more data');
        formatter.format(data, [0], 1);

        var options =
        {
            width:      286,
            region:     'US',
            resolution: 'provinces',
            colorAxis:  { colors: ['#abdfab', '#006600'] },
            legend:     'none'
        };

        var chart = new google.visualization.GeoChart( document.getElementById( 'chart_div' ) );
        chart.draw( data, options );

    };

</script>

<div id="chart_div" style="width: 286px; height: 180px;"></div>

对于遇到此问题的任何人,您都可以在文档中的图表选项tooltip: {isHtml: true}

Take a look at using data table roles . 看一下使用数据表角色 In a nutshell, you add a new column with a role of tooltip , and then you can customize what appears however you'd like. 简而言之,您将添加一个具有tooltip角色的新列,然后可以自定义显示的内容。

I've used this before successfully with line and column charts, but the chart gallery pages for line and column charts explicitly mention that they support roles, whereas the geo chart page does not. 我之前已成功将其用于折线图和柱形图,但是折线图和柱形图的图表库页面明确提到它们支持角色,而地理图表页面则不支持。 That may not mean that they aren't supported; 那可能并不意味着它们不受支持。 it may just not be a documented feature. 它可能只是未记录的功能。

From all my research and testing I am almost positive that it is currently impossible to add a new line to the tooltip text inside a GeoChart . 从我的所有研究和测试中,我几乎可以肯定, 目前不可能在GeoChart中的工具提示文本中添加新行 See this question for the solution I eventually came up with. 请参阅此问题以获取我最终想到的解决方案。 This did not solve the new line problem, but reformatted the tooltip in a way that worked for my application and may help others. 这并不能解决新的换行问题,而是以一种对我的应用程序有用并且可以帮助其他人的方式重新格式化了工具提示。

Tested & working.测试和工作。 Save the data as a multi-line CSV.将数据保存为多行 CSV。

To do this, format multi-lines to have quotes.为此,请格式化多行以包含引号。

IMPORTANT - you have to use \r for the real end-of-line characters, not \r or \r\n like normal.重要 - 您必须使用 \r 作为真正的行尾字符,而不是像平常那样使用 \r 或 \r\n。

eg例如

123,456,"this\n\is\nfour\nlines",678\r
234,567,"another\nmulti",789\r

All google things that use this data from now-on understand how to show it with multi-lines properly.从现在开始,所有使用此数据的谷歌事物都知道如何正确地用多行显示它。

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

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