如何减少谷歌图表功能代码?

时间:2016-06-08 11:38:02

标签: javascript angularjs charts google-visualization

enter image description here如果可以减少代码行。我的结果很好。请指导我减少Angular函数的代码,请看附带的屏幕截图代码结束后得到的结果是什么。

$scope.Customer_profileMonth_data = function(data){
$scope.data: [
    {
        "VisitTime": "08:36:23",
        "orgDwellTime": 27,
        "consumerID": 7245,
        "ConsumerAge": 46,
        "VisitDate": "2016-01-11",
        "DAY": "Monday",
        "ConsumerGender": "M"
    },
    {
        "VisitTime": "17:08:16",
        "orgDwellTime": 64,
        "consumerID": 7245,
        "ConsumerAge": 46,
        "VisitDate": "2016-01-11",
        "DAY": "Monday",
        "ConsumerGender": "M"
    },
    {
        "VisitTime": "21:29:51",
        "orgDwellTime": 2,
        "consumerID": 7245,
        "ConsumerAge": 46,
        "VisitDate": "2016-01-11",
        "DAY": "Monday",
        "ConsumerGender": "M"
    },
    {
        "VisitTime": "17:18:11",
        "orgDwellTime": 10,
        "consumerID": 7245,
        "ConsumerAge": 46,
        "VisitDate": "2016-01-12",
        "DAY": "Tuesday",
        "ConsumerGender": "M"
    },
    {
        "VisitTime": "20:16:48",
        "orgDwellTime": 39,
        "consumerID": 7245,
        "ConsumerAge": 46,
        "VisitDate": "2016-01-13",
        "DAY": "Wednesday",
        "ConsumerGender": "M"
    },
    {
        "VisitTime": "16:26:14",
        "orgDwellTime": 55,
        "consumerID": 7245,
        "ConsumerAge": 46,
        "VisitDate": "2016-01-20",
        "DAY": "Wednesday",
        "ConsumerGender": "M"
    },
    {
        "VisitTime": "12:51:42",
        "orgDwellTime": 22,
        "consumerID": 7245,
        "ConsumerAge": 46,
        "VisitDate": "2016-01-21",
        "DAY": "Thursday",
        "ConsumerGender": "M"
    },
    {
        "VisitTime": "17:43:17",
        "orgDwellTime": 76,
        "consumerID": 7245,
        "ConsumerAge": 46,
        "VisitDate": "2016-01-27",
        "DAY": "Wednesday",
        "ConsumerGender": "M"
    }]
    var s ="count";
    var d ="orgDwellTime";
    var e ="orgDwellTimeavg";

    for(var i=1; i<=62; i++){
        //s = s+i
        //console.log(s+i);
        $scope[s+i] = 0;
        $scope[d+i] = 0;
        $scope[e+i] = 0;
        //console.log($scope[s])        
    }

    $scope.Monthgender_details = data;
    //$rootScope.footfallmonth= $scope.Monthgender_details;

    for(var i=0; i<$scope.Monthgender_details.length; i++){
        $scope.splitmonth =  $scope.Monthgender_details[i].VisitDate.split('-');
        $scope.customer_profile_gender = $scope.Monthgender_details[i].ConsumerGender;
    //console.log("footfall api calling pages ", $scope.yyyy);
            if($scope.splitmonth[2] == "01"){
                if($scope.customer_profile_gender =="M"){
                    $scope.count1 ++;   
                }else if($scope.customer_profile_gender =="F"){
                    $scope.count2 ++;
                }

            }else if($scope.splitmonth[2] == "02"){
                if($scope.customer_profile_gender =="M"){
                    $scope.count3 ++;   
                }else if($scope.customer_profile_gender =="F"){
                    $scope.count4 ++;
                }

            }else if($scope.splitmonth[2] == "03"){

                if($scope.customer_profile_gender =="M"){
                    $scope.count5 ++;   
                }else if($scope.customer_profile_gender =="F"){
                    $scope.count6 ++;
                }

            }else if($scope.splitmonth[2] == "04"){

                if($scope.customer_profile_gender =="M"){
                    $scope.count7 ++;   
                }else if($scope.customer_profile_gender =="F"){
                    $scope.count8 ++;
                }

            }else if($scope.splitmonth[2] == "05"){

                if($scope.customer_profile_gender =="M"){
                    $scope.count9 ++;   
                }else if($scope.customer_profile_gender =="F"){
                    $scope.count10 ++;
                }

            }else if($scope.splitmonth[2] == "06"){

                if($scope.customer_profile_gender =="M"){
                    $scope.count11 ++;  
                }else if($scope.customer_profile_gender =="F"){
                    $scope.count12 ++;
                }


            }else if($scope.splitmonth[2] == "07"){

                if($scope.customer_profile_gender =="M"){
                    $scope.count13 ++;  
                }else if($scope.customer_profile_gender =="F"){
                    $scope.count14 ++;
                }


            }else if($scope.splitmonth[2] == "08"){

                if($scope.customer_profile_gender =="M"){
                    $scope.count15 ++;  
                }else if($scope.customer_profile_gender =="F"){
                    $scope.count16 ++;
                }


            }else if($scope.splitmonth[2] == "09"){

                if($scope.customer_profile_gender =="M"){
                    $scope.count17 ++;  
                }else if($scope.customer_profile_gender =="F"){
                    $scope.count18 ++;
                }


            }else if($scope.splitmonth[2] == "10"){

                if($scope.customer_profile_gender =="M"){
                    $scope.count19 ++;  
                }else if($scope.customer_profile_gender =="F"){
                    $scope.count20 ++;
                }


            }else if($scope.splitmonth[2] == "11"){

                if($scope.customer_profile_gender =="M"){
                    $scope.count21 ++;  
                }else if($scope.customer_profile_gender =="F"){
                    $scope.count22 ++;
                }


            }else if($scope.splitmonth[2] == "12"){
                if($scope.customer_profile_gender =="M"){
                    $scope.count23 ++;  
                }else if($scope.customer_profile_gender =="F"){
                    $scope.count24 ++;
                }

            }else if($scope.splitmonth[2] == "13"){
                if($scope.customer_profile_gender =="M"){
                    $scope.count25 ++;  
                }else if($scope.customer_profile_gender =="F"){
                    $scope.count26 ++;
                }

            }else if($scope.splitmonth[2] == "14"){

                if($scope.customer_profile_gender =="M"){
                    $scope.count27 ++;  
                }else if($scope.customer_profile_gender =="F"){
                    $scope.count28 ++;
                }
            }else if($scope.splitmonth[2] == "15"){

                if($scope.customer_profile_gender =="M"){
                    $scope.count29 ++;  
                }else if($scope.customer_profile_gender =="F"){
                    $scope.count30 ++;
                }

            }else if($scope.splitmonth[2] == "16"){

                if($scope.customer_profile_gender =="M"){
                    $scope.count31 ++;  
                }else if($scope.customer_profile_gender =="F"){
                    $scope.count32 ++;
                }

            }else if($scope.splitmonth[2] == "17"){

                if($scope.customer_profile_gender =="M"){
                    $scope.count33 ++;  
                }else if($scope.customer_profile_gender =="F"){
                    $scope.count34 ++;
                }


            }else if($scope.splitmonth[2] == "18"){

                if($scope.customer_profile_gender =="M"){
                    $scope.count35 ++;  
                }else if($scope.customer_profile_gender =="F"){
                    $scope.count36 ++;
                }


            }else if($scope.splitmonth[2] == "19"){

                if($scope.customer_profile_gender =="M"){
                    $scope.count37 ++;  
                }else if($scope.customer_profile_gender =="F"){
                    $scope.count38 ++;
                }


            }else if($scope.splitmonth[2] == "20"){

                if($scope.customer_profile_gender =="M"){
                    $scope.count39 ++;  
                }else if($scope.customer_profile_gender =="F"){
                    $scope.count40 ++;
                }


            }else if($scope.splitmonth[2] == "21"){

                if($scope.customer_profile_gender =="M"){
                    $scope.count41 ++;  
                }else if($scope.customer_profile_gender =="F"){
                    $scope.count42 ++;
                }


            }else if($scope.splitmonth[2] == "22"){

                if($scope.customer_profile_gender =="M"){
                    $scope.count43 ++;  
                }else if($scope.customer_profile_gender =="F"){
                    $scope.count44 ++;
                }


            }else if($scope.splitmonth[2] == "23"){
                if($scope.customer_profile_gender =="M"){
                    $scope.count45 ++;  
                }else if($scope.customer_profile_gender =="F"){
                    $scope.count46 ++;
                }
            }else if($scope.splitmonth[2] == "24"){

                if($scope.customer_profile_gender =="M"){
                    $scope.count47 ++;  
                }else if($scope.customer_profile_gender =="F"){
                    $scope.count48 ++;
                }


            }else if($scope.splitmonth[2] == "25"){

                if($scope.customer_profile_gender =="M"){
                    $scope.count49 ++;  
                }else if($scope.customer_profile_gender =="F"){
                    $scope.count50 ++;
                }


            }else if($scope.splitmonth[2] == "26"){

                if($scope.customer_profile_gender =="M"){
                    $scope.count51 ++;  
                }else if($scope.customer_profile_gender =="F"){
                    $scope.count52 ++;
                }


            }else if($scope.splitmonth[2] == "27"){

                if($scope.customer_profile_gender =="M"){
                    $scope.count53 ++;  
                }else if($scope.customer_profile_gender =="F"){
                    $scope.count54 ++;
                }


            }else if($scope.splitmonth[2] == "28"){

                if($scope.customer_profile_gender =="M"){
                    $scope.count55 ++;  
                }else if($scope.customer_profile_gender =="F"){
                    $scope.count56 ++;
                }


            }else if($scope.splitmonth[2] == "29"){
                if($scope.customer_profile_gender =="M"){
                    $scope.count57 ++;  
                }else if($scope.customer_profile_gender =="F"){
                    $scope.count58 ++;
                }

            }else if($scope.splitmonth[2] == "30"){
                if($scope.customer_profile_gender =="M"){
                    $scope.count59 ++;  
                }else if($scope.customer_profile_gender =="F"){
                    $scope.count60 ++;
                }
            }else if($scope.splitmonth[2] == "31"){
                if($scope.customer_profile_gender =="M"){
                    $scope.count61 ++;  
                }else if($scope.customer_profile_gender =="F"){
                    $scope.count62 ++;
                }
            }
    }

    $scope.Customer_genderM =   [$scope.count1, $scope.count3, $scope.count5, $scope.count7, $scope.count9, $scope.count11, $scope.count13, $scope.count15, $scope.count17, $scope.count19, $scope.count21, $scope.count23, $scope.count25, $scope.count27, $scope.count29, $scope.count31, $scope.count33, $scope.count35, $scope.count37, $scope.count39, $scope.count41, $scope.count43, $scope.count45, $scope.count47, $scope.count49, $scope.count51, $scope.count53, $scope.count55, $scope.count57, $scope.count59, $scope.count61];
    $scope.Customer_genderF =   [$scope.count2, $scope.count4, $scope.count6, $scope.count8, $scope.count10, $scope.count12, $scope.count14, $scope.count16, $scope.count18, $scope.count20, $scope.count22, $scope.count24, $scope.count26, $scope.count28, $scope.count30, $scope.count32, $scope.count34, $scope.count36, $scope.count38, $scope.count40, $scope.count42, $scope.count44, $scope.count46, $scope.count48, $scope.count50, $scope.count52, $scope.count54, $scope.count56, $scope.count58, $scope.count60, $scope.count62];
//$scope.Customer_genderM = [22,34,21,35,10, 24, 20, 32, 18, 5,10, 24, 20, 32, 18, 5,10, 24, 20, 32, 18, 5,10, 24, 20, 32, 18, 5, 40, 54]
//$scope.Customer_genderF = [32, 18, 5,10, 24, 20, 32, 18, 5,10, 24, 20, 32, 18, 5, 40, 54, 0, 22,34,21,35,10, 24, 20,32, 18, 5,10, 24]
    $scope.users = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24','25', '26', '27', '28', '29', '30', '31'];
    console.log($scope.Customer_genderM);
    console.log($scope.Customer_genderF);
    console.log($scope.users);
    CustomerProfiling_gender($scope.Customer_genderM, $scope.Customer_genderF, $scope.users);

}
$.getScript("https://www.google.com/jsapi", function () {
google.load('visualization', '1.1' , 
{ 'callback': CustomerProfiling_gender,'packages': ['corechart'] });
});


 function CustomerProfiling_gender(genderM, genderF, month) {
//console.log("numberqqqq",genderM);
var Customer_genderM = genderM;
var Customer_genderF = genderF;
var Customer_gender_month = month;
var month = Customer_gender_month;
var male = Customer_genderM;
var female=Customer_genderF;
//console.log("numberqqqq",Customer_genderM);
    //console.log("numberqqqq",Customer_genderF);
var arr=[];
$scope.output=[];
var personId;
var personIndex;
for(var i=0;i<month.length;i++){
 personIndex = {
    "month": month[i],
     "genderM": male[i],
     "genderF": female[i],
//  ^ computed property name


};

 $scope.output.push(personIndex)



}
//console.log('output',$scope.output);




var customer = [Array('Month', 'Male', 'Female')]; 
$scope.output.forEach(function(value){

    customer.push([value.month, value.genderM, value.genderF]);
})

var data = google.visualization.arrayToDataTable(customer);
var options = {
    isStacked: 'percent',
    width: 510,
    height: 300,

    legend: { position: 'top', maxLines: 11 },
    bar: { groupWidth: '75%' },
    vAxis: { gridlines: { count: 12 } }
  };
  var chart = new google.visualization.ColumnChart(document.getElementById("barchart_values"));
  chart.draw(data, options);
  //console.log("Customer gender",data);
}

