Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. <html>
  2.  
  3. <head>
  4. <title>Conexão google chart MSSQL</title>
  5. <!--Load the AJAX API-->
  6. <script type="text/javascript" src="https://www.google.com/jsapi"></script>
  7. <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
  8. </head>
  9.  
  10. <body>
  11.  
  12. <div id="chart_div"></div>
  13.  
  14. <script type="text/javascript">
  15. // Carregue o API Visualization e o pacote piechart.
  16. google.load('visualization', '1', {'packages':['corechart']});
  17.  
  18. // Defina um callback a ser executado quando o API de visualização do Google é carregado.
  19. google.setOnLoadCallback(drawChart);
  20.  
  21. function drawChart() {
  22. var jsonData = $.ajax({
  23. url: 'grafico.php',
  24. dataType:'json',
  25. async: false
  26. }).responseText;
  27.  
  28. // Coloque aqui as configurações do gráfico
  29. var options = {'title':'Abertos a mais de 30 dias',
  30. 'width':1000,
  31. 'height':680
  32. };
  33.  
  34. // Create our data table out of JSON data loaded from server.
  35. var data = new google.visualization.DataTable(jsonData);
  36.  
  37. // Instancia o gráfico com as opções definida na function drawChart() lá em cima
  38. var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
  39.  
  40. //Desenha o gráfico
  41. chart.draw(data, options);
  42. }
  43. </script>
  44. </body>
  45. </html>
  46.  
  47. <!-- FUNÇÃO PHP PARA CONEXÃO COM O BANCO DE DADOS -->
  48. <?PHP
  49. header("Content-Type: text/html; charset=iso-8859-1", true);
  50. //Dados do Banco
  51. $username = "...";
  52. $password = "...";
  53. $host = "...";
  54. $dbname = "...";
  55.  
  56. //Conexão com o banco de dados
  57. $con = mssql_connect($host, $username, $password) or die (mssql_get_last_message());
  58. @mssql_select_db ($dbname) or die ("Não foi possível selecionar o banco de dados!");
  59. echo 'conectado';
  60.  
  61. //Consulta no banco de dados
  62. $instrucaoSQL = "SELECT grupo.last_name AS 'Grupo Selecionado',count(*) AS'Contador' FROM call_req ORDER BY count(*) desc";
  63. $resultado = mssql_query($instrucaoSQL);
  64.  
  65. //Inicializando as variáveis
  66. $table = array();
  67. $rows = array();
  68. $flag = true;
  69.  
  70. //Criando as colunas dentro do array
  71. $table['cols'] = array(
  72. array('label' => 'GRUPO', 'type' => 'string'),
  73. array('label' => 'QUANTIDADE', 'type' => 'number')
  74. );
  75.  
  76. //Preenchendo as linhas do array auxiliar "$row" com os dados do banco
  77. while($row = mssql_fetch_assoc($resultado)) {
  78. $temp = array();
  79. $temp[] = array('v' => (string) $row['Grupo Selecionado']);
  80. $temp[] = array('v' => (int) $row['Contador']);
  81.  
  82. $rows[] = array('c' => $temp);
  83. }
  84.  
  85. //Adiciona o array auxiliar "$row" como um array dentro da variavel tabela.
  86. $table['rows'] = $rows;
  87.  
  88. //"json_encode" é uma função do próprio php que irá transformar o array em JSON
  89. $jsonTable = json_encode($table);
  90. echo $jsonTable;
  91. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement