<commit_before>(function(angular, $, _) {
  angular.module('civicase').directive('civicaseTypeDurationChart', function(crmApi) {
    return {
      restrict: 'AE',
      scope: {
        civicaseTypeDurationChart: '='
      },
      link: function($scope, $el, $attr) {
        var dc = CRM.visual.dc, d3 = CRM.visual.d3, crossfilter = CRM.visual.crossfilter;
        var ts = $scope.ts = CRM.ts('civicase');

        function fullDraw(data) {
          $el.find('svg').remove();

          var chart = dc.rowChart($el[0]),
            ndx                 = crossfilter(data),
            caseTypeDimension   = ndx.dimension(function(d) {return d['case_type_id.title'];}),
            countGroup          = caseTypeDimension.group().reduce(
              function onAdd(p,v) { p.count += v.count; p.total += (v.count * v.average_duration); return p;},
              function onRemove(p,v) { p.count -= v.count; p.total -= (v.count * v.average_duration); return p; },
              function onInit() {return {count: 0, total: 0}; }
            );
          function avg(p) { return (p.count) ? Math.round(p.total / p.count) : 0; }

          chart
            .width(300)
            .height(125)
            .elasticX(true)
            .colors(d3.scale.category20())
            .dimension(caseTypeDimension)
            .group(countGroup)
            .valueAccessor(function(d) { return avg(d.value); } )
            .title(function(d){return ts('%1 days', {1: avg(d.value)});})
            .render();

          chart.onClick = function(){};

          // CSS would be more maintainable, but that only works in Chrome.
          chart.svg().selectAll('rect').attr('rx', 5).attr('ry', 5);
        }

        $scope.$watchCollection('civicaseTypeDurationChart', function(params){
          // crmApi('Case', 'gettypestats', params).then(function(response){
          //   var data = response.values;
          var data = [
            {'case_type_id.title': 'Fixme', 'count': 5, average_duration: 32.7},
            {'case_type_id.title': 'Fixme', 'count': 7, average_duration: '12.25'},
            {'case_type_id.title': 'Todo', 'count': 19, average_duration: '15.4'},
            {'case_type_id.title': 'Blank', 'count': 30, average_duration: 13.2},
            {'case_type_id.title': 'Placeholder', 'count': 11, average_duration: '19.7'}
          ];
            data.forEach(function(x) {
              x.count = +x.count;
              x.average_duration = +x.average_duration;
            });
            fullDraw(data);
          // });
        });
      }
    };
  });
})(angular, CRM.$, CRM._);
<commit_msg>CRM-21179 - TypeDurationChart - Load real data<commit_after>(function(angular, $, _) {
  angular.module('civicase').directive('civicaseTypeDurationChart', function(crmApi) {
    return {
      restrict: 'AE',
      scope: {
        civicaseTypeDurationChart: '='
      },
      link: function($scope, $el, $attr) {
        var dc = CRM.visual.dc, d3 = CRM.visual.d3, crossfilter = CRM.visual.crossfilter;
        var ts = $scope.ts = CRM.ts('civicase');

        function fullDraw(data) {
          $el.find('svg').remove();

          var chart = dc.rowChart($el[0]),
            ndx                 = crossfilter(data),
            caseTypeDimension   = ndx.dimension(function(d) {return d['case_type_id.title'];}),
            countGroup          = caseTypeDimension.group().reduce(
              function onAdd(p,v) { p.count += v.count; p.total += (v.count * v.average_duration); return p;},
              function onRemove(p,v) { p.count -= v.count; p.total -= (v.count * v.average_duration); return p; },
              function onInit() {return {count: 0, total: 0}; }
            );

          var format = d3.format('.2f');
          function avg(p) { return (p.count) ? format(p.total / p.count) : 0; }

          chart
            .width(300)
            .height(125)
            .elasticX(true)
            .colors(d3.scale.category20())
            .dimension(caseTypeDimension)
            .group(countGroup)
            .valueAccessor(function(d) { return avg(d.value); } )
            .title(function(d){return ts('%1 days', {1: avg(d.value)});})
            .render();

          chart.onClick = function(){};

          // CSS would be more maintainable, but that only works in Chrome.
          chart.svg().selectAll('rect').attr('rx', 5).attr('ry', 5);
        }

        $scope.$watchCollection('civicaseTypeDurationChart', function(params){
          crmApi('Case', 'gettypestats', params).then(function(response){
            response.values.forEach(function(x) {
              x.count = +x.count;
              x.average_duration = +x.average_duration;
            });
            fullDraw(response.values);
          });
        });
      }
    };
  });
})(angular, CRM.$, CRM._);

{'fraction_non_alphanumeric': 0.16338378602529546, 'fraction_numerical': 0.01430644826871242, 'mean_word_length': 2.009357454772302, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1987021', 'n_tokens_mistral': 1547, 'n_tokens_neox': 1470, 'n_words': 272}