18

我正在寻找用图标替换数字的数据格式。

据我所知,谷歌目前没有格式化程序可以这样做:

http://code.google.com/apis/chart/interactive/docs/reference.html#formatters

文档中有关于自定义格式化程序的简短提及,但我似乎找不到任何关于如何开始编写自定义格式化程序的文档。

谁能指出我正确的方向?

StackOverflow 上有一个类似的问题:Write a custom formatter for Google Charts Api。然而,这个问题只是使用内置的格式化程序(我认为我不能使用)解决了。

4

3 回答 3

23

认为你不能让当前的格式化程序做你想做的事,但你应该能够很容易地制作自己的格式化程序。我在下面将 iconFormatter 放在一起作为示例-可以对其进行调整以执行您真正需要的任何操作。

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript">
      google.load('visualization', '1', {packages: ['table']});
    </script>
    <script type="text/javascript">
      /**
      * Gviz icon formatter
      * @param {Object<Number, String>} Map of numbers to icon URIs
      */
      var iconFormatter = function(iconMap) {
        this.iconMap = iconMap;
      }
      /**
       * Formats a gviz DataTable column
       * @param {Object} dt DataTable to format
       * @param {Number} column index number
       */
      iconFormatter.prototype.format = function(dt, column) {
        for (var i=0;i<dt.getNumberOfRows();i++) {
          var formattedValue = this.iconMap[dt.getValue(i, column)];
          var htmlString = "<img src="+formattedValue+" />";
          dt.setFormattedValue(i, column, htmlString);
          // underlying value preserved
          console.log(dt.getValue(i, column)); 
        }
      }
    </script>
    <script>
      function drawVisualization() {
        // Create and populate the data table.
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Name');
        data.addColumn('number', 'Height');
        data.addRows(3);
        data.setCell(0, 0, 'Tong Ning mu');
        data.setCell(1, 0, 'Huang Ang fa');
        data.setCell(2, 0, 'Teng nu');
        data.setCell(0, 1, 174);
        data.setCell(1, 1, 523);
        data.setCell(2, 1, 86);

        var iconMap = {
          174: "http://farm1.static.flickr.com/76/buddyicons/63892905@N00.jpg?1149480603",
          523: "http://farm1.static.flickr.com/28/buddyicons/20741728@N00.jpg?1129271399",
          86: "http://farm3.static.flickr.com/2698/buddyicons/70986564@N00.jpg?1303489082"
          // other numbers
        }

        // Create and draw the visualization.
        visualization = new google.visualization.Table(document.getElementById('table'));
        // apply our formatter, just like normal
        var formatter = new iconFormatter(iconMap);
        formatter.format(data, 1);
        // allow html, just like any html formatter will require
        visualization.draw(data, {allowHtml: true});
      }
      google.setOnLoadCallback(drawVisualization);
    </script>
  </head>
  <body>
    <div id="table"></div>
  </body>
</html>

希望有帮助。

于 2011-09-14T13:25:10.607 回答
4

内置的 DataTable 格式化程序目前仅允许使用格式化字符串(模式)进行自定义。不支持使用回调函数进行自定义。这是一个支持回调函数的格式化程序类。

/**
 * Custom formatter class for Google Visualization DataTable
 * Can be used like a built-in formatters when they are not enough. 
 * https://developers.google.com/chart/interactive/docs/reference#formatters
 * Changes the displayed/formatted value. The value itself remains unchanged.
 *
 * @param function() custom value conversion function
 *  this function 
 *  - takes one value as input (the original value) and 
 *  - returns the formatted value
 */
var CustomFormatter = function(formatValue) {
  this.formatValue = formatValue;
}

/**
 * Formats a Google DataTable column
 * @param {Object} dt DataTable to format
 * @param {Number} column index number
 */
CustomFormatter.prototype.format = function(dt, column) {
  for (var i = 0; i < dt.getNumberOfRows(); i++) {
    var value = dt.getValue(i, column);
    dt.setFormattedValue(i, column, this.formatValue(value));
  }
}

这个类可以像内置的格式化函数一样使用,但是在构造函数中有一个回调函数,比如这个将数字转换为 HH:MM:SS

var customFormatter = new CustomFormatter(function(value) {
    var hours = parseInt(value / 3600) % 24;
    var minutes = parseInt(value / 60) % 60;
    var seconds = value % 60;
    return (hours < 10 ? "0" + hours : hours) +
      ":" + (minutes < 10 ? "0" + minutes : minutes) +
      ":" + (seconds < 10 ? "0" + seconds : seconds);
});
customFormatter.format(dataTable, 1);

这是工作演示小提琴:http: //jsfiddle.net/o5tdt2r8/10/

或者在 DataView 中使用 addColumn,如其他答案中所述。

感谢@oli 的原始答案。我将自定义回调格式化函数与通用格式化程序分开。

于 2016-06-06T19:53:47.737 回答
3

与数据不同,数据视图允许通过定义计算列SetColumns

google.charts.load('current', {'packages':['table']});

google.charts.setOnLoadCallback(function() {
  // Create and populate the data table.
  var dataTable = new google.visualization.DataTable();
  dataTable.addColumn('string', 'Name');
  dataTable.addColumn('number', 'Icon Nr');
  dataTable.addRow(['Tong Ning mu', 174]);
  dataTable.addRow(['Huang Ang fa', 523]);
  dataTable.addRow(['Teng nu', 86]);

  // Create and define the data view with custom formatter in the calc property
  var dataView = new google.visualization.DataView(dataTable);
  dataView.setColumns([0, {
    type: 'string',
    label: 'Icon',
    calc: function(dataTable, rowNr) {
      var iconMap = {
        174: "http://farm1.static.flickr.com/76/buddyicons/63892905@N00.jpg?1149480603",
        523: "http://farm1.static.flickr.com/28/buddyicons/20741728@N00.jpg?1129271399",
        86: "http://farm3.static.flickr.com/2698/buddyicons/70986564@N00.jpg?1303489082"
      }
      var value = dataTable.getValue(rowNr, 1); // 1 is the column
      var formattedValue = iconMap[value]; // convert
      var htmlString = "<img src=" + formattedValue + " />"; // wrap
      // console.log(rowNr, value, formattedValue); // debug
      return htmlString;
    }
  }]);

  // Create and draw the visualization.
  visualization = new google.visualization.Table(document.getElementById('table'));
  visualization.draw(dataView, {
    allowHtml: true // allow html, just like any html formatter will require
  });
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="table"></div>

于 2016-06-08T11:49:02.620 回答