我希望格式化我的数据,用图标替换数字。
据我所知,Google目前没有格式化程序:
http://code.google.com/apis/chart/interactive/docs/reference.html#formatters
有关自定义格式化程序的文档中有一个简短的提及,但我似乎无法找到有关如何开始编写自定义格式化程序的任何文档。
有人能指出我正确的方向吗?
StackOverflow上有一个类似的问题:Write a custom formatter for Google Charts Api。然而,问题只是使用内置格式化程序解决(我不会认为我可以使用)。
答案 0 :(得分: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>
希望有所帮助。
答案 1 :(得分:3)
内置的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的原始答案。我将自定义回调格式化函数与通用格式化程序分开。
答案 2 :(得分:2)
与数据表不同,数据视图允许通过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>