chart-pie2-demo.js 11.8 KB
Newer Older
reynaldi adriantama's avatar
reynaldi adriantama committed
1 2 3 4 5
// Set new default font family and font color to mimic Bootstrap's default styling
Chart.defaults.global.defaultFontFamily = 'Nunito', '-apple-system,system-ui,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial,sans-serif';
Chart.defaults.global.defaultFontColor = '#858796';
$("#websiteName").html(websitetitle);

6 7 8 9 10 11 12 13 14 15 16 17 18
window.setInterval(function(){
  let country = $('#selectcountryvl').val();
  let province =  $('#selectprovincevl').val();
  let selectyear = $('#selectyearvisit').val();
  let selectmonth = $('#selectmonthvisit').val();

  changevistorbytime(selectyear, selectmonth, country, province);
  if(province){
    $('#visitorLocation').html('Visitor Location City');
  }else{
    $('#visitorLocation').html('Visitor Location Province');
  }

reynaldi adriantama's avatar
reynaldi adriantama committed
19
},10000); 
20

reynaldi adriantama's avatar
reynaldi adriantama committed
21
LoadDadta(validasiToken);
Anggi Saputra's avatar
Anggi Saputra committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
function LoadDadta(token){
    $(document).ready(function(){
    /* save data */
        $.ajax({
          type: 'POST',
          dataType: 'json',
          url: baseURL + '/api/loadvisitorbytime',
          data: {
              token   : token,
              year : null,
              month : null,
              country : null,
              province : null
          },
            success: function(result){
                if(result.code == 0){
                    let data = result.data;
                            
                    var location = [];
                    let jumlah = [];
                            //let counter = 0;
                    for(x in data){
                        location[x] = data[x].location;
                        jumlah[x] = data[x].total;
                    }
                    
                    var myPieChart =  {
reynaldi adriantama's avatar
reynaldi adriantama committed
49 50
                        type: 'pie',
                        data: {
Anggi Saputra's avatar
Anggi Saputra committed
51
                            labels: location,
reynaldi adriantama's avatar
reynaldi adriantama committed
52
                            datasets: [{
Anggi Saputra's avatar
Anggi Saputra committed
53 54 55 56
                                data: jumlah,
                                backgroundColor: ['#4e73df', '#1cc88a', '#36b9cc', '#f6c23e', "#c5c5c5"],
                                hoverBackgroundColor: ['#2e59d9', '#17a673', '#2c9faf', '#ffd700', '#c5c5c5'],
                                hoverBorderColor: "rgba(234, 236, 244, 1)",
reynaldi adriantama's avatar
reynaldi adriantama committed
57 58 59 60 61
                            }],
                        },
                        options: {
                            maintainAspectRatio: false,
                            tooltips: {
Anggi Saputra's avatar
Anggi Saputra committed
62 63 64 65 66 67 68 69
                                backgroundColor: "rgb(255,255,255)",
                                bodyFontColor: "#858796",
                                borderColor: '#dddfeb',
                                borderWidth: 1,
                                xPadding: 15,
                                yPadding: 15,
                                displayColors: false,
                                caretPadding: 10,
reynaldi adriantama's avatar
reynaldi adriantama committed
70 71
                            },
                            legend: {
Anggi Saputra's avatar
Anggi Saputra committed
72
                                display: false
reynaldi adriantama's avatar
reynaldi adriantama committed
73 74 75
                            },
                            cutoutPercentage: 0,
                        },
Anggi Saputra's avatar
Anggi Saputra committed
76
                    }
reynaldi adriantama's avatar
reynaldi adriantama committed
77

Anggi Saputra's avatar
Anggi Saputra committed
78 79 80 81 82 83 84 85 86 87 88 89
                    var ctx = document.getElementById("myPieChart2").getContext("2d");
                    window.myVisitorPie = getNewChart(ctx, myPieChart);
                    
                    var colour = ['primary','success','info','warning','secondary'];
                    var cols = "";
                    for(y in location){
                      cols += '<span class="mr-2"><i class="fas fa-circle text-'+colour[y]+'"></i> '+location[y]+'</span>';
                    }
                    $('#infoChartLokasi').html(cols);
              
                    $('#visitorLocation').html('Visitor Location Country');
                    
reynaldi adriantama's avatar
reynaldi adriantama committed
90 91 92
                    }else{
                        alert(result.info);
                    }
Anggi Saputra's avatar
Anggi Saputra committed
93 94 95 96
            },
            error: function(xhr) {
                alert(xhr.status+'-'+xhr.statusText);            
            }
reynaldi adriantama's avatar
reynaldi adriantama committed
97
        });
Anggi Saputra's avatar
Anggi Saputra committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
    });
}