1 个答案:

答案 0 :(得分:0)

这样的东西?

var mydata = [
  [ "08:36:23", 27, 7245, 46, "2016-01-11", "Monday", "M" ], 
  [ "17:08:16", 64, 7245, 46, "2016-01-11", "Monday", "M" ], 
  [ "21:29:51", 2, 7245, 46, "2016-01-11", "Monday", "M" ], 
  [ "17:18:11", 10, 7245, 46, "2016-01-12", "Tuesday", "M" ], 
  [ "20:16:48", 39, 7245, 46, "2016-01-13", "Wednesday", "M" ], 
  [ "16:26:14", 55, 7245, 46, "2016-01-20", "Wednesday", "M" ], 
  [ "12:51:42", 22, 7245, 46, "2016-01-21", "Thursday", "M" ], 
  [ "17:43:17", 76, 7245, 46, "2016-01-27", "Wednesday", "M" ]
],
scopeData=[];
for (var i=0;i<mydata.length;i++) {
  scopeData.push({
    "VisitTime":      mydata[i][0],
    "orgDwellTime":   mydata[i][1],
    "consumerID":     mydata[i][2],
    "ConsumerAge":    mydata[i][3],
    "VisitDate":      mydata[i][4],
    "DAY":            mydata[i][5],
    "ConsumerGender": mydata[i][6]
  });
}
    $scope.Customer_profileMonth_data = function(data){
      $scope.data: scopeData;
    var s ="count";
    var d ="orgDwellTime";
    var e ="orgDwellTimeavg";

    for(var i=1; i<=62; i++){
      //s = s+i
      //console.log(s+i);
      $scope[s+i] = 0;
      $scope[d+i] = 0;
      $scope[e+i] = 0;
      //console.log($scope[s])    
    }
    $scope.users=[];
    $scope.Monthgender_details = data;
    //$rootScope.footfallmonth= $scope.Monthgender_details;

    for(var i=0; i<$scope.Monthgender_details.length; i++){
      $scope.splitmonth =  parseInt($scope.Monthgender_details[i].VisitDate.split('-'),10);
      $scope.customer_profile_gender = $scope.Monthgender_details[i].ConsumerGender;
    //console.log("footfall api calling pages ", $scope.yyyy);
      for (var i=0;i<30;i++) {
        $scope.users.push(""+(i+1));
        if($scope.customer_profile_gender =="M"){
          $scope["count"+($scope.splitmonth)]++;  
        }else if($scope.customer_profile_gender =="F"){
          $scope["count"+($scope.splitmonth+1)]++;  
        }
      }
    }
    for (var i=0;i<30;i+=2) { 
      $scope.Customer_genderM.push($scope["count"+i]);
      $scope.Customer_genderF.push($scope["count"+(i+1)]);
    }