0

I am trying to create an interactive graph using d3.js library. I want to alternate between feeding data into my d3 object from either a stored json file or directly from a dictionary (that gets created upon some user input).

To read from a file I use

d3.json('/path/to/file.json')

Upon user input a dictionary d is created. I tried using

user_file = json.dumps(d)
d3.json(user_file)

to no avail.

I have a workaround -- upon user input, create a user_file.json file with json.dump(d, '/path/to/user_file.json') and read directly from there with d3.json('/path/to/user_file.json'). Other than just feeling less elegant this adds the additional problem that the new json file gets cached.

Any advice? I'm new to javascript so advice on 'best practices' would also be appreciated.

1 Answer 1

1

d3.json() normally has two arguments: the url ('/path/to/file.json') and the function that will handle the result which you leave out of your examples. This handler is often an anonymous function declared inside the call to d3.json(), but it can be a named function. I would define a named function to handle the json result, then just use the same function to handle the data created on user input.

function resultHandler(error, data){
    ... do stuff w/ data ...
}

d3.json('/path/to/file.json', resultHandler);

function userInputHandler() {
   ... create data ...
   resultHandler(null, d);
}
Sign up to request clarification or add additional context in comments.

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.