sunspotsex4.php

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    <?php // content="text/plain; charset=utf-8"
    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);

     // Width and height of the graph
    $width = 600; $height = 200;

    // Create a graph instance
    $graph = new Graph($width,$height);

    // Specify what scale we want to use,
    // int = integer scale for the X-axis
    // int = integer scale for the Y-axis
    $graph->SetScale('intint',0,0,0,max($year)-min($year)+1);

    // Setup a title for the graph
    $graph->title->Set('Sunspot example');

    // Setup titles and X-axis labels
    $graph->xaxis->title->Set('(year from 1701)');
    $graph->xaxis->SetTickLabels($year);

    // Setup Y-axis title
    $graph->yaxis->title->Set('(# sunspots)');

    // Create the linear plot
    $lineplot=new LinePlot($ydata);
    $lineplot->SetFillColor('orange@0.5');

    // Add the plot to the graph
    $graph->Add($lineplot);

    // Display the graph
    $graph->Stroke();

    ?>