[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 | // Just keep the last 20 values in the arrays |
---|
| 23 | $year = array_slice($year, -20); |
---|
| 24 | $ydata = array_slice($ydata, -20); |
---|
| 25 | |
---|
| 26 | // Width and height of the graph |
---|
| 27 | $width = 600; $height = 200; |
---|
| 28 | |
---|
| 29 | // Create a graph instance |
---|
| 30 | $graph = new Graph($width,$height); |
---|
| 31 | |
---|
| 32 | // Specify what scale we want to use, |
---|
| 33 | // text = txt scale for the X-axis |
---|
| 34 | // int = integer scale for the Y-axis |
---|
| 35 | $graph->SetScale('textint'); |
---|
| 36 | |
---|
| 37 | // Setup a title for the graph |
---|
| 38 | $graph->title->Set('Sunspot example'); |
---|
| 39 | |
---|
| 40 | // Setup titles and X-axis labels |
---|
| 41 | $graph->xaxis->title->Set('(year)'); |
---|
| 42 | $graph->xaxis->SetTickLabels($year); |
---|
| 43 | |
---|
| 44 | // Setup Y-axis title |
---|
| 45 | $graph->yaxis->title->Set('(# sunspots)'); |
---|
| 46 | |
---|
| 47 | // Create the bar plot |
---|
| 48 | $barplot=new BarPlot($ydata); |
---|
| 49 | |
---|
| 50 | // Add the plot to the graph |
---|
| 51 | $graph->Add($barplot); |
---|
| 52 | |
---|
| 53 | // Display the graph |
---|
| 54 | $graph->Stroke(); |
---|
| 55 | |
---|
| 56 | ?> |
---|