2017-05-03 52 views
1

我試圖創建一個基於以下CSV多線圖:D3多級嵌套的多線圖

storageSystem,poolId,availableVolumeCapacity,date 
system01,0,18031398,20170413 
system01,1,15626268,20170413 
system01,2,61256286,20170413 
system01,3,119514990,20170413 
system02,0,15046668,20170413 
system02,1,12486558,20170413 
system02,2,12303396,20170413 
system03,0,35171335,20170413 
system03,1,17263722,20170413 
system01,0,18031387,20170414 
system01,1,15626257,20170414 
system01,2,61256275,20170414 
system01,3,119514989,20170414 
system02,0,15046657,20170414 
system02,1,12486547,20170414 
system02,2,12303385,20170414 
system03,0,35171324,20170414 
system03,1,17263711,20170414 

這裏的數據是如何連接的關係表: csv relation table

到目前爲止,我已將我的嵌套密鑰定義爲storageSystem,但我如何將poolId設置爲子密鑰?我曾嘗試加入poolId,但這返回Error: <path> attribute d: Expected number,"MNaN,NaNLNaN,NaN".

var parseDate = d3.time.format("%Y%m%d").parse; 

d3.csv("ssytem.csv", function(error, data) { 
    if (error) { 
     throw error; 
    } else { 
     console.log(data); 
    } 
    data.forEach(function(d) { 
      d.date = parseDate(d.date); 
      d.availableVolumeCapacity = +d.availableVolumeCapacity; 
    }); 

    x.domain(d3.extent(data, function(d) { return d.date; })); 
    y.domain([0, d3.max(data, function(d) { return d.availableVolumeCapacity; })]); 

    var dataNest = d3.nest() 
     .key(function(d) {return d.storageSystem; }) 
     .key(function(d) { return d.poolId; }) 
     .entries(data); 

數據對象:

0: Object 
availableVolumeCapacity: 35171324 
date: Thu Apr 13 2017 00:00:00 GMT+0000 (Coordinated Universal Time) 
poolId: "0" 
storageSystem: "system03" 
1: Object 
availableVolumeCapacity: 17263711 
date: Thu Apr 13 2017 00:00:00 GMT+0000 (Coordinated Universal Time) 
poolId: "1" 
storageSystem: "system03" 
+0

請問你的'data'物體看起來像?你可以發佈嗎? – thedude

+0

@thedude當然。謝謝 – Clarkus978

+0

我編輯了我的答案並添加了一個如何訪問嵌套數據的示例 – thedude

回答

2

您再添key電話:

var dataNest = d3.nest() 
    .key(function(d) {return d.storageSystem; }) 
    .key(function(d) {return d.poolId; }) 
    .entries(data); 

這是怎麼dataNest看起來像:

[ 
    { 
    "key": "system01", 
    "values": [ 
     { 
     "key": "0", 
     "values": [ 
      { 
      "storageSystem": "system01", 
      "poolId": "0", 
      "availableVolumeCapacity": "18031398", 
      "date": "20170413" 
      }, 
      { 
      "storageSystem": "system01", 
      "poolId": "0", 
      "availableVolumeCapacity": "18031387", 
      "date": "20170414" 
      } 
     ] 
     }, 
     { 
     "key": "1", 
     "values": [ 
      { 
      "storageSystem": "system01", 
      "poolId": "1", 
      "availableVolumeCapacity": "15626268", 
      "date": "20170413" 
      }, 
      { 
      "storageSystem": "system01", 
      "poolId": "1", 
      "availableVolumeCapacity": "15626257", 
      "date": "20170414" 
      } 
     ] 
     }, 
     ... and so on 

注意到它有兩個層次,從而得到一個實際的數據對象,你將需要訪問一個分組結果成員如下:

var s = `storageSystem,poolId,availableVolumeCapacity,date 
 
system01,0,18031398,20170413 
 
system01,1,15626268,20170413 
 
system01,2,61256286,20170413 
 
system01,3,119514990,20170413 
 
system02,0,15046668,20170413 
 
system02,1,12486558,20170413 
 
system02,2,12303396,20170413 
 
system03,0,35171335,20170413 
 
system03,1,17263722,20170413 
 
system01,0,18031387,20170414 
 
system01,1,15626257,20170414 
 
system01,2,61256275,20170414 
 
system01,3,119514989,20170414 
 
system02,0,15046657,20170414 
 
system02,1,12486547,20170414 
 
system02,2,12303385,20170414 
 
system03,0,35171324,20170414 
 
system03,1,17263711,20170414`; 
 

 
const data = d3.csvParse(s); 
 

 
const dataNest = d3.nest().key(d => d.storageSystem).key(d => d.poolId).entries(data); 
 

 
const container = d3.select('#container'); 
 

 
const lists = container.selectAll('ul').data(dataNest); 
 
const listsEnter = lists.enter().append('ul').text(d => d.key) 
 

 
const items = lists.merge(listsEnter).selectAll('li').data(d => d.values); 
 
const itemsEnter = items.enter().append('li').text(d => `Pool: ${d.key}`) 
 

 
items.merge(itemsEnter).selectAll('p').data(d => d.values) 
 
    .enter().append('p').text(d => `Available Capacity: ${d.availableVolumeCapacity}`)
ul { 
 
    font-weight: bold; 
 
} 
 

 
li { 
 
    font-weight: normal; 
 
    margin: 10px 0; 
 
} 
 

 
p { 
 
    font-size: 13px 
 
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.8.0/d3.min.js"></script> 
 
<div id="container"></div>