2015-08-30 141 views

回答

9

這只是一個倒車域,軸的事情,然後RECT算了一筆賬:

var y = d3.scale.ordinal() 
    .rangeRoundBands([height, 0], .1); // y becomes ordinal 

var x = d3.scale.linear() 
    .rangeRound([0, width]); // x becomes linear 

// change state group to be positioned in the y now instead of x 
var state = svg.selectAll(".state") 
     .data(data) 
     .enter().append("g") 
     .attr("class", "g") 
     .attr("transform", function(d) { return "translate(0," + y(d.State) + ")"; }); 

// rect calculations become 
state.selectAll("rect") 
    .data(function(d) { return d.ages; }) 
    .enter().append("rect") 
    .attr("height", y.rangeBand()) // height in now the rangeband 
    .attr("x", function(d) { return x(d.y0); }) // this is the horizontal position in the stack 
    .attr("width", function(d) { return x(d.y1) - x(d.y0); }) // this is the horizontal "height" of the bar 
    .style("fill", function(d) { return color(d.name); }); 

下面是完整的工作example

+0

謝謝你,完美的作品給我。 –

+0

我喜歡添加兩個特性:第一:條形內部的條形標籤,第二條:x軸用此數據輸入的時間刻度:狀態,int,分機 AL,00:13:00,00:16:02 AK, 00:20:03,0 AZ,00:58:52,0 AR 00:45:30,00:17:39 CA 00,00:54:08 CO 00:35:01, 0 CT,0,00:03:39 –