| [42] | 1 | <?php // content="text/plain; charset=utf-8"
 | 
|---|
 | 2 | require_once ('jpgraph/jpgraph.php');
 | 
|---|
 | 3 | require_once ('jpgraph/jpgraph_line.php');
 | 
|---|
 | 4 | require_once ('jpgraph/jpgraph_bar.php');
 | 
|---|
 | 5 | 
 | 
|---|
 | 6 | function readsunspotdata($aFile, &$aYears, &$aSunspots) {
 | 
|---|
 | 7 |     $lines = @file($aFile,FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
 | 
|---|
 | 8 |     if( $lines === false ) {
 | 
|---|
 | 9 |         throw new JpGraphException('Can not read sunspot data file.');
 | 
|---|
 | 10 |     }
 | 
|---|
 | 11 |     foreach( $lines as $line => $datarow ) {
 | 
|---|
 | 12 |         $split = preg_split('/[\s]+/',$datarow);
 | 
|---|
 | 13 |         $aYears[] = substr(trim($split[0]),0,4);
 | 
|---|
 | 14 |         $aSunspots[] = trim($split[1]);
 | 
|---|
 | 15 |     }
 | 
|---|
 | 16 | }
 | 
|---|
 | 17 | 
 | 
|---|
 | 18 | $year = array();
 | 
|---|
 | 19 | $ydata = array();
 | 
|---|
 | 20 | readsunspotdata('yearssn.txt',$year,$ydata);
 | 
|---|
 | 21 | 
 | 
|---|
 | 22 |  // Width and height of the graph
 | 
|---|
 | 23 | $width = 600; $height = 200;
 | 
|---|
 | 24 | 
 | 
|---|
 | 25 | // Create a graph instance
 | 
|---|
 | 26 | $graph = new Graph($width,$height);
 | 
|---|
 | 27 | 
 | 
|---|
 | 28 | // Specify what scale we want to use,
 | 
|---|
 | 29 | // int = integer scale for the X-axis
 | 
|---|
 | 30 | // int = integer scale for the Y-axis
 | 
|---|
 | 31 | $graph->SetScale('intint');
 | 
|---|
 | 32 | 
 | 
|---|
 | 33 | // Setup a title for the graph
 | 
|---|
 | 34 | $graph->title->Set('Sunspot example');
 | 
|---|
 | 35 | 
 | 
|---|
 | 36 | // Setup titles and X-axis labels
 | 
|---|
 | 37 | $graph->xaxis->title->Set('(year from 1701)');
 | 
|---|
 | 38 | 
 | 
|---|
 | 39 | // Setup Y-axis title
 | 
|---|
 | 40 | $graph->yaxis->title->Set('(# sunspots)');
 | 
|---|
 | 41 | 
 | 
|---|
 | 42 | // Create the linear plot
 | 
|---|
 | 43 | $lineplot=new LinePlot($ydata);
 | 
|---|
 | 44 | 
 | 
|---|
 | 45 | // Add the plot to the graph
 | 
|---|
 | 46 | $graph->Add($lineplot);
 | 
|---|
 | 47 | 
 | 
|---|
 | 48 | // Display the graph
 | 
|---|
 | 49 | $graph->Stroke();
 | 
|---|
 | 50 | 
 | 
|---|
 | 51 | ?>
 | 
|---|