javascript - Sorting large CSV in D3.js -
i'm trying sort medium sized csv file (~3000 lines) using d3.js before passing visualization algorithm.
apparently though there's wrong code sorting seems applied in 'chunks' - see image above. i'm using d3 function:
dataset.sort(function(a,b) { return d3.descending(a.counttotal, b.counttotal); });
here's code:
// d3 code go here // safe way load data var dataset = []; d3.csv("subjects.csv", function(error, rows) { rows.foreach(function(r) { dataset.push({ label: r.label, counttotal: r.counttotal }) }); generatevis(); }); function generatevis(){ var h = dataset.length * 100; //7000; var w = 1000; dataset.sort(function(a,b) { return d3.descending(a.counttotal, b.counttotal); }); var svg = d3.select("#svg-rect").append("svg") .attr('id','mysvg') .attr("width", w ) .attr("height", h); var rects = svg.selectall("rect") .data(dataset) .enter() .append("rect"); rects.attr("x", 0) .attr("y", function(d, i){ return * 10 + 10; }) .attr("width", function(d){ return d.counttotal; }) .attr("height", 8) .append("title") .text(function (d){ return d.label + " has " + d.counttotal + " articles "; }) };
the csv file can found here: https://www.dropbox.com/s/3kwzwqbgts8z2lh/subjects.csv?dl=0
any appreciated!
the sort function sorting alphabetically. convert totals numbers first...
dataset.sort(function(a,b) { return d3.descending(+a.counttotal, +b.counttotal); });
Comments
Post a Comment