jquery - flot piechart from PHP -
i having strange issue creating piechart in flot data php.
it seems drawing incorrectly, , can't figure out why.
my php code (for testing) is:
echo json_encode( '[{ label: "series1", data: 10}, { label: "series2", data: 3}, { label: "series3", data: 9}, { label: "series4", data: 7}, { label: "series5", data: 8}, { label: "series6", data: 17}]' );
my js file is:
$.ajax({ type:'get', datatype:"json", url:'../phpfile.php', success: function(data) { console.log(data); $.plot($("#piechart"),data,{ series: { pie: { show: true } } }); } });
the consol log shows:
[{ label: "series1", data: 10}, { label: "series2", data: 3}, { label: "series3", data: 9}, { label: "series4", data: 7}, { label: "series5", data: 8}, { label: "series6", data: 17}]
which thought correct format flot...
but graphs this:
does have ideas?
i believe json invalid, @ moment, you're trying parse json string, json string (if mean!) currently, when echo out php end echo'ed json_encode()
, i'm provided with:
"[{ label: \"series1\", data: 10},\r\n{ label: \"series2\"]"
furthermore, use php arrays encode json, below:
<?php $arr = array( array( "label" => "series1", "data" => 10 ), array( "label" => "series2", "data" => 3 ), array( "label" => "series3", "data" => 9 ), array( "label" => "series4", "data" => 7 ), array( "label" => "series5", "data" => 8 ), array( "label" => "series7", "data" => 17 ) ); echo json_encode( $arr ); ?>
php json_encode() does accept mixed variable types, it's popularly used php arrays.
with above, i'm able construct pie chart successfully:
Comments
Post a Comment