2015-11-01 17 views
5

Sto tentando di ordinare i nodi in D3 sankey. Voglio che i nodi con valori più grandi siano disegnati più in alto.Ordinamento nodi nel diagramma di sankey d3

Questo è il codice pertinente (based on d3 noob's code) snippet, in cui cerco di ottenere ciò che voglio utilizzando la funzione di ordinamento incorporata di D3. Il risultato non è quello che mi aspetterei.

Dopo aver ispezionato sankey.js, non mi è del tutto chiaro come il plug-in ordini i nodi. Qualsiasi consiglio sull'argomento sarebbe apprezzato.

//set up graph in same style as original example but empty 
    graph = {"nodes" : [], "links" : []}; 

    data.forEach(function (d) { 
     graph.nodes.push({ "name": d.source }); 
     graph.nodes.push({ "name": d.target }); 
     graph.links.push({ "source": d.source, 
      "target": d.target, 
      "value": +d.value }); 
    }); 

//try to sort the nodes and links before indexing them (not working) 
    graph.nodes.sort(function (a,b) {return d3.descending(a.value, b.value); }); 
    graph.links.sort(function (a,b) {return d3.descending(a.value, b.value); }); 

    // return only the distinct/unique nodes 
    graph.nodes = d3.keys(d3.nest() 
     .key(function (d) { return d.name; }) 
     .map(graph.nodes)); 

    // loop through each link replacing the text with its index from node 
    graph.links.forEach(function (d, i) { 
     graph.links[i].source = graph.nodes.indexOf(graph.links[i].source); 
     graph.links[i].target = graph.nodes.indexOf(graph.links[i].target); 
    }); 

    //now loop through each nodes to make nodes an array of objects 
    // rather than an array of strings 
    graph.nodes.forEach(function (d, i) { 
     graph.nodes[i] = { "name": d }; 
    }); 
+0

Se sto leggendo il codice correttamente - 'graph.nodes.sort (function (a, b) {return d3.descending (a.value, b.value);});' nodi fa non ha una proprietà di valore da ordinare su ... – Mark

+0

Vero. Probabilmente dovrò ordinare il valore all'interno di sankey.js dove viene calcolato. Proverò un post i risultati qui. – seb

+0

@seb Hai trovato la soluzione? Se sì, puoi pubblicare? – Danny

risposta

4

Rimozione del seguente riga dalle resolveCollisions() nel file sankey.js fermerà l'ordine dei nodi di cambiare:

function resolveCollisions() { 
    ... 
    // nodes.sort(ascendingDepth); // commented this out to prevent node reordering 
    ... 
} 

I nodi saranno quindi ordinate verticalmente tuttavia sono originariamente popolate. Quindi, se si ordinano i nodi prima di premere i dati, questi verranno visualizzati in ordine.

1

Se si imposta il valore iterations di layout su zero otterrete i nodi nel loro ordine alfabetico (per nome): quello ha funzionato abbastanza bene per me.

var sankey = d3.sankey() 
    .nodeWidth(36) 
    .nodePadding(40) 
    .size([width, height]) 
    .layout(0); /* <<<<<<< setting the iterations to zero */ 
Problemi correlati