Popular a Chart.js with ajax.get

Asked

Viewed 397 times

-1

Talk guys, I have a question here in my project. I have a graph that is like this: inserir a descrição da imagem aqui

And I want to popular it with the data of a webservice in PHP, which will return a JSON and on each button that it clicks, I make a new request. Knowing that the method of storing his data is like this:

data: [ [1, 34], [2, 25], [3, 19], [4, 34], [5, 32], [6, 44] ] };

Now my question is: How do I allocate the data in this way in the graph’s 'date'? Assuming my JSON comes like this:

   [
    {
        "content":[
            {
                "data": "2015-05-05",
                "volume": "280"
            },
            {
                "data": "2015-05-06",
                "volume": "20"
            }
        ]
    }
]

I’ve had difficulties using ajax request data in graphics and ended up leaving pig with PHP in the middle anyway, but this time there is no way haha. If anyone knows how to help, I’d appreciate it. Hugs.

1 answer

0

Using JSON.parse you can access the data within the returned JSON, as shown in the example below:

var retorno =
{
    "content":[
        {
            "data": "2015-05-05",
            "volume": "280"
        },
        {
            "data": "2015-05-06",
            "volume": "20"
        }
    ]
}
objeto = JSON.parse(retorno);
objeto.content[1].data; // "2015-05-05"
objeto.content[1].volume; // "280"
objeto.content[2].data; // "2015-05-06"
objeto.content[2].volume; // "20"
  • understood, but for me to put this data inside Chart I would do how? taking into account the model there of date

  • On the server-side: Put a JSON type response equal to the content attribute. On the front with JS, make an AJAX request, and put the response in content

Browser other questions tagged

You are not signed in. Login or sign up in order to post.