I am working on displaying data through a morris.js donut chart. These data come from a MySQL database through PHP and ajax.
The typical example where you type the data to be displayed right in the code is working fine
Morris.Donut({
element: 'donut-example',
data: [
{ label: "Download Sales", value: 12 },
{ label: "In-Store Sales", value: 30 },
{ label: "Mail-Order Sales", value: 20 }]
});
however, when i call the data from the database, the chart is not displayed. Moreover, firebug does not show any errors.
This is the output i get from PHP:
[{label: "4 pairs",value: "9"},{label: "3 pairs",value: "9"},{label: "2 pairs",value: "6"},{label: "5 pairs",value: "3"},{label: "6 pairs",value: "2"},{label: "1 pair",value: "2"}]
Actually, I tried this output by copying inside the Morris.Donut(); function and it worked fine!
And this is the Ajax code (which doesn't work at all):
$.ajax({
url: "ajax/modas_pares.php",
type: "post",
data: {lotto: 'the_lotto', limit: 20},
success : function (resp){
//alert(resp);
Morris.Donut({
element: 'donut-example',
data: resp
});
},
error: function(jqXHR, textStatus, ex) {
console.log(textStatus + "," + ex + "," + jqXHR.responseText);
}
});
However, if i have the following code:
$.ajax({
url: "ajax/modas_pares.php",
cache: false,
type: "post",
data: {lotto: 'the_lotto', limit: 20},
//dataType: "json",
timeout:3000,
success : function (resp){
//var column_data = $.parseJSON(resp);
//alert(resp);
Morris.Donut({
element: 'donut-example',
data: [{label: "3 pairs",value: "6"},{label: "4 pairs",value: "5"},{label: "2 pairs",value: "5"},{label: "5 pairs",value: "3"},{label: "1 pair",value: "1"},{label: "6 pairs",value: "1"}]
});
},
error: function(jqXHR, textStatus, ex) {
console.log(textStatus + "," + ex + "," + jqXHR.responseText);
}
});
It does work, fine!!!
What i don't get is that, if i tried passing the code through a variable, it does not work!! :
$string='[{label: "4 pairs",value: "9"},{label: "3 pairs",value: "9"},{label: "2 pairs",value: "6"},{label: "5 pairs",value: "3"},{label: "6 pairs",value: "2"},{label: "1 pair",value: "2"}]';
?>
And
$.ajax({
url: "ajax/modas_pares.php",
cache: false,
type: "post",
data: {lotto: 'the_lotto', limit: 20},
//dataType: "json",
timeout:3000,
success : function (resp){
//var column_data = $.parseJSON(resp);
//alert(resp);
Morris.Donut({
element: 'donut-example',
data: '<?php echo $string ?>'
});
},
error: function(jqXHR, textStatus, ex) {
console.log(textStatus + "," + ex + "," + jqXHR.responseText);
}
});
It doesn't work :( if i get it either from a php variable or from a php script through ajax :/
What am i missing? How can i fix this? Why?????
The error comes right where you set up the array. In the PHP section you should make the array as follows:
$resp = array(
array('label' => '5 pairs','value' => 4),
array('label' => '3 pairs','value' => 8)
);
echo json_encode( $resp );
Then you can use:
$.ajax({
url: "ajax/modas_pares.php",
type: "post",
data: {loteria: 'melate', limit: 20},
dataType: "json",
timeout:3000,
success : function (resp){
Morris.Donut({
element: 'donut-example',
data: resp
});
},
error: function(jqXHR, textStatus, ex) {
console.log(textStatus + "," + ex + "," + jqXHR.responseText);
}
});