/** Constroi o gráfico entre periodos de anos retorna true se o grafico foi montado e false se não foi **/ function buildGraphicByYear($stats, $startYear, $lastYear) { $graficoStatus = false; // Para descobrir se entrou no for e vai construir um gráfico $stat = $stats->getRequests(); // Pega todos os anos entre o Ano Inicial e o Ano Final $anos = array(); for ($j = 0, $year = $startYear; $year <= $lastYear; $year++, $j++) { $anos[$j] = $year; } foreach ($stat as $s) { $mes = intval($s->getMonth()); $ano = intval($s->getYear()); $values[$ano][$mes] = $s->getNumberOfRequests(); } /* "gabarito" da linha dos graficos com 12 posicoes, uma para cada mes os valores "-" sao considerados NULLs pelo jpgraph */ $data = array("0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"); /* "gabarito" para as cores das linhas do gráfico */ $cores = array("blue", "yellow", "purple", "cyan", "pink", "red", "orange", "green", "black", "sienna", "darkred", "darkgreen"); // Create the graph. These two calls are always required // $graph = new Graph(700,400,"auto"); $graph = new Graph(900, 300, "auto"); $graph->SetScale("textlin"); /* cria um array bi-dimencional contendo array(ano{array com a quantidade de acessos por mes}) */ $colorIndex = 0; $bars = array(); foreach ($values as $ano => $meses) { /* valores será a variavel que servirá de "datasource" para a barra do grafico inicialmente ele eh inicializada com o gabarito, e serao preenchidos os valores dos meses no laço for logo abaixo */ $valores = $data; for ($k = 0; $k <= count($valores); $k++) { if ($meses[$k]) { $valores[$k] = $meses[$k]; } } if (count($valores) > 12) { unset($valores[0]); } $valores = array_values($valores); /* aqui eu uso "Variáveis Variáveis" do PHP para poder inserir vária linhas no gráfico */ $nome = "barplot" . $ano; ${$nome} = new BarPlot($valores); $cor = $cores[$colorIndex]; ${$nome}->SetFillColor($cor); ${$nome}->SetColor($cor); /*configs para os valores do ponto*/ ${$nome}->value->SetColor("darkred"); ${$nome}->value->SetFont(FF_FONT1, FS_BOLD); ${$nome}->value->SetFormat("%0.1d"); // $$nome->SetWeight(20); // Arrumando para um tamanho mais amigavel if (count($anos) < 3) { ${$nome}->SetWidth(20); } else { if (count($anos) < 4) { ${$nome}->SetWidth(15); } else { if (count($anos) < 6) { ${$nome}->SetWidth(10); } else { if (count($anos) < 8) { ${$nome}->SetWidth(5); } else { if (count($anos) < 11) { ${$nome}->SetWidth(3); } } } } } ${$nome}->value->Show(); ${$nome}->value->iHideZero = true; ${$nome}->setLegend($ano); /*adicionando a linha ao grafico*/ $colorIndex++; // Somente monta o gráfico dos anos exigidos pelo usuário for ($i = 0; $i < count($anos); $i++) { if ($ano == $startYear + $i) { $graficoStatus = true; // entrou no for significa que o gráfico vai ser construido array_push($bars, ${$nome}); } } } /**************************************************************** * Se não existir dados estatísticos para o período selecionado * * Então ele constroi uma imagem com a mensagem de que não * * existem dados estatísticos. * *****************************************************************/ if ($graficoStatus == false) { $graph = new CanvasGraph(600, 30); $t1 = new Text(GRAFIC_STATS_FALSE); $t1->Pos(0.05, 0.5); $t1->SetOrientation('h'); $t1->SetFont(FF_FONT1, FS_BOLD); $t1->SetColor('black'); $graph->AddText($t1); $graph->Stroke(); return $graficoStatus; } $gbplot = new GroupBarPlot($bars); $gbplot->SetWidth(0.9); $graph->Add($gbplot); $graph->yaxis->scale->SetGrace(20); $graph->img->SetMargin(40, 20, 20, 40); $graph->title->Set(ARTICLE_ACCESS); $graph->xaxis->title->Set(MONTHS); $graph->yaxis->title->Set(ACCESSES); $graph->title->SetFont(FF_FONT1, FS_BOLD); $graph->yaxis->title->SetFont(FF_FONT1, FS_BOLD); $graph->xaxis->title->SetFont(FF_FONT1, FS_BOLD); $graph->SetShadow(); $graph->xaxis->SetTickLabels(explode(",", MONTH_LIST)); // Adjust the legend position // $graph->legend->SetLayout(LEGEND_VER); $graph->legend->Pos(0.04, 0.092, "", "center"); $graph->legend->SetLayout(LEGEND_HOR); // Mostra o gráfico somente se, o ano que o usuario entrou existir estatisticas if ($graficoStatus == true) { $graph->Stroke(); } return $graficoStatus; }
require_once 'jpgraph/jpgraph_table.php'; // Setup graph context $graph = new CanvasGraph(430, 150); // Setup the basic table $data = array(array('', 'w631', 'w632', 'w633', 'w634', 'w635', 'w636'), array('Critical (sum)', 13, 17, 15, 8, 3, 9), array('High (sum)', 34, 35, 26, 20, 22, 16), array('Low (sum)', 41, 43, 49, 45, 51, 47), array('Sum:', 88, 95, 90, 73, 76, 72)); // Setup a basic table $table = new GTextTable(); $table->Set($data); // Setup fonts $table->SetFont(FF_TIMES, FS_NORMAL, 11); $table->SetColFont(0, FF_ARIAL, FS_NORMAL, 11); $table->SetRowFont(0, FF_ARIAL, FS_NORMAL, 11); $table->SetRowFont(4, FF_TIMES, FS_BOLD, 14); // Turn off the grid $table->SetGrid(0); // Setup color $table->SetRowFillColor(0, 'lightgray@0.5'); $table->SetRowFillColor(4, 'lightgray@0.5'); $table->SetColFillColor(0, 'lightgray@0.5'); $table->SetFillColor(0, 0, 4, 0, 'lightgray@0.5'); // Set default minimum column width $table->SetMinColWidth(45); // Set default table alignment $table->SetAlign('right'); // Add table to the graph $graph->Add($table); // and send it back to the client $graph->Stroke(); ?>