【问题标题】:Stacked Bar Chart Labels - D3堆积条形图标签 - D3
【发布时间】:2013-12-22 09:51:37
【问题描述】:

我正在尝试将数据标签添加到 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) {
        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")

})

数据

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

我实际上无法弄清楚如何获得每个条形的 X 和 Y 坐标的中点。

帮我安排每个条中间的标签。

【问题讨论】:

    标签: javascript css svg d3.js


    【解决方案1】:

    你只需要调整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")
    

    【讨论】:

    • 我还有一个疑问。我希望列名成为相应栏的标签。我该怎么做?
    • .text() 的动态函数,例如.text(function(d) { return d.name; }).
    • d.name 指的是列名吧?谢谢你,甚至帮助了我。我怎样才能打破这样的文字?假设我想使用 d3 写 3-4 行注释
    • 参见例如this question.
    猜你喜欢
    • 1970-01-01
    • 2016-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多