<?php 
require_once ('jpgraph/jpgraph.php');
require_once ('jpgraph/jpgraph_line.php');
require_once ('jpgraph/jpgraph_bar.php');
 
function readsunspotdata($aFile, &$aYears, &$aSunspots) {
    $lines = @file($aFile,FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
    if( $lines === false ) {
        throw new JpGraphException('Can not read sunspot data file.');
    }
    foreach( $lines as $line => $datarow ) {
        $split = preg_split('/[\s]+/',$datarow);
        $aYears[] = substr(trim($split[0]),0,4);
        $aSunspots[] = trim($split[1]);
    }
}
 
$year = array();
$ydata = array();
readsunspotdata('yearssn.txt',$year,$ydata);
 
$year = array_slice($year, -20);
$ydata = array_slice($ydata, -20);
 
 
$width = 600; $height = 200;
 
$graph = new Graph($width,$height);
 
$graph->SetScale('textint');
 
$graph->title->Set('Sunspot example');
 
$graph->xaxis->title->Set('(year)');
$graph->xaxis->SetTickLabels($year);
 
$graph->yaxis->title->Set('(# sunspots)');
 
$barplot=new BarPlot($ydata);
 
$graph->Add($barplot);
 
$graph->Stroke();
 
?> |