Advertisement
LucianoCharles2017

chartJS

Aug 22nd, 2018
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.86 KB | None | 0 0
  1. <?php
  2. $host = 'localhost';
  3. $dbuser = 'root';
  4. $dbpass = '';
  5. $dbname = 'grupo++';
  6. $options = [PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES UTF8"];
  7.  
  8. $db = new PDO("mysql:dbname=" . $dbname . ";host=" . $host, $dbuser, $dbpass, $options);
  9. $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  10. $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
  11.  
  12. $sql = "SELECT * FROM usuarios";
  13. $stmt = $db->prepare($sql);
  14. try {
  15.     $stmt->execute();
  16.     if ($stmt->rowCount() > 0) {
  17.         $dados = $stmt->fetchAll();
  18.     }
  19. } catch (PDOException $e) {
  20.     die($e->getMessage());
  21. }
  22. $labels = null;
  23. $dados_chart = null;
  24. foreach ($dados as $user) {
  25.     $labels[] = $user['nome'];
  26.     $dados_chart[] = $user['idade'];
  27. }
  28. ?>
  29. <!DOCTYPE>
  30. <html lang="pt-br">
  31.     <head>
  32.         <title>Gráficos com ChartJs</title>
  33.     </head>
  34.     <body>
  35.  
  36.         <?php ?>
  37.  
  38.  
  39.         <div id="container" style="width: 75%;">
  40.             <canvas id="myChart" width="200" height="100"></canvas>
  41.         </div>
  42.  
  43.         <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
  44.         <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.min.js"></script>
  45.         <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.bundle.min.js"></script>
  46.  
  47.         <script>
  48.             var ctx = document.getElementById("myChart").getContext('2d');
  49.             var myChart = new Chart(ctx, {
  50.                 type: 'bar',
  51.                 data: {
  52.                     labels: <?php echo json_encode($labels); ?>,
  53.                     datasets: [{
  54.                             label: 'Idade do Usuário',
  55.                             data: <?php echo json_encode($dados_chart); ?>,
  56.                             backgroundColor: [
  57.                                 'rgba(255, 99, 132, 0.2)',
  58.                                 'rgba(54, 162, 235, 0.2)',
  59.                                 'rgba(255, 206, 86, 0.2)',
  60.                                 'rgba(75, 192, 192, 0.2)',
  61.                                 'rgba(153, 102, 255, 0.2)'
  62.                             ],
  63.                             borderColor: [
  64.                                 'rgba(255,99,132,1)',
  65.                                 'rgba(54, 162, 235, 1)',
  66.                                 'rgba(255, 206, 86, 1)',
  67.                                 'rgba(75, 192, 192, 1)',
  68.                                 'rgba(153, 102, 255, 1)'
  69.                             ],
  70.                             borderWidth: 1
  71.                         }]
  72.                 },
  73.                 options: {
  74.                     scales: {
  75.                         yAxes: [{
  76.                                 ticks: {
  77.                                     beginAtZero: true
  78.                                 }
  79.                             }]
  80.                     }
  81.                 }
  82.             });
  83.         </script>
  84.     </body>
  85. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement