Javascript 如何在透视Google可视化数据表时保留工具提示

Javascript 如何在透视Google可视化数据表时保留工具提示,javascript,php,google-visualization,Javascript,Php,Google Visualization,我正在使用@asgallant中的一些优秀代码来透视一个用于折线图的Google数据表。他的代码如下: 在我的原始数据表中,我有一个第四列,但它是一个工具提示字符串。当我用他的代码透视表时,如果工具提示不再可用,我会显示工具提示。我想使用提供的工具提示,对于“创建”的单元格,使用一些标准工具提示文本。以下是我的初始表设置版本的外观 var data = new google.visualization.DataTable(); data.addColumn('number', 'A'); dat

我正在使用@asgallant中的一些优秀代码来透视一个用于折线图的Google数据表。他的代码如下:

在我的原始数据表中,我有一个第四列,但它是一个工具提示字符串。当我用他的代码透视表时,如果工具提示不再可用,我会显示工具提示。我想使用提供的工具提示,对于“创建”的单元格,使用一些标准工具提示文本。以下是我的初始表设置版本的外观

var data = new google.visualization.DataTable();
data.addColumn('number', 'A');
data.addColumn('string', 'B');
data.addColumn('number', 'C');
data.addColumn({type: 'string', role: 'tooltip'});
data.addRows([
    [1, 'foo', 6, 't1'],
    [2, 'foo', 2, 'hello'],
    [3, 'foo', 1, 't2'],
    [4, 'foo', 3, 'test'],
    [1, 'bar', 7, 't3'],
    [2, 'bar', 3, 'again'],
    [1, 'baz', 8, 't4'],
    [2, 'baz', 4, 'and'],
    [2, 'cad', 5, 't5'],
    [3, 'cad', 6, 'again'],
    [1, 'qud', 9, 'x'],
    [5, 'qud', 1, 'z']
]);
有人能提供一些帮助吗

--更新--


编辑初始addRows语句,使其更能代表我的数据模型。

为了保留工具提示,您需要向视图和透视数据中添加工具提示列:

for (var i = 0; i < distinctValues.length; i++) {
    viewColumns.push({
        type: 'number',
        label: distinctValues[i],
        calc: (function (x) {
            return function (dt, row) {
                // return values of C only for the rows where B = distinctValues[i] (passed into the closure via x)
                return (dt.getValue(row, 1) == x) ? dt.getValue(row, 2) : null;
            }
        })(distinctValues[i])
    });
    groupColumns.push({
        column: (i * 2) + 1,
        type: 'number',
        label: distinctValues[i],
        aggregation: google.visualization.data.sum
    });
    // add columns for the tooltips
    viewColumns.push({
        type: 'string',
        calc: (function (x) {
            return function (dt, row) {
                // return values of the tooltip column only for the rows where B = distinctValues[i] (passed into the closure via x)
                return (dt.getValue(row, 1) == x) ? dt.getValue(row, 3) : null;
            }
        })(distinctValues[i])
    });
    groupColumns.push({
        column: (i * 2) + 2,
        type: 'string',
        aggregation: function (values) {
            return values.join('');
        }
    });
}
for(变量i=0;i
group函数无法正确处理列角色,因此您必须在以后设置它们:

// fix the tooltip column roles, since the group function won't create them properly
for (var i = 1; i < pivotedData.getNumberOfColumns(); i++) {
    if (pivotedData.getColumnType(i) == 'string') {
        pivotedData.setColumnProperty(i, 'role', 'tooltip');
    }
}
//修复工具提示列角色,因为组函数无法正确创建它们
对于(var i=1;i

请参见示例:

您希望如何确定工具提示文本?@asgallant-以上面的预透视表为例,我希望工具提示列中已有的内容都能转到“透视”视图。当需要创建一个新的单元格时,列a的值为3,我希望工具提示类似于“N/a”。如果多个单元格被组合在一起,比如
'qud'
行?@asgallant-我更新了“addRows”行,使其更符合我的模型。在我的情况下,A列和B列的组合将始终是唯一的,不会发生组合。对于“已创建”的条目,在何处/如何设置默认工具提示?数据透视后,是否可以将第一列设置为字符串,使图表是离散的而不是连续的?您所说的“创建”项是什么意思?您的第一列可以是您想要的任何类型-它不必是数字。如果您想为分组保留数字,但稍后将其字符串化,则可以通过DataView实现这一点。