我正在使用tsv文件中存储的数据使用d3.js创建条形图。我想在每个栏中插入文本。我该怎么办?
我什至尝试了这个solution,但是没有用。

这是我函数的代码:

var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;

var formatPercent = d3.format(".0%");

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

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

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

var yAxis = d3.svg.axis()
        .scale(y)
        .orient("left");

var svg = d3.select("body").append("svg")
        .attr("width", width + margin.left + margin.right)
        .attr("height", height + margin.top + margin.bottom)
      .append("g")
        .attr("transform", "translate(" + margin.left + "," + margin.top + ")");


function visualize(file){
    d3.tsv(file, function(error, data) {

      data.forEach(function(d) {
        d.weight = +d.weight;
      });

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


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

      svg.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("weight");

      svg.selectAll(".bar")
          .data(data)
        .enter().append("rect").style("fill", function (d){return d.color;})
          .attr("class", "bar")
          .attr("x", function(d) { return x(d.concept); })
          .attr("width", x.rangeBand())
          .attr("y", function(d) { return y(d.weight); })
          .attr("height", function(d) { return height - y(d.weight); });

       svg.selectAll("text")
       .data(data)
       .enter()
       .append("text")
       .text(function(d) {
            return d.concept;
       })
       .attr("text-anchor", "middle")
       .attr("x", x)
       .attr("y",y)
       .attr("font-family", "sans-serif")
       .attr("font-size", "11px")
       .attr("fill", "white");

    });

}


我所有带有tsv文件的代码都在这里:full code

最佳答案

AmeliaBR像往常一样是正确的,并且我只给出这个答案,因为在我处理它时,我看到自己更改了代码,以便它真正利用了Enter,Update,Exit选择范式。在这方面,我作了很多修改。这是代码FWIW:

var margin = {top: 20, right: 20, bottom: 30, left: 40},
    width = 960 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom;

var formatPercent = d3.format(".0%");

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

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

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

var yAxis = d3.svg.axis()
    .scale(y)
    .orient("left");

var svg = d3.select("body").append("svg")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + margin.top + margin.bottom)
.append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

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

svg.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("weight");

var g = svg.append("g");

function update(file){
d3.tsv(file, function(error, data) {

data.forEach(function(d) {
    d.weight = +d.weight;
});

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

var bar = g.selectAll(".bar")
    .data(data);

bar.enter()
  .append("rect")
    .attr("class","bar");

bar
    .style("fill", function (d){return d.color;})
    .attr("class", "bar")
    .attr("x", function(d) { return x(d.concept); })
    .attr("width", x.rangeBand())
    .attr("y", function(d) { return y(d.weight); })
    .attr("height", function(d) { return height - y(d.weight); });

bar.exit().remove();

var text = g.selectAll(".text")
    .data(data);

text.enter()
  .append("text")
    .attr("class","text");

text
    .attr("text-anchor", "right")
    .attr("x", function(d) { return x(d.concept); })
    .attr("y", function(d) { return y(d.weight) + 22;})
    .attr("font-family", "sans-serif")
    .attr("font-size", "11px")
    .attr("fill", "white")
    .text(function(d) {
    return d.concept;
});

text.exit().remove();
});
}


就像通过update("data16.tsv")然后是update("data15.tsv")来调用它。

关于javascript - 在d3js图表中的条形上方添加文本-未添加<text>元素,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21734419/

10-09 23:22