【发布时间】:2017-08-16 22:35:25
【问题描述】:
我试图从一组结果中填充 morris.js 图表。在我的控制器中,我创建了一个结果数组并使用 json_encode 创建一个 json 数组,这是使用 print_r 在我的视图中的输出:
{"Positive":7,"Negative":26,"Pending":786,"Contact the Clinic":242,"Misc":2}
如何将此数据传递给我的 morris.js 图表以使用此数据作为标签/值对来填充图表?我尝试的每件事都会得到一个空白图表或“未定义”变量或“NaN”。这是我的控制器:
function execute_search()
{
// Retrieve the posted search term.
$search_term = $this->input->post('search');
// Get results count and pass it as json:
$data = $this->search_model->count_res('$p_results_data');
$pos = 0; $neg= 0; $pen = 0; $cont = 0; $misc = 0;
foreach ($data as $item) {
if ($item['result'] === 'Positive') {
$pos++;
} elseif ($item['result'] === 'Negative') {
$neg++;
} elseif ($item['result'] === 'Pending') {
$pen++;
} elseif ($item['result'] === 'Contact the Clinic') {
$cont++;
} else {
$misc++;
}
}
$res = array("Positive"=>$pos, "Negative"=>$neg, "Pending"=>$pen, "Contact the Clinic"=>$cont, "Misc"=>$misc);
$data = json_encode($res);
// Use the model to retrieve results:
$this->data['results'] = $this->search_model->get_results($search_term);
$this->data['chart'] = $data;
$this->data['totals'] = $this->search_model->total_res('$total_res');
// Pass the results to the view.
$this->data['subview'] = ('user/search_results');
$this->load->view('_layout_admin', $this->data);
}
还有我的 morris.js:
$results = "<?php echo $chart ?>";
new Morris.Donut({
element: 'donutEg',
data: [
$results
],
});
非常感谢任何帮助
【问题讨论】:
-
你是如何加载你的
morris.js的?如果您使用 HTML 中的脚本标记执行此操作,则 php 服务器不会解析该文件。 -
-
我的脚本已加载到我的页脚中
标签: javascript php codeigniter morris.js