0

Json data is not displaying on the highcharts graphs. :( Please help.. Used getJSON that is not parsing at all. .post works shows msg.. dont know how to resolve..My graph code..

$(function () {
    var chart;
    $.post("data.php", function(json)
    {
        //alert("Data Loaded: " + json);
        chart = new Highcharts.Chart({
            chart: {
                renderTo: 'container1',
                type: 'area'
            },
            yAxis: {
                type: 'double',
                min: 0
            },
            xAxis: {
                type: 'datetime',
                labels: {
                    formatter: function () {
                        return Highcharts.dateFormat('%b %y', this.value);
                    },
                    dateTimeLabelFormats: {
                        month: '%b \'%y',
                        year: '%Y'
                    }
                }
            },
            legend: {
                layout: 'vertical',
                align: 'right',
                verticalAlign: 'top',
                x: -10,
                y: 100,
                borderWidth: 0
            },
            series: json
        });
    });       
});

This is my php code.. to get the json data

$mysql_connect = mysql_connect($db_host, $db_user, $db_pword, $db_name);
$query = "Select DATE_FORMAT(`timestamp`,'%Y,%m,%d') as date, Count(*) as       frequency from table group by date order by date,frequency asc";

if (!$mysql_connect)  die("unable to connect to database: " . mysql_error());
@mysql_select_db($db_name) or die( "Unable to select database");

$result = mysql_query($query);
$response = array();

if($result === FALSE)
{
    die(mysql_error());
}

echo "[";
$results = array();
while($row=mysql_fetch_array($result))
{
    $results[] = "[Date.UTC(".$row ['date']."),".$row['frequency']."]";
}

echo implode(',',$results); 
echo "]";
mysql_close($mysql_connect);
2
  • possible duplicate of Highcharts to call json data Commented Sep 6, 2013 at 20:08
  • The mysql_* functions are no longer maintained and shouldn't be used in any new codebase. It is being phased out in favor of newer APIs. Instead you should use prepared statements with either PDO or MySQLi. Commented Sep 6, 2013 at 22:42

1 Answer 1

1

Change your series assignment:

    series:[{
        data: json
    }]

http://jsfiddle.net/bDREA/

Also, I would change your php code to build the actual array and then json_encode it instead of creating a string.

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.