简体   繁体   English

如何在D3中堆叠条形图的每个布局上显示数据值

[英]How to display data value on every layout of stacked barchart in d3

I have created a stacked bar chart in d3. 我在d3中创建了一个堆积的条形图。

Here I want to display the value like this below example (This is Simple Bar chart) 在这里,我想显示以下示例所示的值(这是简单的条形图)

http://bl.ocks.org/enjalot/1218567 http://bl.ocks.org/enjalot/1218567

But not outside the bar, inside the bar like below : 但是不在酒吧外面,在酒吧里面像下面这样:

http://bl.ocks.org/diethardsteiner/3287802 http://bl.ocks.org/diethardsteiner/3287802

This is my Stacked function which is working fine : 这是我的堆叠函数,可以正常工作:

function barStack(d) 
{
    var l = d[0].length
    while (l--) {
        var posBase = 0,
            negBase = 0;
        d.forEach(function (d) {
            d = d[l]
            d.size = Math.abs(d.y)
            if (d.y < 0) {
                d.y0 = negBase
                negBase -= d.size
            } else {
                d.y0 = posBase = posBase + d.size
            }
        })
    }
    d.extent = d3.extent(d3.merge(d3.merge(d.map(function (e) {
        return e.map(function (f) {
            return [f.y0, f.y0 - f.size]
        })
    }))))
    return d
}

For stacked Bar 对于堆叠的酒吧

 svg.selectAll(".series")
    .data(data)
    .enter()
  .append("g")
    .attr("class", "g")
    .style("fill", function (d, i) {return color(i)})
    .selectAll("rect")
    .data(Object)
    .enter()
  .append("rect")
    .attr("x", function (d, i) {return x(x.domain()[i])})
    .attr("y", function (d) { return y(d.y0) })
    .attr("height", function (d) { return y(0) - y(d.size) })
    .attr("width", x.rangeBand())
;

This was also running fine. 这也很好。 My data is like that 我的数据就是这样

var data = [{x:"abc",  y1:"3",  y2:"4",  y3:"10"},
        {x:"abc2", y1:"6",  y2:"-2", y3:"-3" },
        {x:"abc3", y1:"-3", y2:"-9", y3:"4"}
       ]

Now I want to show this value of y1, y2 and y3 in every stacked layout. 现在,我想在每个堆叠布局中显示y1,y2和y3的值。 I have tried this below code, but this is not displaying the value over layout. 我已经在下面的代码中尝试过此方法,但这未在布局中显示值。

svg.selectAll("text")
     .data(data)
     .enter()
     .append("text")
     .attr("transform", "translate(50,0)")
     .attr("text-anchor", "middle")
     .attr("x", function(d, i) {return (i * (width / data.length)) + ((width / data.length - 50) / 2);})
     .attr("y", function(d) {return y(0) - y(d.size) + 14;})
     .attr("class", "yAxis")
     .text(function(d) {return y(d.size);})
    ;   

Please help me on this, where I need to change or what exact I need to put instead of this or might be above code was totally wrong. 请帮我解决这个问题,在这里我需要更改或者我需要写些什么而不是上面的代码,或者上面的代码是完全错误的。 Please let me know if any more input required by me. 请让我知道我是否需要更多输入。 I have the total POC and I can share that too. 我拥有全部POC,我也可以分享。

i have added all code in jsfiddle 我已经在jsfiddle中添加了所有代码

http://jsfiddle.net/goldenbutter/HZqkm/

Here is a fiddle that does what you want. 是一个小提琴,可以满足您的需求。

var plots = svg.selectAll(".series").data(data)
    .enter()
    .append("g")
    .classed("series",true)
    .style("fill", function(d,i) {return color(i)})

plots.selectAll("rect").data(Object)
    .enter().append("rect")
    .attr("x",function(d,i) { return x(x.domain()[i])})
    .attr("y",function(d) { return y(d.y0)})
    .attr("height",function(d) { return y(0)-y(d.size)})
    .attr("width",x.rangeBand());

plots.selectAll("text.lab").data(Object)
    .enter().append("text")
    .attr('fill','black')
    .attr("text-anchor", "middle")
    .attr("x", function(d, i) { return x(x.domain()[i]) + (x.rangeBand()/2)})
    .attr("y", function(d) {return y(d.y0) + 20})
    .text(function(d) {return (d.size).toFixed(2);}); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM