javascript - How to pass JSON to jqChart? -
javascript - How to pass JSON to jqChart? -
client side sends post request php on server side. php returns json client. example:
[["1","-1"],["2","0"],["3","0"],["4","0"],["5","4"],["6","5"],["7","3"]]
now client side have create chart. created chart jquery plugin jqplot , flot, jqchart doesn't show correctly. here jquery code:
if ($jqlib == "flot") { var d2 = [[0, 3], [4, 8], [8, 5], [9, 13]]; $.post('database2json.php', function(data){ $.plot($("#placeholder"), [d2, data]); }, 'json' ); } else if ($jqlib == "jqchart") { $.post('database2json.php', function(podaci){ $('#placeholder').jqchart({ title: { text: 'chart title'}, series: [ { type: 'line', data: podaci } ] }); }); }
with code, flot draws chart line, , jqchart draws chart there not line, it's empty chart.
how solve problem?
edit1: here screenshot of jqchart output:
on x axis draws 1 31 expected, y axis ok, there no line.
edit2: @draganmatek, mentioned jqchart accepts pair values [x, y], x string, date or numeric, , y numeric.
in database there both columns, type int.
php code on server side, fetch these info is:
$result = mysql_query("select dan, temperatura temperatura"); $niz = array(); while ($row = mysql_fetch_array($result)) { $niz[] = array($row['dan'], $row['temperatura']); } $obj = json_encode($niz); echo $obj;
i don't understand why it's both x , y strings after sending json. maybe parse on client side?
edit3: checked firebug on client side this:
[["1","-1"],["2","0"],["3","0"],["4","0"],["5","4"],["6","5"],["7","3"],["8","2"],["9","2"],["10","1"],["11","-2"],["12","-2"],["13","0"],["14","1"],["15","-2"],["16","-1"],["17","-1"],["18","-2"],["19","-1"],["20","3"],["21","-1"],["22","0"],["23","1"],["24","3"],["25","1"],["26","1"],["27","-1"],["28","-1"],["29","4"],["30","5"],["31","5"]]
why so? database there 2 integers.
it seems jqchart needs int array elements , can't deal strings. updated code given below.
$.post('database2json.php', function(podaci){ $.each( podaci, function(i, e) { podaci[i][0] = parseint(e[0]); podaci[i][1] = parseint(e[1]); }); $('#placeholder').jqchart({ title: { text: 'chart title'}, series: [ { type: 'line', data: podaci } ] }); });
javascript jquery arrays jqchart
Comments
Post a Comment