d3条形图将文本附加到栏

时间:2015-06-11 18:20:28

标签: javascript text d3.js charts

我已经按照教程制作了一个条形图,来自alignleft的Scott Murray。我遇到了数据集问题,并将数据集作为文本添加到栏中。

下图:1条形图:来自教程,第2条形图:我想如何显示文字。

enter image description here

到目前为止,这是我的代码:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Tutorial d3 bar chart!</title>
        <script type="text/javascript" src="d3/d3.v3.js"></script>
    </head>
    <body>
        <script type="text/javascript">
            //Width and height
            var w = 500;
            var h = 100;
            var i = 0;
            var barPadding = 1;

            var dataset =  [
                        {key:"Department1", value:6234490},
                        {key:"Department 2", value:9700},
                        {key:"Department 3", value:2954},
            ];

            //Width and height
            var w = 500;
            var h = 100;
            var barPadding = 1;

            //Create SVG element
            var svg = d3.select("body")
                        .append("svg")
                        .attr("width", w)
                        .attr("height", h);

            svg.selectAll("rect")
               .data(dataset)
               .enter()
               .append("rect")
               .attr("x", function(d, i) {
                    return i * (w / dataset.length);
               })
               .attr("y", function(d) {
                    return h - (d * 4);
               })
               .attr("width", w / dataset.length - barPadding)
               .attr("height", function(d) {
                    return d * 4;
               })
               .attr("fill", function(d) {
                    return "rgb(0, 0, " + (d * 10) + ")";
               });

            svg.selectAll("text")
               .data(dataset)
               .enter()
               .append("text")
               .text(function(d) {
                    for(int i = 0; i < dataset.length; i++){
                        return d[i].key;
                    }

               })
               .attr("text-anchor", "middle")
               .attr("x", function(d, i) {
                    return i * (w / dataset.length) + (w / dataset.length - barPadding) / 2;
               })
               .attr("y", function(d) {
                    return h - (d * 4) + 14;
               })
               .attr("font-family", "sans-serif")
               .attr("font-size", "11px")
               .attr("fill", "white");
        </script>
    </body>
</html>

我试图在这部分中添加文字:

svg.selectAll("text")
               .data(dataset)
               .enter()
               .append("text")
               .text(function(d) {
                    for(int i = 0; i < dataset.length; i++){
                        return d[i].key;
                    }

               })

但这只是给了我这个错误: enter image description here

我希望你们能帮助我。

2 个答案:

答案 0 :(得分:2)

尝试将int更改为var,javascript中不存在。

答案 1 :(得分:0)

d3js中的每个函数都提供对数据和索引的访问。 只需使用此

svg.selectAll("text")
           .data(dataset)
           .enter()
           .append("text")
           .text(function(d){return d.key;}
           })

修改

svg.selectAll("g")
           .data(dataset)
           .enter()
           .append("g")
           .append("rect")
           .attr("x", function(d, i) {
                return i * (w / dataset.length);
           })
           .attr("y", function(d) {
                return h - (d * 4);
           })
           .attr("width", w / dataset.length - barPadding)
           .attr("height", function(d) {
                return d * 4;
           })
           .attr("fill", function(d) {
                return "rgb(0, 0, " + (d * 10) + ")";
           })
           .append("text")
           .text(function(d) {                 
                    return d.key;
           })
           .attr("text-anchor", "middle")
           .attr("x", function(d, i) {
                return i * (w / dataset.length) + (w / dataset.length - barPadding) / 2;
           })
           .attr("y", function(d) {
                return h - (d * 4) + 14;
           })
           .attr("font-family", "sans-serif")
           .attr("font-size", "11px")
           .attr("fill", "white");