为什么我不能在 Codeigniter 中使用 Chart.js 创建月度销售报告 [英] Why can't I create monthly sales report with Chart.js in Codeigniter

查看:40
本文介绍了为什么我不能在 Codeigniter 中使用 Chart.js 创建月度销售报告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用 chart.js 创建的图表

我每天从数据库中获取总数,有些日子没有这样的收入:

id |卖家 |日期 |数量 |价格 |全部的1 3636 2019-10-09 10:00 4 1,2 4,82 3045 2019-10-09 15:51 2 0,6 1,23 3636 2019-10-06 11:05 8 1,0 8,04 3636 2019-10-04 14:03 5 0,9 4,55 3636 2019-10-01 14:57 3 0,4 1,2

如您所见,10 月 9 日有 2 次销售,10 月 6 日有 1 次销售,10 月 4 日有 5 次销售,10 月 1 日有 1 次销售.

我为此创建了一个代码,但效果不佳:

//$kazanc_v 是来自控制器的销售日期数组for($i=1;$i<31;$i++){$arr2[]=date('Y').'-'.date('m').'-'.$i;}for($k=0;$k<30;$k++){if(in_array($arr2[$k],$kazanc_v[0])){回声 $kazanc_v->sub_total;} 别的 {回声'0,';}}

当我将数据放入数据集时,图表如下所示:

因为看不到 img:因为看不到 img:[10 月报告]3

如何创建真实报告?

解决方案

首先,让我们明确一点,我看到 $kazanc_v[0]$kazanc_v->sub_total 是对象还是数组?这个变量的输出是什么?

使用这样的输出也会迫使你犯错.如果您想显示每个日期的销售总数或总价格,您应该从数据库中获取按日期分组的数据.

您的模型中的查询应该是这样的;

//在你的模型中$this->db->select("count(quantity) as qty, DATE(order_date) as date")->来自(订单")->where(order_date > "2021-01-01")//本月初->group_by('日期')->get()->result_array();

现在,您需要先创建 x-axis.在您的情况下,一个月的总天数(我们认为是 31 天)

//在你的控制器中$begin = new DateTime(date('Y-m-01'));//或给定日期$end = new DateTime(date('Y-m-t'));$end = $end->modify('+1 天');$interval = new DateInterval('P1D');$dateRange = new DatePeriod($begin, $interval ,$end);

您有一个包含日期的数组,现在您可以使用来自模型的数据创建带有 y 轴和 x 轴的图表数据.

$chartData =[];$kazanc = $this->some_model->some_method();foreach($dateRange 作为 $date){$dataKey = array_search($date->format("Y-m-d"), array_column($kazanc, 'date'));if ($dataKey !== false) {//如果我们有给定日期的数据$chartData[$date->format("Y-m-d")] = $kazanc[$dataKey]['qty'];}别的 {//如果没有记录,创建默认值$chartData[$date->format("Y-m-d")] = 0;}}//发送数据到视图$this->load->view('template', ["chartData" => $chartData]);

现在,您在 $chartData 变量中有 31 天数据的日期(x 轴)和数量(y 轴).

最后,我们可以在视图中打印我们的数据.根据chartjs文档,类似这样的东西.

//在你看来变量选项 = {类型:'线',数据: {标签:<?php echo json_encode(array_keys($chartData));?>,数据集:[{label: '#总数量',数据:<?php echo json_encode(array_values($chartData));?>,边框宽度:1}]},

示例工作代码(php)https://www.tehplayground.com/pmiPYk3yhIpExJqap>

用于 chartjs 的 jsfiddle.https://jsfiddle.net/jwf67voe/

I have a chart which is created with chart.js

I get the totals on a daily basis from the database, and some days there is no earnings like this:

id | seller | date             | quantity | price | total

1    3636    2019-10-09 10:00   4           1,2      4,8
2    3045    2019-10-09 15:51   2           0,6      1,2
3    3636    2019-10-06 11:05   8           1,0      8,0
4    3636    2019-10-04 14:03   5           0,9      4,5
5    3636    2019-10-01 14:57   3           0,4      1,2

Like you see, there are 2 sales on October 9, 1 sale on October 6, 5 sales on October 4, and 1 sale on October 1.

I create a code for this but it does not work very well:

//$kazanc_v is sales dates array which is come from the controller
            for($i=1;$i<31;$i++)
            { 
                $arr2[]=date('Y').'-'.date('m').'-'.$i;
            }
            for($k=0;$k<30;$k++){
                  if(in_array($arr2[$k],$kazanc_v[0])){
                         echo $kazanc_v->sub_total;
                      } else {
                          echo '0,';
                }
             }

When I get data into data sets the chart looks like this:

for can't see img: wrong chart

I cant write '0' in charts data because there is no data in table. I want to write 0 when there is no sale on dates and chart is should be like this which I want

for can't see img : [october reports]3

How can I create real report?

解决方案

First of all, lets make it clear a bit, i see $kazanc_v[0] and $kazanc_v->sub_total is this and object or array? What is the output of this variable?

Also working with such output will force you do a mistake. If you want to show total count of sales or sum price per date, you should get data from database as grouped by date.

your query in your model should be something like this;

//in your model
$this->db->select("count(quantity) as qty, DATE(order_date) as date") 
         ->from("orders")
         ->where(order_date > "2021-01-01") //beginning of this month
         ->group_by('date')
         ->get()
         ->result_array()
 ;

Now, you need to create x-axis first. In your case total day in a month (we consider as 31 days)

//in your controller
$begin = new DateTime( date('Y-m-01') ); //or given date
$end = new DateTime( date('Y-m-t') );
$end = $end->modify( '+1 day' ); 
$interval = new DateInterval('P1D');
$dateRange = new DatePeriod($begin, $interval ,$end);

you have an array with dates, now you can create your chart data with y and x axis with data coming from your model.

$chartData =[];
$kazanc = $this->some_model->some_method();  

foreach($dateRange as $date){
  $dataKey = array_search($date->format("Y-m-d"), array_column($kazanc, 'date'));
  if ($dataKey !== false) { // if we have the data in given date
      $chartData[$date->format("Y-m-d")] = $kazanc[$dataKey]['qty'];
  }else {
      //if there is no record, create default values
     $chartData[$date->format("Y-m-d")] = 0;
  }
}

//send data to view
$this->load->view('template', ["chartData" => $chartData]);

now, you have date (x-axis) and qty (y-axis) for 31 days data in $chartData variable.

Finally, we can print our data in view. According to chartjs documentation, something like this.

// in your view
var options = {
  type: 'line',
  data: {
   labels: <?php echo json_encode(array_keys($chartData)); ?>,
   datasets: [
        {
          label: '# Total Quantity',
          data: <?php echo json_encode(array_values($chartData)); ?>,
          borderWidth: 1
        }
    ]
 },

sample working code (php) https://www.tehplayground.com/pmiPYk3yhIpExJqa

jsfiddle for chartjs. https://jsfiddle.net/jwf67voe/

这篇关于为什么我不能在 Codeigniter 中使用 Chart.js 创建月度销售报告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