d3.tsv查找最大值

时间:2015-08-07 09:34:50

标签: javascript d3.js

这就是我的tsv的样子,但有更多的行,而且值经常变化(.tsv大小约为10MB)

anotherStudent

如何从这个tsv中读出最大值?

这是d3.tsv电话

cdate   ctime   Saarland    Mecklenburg-Vorpommern  Thueringen  Magdeburg   Dessau  Halle   Leipzig Dresden Chemnitz    Karlsruhe   Stuttgart   Tuebingen   Freiburg    Berlin  Brandenburg Schleswig-Holstein  Hamburg Bremen  Trier   Koblenz Rheinhessen-Pfalz   Unterfranken    Oberfranken Mittelfranken   Oberpfalz   Niederbayern    Oberbayern  Schwaben    Kassel  Giessen Darmstadt   Munster Detmold Arnsberg    Duesseldorf Koeln   Luneburg    Weser-Ems   Hannover    Braunschweig
01.01.2011  00:00   1526.66 0.00    497.24  0.00    0.00    0.00    0.00    0.00    0.00    25484.15    319.77  5992.47 92729.72    0.00    0.00    0.00    0.00    551.05  15948.44    9511.05 0.00    12506.98    0.00    11792.09    2354.59 34020.30    40880.95    6813.97 81734.04    0.00    981.48  0.00    0.00    44933.62    1122.27 3760.11 0.00    0.00    27893.19    714.28
01.01.2011  00:15   1526.66 0.00    497.24  0.00    0.00    0.00    0.00    0.00    0.00    25484.15    319.77  5992.47 92729.72    0.00    0.00    0.00    0.00    551.05  15948.44    9511.05 0.00    12506.98    0.00    11792.09    2354.59 34020.30    40880.95    6813.97 81734.04    0.00    981.48  0.00    0.00    44933.62    1122.27 3760.11 0.00    0.00    27893.19    714.28

//修改

现在我在返回+ d3.max

之前添加了+

d3.tsv("WasserNeu.tsv", importTSV);

var importedData = []; //[date][time].city

function importTSV(error, data) {
    
        for (var row in data) { 
            var dataRow = data[row]; //row is just the number of the current object, data[row] represents its contents
            if (importedData[dataRow.cdate] === undefined) {
                importedData[dataRow.cdate] = [];
            }
            if (importedData[dataRow.cdate][dataRow.ctime] === undefined) {
                importedData[dataRow.cdate][dataRow.ctime] = [];
            }
            for (var oneCity in dataRow) {
                if (oneCity != "cdate" && oneCity != "ctime") {

                    if (importedData[dataRow.cdate][dataRow.ctime][oneCity] === undefined) {
                        importedData[dataRow.cdate][dataRow.ctime][oneCity] = [];
                    }
                    importedData[dataRow.cdate][dataRow.ctime][oneCity] = dataRow[oneCity];
                    //console.log("dataPoint ("+oneCity+"): "+dataRow[oneCity]);
                }   
            }

现在我得到最高9672.44,但这不是我的最大值..

2 个答案:

答案 0 :(得分:0)

由于你没有更具体,这将找到整个“网格”中最大单元格的值:

d3.max(data, function(row){ return d3.max(row.values()); });

请查看d3 Arrays documentation了解详情。

importedData结构中,这应得到最大值:

d3.max(d3.values(importedData), function(times){
  return d3.max(d3.values(times), function(cities){
    return d3.max(d3.values(cities));
  });
});

答案 1 :(得分:0)

为什么不在迭代数组时简单地存储它?

我还添加了一行来检查数据是否实际表示为数字(parseFloat)。人们可能会把它排除在外,但它有助于减少错误来源。

d3.tsv("WasserNeu.tsv", importTSV);    

var importedData = [];  //[date][time].city

var valueCache;         //caching value 
var newMaxValue = 0;    //store the max value 
function importTSV(error, data) {
        for (var row in data) { 
            var dataRow = data[row]; //row is just the number of the current object, data[row] represents its contents
            if (importedData[dataRow.cdate] === undefined) {
                importedData[dataRow.cdate] = [];
            }
            if (importedData[dataRow.cdate][dataRow.ctime] === undefined) {
                importedData[dataRow.cdate][dataRow.ctime] = [];
            }
            for (var oneCity in dataRow) {
                if (oneCity != "cdate" && oneCity != "ctime") {

                    if (importedData[dataRow.cdate][dataRow.ctime][oneCity] === undefined) {
                        importedData[dataRow.cdate][dataRow.ctime][oneCity] = [];
                    }

                    valueCache = parseFloat(dataRow[oneCity]); //cache the value to reduce parse and array-access activity
                    if (valueCache > newMaxValue) {
                        newMaxValue = valueCache;
                    }

                    importedData[dataRow.cdate][dataRow.ctime][oneCity] = valueCache;
                }   
            } 

        }

    console.log("NewMax " + newMaxValue);