Example #1
0
<?php

// content="text/plain; charset=utf-8"
require_once 'jpgraph/jpgraph.php';
require_once 'jpgraph/jpgraph_line.php';
$datay = array(1.23, 1.9, 1.6, 3.1, 3.4, 2.8, 2.1, 1.9);
$graph = new Graph(300, 200);
$graph->SetScale("textlin");
$graph->img->SetMargin(40, 40, 40, 40);
$graph->SetShadow();
$graph->SetGridDepth(DEPTH_FRONT);
$graph->title->Set("Example of filled line plot");
$graph->title->SetFont(FF_FONT1, FS_BOLD);
$p1 = new LinePlot($datay);
$p1->SetFillColor("orange");
$p1->mark->SetType(MARK_FILLEDCIRCLE);
$p1->mark->SetFillColor("red");
$p1->mark->SetWidth(4);
$graph->Add($p1);
$graph->Stroke();
Example #2
0
 function generate_graph_image()
 {
     // Create the graph.
     // Set up Font Mapping Arrays
     $fontfamilies = array("Font 0" => FF_FONT0, "Font 1" => FF_FONT1, "Font 2" => FF_FONT2, "Font0" => FF_FONT0, "Font1" => FF_FONT1, "Font2" => FF_FONT2, "Arial" => FF_ARIAL, "Verdana" => FF_VERDANA, "Courier" => FF_COURIER, "Comic" => FF_COMIC, "Times" => FF_TIMES, "Georgia" => FF_GEORGIA, "Trebuchet" => FF_TREBUCHE, "advent_light" => advent_light, "Bedizen" => Bedizen, "Mukti_Narrow" => Mukti_Narrow, "calibri" => calibri, "Forgotte" => Forgotte, "GeosansLight" => GeosansLight, "MankSans" => MankSans, "pf_arma_five" => pf_arma_five, "Silkscreen" => Silkscreen, "verdana" => verdana, "Vera" => FF_VERA);
     $fontstyles = array("Normal" => FS_NORMAL, "Bold" => FS_BOLD, "Italic" => FS_ITALIC, "Bold+Italic" => FS_BOLDITALIC);
     $this->apply_defaults();
     if (!function_exists("imagecreatefromstring")) {
         handle_error("Graph Option Not Available. Requires GD2");
         return;
     }
     if ($this->plot[0]["type"] == "PIE" || $this->plot[0]["type"] == "PIE3D") {
         $graph = new PieGraph($this->width_actual, $this->height_actual, "auto");
         $graph->SetScale("textlin");
         $graph->img->SetMargin($this->marginleft_actual, $this->marginright_actual, $this->margintop_actual, $this->marginbottom_actual);
         $graph->SetMarginColor($this->margincolor_actual);
         $graph->img->SetAntiAliasing();
         $graph->SetColor($this->graphcolor_actual);
         $graph->SetShadow();
         $graph->xaxis->SetTitleMargin($this->marginbottom_actual - 45);
         $graph->yaxis->SetTitleMargin($this->marginleft_actual - 25);
     } else {
         $graph = new Graph($this->width_actual, $this->height_actual, "auto");
         $graph->SetScale("textlin");
         $graph->img->SetMargin($this->marginleft_actual, $this->marginright_actual, $this->margintop_actual, $this->marginbottom_actual);
         $graph->SetMarginColor($this->margincolor_actual);
         $graph->img->SetAntiAliasing();
         $graph->SetColor($this->graphcolor_actual);
         $graph->SetShadow();
         $graph->xaxis->SetTitleMargin($this->marginbottom_actual - 45);
         $graph->yaxis->SetTitleMargin($this->marginleft_actual - 25);
     }
     $lplot = array();
     $lplotct = 0;
     foreach ($this->plot as $k => $v) {
         switch ($v["type"]) {
             case "PIE":
                 $lplot[$lplotct] = new PiePlot($v["data"]);
                 $lplot[$lplotct]->SetColor($v["linecolor"]);
                 foreach ($xlabels as $k => $v) {
                     $xlabels[$k] = $v . "\n %.1f%%";
                 }
                 $lplot[$lplotct]->SetLabels($xlabels, 1.0);
                 $graph->Add($lplot[$lplotct]);
                 break;
             case "PIE3D":
                 $lplot[$lplotct] = new PiePlot3D($v["data"]);
                 $lplot[$lplotct]->SetColor($v["linecolor"]);
                 foreach ($xlabels as $k => $v) {
                     $xlabels[$k] = $v . "\n %.1f%%";
                 }
                 $lplot[$lplotct]->SetLabels($xlabels, 1.0);
                 $graph->Add($lplot[$lplotct]);
                 break;
             case "STACKEDBAR":
             case "BAR":
                 $lplot[$lplotct] = new BarPlot($v["data"]);
                 $lplot[$lplotct]->SetColor($v["linecolor"]);
                 $lplot[$lplotct]->SetWidth(0.8);
                 //$lplot[$lplotct]->SetWeight(5);
                 if ($v["fillcolor"]) {
                     $lplot[$lplotct]->SetFillColor($v["fillcolor"]);
                 }
                 if ($v["legend"]) {
                     $lplot[$lplotct]->SetLegend($v["legend"]);
                 }
                 $graph->Add($lplot[$lplotct]);
                 break;
             case "LINE":
             default:
                 if (count($v["data"]) == 1) {
                     $v["data"][] = 0;
                 }
                 $lplot[$lplotct] = new LinePlot($v["data"]);
                 $lplot[$lplotct]->SetColor($v["linecolor"]);
                 //$lplot[$lplotct]->SetWeight(5);
                 if ($v["fillcolor"]) {
                     $lplot[$lplotct]->SetFillColor($v["fillcolor"]);
                 }
                 if ($v["legend"]) {
                     $lplot[$lplotct]->SetLegend($v["legend"]);
                 }
                 $graph->Add($lplot[$lplotct]);
                 break;
         }
         $lplotct++;
     }
     $graph->title->Set($this->title_actual);
     $graph->xaxis->title->Set($this->xtitle_actual);
     $graph->yaxis->title->Set($this->ytitle_actual);
     $graph->xgrid->SetColor($this->xgridcolor_actual);
     $graph->ygrid->SetColor($this->ygridcolor_actual);
     switch ($this->xgriddisplay_actual) {
         case "all":
             $graph->xgrid->Show(true, true);
             break;
         case "major":
             $graph->xgrid->Show(true, false);
             break;
         case "minor":
             $graph->xgrid->Show(false, true);
             break;
         case "none":
         default:
             $graph->xgrid->Show(false, false);
             break;
     }
     switch ($this->ygriddisplay_actual) {
         case "all":
             $graph->ygrid->Show(true, true);
             break;
         case "major":
             $graph->ygrid->Show(true, false);
             break;
         case "minor":
             $graph->ygrid->Show(false, true);
             break;
         case "none":
         default:
             $graph->ygrid->Show(false, false);
             break;
     }
     $graph->title->SetFont($fontfamilies[$this->titlefont_actual], $fontstyles[$this->titlefontstyle_actual], $this->titlefontsize_actual);
     $graph->title->SetColor($this->titlecolor_actual);
     $graph->xaxis->SetFont($fontfamilies[$this->xaxisfont_actual], $fontstyles[$this->xaxisfontstyle_actual], $this->xaxisfontsize_actual);
     $graph->xaxis->SetColor($this->xaxiscolor_actual, $this->xaxisfontcolor_actual);
     $graph->yaxis->SetFont($fontfamilies[$this->yaxisfont_actual], $fontstyles[$this->yaxisfontstyle_actual], $this->yaxisfontsize_actual);
     $graph->yaxis->SetColor($this->yaxiscolor_actual, $this->yaxisfontcolor_actual);
     $graph->xaxis->title->SetFont($fontfamilies[$this->xtitlefont_actual], $fontstyles[$this->xtitlefontstyle_actual], $this->xtitlefontsize_actual);
     $graph->xaxis->title->SetColor($this->xtitlecolor_actual);
     $graph->yaxis->title->SetFont($fontfamilies[$this->ytitlefont_actual], $fontstyles[$this->ytitlefontstyle_actual], $this->ytitlefontsize_actual);
     $graph->yaxis->title->SetColor($this->ytitlecolor_actual);
     $graph->xaxis->SetLabelAngle(90);
     $graph->xaxis->SetLabelMargin(5);
     $graph->yaxis->SetLabelMargin(5);
     $graph->xaxis->SetTickLabels($this->xlabels);
     $graph->xaxis->SetTextLabelInterval($this->xticklabelinterval_actual);
     $graph->yaxis->SetTextLabelInterval($this->yticklabelinterval_actual);
     $graph->xaxis->SetTextTickInterval($this->xtickinterval_actual);
     $graph->yaxis->SetTextTickInterval($this->ytickinterval_actual);
     $graph->SetFrame(true, 'darkblue', 2);
     $graph->SetFrameBevel(2, true, 'black');
     if ($this->gridpos_actual == "front") {
         $graph->SetGridDepth(DEPTH_FRONT);
     }
     // Display the graph
     $handle = $graph->Stroke(_IMG_HANDLER);
     return $handle;
 }
