Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <head>
- <title>Conexão google chart MSSQL</title>
- <!--Load the AJAX API-->
- <script type="text/javascript" src="https://www.google.com/jsapi"></script>
- <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
- </head>
- <body>
- <div id="chart_div"></div>
- <script type="text/javascript">
- // Carregue o API Visualization e o pacote piechart.
- google.load('visualization', '1', {'packages':['corechart']});
- // Defina um callback a ser executado quando o API de visualização do Google é carregado.
- google.setOnLoadCallback(drawChart);
- function drawChart() {
- var jsonData = $.ajax({
- url: 'grafico.php',
- dataType:'json',
- async: false
- }).responseText;
- // Coloque aqui as configurações do gráfico
- var options = {'title':'Abertos a mais de 30 dias',
- 'width':1000,
- 'height':680
- };
- // Create our data table out of JSON data loaded from server.
- var data = new google.visualization.DataTable(jsonData);
- // Instancia o gráfico com as opções definida na function drawChart() lá em cima
- var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
- //Desenha o gráfico
- chart.draw(data, options);
- }
- </script>
- </body>
- </html>
- <!-- FUNÇÃO PHP PARA CONEXÃO COM O BANCO DE DADOS -->
- <?PHP
- header("Content-Type: text/html; charset=iso-8859-1", true);
- //Dados do Banco
- $username = "...";
- $password = "...";
- $host = "...";
- $dbname = "...";
- //Conexão com o banco de dados
- $con = mssql_connect($host, $username, $password) or die (mssql_get_last_message());
- @mssql_select_db ($dbname) or die ("Não foi possível selecionar o banco de dados!");
- echo 'conectado';
- //Consulta no banco de dados
- $instrucaoSQL = "SELECT grupo.last_name AS 'Grupo Selecionado',count(*) AS'Contador' FROM call_req ORDER BY count(*) desc";
- $resultado = mssql_query($instrucaoSQL);
- //Inicializando as variáveis
- $table = array();
- $rows = array();
- $flag = true;
- //Criando as colunas dentro do array
- $table['cols'] = array(
- array('label' => 'GRUPO', 'type' => 'string'),
- array('label' => 'QUANTIDADE', 'type' => 'number')
- );
- //Preenchendo as linhas do array auxiliar "$row" com os dados do banco
- while($row = mssql_fetch_assoc($resultado)) {
- $temp = array();
- $temp[] = array('v' => (string) $row['Grupo Selecionado']);
- $temp[] = array('v' => (int) $row['Contador']);
- $rows[] = array('c' => $temp);
- }
- //Adiciona o array auxiliar "$row" como um array dentro da variavel tabela.
- $table['rows'] = $rows;
- //"json_encode" é uma função do próprio php que irá transformar o array em JSON
- $jsonTable = json_encode($table);
- echo $jsonTable;
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement