bosnia Posted October 30, 2015 at 12:00 PM Report Share #589361 Posted October 30, 2015 at 12:00 PM (edited) estou a seguir este exemplo http://www.webcodefree.com.br/blog/?s=Google+Chart+API problema leio os dados em json campo valor, acontece que o grafico gerado é todo da mesma cor.. aqui esta o codigo php usado //Inicializando as variáveis $table = array(); $rows = array(); //Criando as colunas dentro do array $table['cols'] = array( array('id' => '','label' => 'ID', 'type' => 'string'), array('id' => '','label' => 'valor', 'type' => 'number'), ); $stmt->execute(); $result = $stmt->fetchAll(); foreach ($result as $row) { $temp = array(); $temp[] = array('v' => (string) $row['ID']); $temp[] = array('v' => (float) $row['valor']); $rows[] = array('c' => $temp); } $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; javascript <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script type="text/javascript" src="http://www.google.com/jsapi"></script> <script src="../backend/js/jquery.js" type="text/javascript"></script> <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: "getdados.php", dataType: "json", async: false }).responseText; alert(jsonData); // Coloque aqui as configurações do gráfico, acesse a documentação para mais opções var options = {'title': 'valores', 'width': 1000, 'height': 680, color: 'red', visibleInLegend: true, is3D:true, }; // Create our data table out of JSON data loaded from server. var wrapper = new google.visualization.ChartWrapper({ chartType: 'ColumnChart', dataTable: jsonData, options:options, containerId: 'chart_div' }); wrapper.draw(); var formatter = new google.visualization.NumberFormat( {prefix: '€ ', negativeColor: 'red', negativeParens: false}); formatter.format(wrapper, 1); //Desenha o gráfico } </script> <div id="chart_div" style="width: 900px; height: 500px;"></div> Edited October 30, 2015 at 12:00 PM by pessantiago Link to comment Share on other sites More sharing options...
HappyHippyHippo Posted October 30, 2015 at 12:05 PM Report Share #589363 Posted October 30, 2015 at 12:05 PM aqui tens como definir a cor das barras https://developers.google.com/chart/interactive/docs/gallery/columnchart#coloring-columns IRC : sim, é algo que ainda existe >> #p@p Portugol Plus Link to comment Share on other sites More sharing options...
bosnia Posted October 30, 2015 at 03:58 PM Author Report Share #589370 Posted October 30, 2015 at 03:58 PM boas a questão é que os valores da tabela podem variar não são fixos. isto é as labels . existem uma maneira que é ler o json e construir um array novo . estou a ver este exmplo http://www.webcodefree.com.br/blog/?p=770 logo o 1 grafico. tou a tentar exportar o meu array indo buscando os campos que quero mas não esta a dar. tenho um ficheiro com esta estrutura em json {"data":[{"REFINTERNA":"ACD PESS","0":"ACD PESS","reclpcinco":"1","1":"1","TotRec":"16.1300","2":"16.1300","Comissoes":"2.9300","3":"2.9300"},{"REFINTERNA":"ACD TRAB","0":"ACD TRAB","reclpcinco":"11","1":"11","TotRec":"2165.9100","2":"2165.9100","Comissoes":"222.9900","3":"222.9900"},{"REFINTERNA":"AUTO","0":"AUTO","reclpcinco":"82","1":"82","TotRec":"15804.1100","2":"15804.1100","Comissoes":"1549.1400","3":"1549.1400"},{"REFINTERNA":"DIVERSOS","0":"DIVERSOS","reclpcinco":"4","1":"4","TotRec":"1286.9100","2":"1286.9100","Comissoes":"125.6800","3":"125.6800"},{"REFINTERNA":"MR HABITA\u00c7AO","0":"MR HABITA\u00c7AO","reclpcinco":"5","1":"5","TotRec":"399.7400","2":"399.7400","Comissoes":"68.2300","3":"68.2300"},{"REFINTERNA":"MR INDUST\/COMER","0":"MR INDUST\/COMER","reclpcinco":"5","1":"5","TotRec":"1454.8900","2":"1454.8900","Comissoes":"210.3500","3":"210.3500"},{"REFINTERNA":"MULTI","0":"MULTI","reclpcinco":"1","1":"1","TotRec":"175.7800","2":"175.7800","Comissoes":"16.4300","3":"16.4300"},{"REFINTERNA":"PROT JURIDICA","0":"PROT JURIDICA","reclpcinco":"3","1":"3","TotRec":"49.9200","2":"49.9200","Comissoes":"5.4900","3":"5.4900"}]} quero manipular para que ficar [['acd pess',''prot juridica],[1,2]] em que um e dois sao valores das comissões Link to comment Share on other sites More sharing options...
bosnia Posted October 30, 2015 at 04:20 PM Author Report Share #589371 Posted October 30, 2015 at 04:20 PM ja consegui... fiz um random só que agora só me aparece apenas o ultimo registo da bd $table = array(); $rows = array(); foreach ($result as $row) { //Criando as colunas dentro do array $table['cols'] = array( array('label' => 'REFINTERNA', 'type' => 'string'), array('label' => array($row['REFINTERNA']), 'type' => 'number'), array('type' => 'string', 'p' => array('role' => 'style')) ); } foreach ($result as $row) { $temp = array(); $temp[] = array('v' => (string) $row['REFINTERNA']); $temp[] = array('v' => (float) $row['Comissoes']); $temp[] = array('v' => randomColor()); $rows[] = array('c' => $temp); } $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; function randomColor() { $str = 'color: #'; $background_colors = array('#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF'); $str = $background_colors[array_rand($background_colors)]; //for ($i = 0; $i < 6; $i++) { // $randNum = rand(0, 15); // switch ($randNum) { // case 10: $randNum = 'A'; // break; // case 11: $randNum = 'B'; // break; // case 12: $randNum = 'C'; // break; // case 13: $randNum = 'D'; // break; // case 14: $randNum = 'E'; // break; // case 15: $randNum = 'F'; // break; // } $str .= $randNum; return $str;} Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now