source: trunk/xgraph/jpgraph/Examples/sunspotsex7.php

Last change on this file was 42, checked in by marrucho, 10 years ago
File size: 1.5 KB
Line 
1<?php // content="text/plain; charset=utf-8"
2require_once ('jpgraph/jpgraph.php');
3require_once ('jpgraph/jpgraph_line.php');
4require_once ('jpgraph/jpgraph_bar.php');
5
6function 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();
20readsunspotdata('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?>
Note: See TracBrowser for help on using the repository browser.