I'm trying to do one graphic in phpplot, I want to group by year and month all the register users in that time and show in a graphic that results.
In database is like that:
id | utilizador | registado
----+------------+--------------------
1 | crisutu | 2014-05-04 10:41:53
2 | blabla | 2014-05-15 04:45:24
3 | hello | 2014-06-15 04:45:24
I want to transform in the graphic like MAY 2014 - 2 register users JUNE 2014 - 1 register user
<?php
#Incluimos a biblioteca
require("phplot-6.1.0/phplot.php");
#Definimos o objeto para inicar a "montagem" do gráfico
#Também delimitamos uma altura e largura do gráfico
$grafico = new PHPlot(750,600);
#Indicamos o formato de imagem a ser usado
$grafico->SetFileFormat("png");
#Indicamos o títul do gráfico e o título dos dados no eixo X e Y do mesmo
$grafico->SetTitle("NSMF");
$grafico->SetXTitle("Meses");
$grafico->SetYTitle("Utilizadores");
function getValues(){
$db = new mysqli("xxxxxxxxx", "xxxxxxxxxxxx", "xxxxxxxxxxxxx", "xxxxxxxxx");
$db->set_charset("utf8");
$query = $db->query("SELECT COUNT(id),registado FROM utilizadores GROUP BY YEAR(registado), MONTH(registado) ");
while($obj = $query->fetch_object()){
$values[] = array($obj->id, $obj->id );
}
return $values;
}
$dados = getValues();
$grafico->SetDataValues($dados);
#Exibimos o gráfico
$grafico->DrawGraph();
?>
Can you help me resolve the problem? Tell me what is wrong?
So here are three mistakes you made in your code:
I do believe that you are trying to access the property id
of fetched object, but the property is actually COUNT(id)
. You may consider using alias in the query, eg:
SELECT COUNT(id) as id, registrado ...
By default PHPlot::setDataValues
expect following format:
array(
array('key1', 'value11', 'value12'),
array('key2', 'value21', 'value22'),
array('key3', 'value31', 'value32'),
)
in your case key
s are months so the first value of every array must be appropriate month, so write $values[] = array($obj->registado, $obj->id );
, instead of $values[] = array($obj->id, $obj->id );
Finally for your date to appear in desired format use php function date
(http://php.net/manual/en/function.date.php) when assigning values to $value
. So use something like this:
$values[] = array(date("%F %Y", $obj->registado), $obj->id);