0

I am trying to create a basic column chart using divs from the example in allignedleft and the one mike has posted. The chart works except when i add text, it inverses.

<!DOCTYPE html>
<head>
<style type="text/css">

div.abc {
 display: inline-block;
 width: 20px;
 height: 75px;
 background-color: teal;
 margin-right: 2px;
}

</style>
</head>
<body>
<script type="text/javascript" src="http://d3js.org/d3.v2.js?2.8.1"></script>
<script>

var dataset = [16,13,8,2,1,10,0,5,24,15,2,6,12];


d3.select("body").selectAll("div")
.data(dataset)
.enter()
.append("div")
.attr("class", "abc")
.style("height", function(d) {
    var barHeight = d * 5;
    return barHeight + "px";
})
.style("margin-top",function(d) {
    var barHeight = d * 5;
    return 500 - barHeight;
})
.style("text-align","center")
.style("color","black")
.text(function (d) { return d; });
</script>
</body>
</html>

thanks

Sriram

4

1 回答 1

0

问题是 div 中文本的垂直对齐方式。如果你添加

vertical-align: bottom;

到你的CSS,它应该再次工作。请注意,您的某些 div 对于文本来说太小了。在这些情况下,标签不会与其他标签对齐。我建议您为标签创建单独的 div 并相应地放置它们。

于 2013-04-18T18:52:25.120 回答