Home > Blockchain >  Having text in D3.js rectangles
Having text in D3.js rectangles

Time:12-20

I'm making an Ajax call to fetch data from an API and drawing rectangles from the returned data objects.

How can I have text (the object id) in the rectangles?

        let height = window.innerHeight;
        
        var canvas = d3.select("body")
            .append("svg")
            .attr("width", "100%")
            .attr("height", height)
            
        $(document).ready(function(){
            getData();
        });
        
        function getData() {
            $.ajax({
                url: "http://api.ntjp.se/coop/api/v1/connectionPoints",
                success: function(result){
                    drawRectangles(result);
                }
            });
        }
        
        function drawRectangles(data) {
            
            var rectangles = canvas.selectAll("rect")
                .data(data)
                .enter()
                .append("rect")
                .attr("width", 80)
                .attr("height", 50)
                .style("stroke-width", "1")
                .style("fill", "none")
                .style("stroke", "black")
                .attr("y", function(d, i) { return i * 100});
            
        }

CodePudding user response:

In SVG, shapes like rect or text cannot contain other shapes. The conventional way around is is to take the special element g (group), which does not have a shape itself, but can hold multiple shape elements.

Then, you can draw the rect and the text for each g element, and position the g element with it's contents exactly where you want it.

let height = window.innerHeight;

var canvas = d3.select("body")
  .append("svg")
  .attr("width", "100%")
  .attr("height", height);

var data = [
  {
    "id": 4,
    "platform": "NTJP",
    "environment": "PROD",
    "snapshotTime": "2021-12-12T00:05:06 0100"
  },
  {
    "id": 5,
    "platform": "SLL",
    "environment": "PROD",
    "snapshotTime": "2021-12-12T00:00:04 0100"
  },
  {
    "id": 6,
    "platform": "NTJP",
    "environment": "QA",
    "snapshotTime": "2021-12-12T00:05:05 0100"
  },
  {
    "id": 7,
    "platform": "SLL",
    "environment": "QA",
    "snapshotTime": "2021-12-12T00:00:05 0100"
  },
  {
    "id": 8,
    "platform": "NTJP",
    "environment": "TEST",
    "snapshotTime": "2021-12-12T00:05:06 0100"
  }
];

var nodes = canvas.selectAll(".node")
  .data(data)
  .enter()
  .append("g")
  .attr("class", "node")
  .attr("transform", function(_, i) {
    return `translate(0, ${i * 100})`;
  })

nodes
  .append("rect")
  .attr("width", 80)
  .attr("height", 50)
  .style("stroke-width", "1")
  .style("fill", "none")
  .style("stroke", "black");

nodes
  .append("text")
  // To make the `text` node have access to the data related to the `g` node
  .datum(function(d) { return d; })
  .attr("y", 25)
  .attr("x", 40)
  .attr("dy", "0.5em")
  .attr("text-anchor", "middle")
  .text(function(d) { return d.platform; });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/6.2.0/d3.min.js"></script>

  • Related