Syehaji93

ChartJS

Nov 5th, 2022
1,179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.50 KB | Software | 0 0
  1. //javascript
  2. $(document).ready(function () {
  3.  
  4.     GetChartData();
  5.  
  6.     function GetChartData() {
  7.         $.ajax({
  8.             url: "<?php echo base_url('/tampilGrafik'); ?>",
  9.             method: 'GET',
  10.             dataType: 'json',
  11.             success: function (data) {
  12.               var label = [];
  13.               var value = [];
  14.               for (var i in data) {
  15.                   label.push(data[i].tipe);
  16.                   value.push(data[i].count);
  17.               }
  18.               const ctx = document.getElementById('myChart').getContext('2d');
  19.               const myChart = new Chart(ctx, {
  20.                
  21.                 type: 'bar',
  22.                 data: {
  23.                     labels: label,
  24.                     datasets: [{
  25.                         label: 'Jumlah Lot',
  26.                         data: value,
  27.                         backgroundColor: [
  28.                             "#0077B6",
  29.                             "#EF233C",
  30.                             "#8338EC",
  31.                             "#E4C1F9",
  32.                             "#FFD6FF",
  33.                             "#57CC99",
  34.                             "#8AC926",
  35.                             "#99D98C",
  36.                             "#D88C9A",
  37.                             "#AED9E0",
  38.                             "#B9FBC0",
  39.                             "#9D4EDD",
  40.                             "#70E000",
  41.                             "#A9DEF9",
  42.                             "#DEB887",
  43.                             "#A9A9A9",
  44.                             "#DC143C",
  45.                             "#F4A460",
  46.                             "#2E8B57",
  47.                             "#1D7A46",
  48.                             "#CDA776",
  49.                             "#989898",
  50.                             "#CB252B",
  51.                             "#E39371",
  52.                         ],
  53.                         borderColor: [
  54.                             "#264653",
  55.                         ],
  56.                         borderWidth: [1, 1, 1, 1, 1,1,1,1, 1, 1, 1,1,1]
  57.                     }]
  58.                 },
  59.                 options: {
  60.                     responsive: true,
  61.                     title: {
  62.                     display: true,
  63.                     position: "top",
  64.                     text: "Data pengecekan lot AX",
  65.                     fontSize: 18,
  66.                     fontColor: "#111"
  67.                     },
  68.                     legend: {
  69.                     display: true,
  70.                     position: "bottom",
  71.                     labels: {
  72.                         fontColor: "#333",
  73.                         fontSize: 16
  74.                     }
  75.                     },
  76.                     scales: {
  77.                         y: {
  78.                             beginAtZero: true,
  79.                             suggestedMax: 12
  80.                         }
  81.                     },
  82.                     layout: {
  83.                         padding: 10
  84.                     }
  85.                 }
  86.             });
  87.  
  88.                 //respondCanvas();
  89.             }
  90.         });
  91.     };
  92.  
  93. });
  94.  
  95. //controller
  96. public function grafik()
  97. {
  98.     $builder = $this->db->query('SELECT ch_name as tipe, ch_delivery as delivery, COUNT(ch_id) as count FROM tb_cek_harian WHERE ch_delivery = "2022-11-02" GROUP BY ch_name ORDER BY (ch_delivery) ASC');
  99.     $chart = $builder->getResultArray();
  100.  
  101.     $data = [
  102.         'title'             => 'Dashboard',
  103.         'chart_data'        => $chart,
  104.     ];
  105.     echo json_encode($data);
  106. }
  107.  
  108. //view
  109. <div class="card-body">
  110.     <canvas id="myChart"></canvas>
  111. </div>
Advertisement
Add Comment
Please, Sign In to add comment