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

yii框架结合charjs统计上一年与当前年数据的方法示例

程序员文章站 2022-06-23 21:50:41
本文实例讲述了yii框架结合charjs统计上一年与当前年数据的方法。分享给大家供大家参考,具体如下:理论上是1年有12个月,但实际上却是去年12个月已经过了,是完整的12个月,今年的12个月还没过,...

本文实例讲述了yii框架结合charjs统计上一年与当前年数据的方法。分享给大家供大家参考,具体如下:

理论上是1年有12个月,但实际上却是去年12个月已经过了,是完整的12个月,今年的12个月还没过,不完整,所以需要补齐

public static function getyearorderchardata() {
    // 获取当前年
    $months = range(1, 12);
    $currentyear = date('y');
    $lastyear = date('y', strtotime("-1 year"));
    // 所有订单
    $allorderdata = self::find()
            ->select(['from_unixtime(create_at,"%y-%m") as char_time', 'count(id) as total_order', 'sum(order_amount) as total_order_amount'])
            ->where(['>=', 'from_unixtime(create_at,"%y")', $lastyear])
            ->groupby('char_time')
            ->all();
    // 已支付订单
    $allpayorderdata = self::find()
            ->select(['from_unixtime(create_at,"%y-%m") as char_time', 'count(id) as total_order', 'sum(pay_amount) as total_order_amount'])
            ->where(['>=', 'from_unixtime(create_at,"%y")', $lastyear])
            ->andwhere(['pay_status' => 2])
            ->groupby('char_time')
            ->all();
    $yearcounttitle   = yii::t('backend', 'year_order_count_title', ['last_year' => $lastyear, 'current_year' => $currentyear]);
    $yearamounttitle  = yii::t('backend', 'year_order_amount_title', ['last_year' => $lastyear, 'current_year' => $currentyear]);
    $yearpaycounttitle = yii::t('backend', 'year_order_pay_count_title', ['last_year' => $lastyear, 'current_year' => $currentyear]);
    $yearpayamounttitle = yii::t('backend', 'year_order_pay_amount_title', ['last_year' => $lastyear, 'current_year' => $currentyear]);
 
    $labels = [];
    // 所有订单
    $lastyearcounts = []; // 前一年月订单总量
    $lastyearamounts = []; // 前一年月订单总额
    $currentyearcounts = []; // 当前年月订单总量
    $currentyearamounts = []; // 当前年月订单额
    $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'] = number_format($allval->total_order_amount / 100, 2, '.', '');
    }
 
    // 已支付订单
    $lastyearpaycounts = []; // 前一年月支付订单总量
    $lastyearpayamounts = []; // 前一年月支付订单总额
    $currentyearpaycounts = []; // 当前年月支付订单总量
    $currentyearpayamounts = []; // 当前年月支付订单额
    $allpayorderdataarr = [];
    foreach($allpayorderdata as $paykey => $payval) {
      $allpayorderdataarr[$payval->char_time]['char_time'] = $payval->char_time;
      $allpayorderdataarr[$payval->char_time]['total_order'] = $payval->total_order;
      $allpayorderdataarr[$payval->char_time]['total_order_amount'] = number_format($payval->total_order_amount / 100, 2, '.', '');
    }
 
    foreach($months as $key => $val) {
      $label = $val . yii::t('backend', 'month');
      $labels[] = $label;
      $themonth = strlen($val) == 2 ? $val : '0' . $val;
      // 上一年
      $lastyearmonth = $lastyear . '-' . $themonth;
      if(array_key_exists($lastyearmonth, $allorderdataarr)) {
        $lastyearcounts[] = $allorderdataarr[$lastyearmonth]['total_order'];
        $lastyearamounts[] = $allorderdataarr[$lastyearmonth]['total_order_amount'];
      } else {
        $lastyearcounts[] = '0';
        $lastyearamounts[] = '0';
      }
      if(array_key_exists($lastyearmonth, $allpayorderdataarr)) {
        $lastyearpaycounts[] = $allpayorderdataarr[$lastyearmonth]['total_order'];
        $lastyearpayamounts[] = $allpayorderdataarr[$lastyearmonth]['total_order_amount'];
      } else {
        $lastyearpaycounts[] = '0';
        $lastyearpayamounts[] = '0';
      }
 
      // 当前年
      $currentyearmonth = $currentyear . '-' . $themonth;
      if(array_key_exists($currentyearmonth, $allorderdataarr)) {
        $currentyearcounts[] = $allorderdataarr[$currentyearmonth]['total_order'];
        $currentyearamounts[] = $allorderdataarr[$currentyearmonth]['total_order_amount'];
      } else {
        $currentyearcounts[] = '0';
        $currentyearamounts[] = '0';
      }
      if(array_key_exists($currentyearmonth, $allpayorderdataarr)) {
        $currentyearpaycounts[] = $allpayorderdataarr[$currentyearmonth]['total_order'];
        $currentyearpayamounts[] = $allpayorderdataarr[$currentyearmonth]['total_order_amount'];
      } else {
        $currentyearpaycounts[] = '0';
        $currentyearpayamounts[] = '0';
      }
    }
    $data = [
      'yearcounttitle'     => $yearcounttitle,
      'yearamounttitle'    => $yearamounttitle,
      'yearpaycounttitle'   => $yearpaycounttitle,
      'yearpayamounttitle'   => $yearpayamounttitle,
      'lastyear'        => $lastyear,
      'currentyear'      => $currentyear,
      'labels'         => $labels,
      'lastyearcounts'     => $lastyearcounts,
      'lastyearamounts'    => $lastyearamounts,
      'currentyearcounts'   => $currentyearcounts,
      'currentyearamounts'   => $currentyearamounts,
      'lastyearpaycounts'   => $lastyearpaycounts,
      'lastyearpayamounts'   => $lastyearpayamounts,
      'currentyearpaycounts'  => $currentyearpaycounts,
      'currentyearpayamounts' => $currentyearpayamounts,
    ];
    return $data;
  }

