Yii framework combined with charjs to achieve statistical 30 day data method

Time:2021-2-23

In this paper, an example of Yii framework combined with charjs to achieve statistical 30 days of data. The details are as follows:

In theory, there should be 30 days of data, but in fact it is not necessarily, so it needs to be completed

public static function getDayOrderCharData($days = 30) {
    $nowday = date ('y-m-d ', strtotime (' - 1day '); // current previous day
    $lastday = date ("y-m-d", strtotime ('-' $days.'day '); // days before
    $daysFormat = [];
    //Gets the date of the days section
    for($i = $days; $i > 0; $i--) {
      $daysFormat[] = date("Y-m-d", strtotime('-'.$i.'day'));
    }
 
    //All users
    $allOrderData = self::find()
            ->select(['FROM_UNIXTIME(create_at,"%Y-%m-%d") as char_time', 'COUNT(id) as total_order', 'SUM(order_amount) as total_order_amount', 'SUM(pay_amount) as total_order_pay_amount'])
            ->where(['>=', 'FROM_UNIXTIME(create_at,"%Y-%m-%d")', $lastDay])
            ->andWhere(['<=', 'FROM_UNIXTIME(create_at,"%Y-%m-%d")', $nowDay])
            ->groupBy('char_time')
            ->all();
    $dayCountTitle   = Yii::t('backend', 'day_order_count_title', ['last_day' => $lastDay, 'now_day' => $nowDay]);
    $dayAmountTitle  = Yii::t('backend', 'day_order_amount_title', ['last_day' => $lastDay, 'now_day' => $nowDay]);
 
    $labels = $daysFormat;
    //All users
    $ordercounts = []; // order quantity
    $orderamounts = []; // order amount
    $orderpayamounts = []; // payment amount
    $allOrderDataArr = [];
    foreach($allOrderData as $allKey => $allVal) {
      $allOrderDataArr[$allVal->char_time]['char_time'] = $allVal->char_time;
      $allOrderDataArr[$allVal->char_time]['total_order'] = $allVal->total_order;
      $allOrderDataArr[$allVal->char_time]['total_order_amount'] = $allVal->total_order_amount;
      $allOrderDataArr[$allVal->char_time]['total_order_pay_amount'] = $allVal->total_order_pay_amount;
    }
 
    foreach($daysFormat as $key => $val) {
      if(array_key_exists($val, $allOrderDataArr)) {
        $orderCounts[] = $allOrderDataArr[$val]['total_order'];
        $orderAmounts[] = $allOrderDataArr[$val]['total_order_amount'];
        $orderPayAmounts[] = $allOrderDataArr[$val]['total_order_pay_amount'];
      } else {
        $orderCounts[] = '0';
        $orderAmounts[] = '0';
        $orderPayAmounts[] = '0';
      }
    }
    $data = [
      'dayCountTitle'    => $dayCountTitle, 
      'dayAmountTitle'    => $dayAmountTitle,
      'orderCountLabel'   => Yii::t('backend', 'day_order_count_label', ['days' => $days]),
      'orderAmountLabel'   => Yii::t('backend', 'day_order_amount_label', ['days' => $days]),
      'orderPayAmountLabel' => Yii::t('backend', 'day_order_pay_amount_label', ['days' => $days]),
      'nowDay'        => $nowDay,
      'lastDay'       => $lastDay,
      'labels'        => $labels,
      'orderCounts'     => $orderCounts,
      'orderAmounts'     => $orderAmounts,
      'orderPayAmounts'   => $orderPayAmounts
    ];
    return $data;
  }

js

//Get order quantity by day
  var dayOrderCountChartCanvas = $('#dayOrderCountChart').get(0).getContext('2d')
  var dayOrderCountChartData = {
   labels : <?= json_encode($dayOrderChar['labels'], true) ?>,
   datasets: [
    {
     label        : '<?= $dayOrderChar['orderCountLabel'] ?>',
     backgroundColor   : 'rgba(0, 192, 293, 0.5)',
     data        : <?= json_encode($dayOrderChar['orderCounts'], true) ?>
    }
   ]
  }
 
  var dayOrderCountChartOptions = {
    scales: {
      xAxes: [{
        gridLines: {
          display: false
        }
      }],
      yAxes: [{
        gridLines: {
          display: false
        }
      }]
    }
  }
 
  var dayOrderCountChart = new Chart(dayOrderCountChartCanvas, {
    type: 'line',
    data: dayOrderCountChartData,
    options: dayOrderCountChartOptions
  });
 
  //Get order and amount by day
  var dayOrderAmounCanvas = $('#dayOrderAmountChart').get(0).getContext('2d')
  var dayOrderAmounData = {
   labels : <?= json_encode($dayOrderChar['labels'], true) ?>,
   datasets: [
    {
     label        : '<?= $dayOrderChar['orderAmountLabel'] ?>',
     backgroundColor   : 'rgba(0, 192, 293, 0.5)',
     data        : <?= json_encode($dayOrderChar['orderAmounts'], true) ?>
    },
    {
     label        : '<?= $dayOrderChar['orderPayAmountLabel'] ?>',
     backgroundColor   : 'rgba(0, 166, 90, 0.5)',
     data        : <?= json_encode($dayOrderChar['orderPayAmounts'], true) ?>
    }
   ]
  }
 
  var dayOrderAmounOptions = {
    scales: {
      xAxes: [{
        gridLines: {
          display: false
        }
      }],
      yAxes: [{
        gridLines: {
          display: false
        }
      }]
    }
  }
 
  var dayOrderAmountChart = new Chart(dayOrderAmounCanvas, {
    type: 'line',
    data: dayOrderAmounData,
    options: dayOrderAmounOptions
  });

Remember, Yii’s as must define common variables in the model

public $char_ Time; // statistics by time
  public $total_ Order; // all orders
  public $total_ order_ Amount; // total amount of all orders
  public $total_ pay_ Order; // pay the order
  public $total_ pay_ Amount; // total order amount
  public $total_ order_ pay_ Amount; // total payment

For more information about Yii, readers who are interested in it can see the following topics: Yii framework introduction and common skills summary, PHP excellent development framework summary, smart template introduction basic course, PHP object-oriented programming introduction course, PHP string usage summary, PHP + MySQL database operation introduction course and PHP common database operation introduction course Summary of writing skills

I hope this article will be helpful to the PHP Programming Based on Yii framework.