Example #3
0
 /**
  * Construct the graph
  * 
  */
 private function Init()
 {
     // Setup limits for color indications
     $lowx = $this->iXMin;
     $highx = $this->iXMax;
     $lowy = $this->iYMin;
     $highy = $this->iYMax;
     $width = $this->iWidth;
     $height = $this->iHeight;
     // Margins
     $lm = 50;
     $rm = 40;
     $tm = 60;
     $bm = 40;
     if ($width <= 300 || $height <= 250) {
         $labelsize = 8;
         $lm = 25;
         $rm = 25;
         $tm = 45;
         $bm = 25;
     } elseif ($width <= 450 || $height <= 300) {
         $labelsize = 8;
         $lm = 30;
         $rm = 30;
         $tm = 50;
         $bm = 30;
     } elseif ($width <= 600 || $height <= 400) {
         $labelsize = 9;
     } else {
         $labelsize = 11;
     }
     if ($this->iSubTitle == '') {
         $tm -= $labelsize + 4;
     }
     $graph = new Graph($width, $height);
     $graph->SetScale('intint', $lowy, $highy, $lowx, $highx);
     $graph->SetMargin($lm, $rm, $tm, $bm);
     $graph->SetMarginColor($this->iMarginColor[$this->iColorMap]);
     $graph->SetClipping();
     $graph->title->Set($this->iTitle);
     $graph->subtitle->Set($this->iSubTitle);
     $graph->title->SetFont(FF_ARIAL, FS_BOLD, $labelsize + 4);
     $graph->subtitle->SetFont(FF_ARIAL, FS_BOLD, $labelsize + 1);
     $graph->SetBox(true, 'black@0.3');
     $graph->xaxis->SetFont(FF_ARIAL, FS_BOLD, $labelsize);
     $graph->yaxis->SetFont(FF_ARIAL, FS_BOLD, $labelsize);
     $graph->xaxis->scale->ticks->Set(CCBPGraph::TickStep, CCBPGraph::TickStep);
     $graph->yaxis->scale->ticks->Set(CCBPGraph::TickStep, CCBPGraph::TickStep);
     $graph->xaxis->HideZeroLabel();
     $graph->yaxis->HideZeroLabel();
     $graph->xaxis->SetLabelFormatString('%d%%');
     $graph->yaxis->SetLabelFormatString('%d%%');
     // For the x-axis we adjust the color so labels on the left of the Y-axis are in black
     $n1 = floor(abs($this->iXMin / 25)) + 1;
     $n2 = floor($this->iXMax / 25);
     if ($this->iColorMap == 0) {
         $xlcolors = array();
         for ($i = 0; $i < $n1; ++$i) {
             $xlcolors[$i] = 'black';
         }
         for ($i = 0; $i < $n2; ++$i) {
             $xlcolors[$n1 + $i] = 'lightgray:1.5';
         }
         $graph->xaxis->SetColor('gray', $xlcolors);
         $graph->yaxis->SetColor('gray', 'lightgray:1.5');
     } else {
         $graph->xaxis->SetColor('darkgray', 'darkgray:0.8');
         $graph->yaxis->SetColor('darkgray', 'darkgray:0.8');
     }
     $graph->SetGridDepth(DEPTH_FRONT);
     $graph->ygrid->SetColor('gray@0.6');
     $graph->ygrid->SetLineStyle('dotted');
     $graph->ygrid->Show();
     $graph->xaxis->SetWeight(1);
     $graph->yaxis->SetWeight(1);
     $ytitle = new Text(CCBPGraph::YTitle, floor($lm * 0.75), ($height - $tm - $bm) / 2 + $tm);
     #$ytitle->SetFont(FF_VERA,FS_BOLD,$labelsize+1);
     $ytitle->SetAlign('right', 'center');
     $ytitle->SetAngle(90);
     $graph->Add($ytitle);
     $xtitle = new Text(CCBPGraph::XTitle, ($width - $lm - $rm) / 2 + $lm, $height - 10);
     #$xtitle->SetFont(FF_VERA,FS_BOLD,$labelsize);
     $xtitle->SetAlign('center', 'bottom');
     $graph->Add($xtitle);
     $df = 'D j:S M, Y';
     if ($width < 400) {
         $df = 'D j:S M';
     }
     $time = new Text(date($df), $width - 10, $height - 10);
     $time->SetAlign('right', 'bottom');
     #$time->SetFont(FF_VERA,FS_NORMAL,$labelsize-1);
     $time->SetColor('darkgray');
     $graph->Add($time);
     // Use an accumulated fille line graph to create the colored bands
     $n = 3;
     for ($i = 0; $i < $n; ++$i) {
         $b = $this->iColorInd[$i][0];
         $k = ($this->iColorInd[$i][1] - $this->iColorInd[$i][0]) / $this->iXMax;
         $colarea[$i] = array(array($lowx, $lowx * $k + $b), array($highx, $highx * $k + $b));
     }
     $colarea[3] = array(array($lowx, $highy), array($highx, $highy));
     $cb = array();
     for ($i = 0; $i < 4; ++$i) {
         $cb[$i] = new LinePlot(array($colarea[$i][0][1], $colarea[$i][1][1]), array($colarea[$i][0][0], $colarea[$i][1][0]));
         $cb[$i]->SetFillColor($this->iColorSpec[$this->iColorMap][$i]);
         $cb[$i]->SetFillFromYMin();
     }
     $graph->Add(array_slice(array_reverse($cb), 0, 4));
     $this->graph = $graph;
 }