欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

yii框架结合charjs实现统计30天数据的方法

程序员文章站 2022-06-23 21:42:39
本文实例讲述了yii框架结合charjs实现统计30天数据的方法。分享给大家供大家参考,具体如下:理论上30天数据应该都有,但实际上却不一定是,所以需要补全public static function...

本文实例讲述了yii框架结合charjs实现统计30天数据的方法。分享给大家供大家参考,具体如下:

理论上30天数据应该都有,但实际上却不一定是,所以需要补全

public static function getdayorderchardata($days = 30) {
    $nowday = date('y-m-d', strtotime('-1day')); // 当前前一天
    $lastday = date("y-m-d", strtotime('-'.$days.'day')); // days天前
    $daysformat = [];
    // 获取到days段的日期
    for($i = $days; $i > 0; $i--) {
      $daysformat[] = date("y-m-d", strtotime('-'.$i.'day'));
    }
 
    // 所有用户
    $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;
    // 所有用户
    $ordercounts = []; // 订单数量
    $orderamounts = []; // 订单金额
    $orderpayamounts = []; // 支付金额
    $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

// 按天获取订单数量
  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
  });
 
  // 按天获取订单及金额
  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
  });

记住,yii的as一定要在模型利定义公用变量

public $char_time; // 按时间统计
  public $total_order; // 所有订单
  public $total_order_amount; // 所有订单总额
  public $total_pay_order; // 支付订单
  public $total_pay_amount; // 支付订单总额
  public $total_order_pay_amount; // 支付总额