Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

195
Views
Having text in D3.js rectangles

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});
            
        }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!