我使用Laravel从数据库中获取数据使用mysql。数据是get,我格式化数据然后转移到主页使用紧凑功能。
主页使用highcharts接收json,但它不起作用。 请帮助找出错误代码的错误。 非常感谢!
laravel代码
//title标题
$title = "GoldenQuant";
//获取灰金净值的数据
$res = HistoryData::where('product_id','1')->orderBy('time','desc')->get();
$data = array();
//将获取的数据重新排版
if(count($res) > 0){
foreach( $res as $value){
$timestamp = strtotime(str_replace('/','-',$value['time']));
$data[] = [ $timestamp , $value['cumulativeNet']];
}
}
//highchart设置内容
//x坐标轴
$highchart['xAxis'] = [ 'title' => [ 'text' => '时间' ] ];
//y坐标轴
$highchart['yAxis'] = [ 'title' => [ 'text' => '净值' ] ];
//数据
$highchart['series'] = [ 'name'=>'灰金净值' , 'data' => $data ];
$highchart['rangeSelector'] = array( 'selected' => '1' );
return view('index.index',compact('title','highchart'));
}
主页javascript代码
<script type="text/javascript" src="/js/highstock.js"></script>
<script type="text/javascript">
$(function(){
$('#highchart').highcharts('StockChart',
{!! json_encode($highchart)!!}
)
})</script>
json
{
"xAxis": {
"title": {
"text": "时间"
}
},
"yAxis": {
"title": {
"text": "净值"
}
},
"series": {
"name": "灰金净值",
"data": [
[
1441900800,
1
],
[
1439222400,
1
]
]
},
"rangeSelector": {
"selected": "1"
}
}