1

this is how my tsv looks like, but with a lot more of rows and the values varies more often (.tsv size is about 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

How can I read out the Maximum Value out of this tsv?

This is the d3.tsv call

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

//Edit

Now I've added just + before return +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));
  });
});

Now I get 9672.44 as maximum, still this isn't my maximum..

S.Schaefer
  • 13
  • 6

2 Answers2

0

Since you weren't more specific, this will find the value of the maximum cell in the entire "grid":

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

Take a look at the d3 Arrays documentation for more information.


From your importedData structure, this should get the maximum:

d3.max(d3.values(importedData), function(times){
  return d3.max(d3.values(times), function(cities){
    return d3.max(d3.values(cities));
  });
});
nkorth
  • 1,684
  • 1
  • 12
  • 28
  • I added my d3.tsv call.. which details do you need? – S.Schaefer Aug 07 '15 at 09:57
  • I need to know what maximum value you want! The maximum value for any city at any time? The city and time where the maximum value occurs? The maximum value for each time? The maximum value for each city? – nkorth Aug 07 '15 at 10:57
  • ah okay :) I need the maximum value for any city at any time – S.Schaefer Aug 07 '15 at 11:01
  • It's an expression, so you'd probably assign the result to a variable and then use it somewhere. Are you new to Javascript? – nkorth Aug 07 '15 at 13:08
  • Yes, I'm new to javascript :D – S.Schaefer Aug 07 '15 at 13:15
  • Ok! Basically, if you want to paste the code in, write `var maxValue = ` before the above code. You can put this block anywhere after you've called `importTSV`. This poses a slight problem since `d3.tsv` is asynchronous. Perhaps add the max value code to the end of the `importTSV` function? – nkorth Aug 07 '15 at 13:48
  • I've done that and tried to the code after the ImportTsv function and in the end of the importtsv function.. nothing worked, console.log keep sayin undefined – S.Schaefer Aug 07 '15 at 15:10
  • Ok, now I added your new code with values() and now I'm getting "importedData.values is not a function" – S.Schaefer Aug 08 '15 at 09:33
  • Whoops! Fixed it again – nkorth Aug 08 '15 at 12:47
  • thanks, this works, but its not the maximum in my data :/ (maximum is 92729.72) your code gives a max of 994.49. Is it possible that the value range is exceeded? – S.Schaefer Aug 08 '15 at 14:36
  • I edited my question above. Do you have any idea why I still don't get the maximum? – S.Schaefer Aug 10 '15 at 08:19
  • Have you checked that the `importedData` structure actually contains the right data, **as numbers instead of strings**? If it's all strings, then `994.49` technically is the correct maximum. See http://stackoverflow.com/a/6810681/685933 – nkorth Aug 10 '15 at 09:28
0

Why not simply store it while iterating through the array?

I also added a line to check that the data is actually represented as number (parseFloat). One could probably leave that out, but it helps reducing error sources.

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);
AndreLung
  • 175
  • 1
  • 6