3

data.php is returning:

{name: 'orcamento', data: [14000.00,8500.00,0.00]},{name: 'real', data: [2038.00,120.00,15000.00]},{name: 'desvio', data: [-11962.00,-8380.00,15000.00]}

I want to use this data to feed a chart. If I type the following code, it works well:

$(document).ready(function(){
    var options={
        chart: {
            renderTo:'container',
            type: 'bar'
        },
        title: {
            text: 'Total Anual'
        },
        xAxis: {
            categories: ['Vendas', 'Serviços', 'Outras']
        },
        series: [{name: 'orcamento', data: [14000.00,8500.00,0.00]},{name: 'real', data: [2038.00,120.00,15000.00]},{name: 'desvio', data: [-11962.00,-8380.00,15000.00]}]
    };
});

But the following code doesn't work:

$(document).ready(function(){
    var options={
        chart: {
            renderTo:'container',
            type: 'bar'
        },
        title: {
            text: 'Total Anual'
        },
        xAxis: {
            categories: ['Vendas', 'Serviços', 'Outras']
        },
        series: [$.get('data.php')]
    };
});

Why?

EDIT: I also tried this without luck:

$(document).ready(function(){
    var dados=[];
    $.ajax({
        url:'data.php',
        success: function(response){
            dados=response
        }
    });

    var options={
        chart: {
            renderTo:'container',
            type: 'bar'
        },
        title: {
            text: 'Total Anual'
        },
        xAxis: {
            categories: ['Vendas', 'Serviços', 'Outras']
        },
        series: [dados] 
    };
});

EDIT2: Also tried the following but I get an empty chart:

$(document).ready(function(){
    var dados=[];

    $.ajax({
        url:'data.php',
        success: function(response){
            dados=response
        },
        async:false
    });

    var options={
        chart: {
            renderTo:'container',
            type: 'bar'
        },
        title: {
            text: 'Total Anual'
        },
        xAxis: {
            categories: ['Vendas', 'Serviços', 'Outras']
        },
        series: [dados] 
    };

    var chart = new Highcharts.Chart(options);
});

In JSONLint, I get the error:

Parse error on line 1:
{    name: 'orcamento', 
-----^
Expecting 'STRING', '}'
2
  • First off, you need to return valid JSON. It looks like you need an array wrapper [] around your entire JSON structure. Second, you can load async result into a variable like you are attempting to do, you need to set options.series from within a success callback to $.get() Commented Mar 27, 2014 at 19:53
  • Thanks, I checked JSONLint, and I got: Parse error on line 1: { name: 'orcamento', -----^ Expecting 'STRING', '}' Commented Mar 27, 2014 at 20:07

2 Answers 2

0

In your edit question, the dados var probably isn't set when you define options. Because ajax response is made asynchronous. Try editing like this:

$(document).ready(function(){
    var dados=[];
    var options = {};
    $.ajax({
        url:'data.php',
        success: function(response){
            dados=$.parseJSON(response); // EDITED
            options={
                chart: {
                    renderTo:'container',
                    type: 'bar'
                },
                title: {
                    text: 'Total Anual'
                },
                xAxis: {
                    categories: ['Vendas', 'Serviços', 'Outras']
                },
                series: [dados] 
            };
            var chart = new Highcharts.Chart(options);
        }
    });
});

In PHP JSON result, put [] like this:

[{name: 'orcamento', data: [14000.00,8500.00,0.00]},{name: 'real', data: [2038.00,120.00,15000.00]},{name: 'desvio', data: [-11962.00,-8380.00,15000.00]}]

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

6 Comments

Thanks, I tried your proposal, I got Uncaught Highcharts error #13: It doesn't make sense.
Where is the call for chart function? Check if this is call after ajax success too.
See my answer edited. You can use async:false too like suggested, by I personaly prefer put inside jQuery ajax call.
The data pulled from the server is: [{name: 'orcamento', data: [14000.00,8500.00,0.00]}, {name: 'real', data: [2038.00,120.00,15000.00]}, {name: 'desvio', data: [-11962.00,-8380.00,15000.00]}] . I get an error in JSONLint "Parse error on line 2: [ { name: 'orcamento', --------------^ Expecting 'STRING', '}'" and no chart.
Also tried it, please see my new question, since it seems the problem is the JSON: stackoverflow.com/questions/22700048/…
|
0

$.get('data.php') will ignore the return results. See https://api.jquery.com/jQuery.get/

You have to use a callback.

Cheers, J

2 Comments

Agreed, also make sure the output headers are set to application/json in data.php
Thanks, I changed my script, but still I get nothing: var dados=[]; $.ajax({ url:'data.php', success: function(response){ dados=response } }); var options={ chart: { renderTo:'container', type: 'bar' }, title: { text: 'Total Anual' }, xAxis: { categories: ['Vendas', 'Serviços', 'Outras'] }, series: [dados] }; var chart = new Highcharts.Chart(options);

Your Answer

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