堆叠条形图标签 - D3 [英] Stacked Bar Chart Labels - D3

查看:192
本文介绍了堆叠条形图标签 - D3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在向d3中的堆叠条形图添加数据标签。



我想将数据标签放在栏的中间。到目前为止,我只是想出了如何在每个栏的顶部添加数据标签。



这是我的代码

  var width = 750,
height = 500;

var x = d3.scale.ordinal()
.rangeRoundBands([0,width],.1);

var y = d3.scale.linear()
.rangeRound([height,0]);

var color = d3.scale.ordinal()
.range([#D70B16,#154CEF,#1A8A55]);

var xAxis = d3.svg.axis()
.scale(x)
.orient(bottom);

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

var tip = d3.tip()
.attr('class','d3-tip')
.offset([ - 10,0])
.html(function(d){
returnValue:+(d.y1 - d.y0)+;
})

var svgContainer = d3 .select(body)
.append(svg)
.attr(width,width)
.attr(height,height)
.append (g)
.attr(transform,translate(+ 30 +,+ 30 +));


d3.csv(data.csv,function(error,data){
color.domain(d3.keys(data [0])。filter (key){
return key!==circle;
}));

data.forEach(function(d){
var y0 = 0 ;
d.hours = color.domain()。map(function(name){
return {name:name,y0:y0,y1:y0 + = + d [name]};
});

d3.select('body')。append('pre')
.text(JSON.stringify(d.hours,null,''));

d.total = d.hours [d.hours.length - 1] .y1;
});

x.domain(data.map function(d){return d.circle;}));
y.domain([0,d3.max(data,function(d){return d.total;})]

svgContainer.append(g)
.attr(class,x axis)
.attr(transform,translate(0,+ height +) )
.call(xAxis);

svgContainer.append(g)
.attr(class,y axis)
.call yAxis)
.append(text)
.attr(transform,rotate(-90))
.attr(y,6)
。 attr(dy,.71em)
.style(text-anchor,end)
.text

var circle = svgContainer.selectAll(。circle)
.data(data)
.enter()。append(g)
.attr (class,g)
.attr(transform,function(d){
returntranslate(+ x(d.circle)+,0);
});

circle.selectAll(rect)
.data(function(d){return d.hours;})
.enter()
.append rect)
.attr(width,x.rangeBand())
.attr(y,function(d){return y(d.y1);})
.attr(height,function(d){return y(d.y0)-y(d.y1);})
.on(mouseover,tip.show)
。 on(mouseout,tip.hide)
.style(fill,function(d){return color(d.name);});


circle.selectAll(text)
.data(function(d){return d.hours;})
.enter b .append(text)
.attr(x,75)
.attr(y,function(d,i){return y(d.y1);})
.style(text-anchor,middle)
.text(test)

})
pre>

数据

  ,value1,value2,value3 
state1,80,10,20
state2,90,5,10
state3,70,15,35
state4,90,3,27
state5,50,25,55
state6,85,8,27

我实际上不知道如何获得X&



帮助我安排每个栏中间的标签。 >解决方案

您只需要调整 text 元素的 y

  circle.selectAll(text)
.data(function(d){return d.hours;} )
.enter()
.append(text)
.attr(x,75)
.attr(y,function(d,i) {return y(d.y1)+(y(d.y0)-y(d.y1))/ 2;})
.style(text-anchor,middle)
.text(test)


I'm trying to add data labels to stacked bar chart in d3.

I wanted the data labels to be in the middle of the bar. So far i just figured out how to add data labels on top of each bar. But actually i wanted those labels to be in the middle of each bar.

Here's my code :

var width = 750,
height = 500;

var x = d3.scale.ordinal()
        .rangeRoundBands([0, width], .1);

var y = d3.scale.linear()
        .rangeRound([height, 0]);

var color = d3.scale.ordinal()
            .range(["#D70B16", "#154CEF", "#1A8A55"]);

var xAxis = d3.svg.axis()
            .scale(x)
            .orient("bottom");

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

var tip = d3.tip()
        .attr('class', 'd3-tip')
        .offset([-10, 0])
        .html(function(d) {
        return "Value: " + (d.y1 - d.y0) + "";
        })

var svgContainer = d3.select("body")
                    .append("svg")
                    .attr("width", width)
                    .attr("height", height)
                    .append("g")
                    .attr("transform", "translate(" + 30 + "," + 30 + ")");


d3.csv("data.csv", function(error, data) {
  color.domain(d3.keys(data[0]).filter(function(key) { 
    return key !== "circle"; 
    }));

  data.forEach(function(d) {
    var y0 = 0;
    d.hours = color.domain().map(function(name) { 
      return {name: name, y0: y0, y1: y0 += +d[name]}; 
      });

    d3.select('body').append('pre')
            .text(JSON.stringify(d.hours, null, '  '));

    d.total = d.hours[d.hours.length - 1].y1;
  });

  x.domain(data.map(function(d) {return d.circle;}));
  y.domain([0, d3.max(data, function(d) {return d.total;})])

  svgContainer.append("g")
              .attr("class", "x axis")
              .attr("transform", "translate(0," + height + ")")
              .call(xAxis);

  svgContainer.append("g")
              .attr("class", "y axis")
              .call(yAxis)
              .append("text")
              .attr("transform", "rotate(-90)")
              .attr("y", 6)
              .attr("dy", ".71em")
              .style("text-anchor", "end")
              .text("Login Hours");

  var circle = svgContainer.selectAll(".circle")
              .data(data)
              .enter().append("g")
              .attr("class", "g")
              .attr("transform", function(d) { 
                return "translate(" + x(d.circle) + ",0)"; 
              });

  circle.selectAll("rect")
        .data(function(d) { return d.hours; })
        .enter()
        .append("rect")
        .attr("width", x.rangeBand())
        .attr("y", function(d) { return y(d.y1); })
        .attr("height", function(d) { return y(d.y0) - y(d.y1); })
        .on("mouseover", tip.show)
        .on("mouseout", tip.hide)
        .style("fill", function(d) { return color(d.name); });


  circle.selectAll("text")
        .data(function(d) { return d.hours; })
        .enter()
        .append("text")
        .attr("x", 75)
        .attr("y", function(d, i) { return y(d.y1) ; })
        .style("text-anchor", "middle")
        .text("test")

})

Data :

state,value1, value2, value3
state1, 80, 10, 20
state2, 90, 5, 10
state3, 70, 15, 35
state4, 90, 3, 27
state5, 50, 25, 55
state6, 85, 8, 27

I actually couldn't figure out how to get the mid point of X & Y co-ordinates of each bar.

Help me in arranging the labels in the middle of each bar.

解决方案

You just need to adjust the y coordinate of the text elements:

circle.selectAll("text")
      .data(function(d) { return d.hours; })
      .enter()
      .append("text")
      .attr("x", 75)
      .attr("y", function(d, i) { return y(d.y1) + (y(d.y0) - y(d.y1))/2; })
      .style("text-anchor", "middle")
      .text("test")

这篇关于堆叠条形图标签 - D3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