loadselectfirst(validasiToken);
function loadselectfirst(tokeni){
    $.ajax({
        type  : "post",
        dataType  : "JSON",
        url   : baseURL + "/api/loadyear",
        data : {
          token : tokeni
        },
        success:function(result){
          let data = result.data;
          option = '';
          option += `<option value=''>All Year</option>`
          for (x in data){
            option += `<option value='`+data[x]['date_part']+`'>`+data[x]['date_part']+`</option>`;
            
          }
          $('#selectyearvisit').html(option);
        }
      });
      
      let dates = new Date();
      let year = dates.getFullYear();
    
      optionmonth = '';
      optionmonth += '<option value="">All Month</option>';
    
      $.ajax({
        type : 'POST',
        dataType : 'JSON',
        url   : baseURL + "/api/loadvisitormonth",
        async : false,
        data : {
          year : year,
          token : validasiToken
        },
        success:function(result){
            let data = result.data;
            var month = [];
          
            for(x in data){
                month[x] = data[x]['month']
                if(month[x]==1){
                month[x]='Jan';
                }else if(month[x]==2){
                month[x]='Feb';
                }else if(month[x]==3){
                month[x]='Mar';
                }else if(month[x]==4){
                month[x]='April';
                }else if(month[x]==5){
                month[x]='May';
                }else if(month[x]==6){
                month[x]='Jun';
                }else if(month[x]==7){
                month[x]='Jul';
                }else if(month[x]==8){
                month[x]='Aug';
                }else if(month[x]==9){
                month[x]='Sept';
                }else if(month[x]==10){
                month[x]='Oct';
                }else if(month[x]==11){
                month[x]='Nov';
                }else if(month[x]==12){
                month[x]='Dec';
                }
                optionmonth += `<option value='`+data[x]['month']+`'>`+month[x]+`</option>`;
                $('#selectmonthvisit').html(optionmonth);
            }
        }
    });
}

$('#selectyearvisit').change(function(){
    let selectyear = $('#selectyearvisit').val();
    let selectmonth = $('#selectmonthvisit').val();
    let country = $('#selectcountryvl').val();
    let province =  $('#selectprovincevl').val();
    optionmonth = '';
    optionmonth += `<option value=''>All Month</option>`;
    $.ajax({
        type : 'POST',
        dataType : 'JSON',
        url   : baseURL + "/api/loadvisitormonth",
        async : false,
        data : {
        year : selectyear,
        token : validasiToken
        },
        success:function(result){

        let data = result.data;
        var month = [];
        
        for(x in data){
            month[x] = data[x]['month']
            if(month[x]==1){
            month[x]='Januari';
            }else if(month[x]==2){
            month[x]='Februari';
            }else if(month[x]==3){
            month[x]='Maret';
            }else if(month[x]==4){
            month[x]='April';
            }else if(month[x]==5){
            month[x]='Mei';
            }else if(month[x]==6){
            month[x]='Juni';
            }else if(month[x]==7){
            month[x]='Juli';
            }else if(month[x]==8){
            month[x]='Agustus';
            }else if(month[x]==9){
            month[x]='September';
            }else if(month[x]==10){
            month[x]='Oktober';
            }else if(month[x]==11){
            month[x]='November';
            }else if(month[x]==12){
            month[x]='Desember';
            }
            optionmonth += `<option value='`+data[x]['month']+`'>`+month[x]+`</option>`;
            $('#selectmonthvisit').html(optionmonth);
           
        }
        }
    });
    
     
  
    changevistorbytime(selectyear, selectmonth, country, province);
});

