I'm stuck!
I need to create highchart with json result. I found some sources here but can't put this to work.
The closest I can get was doing this:
Chart options:
var options = {
chart: {
renderTo: 'tudo',
defaultSeriesType: 'column',
rightMargin: 80
},
title: {
text: 'Weekdays'
},
subtitle: {
text: 'Source: somewhere in a calendar'
},
xAxis: {
labels: {
enabled: false
}
},
yAxis: [
{
min: 0,
title: {
text: 'Amount'
}
},
{
linkedTo: 0,
opposite: true
}
],
series: []
};
ajax call:
$.getJSON('ajax/calc.ajax.php', function(data) {
var series = [];
$.each(data, function(key, value) {
series.name = key;
series.data = value;
options.series.push(name);
});
var chart = new Highcharts.Chart(options);
});
highchart loads ok, and fills the series with Series 1, Series 2 ....
but no graphic is made, he keeps blank. ( something like this: Demo).
wanna know if I'm missing something or everything.
Thanks
update: i change the sql, now i'm working with 2 fields, and with this, the grafic work perfect, now i just want to know if doing like this its ok.
header('Content-Type: application/json');
while(!$res->EOF){
//$json = array("group" => "Web", "action" => "list");
$json[$res->fields["DESMAR"]] = array(intval($res->fields["QTD"]));
//$json["users"] = array(array("name" => "JulioGreff", "status" => "online"));
$res->MoveNext();
}
echo json_encode($json);
It looks like the problem lies in your PHP code. Highcharts expects a series to follow a particular format. In your case, things work out (partly) because
name
is one of the field it is looking for. The data, however, needs to be in one of three formats:[0, 1, 2]
)[[0,0], [1,1], [2,2]]
)You would want the last format, I think. For example:
To get your code to work, you might need to change the inside of your
$.each
function to something like this:...of course, if you wanted to use one of the other forms, it would be pretty easy as well:
In your ajax call -
So, it would be as follows -
Also, considering the JSON you are receiving -
what you are doing in the
$.each
-does not make sense.
series.data is an array. So, it could look like either as follows -
or as follows -
or as follows -
So, make sure that your PHP code produces a JSON that matches an array of one of the above, then
series.data = value
inside your$.each
will work.Update: Sorry, I do Java and have never done PHP so can't help you much with PHP. But, can you try with the following as your PHP, just to see if the chart shows up?
Update: To render pie while keeping the same PHP.
This should draw pie chart.