Question

I'm connecting to mySQL and running a simple query and returning the query into an array, then imploding the array and attempting to graph it using jpgraph, but I don't get any data points.

<?php
// content="text/plain; charset=utf-8"
        include($_SERVER["DOCUMENT_ROOT"] . "/jpgraph/jpgraph.php");  
        include($_SERVER["DOCUMENT_ROOT"] . "/jpgraph/jpgraph_line.php");
        $monday1 = strtotime("this monday");
        $monday2 = date("Y-m-d",$monday1);
//connect to Database and return graph values
        $con=mysql_connect("localhost:3306","root","","mysql");
        $db_selected = mysql_select_db('mysql', $con);
        if (!$db_selected)
        {
            die ('Can\'t use mysql : ' . mysql_error());
        }
        $mondayquery = "SELECT invoicetotal
                FROM thermdata
                WHERE CAST( datestamp AS DATE ) =  '$monday2'
                ORDER BY datehour";
        $mondayresult = mysql_query($mondayquery,$con);
            while($mondayinfo = mysql_fetch_array( $mondayresult )) 
                { 
                    $monimp[] = $mondayinfo['invoicetotal'];
                }

// Convert the array 
        $mondayfinal = implode(",", $monimp);

//set variables for graph values as array
        $datay1 = array($mondayfinal);

// Setup the graph
        $graph = new Graph(1200,600);
        $graph->SetScale("intint");
        $theme_class=new UniversalTheme;
        $graph->SetTheme($theme_class);
        $graph->img->SetAntiAliasing(false);
        $graph->SetMargin(10,10,-500,10);
        $graph->SetBox(false);
        $graph->legend->SetPos(.01,0,'left','top');
        $graph->legend->SetColumns(7);
        $graph->legend->SetFont(FF_FONT2,FS_NORMAL, 72);
        $graph->legend->SetHColMargin(10);
        $graph->legend->SetLineWeight(5);

        $graph->img->SetAntiAliasing();

        $graph->yaxis->HideZeroLabel();
        $graph->yaxis->HideLine(true);
        $graph->yaxis->HideTicks(true,true);
        $graph->yaxis->SetColor('white','white');

        $graph->xgrid->Show();
        $graph->xgrid->SetLineStyle("solid");
        $graph->xaxis->SetTickLabels(array('00','01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24'));
        $graph->xgrid->SetColor('#E3E3E3');

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

// Output line
        $graph->Stroke();
                mysql_close($con);
?>

Basically the database contains a new invoicetotal every hour, and I want to graph them over 24 hours per day of the current week.

The variable $mondayfinal resolves as "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,278,627,1235,1919,2015", but I don't see what I'm doing wrong that's preventing jpgraph from plotting them.

Any help would be greatly appreciated!

Was it helpful?

Solution

You need to make sure your $datay1 array has individual points at each element, ie:

Array ( [0] => a [1] => b [2] => c ) 

Currently, the code would create a single element array with all values at element 0:

Array ( [0] => a,b,c )

This won't work in jpgraph.

Instead, Try explode() on $mondayfinal in csv form to create the proper array or just use $monimp instead as it appears to be in the proper format.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top