简体   繁体   中英

how to add text to cells in d3.grid example d3-js-building-a-grid-of-rectangles

I am new to d3.js and I need to design a visualization that is similar to what is described in here (http://stackoverflow.com/questions/10457170/d3-js-building-a-grid-of-rectangles/10465456#10465456) grid implementation. I am trying to add text to each of the cells having text d.value but not working.

I tried adding .text(function(d){return d.value;} to row.selectAll(".cell") but didn't help. Any ideas how should I do it. I tried something like , but not sure I am doing the right thing.

col.selectAll("rect").append("text")
           .data(function (d) { return d; })
           .enter()
           .append("text")
            .text(function(d,i) {
                console.log(d);
              console.log(i);
                return d.value;
            }) 
            .attr("x", function(d) { return d.x; })
         .attr("y", function(d) { return d.y; });

My question here is how can I access d.value and add it as text to .cell?

Adding a text element to your <rect> elements has no intrinsic meaning, the text elements can be added outside of the rect s, or you can create another sub <g> element for each cell that contains its text and the rect . jsfiddle of the first scenario here: http://jsfiddle.net/Kmf7g/1/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM