2014-01-20 44 views
4

enter image description here有誰知道我會如何將多條形圖製作成單個系列?在一個我已經看到我想要我的圖形看起來的工作示例中,該函數正在用於數據。使用nvd3庫顯示單個系列多條形圖表

function dataFactory(seriesNum, perSeries) { 
return new d3.range(0,seriesNum).map(function(d,i) { return { 
key: 'Stream ' + i, 
values: new d3.range(0,perSeries).map(function(f,j) { 
    return { 
      y: 10 + Math.random()*100, 
      x: j 
     } 
}) 
}; 
}); 
} 

下面是我目前使用的代碼,我也將上傳的圖片,所以你可以看到我的標籤關閉位置,因爲它不是單一系列。

function loadBar(){ 
$.getJSON('data5.json', function (json) { 
var data1 = []; 
for (var key in json) { 
    if (json.hasOwnProperty(key)) { 
     var item = json[key]; 
     data1.push({ 
      key: item.key, 
      values: item.values 
     });    
    } 
} 

var chart; 
nv.addGraph(function() { 
chart = nv.models.multiBarChart() 
    .color(d3.scale.category10().range()) 
    .margin({bottom: 100}) 
    .transitionDuration(300) 
    .delay(0) 
    //.rotateLabels(45) 
    ; 

chart.multibar 
    .hideable(true); 

chart.reduceXTicks(false).staggerLabels(true).groupSpacing(0.2); 

chart.xAxis 
    .axisLabel("Players") 
    .showMaxMin(false); 

chart.yAxis 
    .axisLabel('Hours Played') 
    .tickFormat(d3.format('d')); 

d3.select('#chart1 svg') 
    .datum(data1) 
    .call(chart); 

nv.utils.windowResize(chart.update); 

chart.dispatch.on('stateChange', function(e) { nv.log('New State:', JSON.stringify(e)); }); 

return chart; 
}); 
}); 
} 

$(document).ready(function(){ 
loadBar(); 
}); 

data5.json(以防萬一需要有人看到)

{ 
"Member1": { 
    "key":"test10", 
    "values": [ 
     { 
      "x": "test10", 
      "y": 20 
     } 
    ] 
}, 
"Member2":{ 
    "key":"test9", 
    "values": [ 
     { 
      "x": "test9", 
      "y": 10 
     } 
    ] 
}, 
"Member3":{ 
    "key":"test8", 
    "values": [ 
     { 
      "x": "test8", 
      "y": 4 
     } 
    ] 
}, 
"Member4":{ 
    "key":"test7", 
    "values": [ 
     { 
      "x": "test7", 
      "y": 12 
     } 
    ] 
}, 
"Member5":{ 
    "key":"test6", 
    "values": [ 
     { 
      "x": "test6", 
      "y": 30 
     } 
    ] 
}, 
"Member6":{ 
    "key":"test5", 
    "values": [ 
     { 
      "x": "test5", 
      "y": 8 
     } 
    ] 
} 
, 
"Member7":{ 
    "key":"test4", 
    "values": [ 
     { 
      "x": "test4", 
      "y": 27 
     } 
    ] 
}, 
"Member8":{ 
    "key":"test3", 
    "values": [ 
     { 
      "x": "test3", 
      "y": 17 
     } 
    ] 
}, 
"Member9":{ 
    "key":"test2", 
    "values": [ 
     { 
      "x": "test2", 
      "y": 2 
     } 
    ] 
}, 
"Member10":{ 
    "key":"test1", 
    "values": [ 
     { 
      "x": "test1", 
      "y": 55 
     } 
    ] 
} 
![enter image description here][2]} 

回答

5

用於多條形圖預期的數據格式是對象的陣列,其中的每一個表示數據系列。在每個系列對象中,應該有一個關鍵屬性命名該系列,以及一個包含數據點的值數組。 values數組應該有一個對象,每個bar都有一個分類x值和一個數值y值。

例如,如果我「字符串」的數據生成函數的結果(降低後的參數,所以我只能獲得每五個槓兩個數據系列),它看起來像這樣:

[{ 
    "key": "Stream0", 
    "values": [{ 
     "x": 0, 
     "y": 0.16284738584101344 
    }, { 
     "x": 1, 
     "y": 2.370283172738109 
    }, { 
     "x": 2, 
     "y": 0.1631208266452718 
    }, { 
     "x": 3, 
     "y": 0.24609871793543797 
    }, { 
     "x": 4, 
     "y": 1.5096133160633776 
    }] 
}, { 
    "key": "Stream1", 
    "values": [{ 
     "x": 0, 
     "y": 0.12566330679904006 
    }, { 
     "x": 1, 
     "y": 0.1321859413211272 
    }, { 
     "x": 2, 
     "y": 1.4798247902549135 
    }, { 
     "x": 3, 
     "y": 0.10870538273358979 
    }, { 
     "x": 4, 
     "y": 0.16155091711225184 
    }] 
}] 

該圖如下所示:
NVD3 MultiBar Chart

每個系列都以不同的顏色繪製。條形根據它們的x值並排分組,或者您可以切換到堆疊。

你越來越爲每個類別中的一個窄條的原因是因爲你有11 不同數據系列,每個具有不同 x值一個酒吧。因此,對於每個x值,該圖保留並排繪製的數據序列的空間,即使它們沒有數據也是如此。

您或者需要將所有條形圖組合成一個數據系列,並通過x值標識測試,或者您需要爲它們提供所有相同的x值,並通過系列鍵標識測試。

我知道你已經有了第一個選項,基於your other question on the discrete bar chart function

修改這個代碼,看看是什麼樣子的其他方式(11系列,每個只有一個酒吧)最簡單的方法,就是告訴圖表功能,只需使用恆定值X:

chart.x(function(d){return "test";}) 

了,並且你類似的(多個系列,每個只有一個數據點)的數據,你會得到從條形圖切換到堆積面積圖的圖表,就像這樣: NVD3 Bar Chart, many series, one x-value, grouped display NVD3 Bar Chart, many series, one x-value, stacked display

(PS,你當然想要刪除數字格式tickO rmat功能讓你在這些圖片中不會得到「NaN」!)

+0

哇,非常感謝。 – DiamonW