2014-02-12 39 views
0

我正在使用存储在tsv文件中的数据创建d3.js的条形图。我想在每个栏中插入一个文本。我该怎么办? 我甚至试过这个solution,但不起作用。在d3js图表上添加文本 - 添加<text>元素

这里是我的函数的代码:

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

+0

P.S.我改变了标题。当我第一次阅读“在文本里面插入文本”时,我预料到问题在于你实际上试图将文本添加为​​矩形的子元素(例如在HTML中添加一个跨度的div),这是混淆的另一个来源为d3/svg初学者。 – AmeliaBR

回答

2

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")这样称呼它。

0

当你画一个轴,它创造了里面的每个标签单独<text>元素SVG内部的轴组。因此,如果您尝试选择SVG中的所有<text>元素,则要选择所有轴标签。如果您的轴标签多于文本元素的数据,则您的enter()选择将为空,并且不会发生任何情况。

为了确保您只选择了正确的<text>元素,请给文本标签一个类来区分它们与轴标签。然后使用这个类来缩小您的选择:

svg.selectAll("text.bar-label") 
    .data(data) 
    .enter() 
    .append("text") 
    .attr("class", "bar-label") 
+0

对不起。我想要这种行为http://alignedleft.com/content/03-tutorials/01-d3/130-making-a-bar-chart/18.png。用你的代码修改轴标签 –