i need help how to put data with format true json in highchart. i have json data like this:
{"stocksdata":[{"item_code":"SPH001","stock":"40.0"},
{"item_code":"SPH002","stock":"40.0"},
{"item_code":"SPH003","stock":"40.0"},
{"item_code":"SPH004","stock":"40.0"},
{"item_code":"SPH005","stock":"40.0"},
{"item_code":"SPH006","stock":"20"}]
}
where json data is result from my controller like this:
function get_stock(){
$item_stock = $this->M_item->get_stock();
$data['item_stock']=$item_stock;
$data = array();
$stocksdata = array();
foreach($item_stock as $item_stocks){
$stocksdata []= array(
"item_code"=>$item_stocks->item_code,
"stock"=>$item_stocks->stock
);
}
$data['stocksdata']= $stocksdata;
echo json_encode($data);
}
and this my a part code from view where on top page i added some link like this:
<script src="<?php echo base_url();?>assets/Highcharts/jquery.js"></script>
<script src="http://code.highcharts.com/highcharts.js"></script>
<script type="text/javascript">
function get_stockdata(stockdata){
$.ajax({
type : "post",
data : {stockdata:stockdata},
url : '<?php echo base_url();?>index.php/Home/get_stock',
dataType:"json",
success: function(data){
console.log(data);
//START -->the 3rd lines with marked is updated
var data=[];
for(var i = 0; i < data.length; i++) {
var obj = data[i];
}
//END
}
});
}
var chart1;
jQuery(document).ready(function(){
chart1 = new Highcharts.Chart({
chart:{
renderTo:'container',
type:'line',
borderWidth:1,
spacingRight:20
},
title: {
text: "Item Data"
},
tooltip:{
formatter: function() {
return '<b>'+ this.series.name +'</b><br/>'+
this.x +': '+ this.y+ ('Kg');
}
},
xAxis: {
type: 'category',
allowDecimals: false,
title: {
text: ""
}
},
yAxis: {
title: {
text: "Stocks"
}
},
series: [get_stockdata]
});
});
</script>
<div class="box-body">
<div id="container" width="400" height="246"></div>
</div>
and finally i got a result is blank page bordered. what is wrong in my javascript? previously, thanks for attention. one more i want to get result is like this : http://www.knowstack.com/webtech/charts_demo/highchartsdemo4.html thanks guys.