我正在使用Google图表作为柱形图。我从ajax获取数据。我的问题是当查询结果为NULL时,页面根本没有任何问题。我正在使用GET获取柱形图的数据。我不知道问题出在我的观点或我的查询中。
在我的查询中,我必须显示来自用户公司的所有订单,无论哪个用户已将它们插入表格订单项中。应该来自用户的公司。当用户公司的表orderitems
中没有项目时,会出现问题。然后页面根本不加载。
如果我有查询数据,运行柱形图的脚本,如何制作。 如何检查是否只有数据,以加载图表。
我的观点是:
$(document).ready(function() {
google.load("visualization", "1", {packages:["corechart"]});
$.ajax({
type: 'get',
url: "<?= base_url() ?>index.php/receivedOrders/column_chart/",
dataType: "json",
// cache: false,
success: function(jsonData){
drawChart(jsonData);
}
});
function drawChart(jsonData) {
var data = new google.visualization.DataTable(jsonData);
var options = {
hAxis: {
title: '<?php echo lang("haxis_date"); ?>',
format: 'h:mm a',
},
vAxis: {
title: '<?php echo lang("chart_average_price"); ?>'
},
width:'400',
height:'300',
backgroundColor: 'none',
colors:['#ec8f6e', '#e6693e', '#e0440c', '#f3b49f', '#f6c7b6'],
legend: { position: 'top', maxLines: 3,alignment: 'center' },
};
var chart = new google.visualization.ColumnChart(document.getElementById('column_chart'));
chart.draw(data, options);
}
});
我的柱形图方法(当我调用ajax时)是:
<?php
public function column_chart() {
$size = $this->uri->segment(3);
$interval = $this->uri->segment(4);
$firms = $this->Receivedorders_model->average_price_by_week($size,$interval);
$p = array();
foreach ($firms as $key => $firm) {
$p[$key] = array('c' => array(array(
'v' => $firm['interval'],
),
array(
'v' => round($firm['unitPrice'],2),
)));
}
echo json_encode(array(
'cols' => array(
array('id' => 'name', 'label' => lang("customer"), 'type' => 'string'),
array('id' => 'incomes', 'label' => lang("chart_average_price"), 'type' => 'number'),
),
'rows' => $p
));
}
<?php
class Receivedorders_model extends CI_Model {
public function average_price_by_week($size = null,$interval = null) {
$company = $this->session->userdata('logged_in')['company'];
$now = new DateTime("now");
$now->modify('-6 month');
$six_months =$now->format('Y-m-d');
$this->db->select('MIN(createdDate) AS MinDate');
$this->db->from('ordersheader');
$this->db->join('systemusers', 'systemusers.idSystemUsers = ordersheader.user_id');
$this->db->where('systemusers.company', $company);
$query=$this->db->get();
$min = $query->row_array();
if($min['MinDate'] > $six_months) {
$this->db->select('DATE_FORMAT(DATE_SUB(ordersheader.createdDate, INTERVAL DAYOFWEEK(ordersheader.createdDate)-2 DAY), "%Y-%m-%d") AS "interval"',FALSE);
$this->db->select_avg('unitPrice');
$this->db->from('orderitems');
$this->db->join('ordersheader', 'ordersheader.idOrder=
orderitems.idOrder');
$this->db->join('systemusers', 'systemusers.idSystemUsers = ordersheader.user_id');
$this->db->where('systemusers.company', $company);
if($size) {
$this->db->where('orderitems.eggSize', $size);
}
if($interval) {
$this->db->where('ordersheader.createdDate > ',$current);
}
$this->db->order_by('interval');
$this->db->group_by('interval');
$query=$this->db->get();
return $query->result_array();
}
else {
$this->db->select('DATE_FORMAT(DATE_SUB(ordersheader.createdDate, INTERVAL DAYOFMONTH(ordersheader.createdDate)-1 DAY), "%Y-%m-%d") AS "interval"',FALSE);
$this->db->select_avg('unitPrice');
$this->db->from('orderitems');
$this->db->join('ordersheader', 'ordersheader.idOrder=
orderitems.idOrder');
$this->db->join('systemusers', 'systemusers.idSystemUsers = ordersheader.user_id');
$this->db->where('systemusers.company', $company);
if($size) {
$this->db->where('orderitems.eggSize', $size);
}
if($interval) {
$this->db->where('ordersheader.createdDate > ',$current);
}
$this->db->order_by('interval');
$this->db->group_by('interval');
$query=$this->db->get();
return $query->result_array();
}
}
}
我调用average_incomes_by_customer()
的控制器方法是:
<?php
class ReceivedOrders extends CI_Controller{
public function average_incomes_by_customer()
{
$data['dynamic_view'] = 'charts';
$data['activity_chart']=$this->Receivedorders_model->average_incomes_by_customer();
$data['get_average_price']=$this->Receivedorders_model->get_average_price();
$this->load->view('templates/main',$data);
}
}