Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在Google Visualization API中使用分组聚合保留格式_Javascript_Google Data Api_Google Visualization_Google Datatable - Fatal编程技术网

Javascript 在Google Visualization API中使用分组聚合保留格式

Javascript 在Google Visualization API中使用分组聚合保留格式,javascript,google-data-api,google-visualization,google-datatable,Javascript,Google Data Api,Google Visualization,Google Datatable,使用Google的可视化API,我使用Google.Visualization.data.group根据我的原始数据创建子表。我的原始数据使用{v:“US”,f:“US”}的技巧来显示值以外的内容,但是当我使用聚合函数时,格式被消除,只留下“US”部分 是否有任何方法可以保留原始格式,或者有一种简单的方法可以将其添加回使用组聚合创建的数据表中 样本数据: [2010, {v:"MA", f:"Morocco"}, {v:"002", f:"Africa"}, {v:"002", f:"North

使用Google的可视化API,我使用Google.Visualization.data.group根据我的原始数据创建子表。我的原始数据使用{v:“US”,f:“US”}的技巧来显示值以外的内容,但是当我使用聚合函数时,格式被消除,只留下“US”部分

是否有任何方法可以保留原始格式,或者有一种简单的方法可以将其添加回使用组聚合创建的数据表中

样本数据:

[2010, {v:"MA", f:"Morocco"}, {v:"002", f:"Africa"}, {v:"002", f:"Northern Africa"}, 21.12724],
[2010, {v:"AW", f:"Aruba"}, {v:"019", f:"Americas  "}, {v:"019", f:"Caribbean"}, 0.98],
[2010, {v:"AF", f:"Afghanistan"}, {v:"142", f:"Asia"}, {v:"142", f:"Southern Asia"}, 0.9861],
[2010, {v:"AO", f:"Angola"}, {v:"002", f:"Africa"}, {v:"002", f:"Middle Africa"}, 5.11774],
聚合函数:

var countryData = google.visualization.data.group(
  rawData, 
  [0, 1], 
  [{'column': 4, 'aggregation': google.visualization.data.sum, 'type': 'number'}]
);
编辑:

进一步考虑,可能不可能使用格式分组,因为无法保证每个值的格式是一致的。考虑到这一点,编写一个函数将格式添加到我的数据的每一列可能更好(或者只可能)。所以问题就变成了,“我该怎么做?”

我真的不希望只将原始数据创建为未格式化的值,然后创建用于查找每个值的格式的附加表。这将需要额外的两个表(一个用于28行的区域,一个用于240行以上的国家),然后创建两个函数来查看分组表中的每个值(将有30多年的数据,意味着数千行)并添加这些值

这似乎是一个非常复杂的解决方案

有什么方法可以用修改器函数实现这一点吗?我是否可以编写一个函数,将表中的每个值作为{v:“US”,f:“US”}格式的对象返回?或者有没有一种简单的方法来编写一个列格式化程序,在我的原始表中查找适当的值并采用该格式?对我(必须编写它)和用户(必须加载它)来说,哪一个最省事

编辑2:

看起来我应该能够使用如下内容为新表创建格式化程序:

function (dt, row) {
    return {
        v: (dt.getValue(row, 1) / 1000000),
        f: (dt.getValue(row, 1) / 1000000) + 'M'
    }
}
[2010, "'MA'|'Morocco'", 21.13],
[2010, "'AW'|'Aruba'", 0.98],
[2010, "'AF'|'Afghanistan'", 0.99],
[2010, "'AO'|'Angola'", 5.12],
但问题是,因为我没有处理数字格式,所以我必须创建某种查找表来获取值,在查找表中查找,然后返回适当的格式。看起来我可能还需要一行一行地遍历整个表,这是数千行

我无法想象,如果没有一些蛮力循环和赋值,就没有一种简单的方法可以做到这一点

编辑3:

所以我尝试了一些技巧。我没有将每行设置为值/格式,而是将值/格式部分创建为字符串,然后在分组后使用eval()对对象求值。这很有效。以下是数据:

[2010, "{v: 'MA', f: 'Morocco'}", 21.13],
[2010, "{v: 'AW', f: 'Aruba'}", 0.98],
[2010, "{v: 'AF', f: 'Afghanistan'}", 0.99],
[2010, "{v: 'AO', f: 'Angola'}", 5.12],
以下是新代码:

  var countryCount = countryData.getColumnRange(0).count;

  for (var i = 0; i <= countryCount; i++) {
    countryData.setValue(i, 1, eval('(' + countryData.getValue(i,1) + ')'));
  };

那么我到底做错了什么呢?

好吧,我想出来了(它是多么复杂)

我尝试了一种新的方法。我重新格式化了数据,然后创建了一个函数,根据该字符串中的分隔符返回值/格式。因此,我的数据如下所示:

function (dt, row) {
    return {
        v: (dt.getValue(row, 1) / 1000000),
        f: (dt.getValue(row, 1) / 1000000) + 'M'
    }
}
[2010, "'MA'|'Morocco'", 21.13],
[2010, "'AW'|'Aruba'", 0.98],
[2010, "'AF'|'Afghanistan'", 0.99],
[2010, "'AO'|'Angola'", 5.12],
然后,我使用它来获取第1列的分隔符位置:

var countryCount = countryData.getNumberOfRows();

for (var i = 0; i <= countryCount; i++) {
  var stringToSplit = countryData.getValue(i,1);
  var dividerLocation = stringToSplit.indexOf("|");
  alert("Divider: " + dividerLocation + ", String: " + stringToSplit);
  countryData.setValue(i, 1, splitFormat(dividerLocation, stringToSplit));
};
问题是我将数据的第1列定义为“string”,但firebug告诉我从splitFormat()函数返回的对象是一个对象(因为我认为它是一个数组)。即使我使用v:和f:组件设置原始数据表,它也不希望接受返回的数组对象值,因为FireBug给了我以下非常有用的建议:

"Error: Type mismatch. Value [object Object] does not match type string in column index 1 (table.I.js,137)"
问题是,虽然可以使用{v:,f:}语法定义DataTable,但不能将该语法返回到表中,因为该列的值设置为string。相反,我使用了DataTable的“setFormattedValue”属性来解决这个问题:

  function drawVisualization() {
    var countryTable = new google.visualization.Table(document.getElementById('table'));

    var countryCount = countryData.getNumberOfRows() - 1;

    for (var i = 0; i <= countryCount; i++) {
      var stringToSplit = countryData.getValue(i,1);
      var dividerLocation = stringToSplit.indexOf("|");
      var stringValue = stringToSplit.substring(0, dividerLocation);
      var stringFormat = stringToSplit.substring(dividerLocation + 1);
      countryData.setValue(i, 1, stringValue);
      countryData.setFormattedValue(i, 1, stringFormat);
    };
函数drawVisualization(){
var countryTable=new google.visualization.Table(document.getElementById('Table');
var countryCount=countryData.getNumberOfRows()-1;

对于(var i=0;i我自己也遇到了这个问题。我决定使用一个修饰符将值更改为格式化值,使用原始数据表查找格式化值。这不是非常有效,但它可以工作,而且计算机速度很快

首先创建查找函数:

function getFormatForValue(dataTable, column, value) {

    // we need to spin through column in the dataTable looking
    // for the matching value and then return the formatted value
    var rowcount = dataTable.getNumberOfRows();
    for (var i=0; i<rowcount; i++) {
        if (dataTable.getValue(i, column) === value) {

            // we found it, this will look much better
            return dataTable.getFormattedValue(i, column);    
        }
    }

    // better than nothing
    return value;
}
function copyFormattedValues(oldDataTable, oldColumn, newDataTable, newColumn) {

    var rowcount = newDataTable.getNumberOfRows();
    for (var i=0; i<rowcount; i++) {
        var value = newDataTable.getValue(i, newColumn);
        var formatted = getFormatForValue(oldDataTable, oldColumn, value);
        newDataTable.setFormattedValue(i, newColumn, formatted);
    }

 }
更新:您似乎需要保留值和格式化值。在我显示饼图的情况下,我不想保留原始值。我想这对您不起作用,但我将把这个答案留给其他可能有类似我的简单情况的人

我花了更多的时间在这上面,这里有一个替代方案,将复制格式化值,同时保留原始单元格值

创建使用查找功能的复制功能:

function getFormatForValue(dataTable, column, value) {

    // we need to spin through column in the dataTable looking
    // for the matching value and then return the formatted value
    var rowcount = dataTable.getNumberOfRows();
    for (var i=0; i<rowcount; i++) {
        if (dataTable.getValue(i, column) === value) {

            // we found it, this will look much better
            return dataTable.getFormattedValue(i, column);    
        }
    }

    // better than nothing
    return value;
}
function copyFormattedValues(oldDataTable, oldColumn, newDataTable, newColumn) {

    var rowcount = newDataTable.getNumberOfRows();
    for (var i=0; i<rowcount; i++) {
        var value = newDataTable.getValue(i, newColumn);
        var formatted = getFormatForValue(oldDataTable, oldColumn, value);
        newDataTable.setFormattedValue(i, newColumn, formatted);
    }

 }
源列和目标列相同,但在某些情况下可能不同

当然,理想情况下,所有这些都会自动发生