$('#selectmonthvisit').change(function(){
    let selectyear = $('#selectyearvisit').val();
    let selectmonth = $('#selectmonthvisit').val();
    let country = $('#selectcountryvl').val();
    let province =  $('#selectprovincevl').val();
    
    changevistorbytime(selectyear, selectmonth, country, province);
});


loadselectfirstvl(validasiToken);
function loadselectfirstvl(tokeni){
  option = '';
  $.ajax({
    type  : "post",
    dataType  : "JSON",
    url   : baseURL + "/api/loadcountry",
    data : {
      token : tokeni
    },
    success:function(result){
      let data = result.data;
      option = '';
      option += `<option value=''>All Country</option>`
      for (x in data){  
        option += `<option value = `+data[x]['country']+`>`+data[x]['country']+`</option>`
      }
      $('#selectcountryvl').html(option);
reynaldi adriantama's avatar
reynaldi adriantama committed
263
    }
Anggi Saputra's avatar
Anggi Saputra committed
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
    
  }); 
 
    optionprov = `<option value=''>All Province</option>`
    $('#selectprovincevl').html(optionprov);
  
}

$('#selectcountryvl').change(function(){
    let country = $('#selectcountryvl').val();
    let province =  $('#selectprovincevl').val();
    let selectyear = $('#selectyearvisit').val();
    let selectmonth = $('#selectmonthvisit').val();
    $.ajax({
      type  : "post",
      dataType  : "JSON",
      url   : baseURL + "/api/loadprovince",
      data : {
        token : validasiToken,
        country : country
      },success:function(result){
        let data = result.data;
        optionprov = '';
        optionprov = `<option value=''>All Province</option>`;
  
        for (x in data){
          optionprov += `<option value = '`+data[x]['province']+`'>`+data[x]['province']+`</option>`
        }
        $('#selectprovincevl').html(optionprov);
      }
    });
  
    optionprov = `<option value=''>All Province</option>`
    $('#selectprovincevl').html(optionprov);

    changevistorbytime(selectyear, selectmonth, country, province);
    if(country){
      $('#visitorLocation').html('Visitor Location Province');
    }else{
      $('#visitorLocation').html('Visitor Location Country');
    }    
});


$('#selectprovincevl').change(function(){
    let country = $('#selectcountryvl').val();
    let province =  $('#selectprovincevl').val();
    let selectyear = $('#selectyearvisit').val();
    let selectmonth = $('#selectmonthvisit').val();

    changevistorbytime(selectyear, selectmonth, country, province);
    if(province){
      $('#visitorLocation').html('Visitor Location City');
    }else{
      $('#visitorLocation').html('Visitor Location Province');
    }

});


function changevistorbytime(year, month, country, province){
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: baseURL + '/api/loadvisitorbytime',
        data: {
            token   : validasiToken,
            year : year,
            month : month,
            country : country,
            province : province
        },
        success: function(result){
            // console.log(result.data);
            let data = result.data;
            // console.log(data);

            let location = [];
            let total = [];

            for (x in data){
                location[x] = data[x]['location'];
                total[x] = data[x]['total'];
            }
            myPieChartnew = {
                labels: location,
                datasets: [{
                    data: total,
                    backgroundColor: ['#4e73df', '#1cc88a', '#36b9cc', '#f6c23e', "#c5c5c5"],
                    hoverBackgroundColor: ['#2e59d9', '#17a673', '#2c9faf', '#ffd700', '#c5c5c5'],
                    hoverBorderColor: "rgba(234, 236, 244, 1)",
                }],
              };
              myVisitorPie.data = myPieChartnew;
              window.myVisitorPie.update();

              var colour = ['primary','success','info','warning','secondary'];
              var cols = "";
              for(y in location){
                cols += '<span class="mr-2"><i class="fas fa-circle text-'+colour[y]+'"></i> '+location[y]+'</span>';
              }
              $('#infoChartLokasi').html(cols);
        }
    });
}