2012-05-12 14 views

risposta

70

Se si dispone di un array nidificato di numeri (arrays = [[1, 2], [20, 3]]), nido d3.max:

var max = d3.max(arrays, function(array) { 
    return d3.max(array); 
}); 

o equivalentemente, utilizzare :

var max = d3.max(arrays.map(function(array) { 
    return d3.max(array); 
})); 

Se si desidera ignorare s tring valori, è possibile utilizzare array.filter di ignorare le stringhe:

var max = d3.max(arrays, function(array) { 
    return d3.max(array.filter(function(value) { 
    return typeof value === "number"; 
    })); 
}); 

In alternativa, se si conosce la stringa è sempre in prima posizione, è possibile utilizzare array.slice che è un po 'più efficiente:

var max = d3.max(arrays, function(array) { 
    return d3.max(array.slice(1)); 
}); 

Un'altra opzione è usare una funzione accessor che restituisce NaN per valori che non sono numeri. Ciò farà sì che d3.max ignori quei valori. Comodamente, di JavaScript incorporato Number funzione fa esattamente questo, quindi si può dire:

var max = d3.max(arrays, function(array) { 
    return d3.max(array, Number); 
}); 
+0

Ho lo stesso problema ma quando provo ad applicare il filtro dell'array ma ho il seguente errore: TypeError: undefined non è una funzione (che valuta l'array.filtro (funzione (valore) \t \t { \t \t \t \t ritorno valore typeof === "numero"; \t \t}) '). La mia variabile di scena è una matrice di oggetti caricati da un json. Che c'è ? –

0

Si tratta di un hack crudele, ma guardando lo source code per d3.max, la soluzione migliore potrebbe essere quella di definire uno d3.max1 che scarta il primo elemento copiandolo, ma sostituendo i=-1 con i=0. Il codice a quel link è qui estratto. Nota che non sono un utente regolare di d3.js, ma da quello che so della libreria, vorrai assicurarti che la tua versione abbia un caso f.call come fa questa funzione, in modo che possa rispondere agli aggiornamenti live correttamente .

d3.max = function(array, f) { 
    var i = -1, 
     n = array.length, 
     a, 
     b; 
    if (arguments.length === 1) { 
    while (++i < n && ((a = array[i]) == null || a != a)) a = undefined; 
    while (++i < n) if ((b = array[i]) != null && b > a) a = b; 
    } else { 
    while (++i < n && ((a = f.call(array, array[i], i)) == null || a != a)) a = undefined; 
    while (++i < n) if ((b = f.call(array, array[i], i)) != null && b > a) a = b; 
    } 
    return a; 
}; 

Poi sarebbe solo d3.max(d3.max1(a)).

4

Utilizzare questa:

function arrmax(arrs) { 
    var toplevel = []; 

    var f = function(v) { 
     return !isNaN(v); 
    }; 

    for (var i = 0, l = arrs.length; i<l; i++) { 
     toplevel.push(Math.max.apply(window, arrs[i].filter(f))); 
    } 
    return Math.max.apply(window, toplevel); 
} 

o meglio:

function arrmax(arrs) { 
    if (!arrs || !arrs.length) return undefined; 
    var max = Math.max.apply(window, arrs[0]), m, 
     f = function(v){ return !isNaN(v); }; 
    for (var i = 1, l = arrs.length; i<l; i++) { 
     if ((m = Math.max.apply(window, arrs[i].filter(f)))>max) max=m; 
    } 
    return max; 
} 

Vedi MDN per Array.filter dettagli di metodo.

2

È possibile appiattire un array e applicare una funzione a ciascun membro

Array.prototype.flatten= function(fun){ 
    if(typeof fun!= 'function') fun= ''; 
    var A= [], L= this.length, itm; 
    for(var i= 0; i<L; i++){ 
     itm= this[i]; 
     if(itm!= undefined){ 
      if(!itm.flatten){ 
       if(fun) itm= fun(itm); 
       if(itm) A.push(itm); 
      } 
      else A= A.concat(itm.flatten(fun)); 
     } 
    } 
    return A; 
} 

var a= [["yz", 1, 2], ["xy", 20, 3]], max=-Infinity; 

var max=Math.max.apply(a, a.flatten(Number)); 
2

Se ora esattamente quello colonne che si desidera eseguire il test, è possibile utilizzare:

var columns = ["ColumnA", "ColumnB", "ColumnC"]; 

var max = selectedMax(columns,dataset); 
var min = selectedMin(columns,dataset) 

function selectedMax(columns, dataset) { 
    var max; 
    columns.forEach(function(element, index, array) { 
     var tmpmax = d3.max(dataset, function(d) { 
      return +d[element]; 
     });  
     max = (tmpmax > max || max === undefined) ? tmpmax : max; 
    }); 
    return max; 
} 

function selectedMin(columns, dataset) { 
    var min; 
    columns.forEach(function(element, index, array) { 
     var tmpmin = d3.min(dataset, function(d) { 
      return +d[element]; 
     }); 
     min = (tmpmin < min || min === undefined) ? tmpmin : min; 
    }); 
return min; 
} 
Problemi correlati