How to load json object instead of json file

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

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

I tried to show the result in a d3 js histogram. The problem I am facing is that the code that I have is working for a JSON file, but not for a deserialized object from JSON. Can anyone help me with this?

Part of the script working file for JSON:

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 file name to an object, it does not work.

+4
source share
2 answers

JS- D3 JS ( JS), d3.json - . , , 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})]);

,

+10

, .

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
0

All Articles