js

// 订单总量对比
  var yearordercountchartcanvas = $('#yearordercountchart').get(0).getcontext('2d')
  var yearordercountchartdata = {
   labels : <?= json_encode($orderchar['labels'], true) ?>,
   datasets: [
    {
     label        : '<?= $orderchar['lastyear'] ?>',
     backgroundcolor   : 'rgba(0, 192, 239, 0.5)',
     data        : <?= json_encode($orderchar['lastyearcounts'], true) ?>
    },
    {
     label        : '<?= $orderchar['currentyear'] ?>',
     backgroundcolor   : 'rgba(0, 135, 239, 0.5)',
     data        : <?= json_encode($orderchar['currentyearcounts'], true) ?>
    }
   ]
  }
 
  var yearordercountchartoptions = {
    scales: {
      xaxes: [{
        gridlines: {
          display: false
        }
      }],
      yaxes: [{
        gridlines: {
          display: false
        }
      }]
    }
  }
 
  var yearordercountchart = new chart(yearordercountchartcanvas, {
    type: 'line',
    data: yearordercountchartdata,
    options: yearordercountchartoptions
  });
 
  // 支付订单总量对比
  var yearorderpaycountchartcanvas = $('#yearorderpaycountchart').get(0).getcontext('2d')
  var yearorderpaycountchartdata = {
   labels : <?= json_encode($orderchar['labels'], true) ?>,
   datasets: [
    {
     label        : '<?= $orderchar['lastyear'] ?>',
     backgroundcolor   : 'rgba(0, 166, 90, 0.5)',
     data        : <?= json_encode($orderchar['lastyearpaycounts'], true) ?>
    },
    {
     label        : '<?= $orderchar['currentyear'] ?>',
     backgroundcolor   : 'rgba(0, 166, 11, 0.5)',
     data        : <?= json_encode($orderchar['currentyearpaycounts'], true) ?>
    }
   ]
  }
 
  var yearorderpaycountchartoptions = {
    scales: {
      xaxes: [{
        gridlines: {
          display: false
        }
      }],
      yaxes: [{
        gridlines: {
          display: false
        }
      }]
    }
  }
 
  var yearorderpaycountchart = new chart(yearorderpaycountchartcanvas, {
    type: 'line',
    data: yearorderpaycountchartdata,
    options: yearorderpaycountchartoptions
  });
 
  // 订单总额对比
  var yearorderamountchartcanvas = $('#yearorderamountchart').get(0).getcontext('2d')
  var yearorderamountchartdata = {
   labels : <?= json_encode($orderchar['labels'], true) ?>,
   datasets: [
    {
     label        : '<?= $orderchar['lastyear'] ?>',
     backgroundcolor   : 'rgba(0, 192, 239, 0.5)',
     data        : <?= json_encode($orderchar['lastyearamounts'], true) ?>
    },
    {
     label        : '<?= $orderchar['currentyear'] ?>',
     backgroundcolor   : 'rgba(0, 135, 239, 0.5)',
     data        : <?= json_encode($orderchar['currentyearamounts'], true) ?>
    }
   ]
  }
 
  var yearorderamountchartoptions = {
    scales: {
      xaxes: [{
        gridlines: {
          display: false
        }
      }],
      yaxes: [{
        gridlines: {
          display: false
        }
      }]
    }
  }
 
  var yearorderamountchart = new chart(yearorderamountchartcanvas, {
    type: 'line',
    data: yearorderamountchartdata,
    options: yearorderamountchartoptions
  });
 
  // 支付订单总额对比
  var yearorderpayamountchartcanvas = $('#yearorderpayamountchart').get(0).getcontext('2d')
  var yearorderpayamountchartdata = {
   labels : <?= json_encode($orderchar['labels'], true) ?>,
   datasets: [
    {
     label        : '<?= $orderchar['lastyear'] ?>',
     backgroundcolor   : 'rgba(0, 166, 90, 0.5)',
     data        : <?= json_encode($orderchar['lastyearpayamounts'], true) ?>
    },
    {
     label        : '<?= $orderchar['currentyear'] ?>',
     backgroundcolor   : 'rgba(0, 166, 11, 0.5)',
     data        : <?= json_encode($orderchar['currentyearpayamounts'], true) ?>
    }
   ]
  }
 
  var yearorderpayamountchartoptions = {
    scales: {
      xaxes: [{
        gridlines: {
          display: false
        }
      }],
      yaxes: [{
        gridlines: {
          display: false
        }
      }]
    }
  }
 
  var yearorderpayamountchart = new chart(yearorderpayamountchartcanvas, {
    type: 'line',
    data: yearorderpayamountchartdata,
    options: yearorderpayamountchartoptions
  });

记住,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; // 支付总额