Search code examples
phpparsingurlaxisjpgraph

PHP jpgraph parse x and y axis through a url?


I have a jpgraph script that parses a URL to build a graph for me like this:

graph

How do I send the X-Axis data at the same time, as I need to skip months and the current method does not allow for this?

I'm guessing it would look something like this:

/chart/jpgraph/graphmaker.php?a=(Jan','5)&b=(Feb','3)&c=(Jun','4)

The jpgraph graph code in "graphmaker.php" is:

<?php 

$url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];


$parsedurl =  parse_url($url, PHP_URL_QUERY);

parse_str($parsedurl);


require_once 'src/jpgraph.php';
require_once 'src/jpgraph_line.php';



$datay1 = array($aa,$bb,$cc,$dd,$ee,$ff,$gg,$hh,$ii,$jj,$kk);
$datay2 = array($a,$b,$c,$d,$e,$f,$g,$h,$i,$j,$k);


// Setup the graph
$graph = new Graph(600,200);

$graph->SetScale("textlin",1,5);
$theme_class=new UniversalTheme;

$graph->SetTheme($theme_class);
$graph->img->SetAntiAliasing(false);

$graph->SetBox(false);

$graph->img->SetAntiAliasing();

$graph->yaxis->HideZeroLabel();
$graph->yaxis->HideLine(false);
$graph->yaxis->HideTicks(false,false);

$graph->xgrid->Show();
$graph->xgrid->SetLineStyle("solid");
$graph->xaxis->SetTickLabels(array('Sept','Oct','Nov','Dec','Jan','Feb','Mar','Apr','May','Jun','Jul'));
$graph->xgrid->SetColor('#E3E3E3');

// Create the first line
$p1 = new LinePlot($datay1);
$graph->Add($p1);
$p1->SetColor("#B22222");
$p1->SetLegend('Attitude');

// Create the second line
$p2 = new LinePlot($datay2);
$graph->Add($p2);
$p2->SetColor("#6495ED");
$p2->SetLegend('Progress');

$graph->legend->SetFrameWeight(1);

// Output line
$graph->Stroke();

?>

Solution

  • jpgraph allows skipping fields using "-". I didnt manage to solve this issue, but found a workaround by forwarding the dataset as -,-,2,4,-,1,-,-,5 and making a URI like this:

    /jpgraph/graphmaker.php?a=-&b=-&c=2&d=4&e=-&f=1&g=-&h=-&i=5&j=-
    

    this fix worked and achieve the desired results.

    I used @PandemoniumSyndicate advice here In PHP compare two arrays then make a new array based on a specific structure?

    Then added this to build the URI to pass to jpgraph:

    $urls = range('a', 'j');
    $newArray = array_combine($urls, $ar3);
    $mine = array();
    foreach ($newArray as $key => $value) { $mine[] = "$key=$value"; }
    $plov = implode('&',$mine);
    

    The URI to jpgraph was:

    /jpgraph/graphmaker.php?'.$plov.'
    

    Hpefully this will help others