admin管理员组文章数量:1434959
Here is my code, the text is appending when I inspect the element but it is not showing up
var data = [0, 1, 2];
var width = 100;
var height = 100;
var canvas = d3.select("body")
.append("svg")
.attr("height", 500)
.attr("width", 500)
.attr("fill", "red")
.append("g");
var someData = canvas.selectAll("rect")
.data(data)
.enter()
.append("rect")
.attr("height", height)
.attr("width", width)
.attr("fill", "red")
.attr("transform", function(d){ return "translate(" + height * d + ",0)";});
someData.append("text")
.attr("transform", function(d){ return "translate(" + height * d + ",0)";})
.attr("font-size", "2em")
.attr("color", "black")
.text(function(d){ return d;});
/
What did I do wrong?
Here is my code, the text is appending when I inspect the element but it is not showing up
var data = [0, 1, 2];
var width = 100;
var height = 100;
var canvas = d3.select("body")
.append("svg")
.attr("height", 500)
.attr("width", 500)
.attr("fill", "red")
.append("g");
var someData = canvas.selectAll("rect")
.data(data)
.enter()
.append("rect")
.attr("height", height)
.attr("width", width)
.attr("fill", "red")
.attr("transform", function(d){ return "translate(" + height * d + ",0)";});
someData.append("text")
.attr("transform", function(d){ return "translate(" + height * d + ",0)";})
.attr("font-size", "2em")
.attr("color", "black")
.text(function(d){ return d;});
http://jsfiddle/2GAxG/
What did I do wrong?
Share Improve this question asked Apr 24, 2014 at 5:36 InstinctInstinct 2,2612 gold badges34 silver badges47 bronze badges 1- related: cambridge-intelligence./… – exchange Commented Aug 16, 2020 at 12:44
2 Answers
Reset to default 2Couple of Problems with your code:
The
<text>
cannot go inside a<rectangle>
in svg.The top level group being filled with a color, hides everything inside it.
A solution to the first point is to group the <rectangle>
& <text>
into sub groups of <g>
Update Code: (Demo)
var data = [0, 1, 2];
var width = 100;
var height = 100;
var canvas = d3.select("body")
.append("svg")
.attr("height", 500)
.attr("width", 500)
//.attr("fill", "red")
.append("g");
var someData = canvas.selectAll("rect")
.data(data)
.enter()
.append("g")
.append("rect")
.attr("height", height)
.attr("width", width)
.attr("fill", "red")
.attr("transform",
function(d){ return "translate(" + height * d + ",0)";});
canvas.selectAll("g")
.append("text")
.attr("transform",
function(d){ return "translate(" + height * d + ",30)";})
.attr("font-size", "2em")
.attr("color", "black")
.text(function(d){ return d;});
Are you trying to add text at specific positions within the canvas or within shapes on the canvas? See fiddle: http://jsfiddle/sjp700/6hAg7/
var margin = { top: 40, right: 4, bottom: 40, left: 20 },
width = 800,
height = 800;
var data = [100,200,300];
var svgContainer = d3.select("body").append("svg")
.attr("width", 800)
.attr("height", 800)
.style("background", "red")
.append('g')
.attr('transform', 'translate(' + margin.left + ', ' + margin.top + ')');
var rect = svgContainer.selectAll("rect")
.data(data)
.enter();
rect.append("rect")
.attr("x", function(d){ return d;})
.attr("y", function(d){ return d;})
.attr("width", 100)
.attr("height", 20)
.style("fill", "lightblue");
rect.append("text")
.style("fill", "black")
.style("font-size", "14px")
.attr("dy", ".35em")
.attr("x", function (d) { return d; })
.attr("y", function (d) { return d+10; })
.style("style", "label")
.text(function (d) { return d; });
本文标签: javascriptHow do I append text to my rect in d3Stack Overflow
版权声明:本文标题:javascript - How do I append text to my rect in d3? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745632505a2667357.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论