Jump to content

Recommended Posts

Posted

Boas tardes, consegui fazer o codigo em PHP para apanhar os dados, mas agora queria colocar aqueles dados que tenho num gráfico circular..


<?php
$sql = "SELECT distrito,
        round(count(`distrito`) / (SELECT count(`id`) FROM `users`), 2) * 100 AS 'percentagemregiao'
FROM `users`
GROUP BY `distrito`";
$reshabitacao = mysql_query($sql) or die(mysql_error());
while ($row1 = mysql_fetch_array($reshabitacao)) {
echo $row1['distrito'].": ";
   echo $row1['percentagemregiao']."%</br>";
}
?>

Tenho isto mas queria usar os dados que tenho para fazer um gráfico.

Posted
 <script type="text/javascript">
  function drawVisualization() {
    // Create and populate the data table.
    var data = google.visualization.arrayToDataTable([
 <?php
$sql = "SELECT distrito,
   count(`distrito`) AS 'percentagemregiao'
  FROM `users`
GROUP BY `distrito`";
$reshabitacao = mysql_query($sql) or die(mysql_error());
while ($row1 = mysql_fetch_array($reshabitacao)) {
	  ['Task', 'Hours per Day'],
	  ['Work', 11],
	  ['Eat', 2],
	  ['Commute', 2],
	  ['Watch TV', 3],
	  ['Sleep', 7]
   }
?>
    ]);

    // Create and draw the visualization.
    new google.visualization.PieChart(document.getElementById('visualization')).
	    draw(data, {title:"So, how was your day?"});
  }

  google.setonloadCallback(drawVisualization);
   </script>

 <?php
$sql = "SELECT distrito,
   count(`distrito`) AS 'percentagemregiao'
  FROM `users`
GROUP BY `distrito`";
$reshabitacao = mysql_query($sql) or die(mysql_error());
while ($row1 = mysql_fetch_array($reshabitacao)) {
	  ['Task', 'Hours per Day'],
	  ['Work', 11],
	  ['Eat', 2],
	  ['Commute', 2],
	  ['Watch TV', 3],
	  ['Sleep', 7]
   }
?>

na parte dos parenteses rectos, onde estão os números queria adicionar o "$row1['distrito']" para poder aparecer o numero de vezes q aquele distrito tem.

mas acho q tenho ali coisas a mais.

Podem ajudar-me?

Posted

a mim parece que tens é algo a menos:

/* linha de PHP */<?php
/* linha de PHP */$sql = "SELECT distrito,
/* linha de PHP */           count(`distrito`) AS 'percentagemregiao'
/* linha de PHP */  FROM `users`
/* linha de PHP */GROUP BY `distrito`";
/* linha de PHP */$reshabitacao = mysql_query($sql) or die(mysql_error());
/* linha de PHP */while ($row1 = mysql_fetch_array($reshabitacao)) {
/* linha de PHP */                  ['Task', 'Hours per Day'], /* Isto não deveria ser Javascript ??? */
/* linha de PHP */                  ['Work', 11],              /* Isto não deveria ser Javascript ??? */
/* linha de PHP */                  ['Eat', 2],                /* Isto não deveria ser Javascript ??? */
/* linha de PHP */                  ['Commute', 2],            /* Isto não deveria ser Javascript ??? */
/* linha de PHP */                  ['Watch TV', 3],           /* Isto não deveria ser Javascript ??? */
/* linha de PHP */                  ['Sleep', 7]               /* Isto não deveria ser Javascript ??? */
/* linha de PHP */    }
/* linha de PHP */?>

isso para não falar que o código não fará muito sentido. um ciclo de php com informação da base de dados que não faz nada com a informação ...

IRC : sim, é algo que ainda existe >> #p@p
Posted (edited)

ja consegui por aquilo a funcionar mas penso q o while nao está correcto.


<?php

require_once "config.php";

function rand_colorCode(){
$r = dechex(mt_rand(0,255)); // generate the red component
$g = dechex(mt_rand(0,255)); // generate the green component
$b = dechex(mt_rand(0,255)); // generate the blue component
$rgb = $r.$g.$b;
if($r == $g && $g == $b){
$rgb = substr($rgb,0,3); // shorter version
}
return '#'.$rgb;
}

$code = rand_colorCode();
echo 'This code is <span style="color:'.$code.'">lol</span>';
?>

<!--
You are free to copy and use this sample in accordance with the terms of the
Apache license (http://www.apache.org/licenses/LICENSE-2.0.html)
-->

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
   <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
   <title>
     Google Visualization API Sample
   </title>
   <script type="text/javascript" src="http://www.google.com/jsapi"></script>
   <script type="text/javascript">
     google.load('visualization', '1', {packages: ['corechart']});
   </script>
<?php
$sql = "SELECT distrito,
      count(`distrito`) AS 'percentagemregiao'
FROM `users`
GROUP BY `distrito`";
$reshabitacao = mysql_query($sql) or die(mysql_error());
while ($row1 = mysql_fetch_array($reshabitacao)) {
?>
   <script type="text/javascript">
     function drawVisualization() {
       // Create and populate the data table.
       var data = google.visualization.arrayToDataTable([

         ['<?php echo $row1['distrito']?>', <?php echo $row1['percentagemregiao']; ?>],
['<?php echo $row1['distrito']?>', <?php echo $row1['percentagemregiao']; ?>]
       ]);

       // Create and draw the visualization.
       new google.visualization.PieChart(document.getElementById('visualization')).
           draw(data, {title:"So, how was your day?"});
     }


     google.setonloadCallback(drawVisualization);
   </script>
<?php
}
?>
 </head>
 <body style="font-family: Arial;border: 0 none;">
   <div id="visualization" style="width: 600px; height: 400px;"></div>
 </body>
</html>
Edited by joaocasta
Posted (edited)

no opera deu-me isto:

This code is <span style="color:#2e39a1">lol</span>
<!--
You are free to copy and use this sample in accordance with the terms of the
Apache license (http://www.apache.org/licenses/LICENSE-2.0.html)
-->

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd%22>
<html xmlns="http://www.w3.org/1999/xhtml%22>
 <head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>
  Google Visualization API Sample
</title>
<script type="text/javascript" src="http://www.google.com/jsapi%22></script>
<script type="text/javascript">
  google.load('visualization', '1', {packages: ['corechart']});
</script>
  <script type="text/javascript">
  function drawVisualization() {
	// Create and populate the data table.
	var data = google.visualization.arrayToDataTable([

	  ['LOL', 1],
['LOL', 1]
	]);

	// Create and draw the visualization.
	new google.visualization.PieChart(document.getElementById('visualization')).
		draw(data, {title:"So, how was your day?"});
  }


  google.setonloadCallback(drawVisualization);
</script>
<script type="text/javascript">
  function drawVisualization() {
	// Create and populate the data table.
	var data = google.visualization.arrayToDataTable([

	  ['Set?bal', 4],
['Set?bal', 4]
	]);

	// Create and draw the visualization.
	new google.visualization.PieChart(document.getElementById('visualization')).
		draw(data, {title:"So, how was your day?"});
  }


  google.setonloadCallback(drawVisualization);
</script>
 </head>
 <body style="font-family: Arial;border: 0 none;">
<div id="visualization" style="width: 600px; height: 400px;"></div>
 </body>
</html>

e no chrome onde está LOL, diz Setubal, e á frente 4.

Edited by joaocasta

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.