javascript - How to simplify this D3 code? -


i've got csv data looks this, showing pass rates organisation year:

org,org_cat,2004_passed,2004_total,2005_passed,2005_total,2006_passed,2006_total gsk,industry,35,100,45,100,55,100 

i'm working in d3, , i'd end dictionary of organisations this:

data = {  'gsk': {      'org_cat': 'industry',      'data': [         { 'year': 2004, 'passed': 35, 'total': 100 },         { 'year': 2005, 'passed': 45, 'total': 100 },         { 'year': 2006, 'passed': 55, 'total': 100 }      ]   ] } 

most of straightforward, i've got messy code year columns:

var data = {}; alldata.foreach(function(d) {   data[d.org] = {     'category': d.org_cat,     'data': []   };   (var k in d) {     var temp = {};     (var k in d) {       if (patt.test(k)) {         var res = k.split("_");         if (res[0] in temp) {           temp[res[0]][res[1]] = +d[k];         } else {           temp[res[0]] = {};           temp[res[0]][res[1]] = +d[k];         }       }     }     var temparr = [];     (var y in temp) {       var tempdict = {};       tempdict.year = y;       tempdict.passed = temp[y].passed;       tempdict.total = temp[y].total;       temparr.push(tempdict);     }     // todo: sort year in case keys got shuffled     data[d.org].data = temparr;   } }); 

is there way simplify horrid code?

it's safe assume each row unique organisation.

i don't see why need d3 this. you're code doesn't use either. here's how it, i'm sure there's simpler way may either way :

jsfiddle : https://jsfiddle.net/thatoneguy/dnvheznk/1/

i converted data json use jsfiddle, know how loop through csv overwrite line 14 :

for (var = 0; < data.length; i++) { //loop through data array (this can use on bigger sized array)  

to loop :

alldata.foreach(function(d, i) { //but add 'i' index 

here full commented code converted json data :

var data = [{ //data set converted json easier use    "org": "gsk",    "org_cat": "industry",    "2004_passed": 35,    "2004_total": 100,    "2005_passed": 45,    "2005_total": 100,    "2006_passed": 55,    "2006_total": 100  }];    var newdata = {}; //new data container    (var = 0; < data.length; i++) { //loop through data array (this can use on bigger sized array)    var thisobj = {}; //create empty object    thisobj.org_cat = data[i].org_cat; //set org_cat    thisobj.data = []; //set data empty array populate later      (var key in data[i]) { //loop through data[i]      if (key != 'org' && key != 'org_cat') { //check key not org or org_cat        var thisdata = {}; //create empty data object        var thisyear = key.tostring().substring(0, 4); //get year using substring          thisdata.year = thisyear; //set year        thisdata.passed = data[i][thisyear + '_passed']; //set passed @ year        thisdata.total = data[i][thisyear + '_total']; //set total @ year          thisobj.data.push(thisdata); //push data data array      }    }    var uniquedates = []; //set empty array use remove duplicate items in data array    (var j = 0; j < thisobj.data.length; j++) { //loop through data array created earlier      if (uniquedates.indexof(thisobj.data[j].year) < 0) { //if year doesn't exist in unique array above, push in        uniquedates.push(thisobj.data[j].year); //push in      } else {        thisobj.data.splice(j--, 1); //remove duplicate data      }    }    newdata[data[i].org] = thisobj; //set data @ current org object created above  }  console.log('newdata', newdata) //log new data


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -