这就是我的tsv的样子,但是行数更多,值也更频繁地变化(.tsv大小约为10MB)

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


如何从此tsv中读出最大值?

这是d3.tsv调用



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]);
                }
            }





//编辑

现在我在返回+ d3.max之前添加了+



var maxValue = d3.max(d3.values(importedData), function(times){
  return +d3.max(d3.values(times), function(cities){
    return d3.max(d3.values(cities));
  });
});





现在我得到的最大值是9672.44,但这仍然不是我的最大值。

最佳答案

为什么不在遍历数组时简单地存储它呢?

我还添加了一行以检查数据是否实际表示为数字(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);

关于javascript - d3.tsv查找最大值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31874439/

10-11 23:47