18

I am trying to build a web project where I get details in JSON format, for example:

{
    "file_id": 333, 
    "t": "2016-03-08 12:00:56"
}

I was trying to show the output in d3 js bar chart. The problem I am facing is the code I've got is working for a JSON file but not for an object deserialized from the JSON. Can any one help me out with this?

The part of the working script for JSON file is this:

d3.json("FILENAME", function(error, data) {
    data = JSON.parse(data);
    x.domain(data.map(function(d) { return d.letter }));
    y.domain([0, d3.max(data, function(d) { return d.frequency })]);

If I change the filename to an object its not working.

5
  • Erm... How is this Java? Commented Mar 10, 2016 at 7:58
  • 3
    read the documentation here github.com/mbostock/d3/wiki/Requests it clearly states you have to give a path to file, the function is not made to take an object Commented Mar 10, 2016 at 7:59
  • ys , but how can i proceed if i have a json object Commented Mar 10, 2016 at 8:02
  • have your tried this example >> jsfiddle.net/enigmarm/3HL4a/13 Commented Mar 10, 2016 at 8:08
  • Check this >> stackoverflow.com/questions/29618805/… Commented Mar 10, 2016 at 8:09

2 Answers 2

28

being a JS library D3 works with the JS objects(along with other JS data types) only, d3.json is just a method to load the object from an external file. So if you dont need to load data from external file then dont use d3.json

//d3.json("FILENAME", function(error, data) {
    //data = JSON.parse(data);
    var data = {
        "file_id": 333, 
        "t": "2016-03-08 12:00:56"
    }; //your own object
    x.domain(data.map(function(d) { return d.file_id}));
    y.domain([0, d3.max(data, function(d) { return d.t})]);

hope it helps

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

2 Comments

What about pulling the json data from a cookie? I pulled the json data from a cookie to a variable, but cannot get my graph to update using the d3.json function. It appears from this answer that one cannot do this with the d3.json function. Can you use the function and pull the data from a cookie, or just a file only?
It doesn't matter from where and how you pull the data. in the end its all about passing required data to d3 methods. If you need to fetch it from a file use d3.json, otherwise it's not required. It will be better if you can provide some code about how you are doing it, otherwise it's hard to guess what's the problem you are facing. a jsfiddle or any similar service link to your code will be more helpful.
1

If I change the filename to an object its not working.

Then you should call it in a function and pass the object in the params:

var obj = {}; // the object to be be passed for chart

function updateGraph(o){ // get in the params here
    var x = d3.scale.ordinal().rangeRoundBands([0, width]),
        y = d3.scale.linear().range([height, 0]);

    x.domain(o.letter);
    y.domain([0, o.frequency]);
}

updateGraph(obj); // <----pass it here

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.