简体   繁体   English

Google Visualization API 中的逗号分隔数据

[英]Comma Separated Data in Google Visualisation API

I'm using GeoChart from the Google Visualisation API to create a dynamic map containing site usage information.我正在使用 Google Visualization API 中的 GeoChart 创建包含站点使用信息的动态地图。 Due to the large metric values (8 digits), I need to comma separate these numbers to make the map more readable.由于度量值很大(8 位),我需要用逗号分隔这些数字以使地图更具可读性。 I've written a function that adds commas to these number, but this then causes errors with the API drawing the map.我编写了一个函数,为这些数字添加逗号,但这会导致绘制地图的 API 出错。

For example, when comma separating 1 of the 3 columns of data (Country, Visits, Unique Visitors), the label for that column will be removed in the country tool tips.例如,当用逗号分隔 3 列数据(国家/地区、访问次数、唯一身份访问者)中的 1 列时,该列的标签将在国家/地区工具提示中删除。 Comma separating 2 of the columns then leads to the entire map failing, with an 'Expecting 2 columns' error.逗号分隔 2 列然后导致整个地图失败,并出现“预期 2 列”错误。 This must be due to the commas in the number string not being escaped and therefore causing errors in the JSON array.这一定是由于数字字符串中的逗号没有被转义,因此导致 JSON 数组中的错误。 I've tried inserting '\\,' instead of a regular comma to no avail.我试过插入'\\,'而不是普通的逗号,但无济于事。 Any work arounds or solutions?任何变通方法或解决方案?

Taking a step back I think the better way to solve the problem is to format your raw data using the NumberFormat class in google visualizations called google.visualization.NumberFormat.退后一步,我认为解决问题的更好方法是使用名为 google.visualization.NumberFormat 的 google 可视化中的 NumberFormat 类来格式化原始数据。 More info here: google.visualization.NumberFormat更多信息: google.visualization.NumberFormat

Here is an example of how to format the 2nd column to have commas as the grouping separator:以下是如何格式化第二列以使用逗号作为分组分隔符的示例:

function drawVisualization() {
  var data = google.visualization.arrayToDataTable([
    ['Country', 'Popularity'],
    ['Germany', 200],
    ['United States', 300],
    ['Brazil', 400],
    ['Canada', 500],
    ['France', 60000000000000000],
    ['RU', 700]
  ]);

  var formatter = new google.visualization.NumberFormat({pattern:'###,###'} );
  formatter.format(data, 1);
  
  var geochart = new google.visualization.GeoChart(
      document.getElementById('visualization'));
  geochart.draw(data, {width: 556, height: 347, tooltip: {textStyle: {color: 'blue', fontName: 'Tahoma', fontSize: '15'}}});
}

I used the google visualization playground to test it out.我使用谷歌可视化游乐场来测试它。

Update更新

Modified the geochart.draw line to include tooltip text styling.修改 geochart.draw 线以包含工具提示文本样式。 It will style the all of the text in the tooltip.它将为工具提示中的所有文本设置样式。 I haven't seen anyway to make fine grain customizations.我还没有看到进行细粒度定制。 Here is more info: Geochart Configuration Options这是更多信息: 地理图表配置选项

According to the official specification for the CSV file format, including commas in data is possible by wrapping the data in double-quotes.根据 CSV 文件格式的官方规范,通过将数据用双引号括起来,可以在数据中包含逗号。

Basic rules and examples : 基本规则和示例

Any field may be quoted (that is, enclosed within double-quote characters).任何字段都可以被引用(即用双引号括起来)。 Some fields must be quoted, as specified in following rules.某些字段必须引用,如以下规则中所指定。

"1997","Ford","E350"

Fields with embedded commas must be quoted.必须引用带有嵌入逗号的字段。

1997,Ford,E350,"Super, luxurious truck"

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

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