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

298
Views
D3 JS - Why d3 is not appending a string?

I have this json file:

[
    {
        "id": "1",
        "name": "Hello world",
        "shape": "rect",
        "fill": "pink"
    }
]

And I also have this javascript file:

const svg = d3.select('svg')
var stringToHTML = function (str) {
    var parser = new DOMParser();
    var doc = parser.parseFromString(str, 'text/html');
    return doc.body;
};
d3.json("tree.json").then(data => {
    const elements = svg.selectAll('*')
        .data(data);
    elements.enter()
        .append("rect")
            .attr("x", 20)
            .attr("y", 20)
            .attr("width", 100)
            .attr("height", 100)
            .attr("fill", d => d.fill)
    elements.enter()
        .append('text')
            .attr("x", 50)
            .attr("y", 60)
            .html("text", d => {d.name})
    
})

and this html file:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <svg width="600" height="600">
    </svg>

    <script src="https://d3js.org/d3.v7.min.js"></script>
    <script src="index.js"></script>
</body>

</html>

Now the output is:

1st output

but when I switch "rect" with d => d.shape it doesn't behave as before.

d3.json("tree.json").then(data => {
    const elements = svg.selectAll('*')
        .data(data);
    elements.enter()
        .append(d => d.)
            .attr("x", 20)
            .attr("y", 20)
            .attr("width", 100)
            .attr("height", 100)
            .attr("fill", d => d.fill)

But how is it different than the previous one? I also tried outputting d.shape, and it prints string. Then how can I make something that will create a shape according to the shape data?

about 4 years ago · Juan Pablo Isaza
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!