AngularJs:如何根据一个月的平均周数对数据进行分组?

时间:2018-11-15 21:18:17

标签: javascript angularjs d3.js c3.js

我有一个函数,可以使用C3.js库在图表上绘制90天的数据。我有基于count变量定义的开始日期和结束日期。有什么方法可以根据周将90天的数据分组。 1周= 1个数据点(平均7天)。 4周= 4个数据点同样。 90天= 12个数据点,依此类推。

function draw__chart(model, type){

    var count = 0

    if(type == 'week'){
        count = 7;
    }else if (type == 'month'){
        dcount = 30;
    }else if (type == '3_months'){
        count = 90; 
    }

    $scope.end_daterange = new Date();
    $scope.end_daterange = moment($scope.end_daterange).format('MM/DD/YYYY')            

    $scope.begin_daterange = new Date($scope.end_daterange);
    $scope.begin_daterange.setDate($scope.begin_daterange.getDate()- count);
    $scope.begin_daterange = moment($scope.begin_daterange).format('MM/DD/YYYY') 


    $date_id = ["date_id"];
    $volume1 = ["volume1"];
    $volume2 = ["volume2"];


    angular.forEach(model, function(value, key) {

        //Date for Chart
        if(dateCheck($scope.begin_daterange,$scope.end_daterange,moment(value.log_date_id).format('MM/DD/YYYY'))){              
            $date_id.push(moment(value.log_date_id).format('MM/DD/YYYY'));
            $volume1.push(value.volume1);
            $volume2.push(value.volume2);

        }

    });

    //Drawing Second Chart
    d3.select("#volume_graph svg").remove();

    setTimeout(function() {

        $scope.volume_id = c3.generate({
            bindto: d3.select('#volume_graph'),
            data: {
                x    : 'date_id',
                columns: [
                    $date_id,

                ],
                },
            },
            axis: {
                x: {
                    type: 'category',
                    tick: {
                        rotate: -90,
                        multiline: false
                    },
                    height: 70
                },
                y: {
                    label: {
                        text: 'Percentage',
                        position: 'outer-middle'
                    }
                },

            },
            tooltip: {
                show: true
            }
        }); 

    }, 100);

};

验证日期范围的功能:

function dateCheck(from,to,check) {

        var fDate,lDate,cDate;
        fDate = Date.parse(from);
        lDate = Date.parse(to);
        cDate = Date.parse(check);

        if((cDate <= lDate && cDate >= fDate)) {
            return true;
        }

        return false;
    };

HTML:

<div id="volume_graph" ng-hide="loading"></div>

0 个答案:

没有答案