Elvira
Elvira

Reputation: 1440

d3 Bar Chart append text to bar

I've followed the tutorial to make a bar chart from Scott Murray from alignedleft. I'm having problem with my dataset and adding the dataset to a bar as text.

The image below: 1 bar chart: from the tutorial , 2nd bar chart: how I want to display the text.

enter image description here

Here's my code so far:

<!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>

I've tried to add the text in this part:

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

               })

But that just gives me this error: enter image description here

I hope you guys can help me out.

Upvotes: 2

Views: 11803

Answers (2)

Pbd
Pbd

Reputation: 1319

Every function in d3js provides access to data and the index. Just use this

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

EDIT

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");

Upvotes: 1

soote
soote

Reputation: 3260

Try changing int to var, int doesn't exist in javascript.

Upvotes: 3

Related Questions