0

I want to write a code snippet in d3js that will read a json file, and display a simple bar chart based on the contents of the json file. My json file, mydata.json, is:

[
    {“name”: “Apollo_Diomedes”, “age”: 300},
    {“name”: “Bjorn_the_Fellhanded”, “age”: 900},
    {“name”: “Jonah_Orion”, “age”: 500}
]

d3js code snippet, from index.html:

<body>
<script>
d3.json("mydata.json", function(data) {

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

    canvas.selectAll("rect")
        .data(data)
        .enter()
            .append("rect")
            .attr("width", function(x) { return x.age; })
            .attr("height", 45)
            .attr("y", function(x, i) { return i * 50; })
            .attr("fill", "blue")
    });
</script>
</body>

I'm using d3 v4: <script src="http://d3js.org/d3.v4.js"></script>.

I've tried opening the index.html file directly, and opening a local server and navigating to localhost:8080 as directed here; both just show me a blank screen. Both index.html and mydata.json are saved in the same folder on my desktop.

For those interested, it's actually this slightly outdated youtube tutorial; it runs d3 v3.

P.S. I can read in info from a CSV, but not json.

1
  • you have a problem with parsing the JSON data. The problem is in your JSON formatted data not how you call it. i just replace your “ with ' and it works just fine [ {"name": "Apollo_Diomedes", "age": 300}, {"name": "Bjorn_the_Fellhanded", "age": 900}, {"name": "Jonah_Orion", "age": 500} ] Commented Feb 26, 2017 at 15:50

1 Answer 1

1

It sounds like you have problems with parsing the JSON data. Try replacing your quotation marks to the English ones (").

And in general, always check the console log of your Web browser. I am certain that you will some error printed there.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.