绘制每周每天的用户交易

时间:2015-06-05 13:41:09

标签: php mysql plot

在我的数据库中有Transaction表。此表包含有关用户资金交易的信息。我的任务是生成两个图表(图表),显示每天和每月的交易金额。

我应该实现的图表示例显示在下图中:

enter image description here

从图中可以看出,当每天没有交易时,总价值应为0.

在数据库中,我使用以下查询按天分组我的交易:

select DATE_FORMAT(t.transactionDate ,"%b %d") as dayName, sum(t.amount) total from `Transaction` t, t.transactionStatus = 1 and t.user=17
group by dayName
order by t.transactionDate asc;

当每天都有交易时,这就像魅力一样,但是当有一天没有任何交易时,这种方法并不正常。让我们在DB中说我们有以下交易:

May 1st 20$
May 1st 30$
May 1st 38$
May 2nd 20$
May 4th 100$
May 4th 50$

所以5月3日没有交易。

当我对这些交易进行分组时,我得到以下结果:

May 1st 88$
May 2nd 20$
May 4th 150$

我现在想要的是生成May 3rd 0$。是否可以直接在DB中执行,或者我必须使用PHP处理它?<​​/ p>

如果我必须使用PHP处理它,任何想法?

使用Chart JS库绘制I&#39;以下是数据输入示例:

        var areaChartData = {
          labels: ["May 01", "May 02", "May 04"],
          datasets: [
            {
              label: "Transactions",
              fillColor: "rgba(210, 214, 222, 1)",
              strokeColor: "rgba(210, 214, 222, 1)",
              pointColor: "rgba(210, 214, 222, 1)",
              pointStrokeColor: "#c1c7d1",
              pointHighlightFill: "#fff",
              pointHighlightStroke: "rgba(220,220,220,1)",
              data: [88,20,150]
            },

          ]
        };

正如您所看到的,我的想法是直接在图表中使用数据库中的值,其中事务日期是我的X轴,而total是我的Y轴。

2 个答案:

答案 0 :(得分:1)

一种方法是,首先将所有可能的日期和值作为PHP数组:

$labels = array(
     "May 1st" => 0,
     "May 2nd" => 0,
     "May 3rd" => 0,
     "May 4th" => 0,
     "May 6th" => 0,
     "May 6th" => 0,
     ....

);

然后将数据库中的所有值填充到数组中的相应日期键

...
foreach ( $rows as $row )
{
   $labels[$row['date']] = $row['agg_val'];
   ...

现在,您可以迭代$labels数组并输出值。那些不在DB(无事务)中的那些被初始化为零。

答案 1 :(得分:1)

查看您的查询,它会生成 //this one causes php run error $collection->update(array("scienceNumber"=>$scienceNumber), array('$push'=>array(array("comments"=> $comment), array("ids"=> $userID), array("times"=> $time))), array("upsert"=>true)); //this one runs a query but only pushes $time onto the times array, ignoring the other push instructions //that outcome totally makes sense since I am assigning all these different arrays to the same key...no complaints...but then the above didn't work either $collection->update(array("scienceNumber"=>$scienceNumber), array('$push'=>array("comments"=> $comment), '$push'=>array("ids"=> $userID), '$push'=>array("times"=> $time) ), array("upsert"=>true)); ,如下所示,

$result

此脚本将为chartdata准备数组,

array (size=3)
  0 => 
    array (size=2)
      'dayName' => string 'May 01' (length=6)
      'total' => int 88
  1 => 
    array (size=2)
      'dayName' => string 'May 02' (length=6)
      'total' => int 20
  2 => 
    array (size=2)
      'dayName' => string 'May 04' (length=6)
      'total' => int 150

现在,您的$begin = reset($result) ['dayName']; // 1st date in result $end = end($result) ['dayName']; // last date in result $begin = new DateTime($begin); $end = new DateTime($end); $end->modify('+1 day'); $interval = DateInterval::createFromDateString('1 day'); $period = new DatePeriod($begin, $interval, $end); function search($input, $day) { foreach($input as $k => $v) { if ($v['dayName'] == $day) return $k; } return false; } $chartdata = []; // array for chartdata foreach($period as $dt) // loop for everyday from 1st to last day { $format = $dt->format('M d'); $flag = search($result, $dt->format('M d')); $chartdata[$format] = is_int($flag) ? $result[$flag]['total'] : 0; } 包含交易表中缺少的一天$chartdata

现在,将其用作

May 03

labels: <?php echo json_encode(array_keys($chartdata))?>,