HTML
<div style="text-align: center" id="call"> <button class="layui-btn" value="0">本周</button> <button class="layui-btn" value="1">当月</button> <button class="layui-btn" value="2">上月</button> <button class="layui-btn" value="3">上上月</button> </div> <div id="container_call_distribution" style="min-width:400px;height:400px"></div>
js
$(document).on('click','#call>button',function(){ var dat = { mobilePhone: "<?php echo $data['mobilePhone'] ;?>", time: this.value }; console.log(dat); $.post('/index/user/callDistribution',dat,function(res){ var start_times = res.start_times; var sum_total = res.sum_total; call(start_times,sum_total) }); }); $(document).ready(function() { var dat = { mobilePhone: "<?php echo $data['mobilePhone'] ;?>", time: 4 }; console.log(dat); $.post('/index/user/callDistribution',dat,function(res){ var start_times = res.start_times; var sum_total = res.sum_total; call(start_times,sum_total) }); });
//24小时通话分布 function call(start_times,sum_total){ $(function () { $('#container_call_distribution').highcharts({ chart: { type: 'column' }, title: { text: '24小时通话分布' }, // subtitle: { // text: '' // }, xAxis: { categories: start_times, crosshair: true }, yAxis: { min: 0, title: { text: '次数' } }, plotOptions: { column: { borderWidth: 0 } }, series: [{ name:'通话分布', data: sum_total }] }); }); }
php
public function callDistribution($mobilePhone='',$time='') { //本周 $where2 = ''; if($time==0){ $week = date('Y-m-d',strtotime('this week Monday',time())); $where2['start_time'] = ['>',$week]; //当月 }elseif($time==1){ $lastmonth = date('Y-m-01'); $where2['start_time'] = ['>',$lastmonth]; //上月 }elseif($time==2){ $lastmonth = date('Y-m-01'); $beforemonth = date('Y-m-d',strtotime($lastmonth.' -1 month')); $where2['start_time'] = array('BETWEEN',array($beforemonth,$lastmonth)); }elseif($time==3){ $month = date('Y-m-01'); $lastmonth = date('Y-m-d',strtotime($month.' -1 month')); $beforemonth = date('Y-m-d',strtotime($lastmonth.' -2 month')); $where2['start_time'] = array('BETWEEN',array($beforemonth,$lastmonth)); }elseif($time==4){ $day = date('Y-m-d',strtotime('-1 day')); $where2['start_time'] = ['>',$day]; }else{ $week = date('Y-m-d',strtotime('this week Monday',time())); $where2['start_time'] = ['>',$week]; } $container_call_distribution = model('CreditMobileCalls')->field("sum(cell_phone=$mobilePhone) sum_total, left(right(start_time,8),2) start_times") ->where('cell_phone',$mobilePhone) ->where($where2) ->group('start_times') ->select()->toArray(); $data['start_times'] = string_conversion(array_column($container_call_distribution,'start_times')); $data['sum_total'] = string_conversion(array_column($container_call_distribution,'sum_total')); return $data; }
start_time 字段 是varchar(255)不能用thinkphp 的 whereTime