laravel + vue实现的数据统计绘图(今天、7天、30天数据)
程序员文章站
2022-07-06 10:50:49
前言
本文主要是按照时段统计今天、7天、30天的数据,利用laravel+vue实现的,下面话不多说了,来一起看看详细的介绍吧
效果图:
1. 前端vue...
前言
本文主要是按照时段统计今天、7天、30天的数据,利用laravel+vue实现的,下面话不多说了,来一起看看详细的介绍吧
效果图:
1. 前端vue
使用vue-highcharts
<highcharts :options="options"></highcharts>
data() { return { options: { title: { text: '' }, xaxis: { categories: [] }, yaxis: { title: { text: '' }, plotlines: [{ value: 0, width: 1, color: '#808080' }] }, legend: { layout: 'horizontal', align: 'center', verticalalign: 'bottom', borderwidth: 0 }, credits: { enabled: false // 去掉highcharts商标 }, series: [] } } },
请求数据处理:
gettiminghistoryact(time) { gettiminghistory(time).then(response => { const curhour = new date().gethours() const hoursarr = [] const dayarr = [] const seriesdata = [] switch (time) { case 1: seriesdata.length = 0 for (let i = 0; i <= curhour; i++) { hoursarr.push(i < 10 ? '0' + i : '' + i) seriesdata[i] = 0 } this.options.xaxis.categories = hoursarr.map(x => x + ':00') response.data.foreach(record => { const index = hoursarr.indexof(record.hour) if (index > -1) { seriesdata[index] = record.count } }) break case 7: seriesdata.length = 0 for (let i = 0; i < 7; i++) { const ymd = new date(new date() - 24 * 60 * 60 * 1000 * i).tolocalestring().split(' ')[0] const ymdarr = ymd.split('/') if (ymdarr[1] * 1 < 10) { ymdarr[1] = '0' + ymdarr[1] } if (ymdarr[2] * 1 < 10) { ymdarr[2] = '0' + ymdarr[1] } seriesdata[i] = 0 dayarr.unshift(ymdarr.join('-')) } this.options.xaxis.categories = dayarr.map(x => x.substr(5)) response.data.foreach(record => { const index = dayarr.indexof(record.date) if (index > -1) { seriesdata[index] = record.count } }) break case 30: // 同7天 break } this.options.series = [{ name: '商品点击', data: seriesdata }] }) },
2. 后台laravel
mysql测试数据:
1 5440935 1 时尚博主家《心之语》 2018-07-28 19:20:49
2 5440935 1 时尚博主家《心之语》 2018-07-29 15:26:21
3 5440935 1 测试方案1 2018-07-29 15:38:43
...
public function gettiminghistory($time) { switch ($time) { case '1': $data = statsplanclick::where('created_at','<', carbon::now())->where('created_at','>', carbon::today())->select([db::raw('date_format(created_at,\'%h\') as hour'), db::raw('count("*") as count')])->groupby('hour')->get(); break; case '7': $data = statsplanclick::where('created_at','<', carbon::now())->where('created_at','>', carbon::today()->subdays(7))->select([db::raw('date(created_at) as date'), db::raw('count("*") as count')])->groupby('date')->get(); break; case '30': $data = statsplanclick::where('created_at','<', carbon::now())->where('created_at','>', carbon::today()->subdays(30))->select([db::raw('date(created_at) as date'), db::raw('count("*") as count')])->groupby('date')->get(); break; default: # code... break; } return $this->successwithdata($data); }
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。