[Note 2] statistical drawing of laravel data (data of today, 7 days and 30 days)

Time:2021-6-24

Development Notes: Statistics of today’s, 7 days’ and 30 days’ data by time period

[Note 2] statistical drawing of laravel data (data of today, 7 days and 30 days)

1. Front end Vue

usevue-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 // remove the trademark of highcharts
        },
        series: []
      }
    }
  },

Request data processing:

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:
            //Same as 7 days
            break
        }
        this.options.series = [{
          Name: 'product click',
          data: seriesData
        }]
      })
    },

2. Laravel in the background

MySQL test data:

1 5440935 1 fashion blogger "words of the heart" 2018-07-28 19:20:49
2 5440935 1 fashion blogger "words of the heart" 2018-07-29 15:26:21
3 5440935 1 test plan 1 2018-07-29 15:38:43
...
public function getTimingHistory($time)
{
    $time = (int)$time;
    $data = StatsPlanClick::where('created_at','<', Carbon::now())
            ->where('created_at','>', $time > 1 ? Carbon::today()->subDays($time)Carbon::today())
            ->select([$time > 1 ? DB::raw('DATE(created_at) as time') : DB::raw('DATE_FORMAT(created_at,\'%H\') as time'), DB::raw('COUNT("*") as count')])
            ->groupBy('time')
            ->get();
    }
    return $this->successWithData($data);
}

Recommended Today

React source code part2 — rendering principle of react15 and react16

React source code Part-1 – algebraic effect: https://segmentfault.com/a/11…React source code part3 – fiber architecture: https://segmentfault.com/a/11…React source code Part4 – render rendering (Mount stage): https://segmentfault.com/a/11…React source code Part4 – render rendering (update stage): https://segmentfault.com/a/11…React source code Part5 – commit phase (handling class component life cycle): https://segmentfault.com/a/11…React source code part6 – commit phase (beforemutation): https://segmentfault.com/a/11…React source code […]