2016-10-12 11 views
1

積み重ねた図を描きました。ここでは、すべての長方形の先端に合計値(yaxis値を意味します)を配置しようとしています。私は細部をフェッチするようにコード化しましたが、ここでは問題はすべてのレイヤーの先端にあるすべてのレイヤー値を取得していますが、最後のレイヤー値だけを表示する必要があるということです。積み重なった棒グラフの各矩形の先端の積み上げ棒の高さd3を使った棒グラフ

この問題は次の図に示されています。 enter image description hereenter image description here

私のコードでは、誰も私を助けることができる

var fData =  
[{"orders":"A","Total_Orders":76,"A_Lines":123,"B_Lines":0,"C_Lines":0,"D_Lines":0,"Total_Lines":123,"Total_Units":3267}, 
{"orders":"B","Total_Orders":68,"A_Lines":0,"B_Lines":107,"C_Lines":0,"D_Lines":0,"Total_Lines":107,"Total_Units":3115}, 
{"orders":"C","Total_Orders":81,"A_Lines":0,"B_Lines":0,"C_Lines":123,"D_Lines":0,"Total_Lines":123,"Total_Units":3690}, 
{"orders":"D","Total_Orders":113,"A_Lines":0,"B_Lines":0,"C_Lines":0,"D_Lines":203,"Total_Lines":203,"Total_Units":7863}, 
{"orders":"AB","Total_Orders":62,"A_Lines":70,"B_Lines":76,"C_Lines":0,"D_Lines":0,"Total_Lines":146,"Total_Units":1739}, 
{"orders":"AC","Total_Orders":64,"A_Lines":77,"B_Lines":0,"C_Lines":79,"D_Lines":0,"Total_Lines":156,"Total_Units":2027}, 
{"orders":"AD","Total_Orders":100,"A_Lines":127,"B_Lines":0,"C_Lines":0,"D_Lines":144,"Total_Lines":271,"Total_Units":6467}, 
{"orders":"BC","Total_Orders":64,"A_Lines":0,"B_Lines":80,"C_Lines":84,"D_Lines":0,"Total_Lines":164,"Total_Units":1845}, 
{"orders":"BD","Total_Orders":91,"A_Lines":0,"B_Lines":108,"C_Lines":0,"D_Lines":135,"Total_Lines":243,"Total_Units":4061}, 
{"orders":"CD","Total_Orders":111,"A_Lines":0,"B_Lines":0,"C_Lines":132,"D_Lines":147,"Total_Lines":279,"Total_Units":5011}, 
{"orders":"ABC","Total_Orders":45,"A_Lines":58,"B_Lines":63,"C_Lines":55,"D_Lines":0,"Total_Lines":176,"Total_Units":1245}, 
{"orders":"ABD","Total_Orders":69,"A_Lines":105,"B_Lines":87,"C_Lines":0,"D_Lines":116,"Total_Lines":308,"Total_Units":4538}, 
{"orders":"ACD","Total_Orders":66,"A_Lines":91,"B_Lines":0,"C_Lines":88,"D_Lines":132,"Total_Lines":311,"Total_Units":4446},{ 
{"orders":"BCD","Total_Orders":68,"A_Lines":0,"B_Lines":84,"C_Lines":95,"D_Lines":111,"Total_Lines":290,"Total_Units":4187}, 
{"orders":"ABCD","Total_Orders":56,"A_Lines":96,"B_Lines":90,"C_Lines":93,"D_Lines":143,"Total_Lines":422,"Total_Units":6331}] 
var headers = ["A_Lines", "B_Lines", "C_Lines", "D_Lines"]; 
      var colors = ["#9999CC", "#F7A35C", "#99CC99", "#CCCC99"]; 
      var colorScale = d3.scale.ordinal() 
           .domain(headers) 
           .range(colors); 
      var layers = d3.layout.stack()(headers.map(function (count) { 
       return fData.map(function (d, i) { 
        // alert(d); 
        return { x: d.ORDER_TYPE, y: +d[count], color: colorScale(count) }; 
       }); 
      })); 
      //StackedBar Rectangle Max 

      var yStackMax = d3.max(layers, function (layer) { return d3.max(layer, function (d) { return d.y0 + d.y; }); }); 
      // Set x, y and colors 
      var xScale = d3.scale.ordinal() 
     .domain(layers[0].map(function (d) { return d.x; })) 
     .rangeRoundBands([25, width], .08); 

      var y = d3.scale.linear() 
       .domain([0, yStackMax]) 
       .range([height, 0]); 

      // var color = d3.scale.ordinal() 
      //.domain(headers) 
      // .range(["#9999CC", "#F7A35C", "#99CC99", "#CCCC99"]); 

      // Define and draw axes 
      var xAxis = d3.svg.axis() 
         .scale(xScale) 
         .tickSize(1) 
         .tickPadding(6) 
         .orient("bottom"); 

      var yAxis = d3.svg.axis() 
         .scale(y) 
         .orient("left") 
         .tickFormat(d3.format(".2s")); 

      var layer = svg.selectAll(".layer") 
       .data(layers) 
       .enter().append("g") 
       .attr("class", "layer") 
       .style("fill", function (d, i) { return colorScale(i); }); 

      var rect = layer.selectAll("rect") 
       .data(function (d) { return d; }) 
       .enter().append("rect") 
       .attr("x", function (d) { return xScale(d.x); }) 
       .attr("y", height) 
       .attr("width", xScale.rangeBand()) 
       .attr("height", 0) 
       .attr("class", function (d) { 
        return "rect bordered " + "color-" + d.color.substring(1); 
       }); 



      layer.selectAll("text.rect") 
      .data(function (layer) { return layer; }) 
      .enter().append("text") 
      .attr("text-anchor", "middle") 
      .attr("x", function (d) { return xScale(d.x) + xScale.rangeBand()/2; }) 
      .attr("y", function (d) { return y(d.y + d.y0) - 3; }) 
      .text(function (d) { return d.y + d.y0; }) 
      .style("fill", "4682b4"); 


      //********** AXES ************ 
      svg.append("g") 
       .attr("class", "x axis") 
       .attr("transform", "translate(0," + height + ")") 
       .call(xAxis) 
       .selectAll("text").style("text-anchor", "end") 
        .attr("dx", "-.8em") 
        .attr("dy", ".15em") 
        .attr("transform", function (d) { 
         return "rotate(-45)" 
        }); 

      svg.attr("class", "x axis") 
       .append("text") 
       .attr("text-anchor", "end") // this makes it easy to centre the text as the transform is applied to the anchor 
       .attr("transform", "translate(" + (width/2) + "," + (height + 60) + ")") // centre below axis 
       .text("Order Velocity Group"); 


      svg.append("g") 
       .attr("class", "y axis") 
       .attr("transform", "translate(20,0)") 
       .call(yAxis) 
       .append("text") 
       .attr("transform", "rotate(-90)") 
       .attr({ "x": -75, "y": -70 }) 
       .attr("dy", ".75em") 
       .style("text-anchor", "end") 
       .text("No. Of Lines"); 

      //********** LEGEND ************ 

      var legend = svg.selectAll(".legend") 
         .data(headers.slice().reverse()) 
        .enter().append("g") 
        .attr("class", "legend") 
        .attr("transform", function (d, i) { return "translate(" + i * (-100) + "," + (height + 50) + ")"; }); 

      legend.append("rect") 
       .attr("x", width - 18) 
       .attr("width", 18) 
       .attr("height", 18) 
      //.style("fill", color); 
       .style("fill", function (d, i) { return colors[i]; }) 
       .on("mouseover", function (d, i) { 
          svg.selectAll("rect.color-" + colors[i].substring(1)).style("stroke", "blue"); 
           }) 
       .on("mouseout", function (d, i) { 
          svg.selectAll("rect.color-" + colors[i].substring(1)).style("stroke", "white"); 
           }); 

      legend.append("text") 
        .attr("x", width - 24) 
        .attr("y", 9) 
        .attr("dy", ".35em") 
        .style("text-anchor", "end") 
        .text(function (d) { return d; }); 


      transitionStacked(); 
      function transitionStacked() { 

       y.domain([0, yStackMax]); 

       rect.transition() 
        .duration(500) 
        .delay(function (d, i) { return i * 10; }) 
        .attr("y", function (d) { return y(d.y0 + d.y); }) 
        .attr("height", function (d) { return y(d.y0) - y(d.y0 + d.y); }) 
        .transition() 
        .attr("x", function (d) { return xScale(d.x); }) 
        .attr("width", xScale.rangeBand()); 

       rect.on('mouseover', tip.show) 
       .on('mouseout', tip.hide) 

      }; 

以下に示します。ここで

+0

ホバリングされた矩形アイテムのすべてのレイヤーの合計を示すヒントを表示しますか? –

+0

いいえ私は2番目の画像のようにバーとしてテキストの合計を期待しています(ホバーではなく、矩形の上のテキストとして表示されます) –

+0

エラーはすべてのレイヤ値を取得していますが、両方の画像のx軸の「ABC」をよく見ると問題が発生します –

答えて

2

は何が必要です:

layer.selectAll("text.rect") 
    .data(fData) 
    .enter() 
    .append("text") 

しかし、このデータセットが正しいYSCALE値を持っていないとして、我々はする必要があります:まず、テキストのためのデータとしてfDataを設定しますフィルタを使用してそれを抽出します。

.attr("y", function (d) { 
    var filtered = fData.filter(function(e){ 
     return e.Total_Units == d.Total_Units 
    }); 
    return y(filtered[0].Total_Lines) - 3; 
}) 

すべて一緒:ここ

layer.selectAll("text.rect") 
    .data(fData) 
    .enter().append("text") 
    .attr("text-anchor", "middle") 
    .attr("x", function(d) { 
     return xScale(d.orders) + xScale.rangeBand()/2; 
    }) 
    .attr("y", function(d) { 
     var filtered = fData.filter(function(e) { 
      return e.Total_Units == d.Total_Units 
     }); 
     return y(filtered[0].Total_Lines) - 3; 
    }) 
    .text(function(d) { 
     return d.Total_Units 
    }) 
    .style("fill", "4682b4"); 

はあなたのフィドルです:https://jsfiddle.net/wnwb6meh/

+0

同じことをやってみましたが、ここで問題はスタックバーにテキストを配置していることです。 1つのバーに複数のパートが含まれているように、すべてのパートに配置されています。私は最後に(上の)部分だけが必要です。 –

+0

私はすぐに編集します。 –

+0

私はそれ自体の前にそれを試しましたが、ここでは最後の積み重ねバーの上に置く必要がありますが、それはすべてのバーに配置されています。私のfig1のABCバーを質問してください。 ABCのAスタックとBスタックバーの高さが非常に低い場合、配置している合計が重なっています。 –

関連する問題