首页 > 解决方案 > Highchart 使用 Codeigniter 显示空白页

问题描述

作为学习highchart的初学者,我不知道为什么图表无法显示在视图中。我的控制器正确显示了 json 数据,但是当我在 $.getJSON 中调用控制器以在图表中显示数据时,我得到了一个空白页。

有人可以帮我在视图中显示图表吗?

谢谢

我的控制器

public  function chartBarData() 
{
$query1 = $this->model_admin->getBarCha1('2019-01-24','2019-01-25');
$category = array();
$category['name'] = 'Medico';

$series1 = array();
$series1['name'] = 'Paciente';


foreach ($query1 as $row)
{
$category['data'][] = $row->Medico;
$series1['data'][] = $row->Paciente;

}

$result = array();
array_push($result,$category);
array_push($result,$series1);


print json_encode($result, JSON_NUMERIC_CHECK);
}

打印 json_encode($result, JSON_NUMERIC_CHECK) 结果:

[{"name":"Medico","data":["baptiste prophete","JUAN ALBERTO SUERO","ADRIANA MATEO","RAUL ARISTY COMAS","VICTORIA LILA FRANCO","IVANHOE LINARES VENTURA"]},{"name":"Paciente","data":[2,3,15,13,1,6]}]

风景

<div id="container" style="min-width: 310px; height: 400px; max-width: 600px; margin: 0 auto"></div>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://code.highcharts.com/modules/export-data.js"></script>

<script type="text/javascript">

$(document).ready(function() {
var options = {
chart: {
renderTo: 'container',
type: 'column',
marginRight: 130,
marginBottom: 25
},
title: {
text: 'Doctor patients view',
x: -20 //center
},
subtitle: {
text: '',
x: -20
},
xAxis: {
categories: []
},
yAxis: {
title: {
text: 'Amount'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
tooltip: {
formatter: function() {
return '<b>'+ this.series.name +'</b><br/>'+
this.x +': '+ this.y;
}
},
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'top',
x: -10,
y: 100,
borderWidth: 0
},
series: []
}

$.getJSON("<?php echo site_url('chartBarData');?>", function(json) {
options.xAxis.categories = json[0]['data'];
options.series[0] = json[1];
chart = new Highcharts.Chart(options);
});
});
</script>

标签: highchartscodeigniter-3

解决方案


推荐阅读