JpGraph: Beispiel: BarPlot: Statistik der verwendeten Browser eines Besuchers
geschrieben von Julian Hofmann am 18. Dezember 2008 - zuletzt aktualisiert am 5. August 2013
<?PHP function gProzent($alle, $anteil, $stellen = 2) { if ($anteil > 0) return (round(($anteil / ($alle / 100 )), $stellen)); else return 0; } function sBrowser($stext) { if (strpos($stext, "Googlebot") > 0) return(10); if (strpos($stext, "oogle-Sitemaps") > 0) return(10); if (strpos($stext, "Yahoo!") > 0) return(11); if (strpos($stext, "snbot") > 0) return(12); if (strpos($stext, "Safari") > 0) return (4); if (strpos($stext, "Firefox") > 0) return (2); if (strpos($stext, "MSIE") > 0) return (0); if (strpos($stext, "pera/") > 0) return (3); if (strpos($stext, "BonEcho") > 0) return (4); if (strpos($stext, "ozilla") > 0) return (1); return (5); } $browser = array("Internet\nExplorer", "Mozilla\n(Engine)", "Mozilla\nFirefox", "Opera", "Safari", "andere"); for ($z = 0; $z < count($browser); $z++) $aBrowser[$z] = 0; $monate = array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"); $handle = fopen("access_log", "r"); while (!feof($handle)) { $zeile = fgets($handle, 4096); $gBrowser = explode("\"", $zeile); if (strstr($gBrowser[1], "?")) { $p = explode(" ", $zeile); $datum = ((substr($p[3], 8, 4))."-" .(array_search(substr($p[3], 4, 3), $monate) + 1) ."-{$p[3][1]}{$p[3][2]} ".(substr($p[3], 13, 8))); $ts = strtotime($datum); $Besucher[$p[0]][0] = $ts; $oBrowser = $fBrowser; $fBrowser = sBrowser($gBrowser[count($gBrowser) - 2]); if ( (!($Besucher[$p[0]][1])) || (($oBrowser <> $fBrowser) && ($p[0] == $oClient)) || (($Besucher[$p[0]][1] - $Besucher[$p[0]][0]) > 86400) ) $Besucher[$p[0]][2][] = $fBrowser; if ($ts > $Besucher[$p[0]][1]) $Besucher[$p[0]][1] = $ts; $oClient = $p[0]; } } fclose ($handle); foreach ($Besucher as $Anzahl) { foreach ($Anzahl[2] as $Ident) if (array_key_exists($Ident, $browser)) $aBrowser[$Ident]++; } $gesamt = array_sum($aBrowser); foreach ($aBrowser as $Ident => $Anzahl) { $pBrowser[$Ident] = gProzent($gesamt, $Anzahl); $nullBrowser[$Ident] = 0; } $graph = new Graph(600, 300, "auto"); $graph->SetScale("textlin"); $graph->SetY2Scale("lin"); $graph->Set90AndMargin(70, 20, 55, 58); $graph->SetMarginColor("#EBEBEB"); $graph->title->Set("(echte) Besucher nach Browser"); $graph->title->SetFont(FF_VERDANA,FS_BOLD, 9); $graph->title->SetMargin(8); $graph->title->SetColor("black"); $graph->xaxis->SetFont(FF_VERDANA, FS_BOLD, 7); $graph->yaxis->SetFont(FF_VERDANA, FS_BOLD, 7); $graph->y2axis->SetFont(FF_VERDANA, FS_BOLD, 7); $graph->yaxis->SetLabelMargin(12); $graph->y2axis->SetLabelMargin(12); $graph->xaxis->SetLabelMargin(12); $graph->xaxis->HideZeroLabel(); $graph->xaxis->SetTickLabels($browser); $graph->xgrid->Show(false, false); $graph->ygrid->Show(true, false); $graph->ygrid->SetColor("lightblue"); $graph->ygrid->SetFill(true, "#EFEFEF@0.6", "#BBCCFF@0.6"); $graph->ygrid->SetLineStyle("dashed"); $graph->ygrid->SetWeight(1); $graph->xaxis->SetLabelAlign("right", "center", "right"); $graph->yaxis->SetLabelAlign("center", "bottom"); $graph->yaxis->SetColor("#D5552A"); $graph->y2axis->SetColor("#769FEB"); $graph->yaxis->scale->SetGrace(5); $graph->y2axis->scale->SetGrace(5); $graph->yaxis->HideZeroLabel(); $graph->y2axis->HideZeroLabel(); $graph->yaxis->HideLastTickLabel(); $graph->y2axis->HideLastTickLabel(); $bplot0 = new BarPlot($nullBrowser); $bplot1 = new BarPlot($pBrowser); $bplot1->SetFillColor("#D5552A"); $bplot1->SetWidth(0.5); $bplot1->value->Show(); $bplot1->value->SetFormat("%01.1f%%"); $bplot1->value->SetFont(FF_FONT0, FS_BOLD); $bplot1->value->SetColor("black"); $bplot2 = new BarPlot($aBrowser); $bplot2->SetFillColor("#769FEB"); $bplot2->SetWidth(0.5); $bplot2->value->Show(); $bplot2->value->SetFormat("%d"); $bplot2->value->SetFont(FF_FONT0, FS_BOLD); $bplot2->value->SetColor("black"); $bplot1->SetLegend("prozentual"); $bplot2->SetLegend("absolut"); $graph->legend->Pos(0.5, 0.97, "center", "bottom"); $graph->legend->SetLayout(LEGEND_HOR); $graph->legend->SetColor("darkblue"); $graph->legend->SetFont(FF_VERDANA, FS_BOLD, 7); $graph->legend->SetFillColor("white"); $graph->legend->SetShadow(FALSE); $gbplot1 = new GroupBarPlot(array($bplot1, $bplot0)); $gbplot2 = new GroupBarPlot(array($bplot0, $bplot2)); $graph->Add($gbplot1); $graph->AddY2($gbplot2); $graph->Stroke("include/tmp/jpgraph/JpGraph_VergleichBrowser.png"); ?>