12个月份统计分组

简介: /* * 根据月份来统计trade里面的入账,支出,总盈利 */ public function countMonth(){ $in = array(); $out = array(); $res = arr...

/*
* 根据月份来统计trade里面的入账,支出,总盈利 */ public function countMonth(){ $in = array(); $out = array(); $res = array(); $year = date("Y",time()); $in_sql = 'SELECT SUM(money) AS mon, FROM_UNIXTIME(cdate, "%m") AS m FROM trade WHERE way_type=1 GROUP BY m'; $out_sql = 'SELECT SUM(money) AS mon, FROM_UNIXTIME(cdate, "%m") AS m FROM trade WHERE way_type=0 GROUP BY m'; $res = 'SELECT SUM( IF (way_type>0,money,-money)) AS mon, FROM_UNIXTIME(cdate, "%m") AS m FROM trade GROUP BY m'; $obj = new Table("trade"); $inpdo = $obj->query($in_sql); $outpdo = $obj->query($out_sql); $respdo = $obj->query($res); $inRows = $inpdo->fetchAll(); $outRows = $outpdo->fetchAll(); $resRows = $respdo->fetchAll(); $in = $this->formartMonth($inRows); $out = $this->formartMonth($outRows); $res = $this->formartMonth($resRows); return array(implode(",", $in),implode(",", $out), implode(",", $res), ); } public function formartMonth($rows){ $arr = array(); for($i=1; $i<=12; $i++){ foreach($rows as $v){ if(intVal($v['m']) == $i){ $arr[$i] = $v['mon']; break; } $arr[$i] = 0; } } return $arr; }

 

结合chart.js插件,html代码:

<div>
             <ul id="before-bg-color-list" class="simple-list color-list">
                 <li class="before-fg-emerald padding10 margin10 place-left">收入</li>
                 <li class="before-fg-orange padding10 margin10  place-left">支出</li>
                 <li class="before-fg-red padding10  margin10 place-left">总和</li>
             </ul>
         </div>
         <div style="width: 70%">
            <canvas id="canvas"></canvas>
        </div>


        <script>
        var randomScalingFactor = function(){ return Math.round(Math.random()*100)};
    
        var barChartData = {
            labels : ["一月",
                      "二月",
                      "三月",
                      "四月",
                      "五月",
                      "六月",
                      "七月",
                      "八月",
                      "九月",
                      "十月",
                      "十一月",
                      "十二月"
                      ],
            datasets : [
                {
                    fillColor : "#008a00",
                    strokeColor : "rgba(220,220,220,0.8)",
                    highlightFill: "rgba(220,220,220,0.75)",
                    highlightStroke: "rgba(220,220,220,1)",
                    data : [
                        <?php echo $countData?$countData[0]:"0,0,0,0,0,0,0,0,0,0,0,0"; ?>
                    ]
                },
                {
                    fillColor : "#fa6800",
                    strokeColor : "rgba(151,187,205,0.8)",
                    highlightFill : "rgba(151,187,205,0.75)",
                    highlightStroke : "rgba(151,187,205,1)",
                    data : [
                        <?php echo $countData?$countData[1]:"0,0,0,0,0,0,0,0,0,0,0,0"; ?>
                    ]
                },
                {
                    fillColor : "#ce352c",
                    strokeColor : "rgba(151,187,205,0.8)",
                    highlightFill : "rgba(151,187,205,0.75)",
                    highlightStroke : "rgba(151,187,205,1)",
                    data : [
                        <?php echo $countData?$countData[2]:"0,0,0,0,0,0,0,0,0,0,0,0"; ?>
                    ]
                }
            ]
    
        }
        window.onload = function(){
            var ctx = document.getElementById("canvas").getContext("2d");
            window.myBar = new Chart(ctx).Bar(barChartData, {
                responsive : true
            });
        }
    
        </script>
    </div>

效果如下:

目录
相关文章
|
2月前
|
自然语言处理 算法 前端开发
1484. 按日期分组销售产品
1484. 按日期分组销售产品
21 0
|
7月前
|
测试技术
统计天数
统计天数
|
分布式计算 Java 大数据
MapReduce基础编程之按日期统计及按日期排序(下)
MapReduce基础编程之按日期统计及按日期排序(下)
259 0
MapReduce基础编程之按日期统计及按日期排序(下)
7-19 树种统计 (10 分)
7-19 树种统计 (10 分)
85 0
|
分布式计算 Java Hadoop
MapReduce基础编程之按日期统计及按日期排序(上)
MapReduce基础编程之按日期统计及按日期排序(上)
206 0
MapReduce基础编程之按日期统计及按日期排序(上)
|
存储 Unix 关系型数据库
关于日期及时间字段的查询
在项目开发中,一些业务表字段经常使用日期和时间类型,而且后续还会牵涉到这类字段的查询。关于日期及时间的查询等各类需求也很多,本篇文章简单讲讲日期及时间字段的规范化查询方法。
200 0
关于日期及时间字段的查询
生成以周统计的表头,跨月份的周算在后一个月
这是人力统计的一个表格的表头,根据月份,划分周,每周从周一开始到周日(国内习惯性)。而跨月份的周算在前一个月还是后一个月,我们的需求是算在后一个月。根据情况而定。
172 0
生成以周统计的表头,跨月份的周算在后一个月
|
BI
利用时间维度统计分页
在进行数据统计的时候,不能简单的通过数据表数据条数进行分页。这时,可以通过时间维度进行分页。 //以时间作为查询条件进行用户统计查询 $start_date = request('date')['start'] ? : ''; $end_date = request('date')['end'].
808 0