2016-12-14 36 views
0

我想根據條形圖上顯示的條數來調整條形圖的寬度。這是我的圖表,它顯示了足夠寬度的記錄,並且很好地顯示。 enter image description here計算dc.js中條形圖中的條數

這裏是另一種情況,其中只有兩個條以相同的寬度顯示,看起來很奇怪。 enter image description here

對於這種情況,我需要根據酒吧的數量調整圖表的寬度,以便我的圖表看起來很漂亮。

這裏是我的腳本

var width = ??; // Here how should I calculate width based on the number of bars? 
redrawCustomerCodeBarChart 
     .width(width) 
     .height(400) 
     .margins({top: 10, right: 10, bottom: 70, left: 30}) 
     .dimension(customerNamer) 
     .group(customerNameGroupr) 
     .x(d3.scale.ordinal().domain(newCoh.map(function (d) {return d.customerName; }))) 
     .xUnits(dc.units.ordinal) 
     .elasticY(true) 
     .renderHorizontalGridLines(true) 
     .on('renderlet',function(chart){ 
       chart.selectAll("g.x text") 
       .attr('dx', '-15') 
       .attr('transform', "rotate(-55)"); 
      }) 
     .on('filtered.monitor', function(d) { 
       // return d.key + ' (' + d.value + ')'; 
       if(d.value !== undefined) 
       size= d.value; 
      }) 
     .controlsUseVisibility(true); 
+0

在這種情況下,我們可以使用'.valueAccessor'或'.keyAccessor'來返回鍵值對 – ashishraaj

回答

1

在大多數dc.js圖表​​的一部分,該數據從group.all()讀取。所以酒吧的數量是簡單的

customerNameGroupr.all().length 

然後適當地相乘。

如果作爲東西被過濾的條數可能會改變(使用remove_empty_bins爲例),那麼你會希望寬度設置每個重繪之前:

chart.on('preRedraw', function(chart) { 
    chart.width(chart.group().all().length * BAR_WIDTH); 
}); 

這可能是凌亂的,但。

+0

,並且可以說哪個屬性將用於設置或固定條的寬度。 – ashishraaj

+0

或者我們不能將條形圖的寬度設置爲自動。並感謝戈登:) – ashishraaj

+1

有趣的是,它看起來像條寬度總是計算,並沒有辦法直接設置它。所以我認爲你設定圖表寬度的技巧是你可以做的最好的。 – Gordon