Morris Js 和 Codeigniter 从控制器传递数据
Morris Js and Codeigniter pass data from a controller
我有以下控制器从我的 DBase 中选取数据:
函数chart_js() {
$rows = '';
$query = "SELECT clnt_id,date_added FROM job_card ORDER BY date_added DESC LIMIT 0, 5";
$result = $this->db->query($query);
$total_rows = $result->num_rows;
if ($result) {
$rows = $result->result_array();
}
print json_encode($rows, JSON_NUMERIC_CHECK);
}
并按以下格式将其传递给 json_encode:
[{"clnt_id": 10, "date_added": "2015-11-02 12:28:01"}, {"clnt_id": 11, "date_added": "2015-11-01 07:06:56"} , {"clnt_id": 9, "date_added": "2015-10-30 22:14:48"}, {"clnt_id": 7, "date_added": "2015-10-30 06:15:55"}, {"clnt_id": 10, "date_added": "2015-10-30 06:03:50"}]
以上数据是返回数据的准确格式。
我用上面的数据绘制了一个静态线图,效果很好。以下是我的观点:
<body class="nav-md">
<div class="panel panel-default">
<div class="panel-heading">
<i class="fa fa-bar-chart-o fa-fw"></i> Account Registrations Past 7 days
</div>
<!-- /.panel-heading -->
<div class="panel-body">
<div id="acctregs" style="height: 300px;"></div>
</div>
<!-- /.panel-body -->
</div>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/morris.js/0.5.1/morris.css">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/raphael/2.1.0/raphael-min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/morris.js/0.5.1/morris.min.js"></script>
<script>
$(document).ready(function () {
var acct_regs = [{"clnt_id": 10, "date_added": "2015-11-02 12:28:01"}, {"clnt_id": 11, "date_added": "2015-11-01 07:06:56"}
, {"clnt_id": 9, "date_added": "2015-10-30 22:14:48"}, {"clnt_id": 7, "date_added": "2015-10-30 06:15:55"}, {"clnt_id"
: 10, "date_added": "2015-10-30 06:03:50"}];
var acctregs = new Morris.Line({
// ID of the element in which to draw the chart.
element: 'acctregs',
// Chart data records -- each entry in this array corresponds to a point on
// the chart.
data: acct_regs,
// The name of the data record attribute that contains x-values.
xkey: 'date_added',
// A list of names of data record attributes that contain y-values.
ykeys: ['clnt_id'],
// Labels for the ykeys -- will be displayed when you hover over the
// chart.
labels: ['Value'],
dateFormat: function (x) {
return new Date(x).toString().split("00:00:00")[0];
}
});
});
</script>
</body>
但是,当我尝试通过将 var acct_regs 替换为:
从与 Dbase 交互的控制器检索此数据时
var acct_regs = "<?php echo site_url('operations/char_js'); ?>";
我收到以下错误:
TypeError: a is undefined
...,h,i,j,k,l;return"number"==typeof a?a:(c=a.match(/^(\d+) Q(\d)$/),e=a.match(/^(\...
来自 morris.min.js。
将此数据从控制器传递到视图以显示动态图形的最佳方法是什么?
您需要ajax数据使用$.getJSON
尝试:
$.getJSON("<?php echo site_url('operations/char_js'); ?>", function (json) {
var acctregs = new Morris.Line({
// ID of the element in which to draw the chart.
element: 'acctregs',
// Chart data records -- each entry in this array corresponds to a point on
// the chart.
data: json,
// The name of the data record attribute that contains x-values.
xkey: 'date_added',
// A list of names of data record attributes that contain y-values.
ykeys: ['clnt_id'],
// Labels for the ykeys -- will be displayed when you hover over the
// chart.
labels: ['Value'],
dateFormat: function (x) {
return new Date(x).toString().split("00:00:00")[0];
}
});
});
我有以下控制器从我的 DBase 中选取数据:
函数chart_js() {
$rows = '';
$query = "SELECT clnt_id,date_added FROM job_card ORDER BY date_added DESC LIMIT 0, 5";
$result = $this->db->query($query);
$total_rows = $result->num_rows;
if ($result) {
$rows = $result->result_array();
}
print json_encode($rows, JSON_NUMERIC_CHECK);
}
并按以下格式将其传递给 json_encode:
[{"clnt_id": 10, "date_added": "2015-11-02 12:28:01"}, {"clnt_id": 11, "date_added": "2015-11-01 07:06:56"} , {"clnt_id": 9, "date_added": "2015-10-30 22:14:48"}, {"clnt_id": 7, "date_added": "2015-10-30 06:15:55"}, {"clnt_id": 10, "date_added": "2015-10-30 06:03:50"}]
以上数据是返回数据的准确格式。 我用上面的数据绘制了一个静态线图,效果很好。以下是我的观点:
<body class="nav-md">
<div class="panel panel-default">
<div class="panel-heading">
<i class="fa fa-bar-chart-o fa-fw"></i> Account Registrations Past 7 days
</div>
<!-- /.panel-heading -->
<div class="panel-body">
<div id="acctregs" style="height: 300px;"></div>
</div>
<!-- /.panel-body -->
</div>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/morris.js/0.5.1/morris.css">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/raphael/2.1.0/raphael-min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/morris.js/0.5.1/morris.min.js"></script>
<script>
$(document).ready(function () {
var acct_regs = [{"clnt_id": 10, "date_added": "2015-11-02 12:28:01"}, {"clnt_id": 11, "date_added": "2015-11-01 07:06:56"}
, {"clnt_id": 9, "date_added": "2015-10-30 22:14:48"}, {"clnt_id": 7, "date_added": "2015-10-30 06:15:55"}, {"clnt_id"
: 10, "date_added": "2015-10-30 06:03:50"}];
var acctregs = new Morris.Line({
// ID of the element in which to draw the chart.
element: 'acctregs',
// Chart data records -- each entry in this array corresponds to a point on
// the chart.
data: acct_regs,
// The name of the data record attribute that contains x-values.
xkey: 'date_added',
// A list of names of data record attributes that contain y-values.
ykeys: ['clnt_id'],
// Labels for the ykeys -- will be displayed when you hover over the
// chart.
labels: ['Value'],
dateFormat: function (x) {
return new Date(x).toString().split("00:00:00")[0];
}
});
});
</script>
</body>
但是,当我尝试通过将 var acct_regs 替换为:
从与 Dbase 交互的控制器检索此数据时var acct_regs = "<?php echo site_url('operations/char_js'); ?>";
我收到以下错误:
TypeError: a is undefined
...,h,i,j,k,l;return"number"==typeof a?a:(c=a.match(/^(\d+) Q(\d)$/),e=a.match(/^(\...
来自 morris.min.js。 将此数据从控制器传递到视图以显示动态图形的最佳方法是什么?
您需要ajax数据使用$.getJSON
尝试:
$.getJSON("<?php echo site_url('operations/char_js'); ?>", function (json) {
var acctregs = new Morris.Line({
// ID of the element in which to draw the chart.
element: 'acctregs',
// Chart data records -- each entry in this array corresponds to a point on
// the chart.
data: json,
// The name of the data record attribute that contains x-values.
xkey: 'date_added',
// A list of names of data record attributes that contain y-values.
ykeys: ['clnt_id'],
// Labels for the ykeys -- will be displayed when you hover over the
// chart.
labels: ['Value'],
dateFormat: function (x) {
return new Date(x).toString().split("00:00:00")[0];
}
});
});