// Create some random data for the plot. We use the current time for the // first X-position //------------------------------------------------------------------ $datay = array(); $datax = array(); $ts = time(); $n = 70; // Number of data points for ($i = 0; $i < $n; ++$i) { $datax[$i] = $ts + $i * 150000; $datay[$i] = rand(5, 60); $datay2[$i] = rand(1, 8); } // Now get labels at the start of each month $dateUtils = new DateScaleUtils(); list($tickPositions, $minTickPositions) = $dateUtils->getTicks($datax, DSUTILS_MONTH1); // Now create the real graph // Combine a line and a bar graph // We add some grace to the end of the X-axis scale so that the first and last // data point isn't exactly at the very end or beginning of the scale $grace = 400000; $xmin = $datax[0] - $grace; $xmax = $datax[$n - 1] + $grace; // Overall width of graphs $w = 450; // Left and right margin for each graph $lm = 25; $rm = 15; //---------------------- // Setup the line graph //----------------------
//------------------------------------------------------------------ // Create some random data for the plot. We use the current time for the // first X-position //------------------------------------------------------------------ $datay = array(); $datax = array(); $ts = time(); $n = 70; // Number of data points for ($i = 0; $i < $n; ++$i) { $datax[$i] = $ts + $i * 150000; $datay[$i] = rand(5, 60); $datay2[$i] = rand(1, 8); } // Now get labels at the start of each month list($tickPositions, $minTickPositions) = DateScaleUtils::getTicks($datax, DSUTILS_MONTH1); // Now create the real graph // Combine a line and a bar graph // We add some grace to the end of the X-axis scale so that the first and last // data point isn't exactly at the very end or beginning of the scale $grace = 400000; $xmin = $datax[0] - $grace; $xmax = $datax[$n - 1] + $grace; // Overall width of graphs $w = 450; // Left and right margin for each graph $lm = 25; $rm = 15; //---------------------- // Setup the line graph //----------------------
$graph = new Graph($width, $height); // We set the x-scale min/max values to avoid empty space // on the side of the plot $graph->SetScale('intlin', 0, 0, min($xdata), max($xdata)); $graph->SetMargin(60, 20, 40, 60); // Setup the titles $graph->title->SetFont(FF_ARIAL, FS_BOLD, 12); $graph->title->Set('Development since 1984'); $graph->subtitle->SetFont(FF_ARIAL, FS_ITALIC, 10); $graph->subtitle->Set('(Example using DateScaleUtils class)'); // Setup the labels to be correctly format on the X-axis $graph->xaxis->SetFont(FF_ARIAL, FS_NORMAL, 8); $graph->xaxis->SetLabelAngle(30); // The second paramter set to 'true' will make the library interpret the // format string as a date format. We use a Month + Year format $graph->xaxis->SetLabelFormatString('M, Y', true); // Get manual tick every second year list($tickPos, $minTickPos) = $dateUtils->getTicks($xdata, DSUTILS_YEAR2); $graph->xaxis->SetTickPositions($tickPos, $minTickPos); // First add an area plot $lp1 = new LinePlot($ydata, $xdata); $lp1->SetWeight(0); $lp1->SetFillColor('orange@0.85'); $graph->Add($lp1); // And then add line. We use two plots in order to get a // more distinct border on the graph $lp2 = new LinePlot($ydata, $xdata); $lp2->SetColor('orange'); $graph->Add($lp2); // And send back to the client $graph->Stroke();
/** * Executes test action * */ public function executeTest() { /* In order to use fonts, you must define the location to your fonts: define('TTF_DIR', '/usr/X11R6/share/fonts/ttf/'); */ //------------------------------------------------------------------ // Create some random data for the plot. We use the current time for the // first X-position //------------------------------------------------------------------ $datay = array(); $datax = array(); $ts = time(); $n = 70; // Number of data points for ($i = 0; $i < $n; ++$i) { $datax[$i] = $ts + $i * 150000; $datay[$i] = rand(5, 60); $datay2[$i] = rand(1, 8); } // Now get labels at the start of each month $date = new DateScaleUtils(); list($tickPositions, $minTickPositions) = $date->getTicks($datax, DSUTILS_MONTH1); // Now create the real graph // Combine a line and a bar graph // We add some grace to the end of the X-axis scale so that the first and last // data point isn't exactly at the very end or beginning of the scale $grace = 400000; $xmin = $datax[0] - $grace; $xmax = $datax[$n - 1] + $grace; // Overall width of graphs $w = 450; // Left and right margin for each graph $lm = 25; $rm = 15; //---------------------- // Setup the line graph //---------------------- $graph = new sfJpGraph('line', $w, 250); $graph = $graph->getJpGraph(); $graph->SetScale('linlin', 0, 0, $xmin, $xmax); $graph->SetMargin($lm, $rm, 10, 30); $graph->SetMarginColor('white'); $graph->SetFrame(false); $graph->SetBox(true); $graph->xaxis->SetTickPositions($tickPositions, $minTickPositions); $graph->xaxis->SetLabelFormatString('My', true); $graph->xgrid->Show(); $p1 = new LinePlot($datay, $datax); $graph->Add($p1); //---------------------- // Setup the bar graph //---------------------- $graph2 = new sfJpGraph('bar', $w, 110); $graph2 = $graph2->getJpGraph(); $graph2->SetScale('linlin', 0, 0, $xmin, $xmax); $graph2->SetMargin($lm, $rm, 5, 10); $graph2->SetMarginColor('white'); $graph2->SetFrame(false); $graph2->SetBox(true); $graph2->xgrid->Show(); $graph2->xaxis->SetTickPositions($tickPositions, $minTickPositions); $graph2->xaxis->SetLabelFormatString('My', true); $graph2->xaxis->SetPos('max'); $graph2->xaxis->HideLabels(); $graph2->xaxis->SetTickSide(SIDE_DOWN); $b1 = new BarPlot($datay2, $datax); $b1->SetFillColor('teal'); $b1->SetColor('teal:1.2'); $graph2->Add($b1); //----------------------- // Create a multigraph //---------------------- $mgraph = new sfJpGraph('mgraph', $w, 110); $mgraph = $mgraph->getJpGraph(); $mgraph->SetImgFormat('jpeg', 60); $mgraph->SetMargin(2, 2, 2, 2); $mgraph->SetFrame(true); $mgraph->AddMix($graph, 0, 0, 85); $mgraph->AddMix($graph2, 0, 250, 85); $mgraph->Stroke(); return sfView::NONE; }
function postProcess() { global $jpgraphTimeAlign, $jpgraphTickAlign, $dateUtils; $this->color_list = split(",", $this->colors); if ($this->hasxgrid) { $this->graph->xgrid->Show(); } if ($this->hasygrid) { $this->graph->ygrid->Show(); } if ($this->xlabel) { $this->graph->xaxis->title->Set($this->xlabel); } if ($this->ylabel) { $this->graph->yaxis->title->Set($this->ylabel); } if ($this->xlabelformat) { $this->graph->xaxis->SetLabelFormatString($this->xlabelformat, $this->xistime); } if ($this->ylabelformat) { $this->graph->yaxis->SetLabelFormatString($this->ylabelformat); } if ($this->fill) { $tmp = split(",", $this->fill); if (count($tmp) == 2) { $this->graph->ygrid->SetFill(true, $tmp[0], $tmp[1]); } else { if (count($tmp) == 1) { $this->graph->ygrid->SetFill(true, $tmp[0], $tmp[0]); } else { throw new JpgraphMWException("Error while parsing fill value (" . $this->fill . ")."); } } } if ($this->dateformat) { $this->graph->xaxis->scale->SetDateFormat($this->dateformat); } if ($this->usettf) { $this->graph->xaxis->SetFont($this->font); $this->graph->yaxis->SetFont($this->font); $this->graph->xaxis->title->SetFont($this->font); $this->graph->yaxis->title->SetFont($this->font); $this->graph->xaxis->SetLabelAngle($this->rotatexlegend); $this->graph->yaxis->SetLabelAngle($this->rotateylegend); $this->graph->legend->SetFont($this->font); } if ($this->legendposition) { $tmp = split(",", $this->legendposition); $this->graph->legend->Pos($tmp[0], $tmp[1]); } $this->graph->yaxis->scale->SetAutoMin($this->min); $this->graph->yaxis->scale->SetAutoMax($this->max); if ($this->timealign) { $timealign = split(",", $this->timealign); $this->graph->xaxis->scale->SetDateAlign($jpgraphTimeAlign[$timealign[0]], $jpgraphTimeAlign[$timealign[1]]); } if ($this->tickalign) { $dateUtils = new DateScaleUtils(); list($tickPos, $minTickPos) = $dateUtils->getTicks($this->datax, $jpgraphTickAlign[$this->tickalign]); $this->graph->xaxis->SetTickPositions($tickPos, $minTickPos); } }