2016-07-25 124 views
0

我有看起來像這樣,顯示合格率按組織逐年一些CSV數據:如何簡化這個D3代碼?

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

我在D3的工作,和我想要的組織就像一本字典落得這樣的:

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

這其中大部分是簡單的,但我有非常亂碼年度列:

var data = {}; 
allData.forEach(function(d) { 
    data[d.org] = { 
    'category': d.org_cat, 
    'data': [] 
    }; 
    for (var k in d) { 
    var temp = {}; 
    for (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 = []; 
    for (var y in temp) { 
     var tempDict = {}; 
     tempDict.year = y; 
     tempDict.passed = temp[y].passed; 
     tempDict.total = temp[y].total; 
     tempArr.push(tempDict); 
    } 
    // TODO: sort by year in case the keys got shuffled 
    data[d.org].data = tempArr; 
    } 
}); 

有沒有辦法簡化這個horri d代碼?

假設每一行都是唯一的組織是安全的。

+0

退房d3.nest? – reptilicus

+1

其實我收回了那個,數據很混亂 – reptilicus

+0

@reptilicus我知道,這不是我的數據! – Richard

回答

2

我不明白你爲什麼需要D3來做到這一點。你的代碼也不會使用它。這是我會怎麼做,我敢肯定有另一種更簡單的方式,但它可以幫助你任何一種方式:

的jsfiddle:https://jsfiddle.net/thatOneGuy/dnvheznk/1/

我轉換數據,以JSON用的jsfiddle使用,但你已經知道如何通過CSV循環,使剛剛覆蓋14行:

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

要你的循環:

allData.forEach(function(d, i) { //but add the 'i' to index 

這裏是轉換JSO完全註釋代碼N個數據:

var data = [{ //data set converted to JSON for 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 
 

 
for (var i = 0; i < data.length; i++) { //loop through data array (this is so you can use this on a bigger sized array) 
 
    var thisObj = {}; //create empty object 
 
    thisObj.org_cat = data[i].org_cat; //set org_cat 
 
    thisObj.data = []; //set data to empty array to populate later 
 

 
    for (var key in data[i]) { //loop through data[i] 
 
    if (key != 'org' && key != 'org_cat') { //check this key is not org or org_cat 
 
     var thisData = {}; //create empty data object 
 
     var thisYear = key.toString().substring(0, 4); //get year by using substring 
 

 
     thisData.year = thisYear; //set year 
 
     thisData.passed = data[i][thisYear + '_passed']; //set passed at this year 
 
     thisData.total = data[i][thisYear + '_total']; //set total at this year 
 

 
     thisObj.data.push(thisData); //push this data to data array 
 
    } 
 
    } 
 
    var uniqueDates = []; //set empty array to use to remove duplicate items in data array 
 
    for (var j = 0; j < thisObj.data.length; j++) { //loop through data array created earlier 
 
    if (uniqueDates.indexOf(thisObj.data[j].year) < 0) { //if this year doesn't exist in unique array above, push it in 
 
     uniqueDates.push(thisObj.data[j].year); //push it in 
 
    } else { 
 
     thisObj.data.splice(j--, 1); //remove duplicate data 
 
    } 
 
    } 
 
    newData[data[i].org] = thisObj; //set data @ current org to be object created above 
 
} 
 
console.log('newData', newData) //log new data

+0

謝謝 - 是的,使用D3不是強制性的,只是認爲它可能會簡化事情! – Richard