2016-06-08 1 views
0

사람들이 내 앱에서 소셜 버튼을 클릭 한 횟수를 추적합니다. 하루에 클릭 한 번만 크레딧을주고 싶습니다. 현재 소셜 플랫폼 별 클릭 수를 필터링하고 있지만 하루에 그룹화하는 방법을 파악하지 못하고 하루에 1 회만 계산할 수 있습니다.날짜 별 그룹 별 각도 필터

컨트롤러 :

Data.get('stats/'+$scope.uid).then(function(data){ 
    $scope.stats = data.data; 

    // determine if the user has any stats 
    $scope.hasStats = $scope.stats.length; 

    $scope.topUsersObjects = $filter('filter')($scope.stats, { 
     action: "completed" 
    }); 
    $scope.facebookObjects = $filter('filter')($scope.stats, { 
     platform: "facebook", 
     action: "completed" 
    }); 
    $scope.twitterObjects = $filter('filter')($scope.stats, { 
     platform: "twitter", 
     action: "completed" 
    }); 
    $scope.facebookClickedObjects = $filter('filter')($scope.stats, { 
     platform: "facebook", 
     action: "clicked" 
    }); 
    $scope.twitterClickedObjects = $filter('filter')($scope.stats, { 
     platform: "twitter", 
     action: "clicked" 
    }); 

    $scope.labels = ["Facebook", "Twitter", "Clicked"]; 

    var totalClicked = $scope.facebookClickedObjects.length + $scope.twitterClickedObjects.length; 
    $scope.data = [$scope.facebookObjects.length, $scope.twitterClickedObjects.length, totalClicked]; 
}); 

UPDATE (샘플 $의 scope.stats) : 어떤 도움이 크게 감사합니다

Object 

action:"clicked ls" 
guid:"1200041" 
id:"52" 
platform:"facebook" 
timeclicked:"2015-10-12 21:46:26" 
uid:"44043077e53d" 

아래에있는 내 현재 코드입니다.

최종 업데이트] 답변 :

윌리엄-B @ 감사는 당신의 대답은 그러나 일,이 같은 일을 결국. 이것이 좋은 접근 방법이 아닌지 알려주십시오. 도와 주신 모든 분들께 감사드립니다. 당신이 reduce에 클릭 별 일의 카운트 클릭의 배열을 원하는처럼

// facebook group by days, completed 
$scope.facebookGroupByDay = _.groupBy($scope.facebookObjects, function(item) { 
    return item.timeclicked.substring(0,10); 
}); 

// get the number of days with a completed post 
$scope.fbCount = Object.keys($scope.facebookGroupByDay); 

// twitter group by days, clicked 
$scope.twitterGroupByDay = _.groupBy($scope.twitterClickedObjects, function(item) { 
    return item.timeclicked.substring(0,10); 
}); 

// get the number of days with a clicked tweet 
$scope.twCount = Object.keys($scope.twitterGroupByDay); 
+0

'$ scope.stats'의 json 스키마는 무엇입니까? 귀하의 예를 어디에도 날짜가 표시되지 않습니다. –

+0

@DanielGimenez $ scope.stats의 예는 update를 참조하십시오. – Jason

답변

1

는 소리가 난다.

var days = {}; // let's keep track of days clicked here during the reduction 

var clicksByDay = $scope.stats.reduce(function (clickCount, stat) { 
    var day = stat.timeclicked.split(' ')[0]; 
    if (!days[day]) { 
    // we didnt count this day yet 
    clickCount++; 
    days[day] = true; // mark this day as counted 
    } 
    return clickCount; 
}, 0); 

$ 경우 scope.stats은 다음과 같습니다

[ 
    { timeclicked: '2015-10-12 12:34:56' }, 
    { timeclicked: '2015-10-12 21:46:26'}, 
    { timeclicked: '2015-10-13 21:46:26' } 
] 

clicksByDay이 경우이 될 것입니다.

P. 여러 제어기의 템플리트에서 필요한 경우 이러한 유형의 축소에 대한 자체 앵글 필터를 정의하는 것을 막을 수있는 방법은 없습니다.

+0

어떻게하면됩니까? – Jason

+0

아주 쉽게! https://docs.angularjs.org/tutorial/step_11 –

+0

FINAL UPDATE ANSWER에서 내 게시물을 수정하십시오.를 참조하십시오. 고맙습니다 – Jason