Menu

No image displayed and no error message with plot data from database

Eric
2015-03-27
2015-03-27
  • Eric

    Eric - 2015-03-27

    I'm truly vexed. I am able to successfully plot and display a line graph when I declare the data array directly but cannot get it to work when I grab the same data from the database. I have compared the two arrays that get produced and they are identical. I have combared the plot object produced in both cases and they are also identical. But the DrawGraph method works in one case and not the other. Here is the code:

    // Grab the session data
    $databaseData = $_SESSION['data'];
    
    $data = array();
    
    for ($i = 0; $i < count($databaseData); $i++) {
        // Get the date interval associated with the week
        $strDateInterval = getDateInterval($databaseData[$i]['WeekNo'], $databaseData[$i]['Year']);
        $data[$i] = array((string)$strDateInterval, (float)$databaseData[$i]['Yvalues']);
    }
    
    // DEBUGGING: The $data array below is identical to the one produced above.
    
    // var_dump($data);
    
    // $data = array(
    //  array('2015-03-02 to 2015-03-09',0.0),
    //  array('2015-02-23 to 2015-03-01',7.9),
    //  array('2015-02-16 to 2015-02-22',0.0),
    //  array('2015-02-09 to 2015-02-15',4.8),
    //  array('2015-02-02 to 2015-02-08',38.7),
    //  array('2015-01-26 to 2015-02-01',28.5),
    //  array('2015-01-19 to 2015-01-25',52.05),
    //  array('2015-01-12 to 2015-01-18',26.85),
    //  array('2015-01-05 to 2015-01-11',44.7),
    //  array('2014-12-29 to 2015-01-04',28.9)
    // );
    
        // end DEBUGGING
    
    require_once "phplot.php";
    
    // Create a PHPlot object which will make an 800x400 pixel image:
    $plot = new PHPlot(800, 400);
    
    // Set the line styles
    $linestyles = array('solid');
    $plot->SetLineStyles($linestyles);
    
    // Set the point styles
    $pointshapes = array('box');
    $plot->SetPointShapes($pointshapes);
    
    // Set the line colors 
    $colors = array('#ff6600');
    $plot->SetDataColors($colors);
    
    // Use TrueType fonts:
    $plot->SetDefaultTTFont('./arial.ttf', 12);
    $plot->SetFont('x_label','arial',12);
    $plot->SetXLabelAngle(90);
    
    $plot->SetFont('y_label','arial',12);
    
    // Set the main plot title:
    $plot->SetTitle('My Plot');
    
    // Select the data array representation and store the data:
    $plot->SetDataType('text-data');
    $plot->SetDataValues($data);
    
    // Turn off X axis ticks and labels because they get in the way:
    $plot->SetXTickLabelPos('none');
    $plot->SetXTickPos('none');
    
    // Create the plot
    $plot->DrawGraph();
    //echo"<br><br>"; var_dump($plot);
    
    ?>
    
     
  • lbayuk

    lbayuk - 2015-03-27

    What exactly are you changing in the script when you go from static data to database data? Because if it isn't the data array, it must be something else.

    Are you able to check the web server error log to see if PHP has reported any errors? Since the script is trying to return an image, PHP errors won't be shown in the output.

    You should try to determine if you are getting no output at all, or if it is sending a corrupt image (usually caused by the script sending out some text before the image). If you can't use a tool like 'wget' (command-line page fetcher) - perhaps because of session setup - you should be able to do it in a browser. For example, in Firefox, use Tools > Page Info, go to the Media tab, and select the missing image. Is the size 0 or higher? If not zero, you can use Save As to save it to disk and then look at it, preferraby as a hex dump. It should start with 0x89 then the letters P N G (assuming a PNG file). If there is anything else before, that's your problem. You would need to find out what is outputing that before the image and get it to stop.

     
  • Eric

    Eric - 2015-03-27

    Thanks so much for the response! I'll do as you suggest and let you know what I find out. As far as changes all I do is uncomment the $data array you see commented out above. I don't even remove the php code that creates the $data array from the database; I just redefine $data and-- voila!-- it works just fine.

     
  • Eric

    Eric - 2015-03-27

    Here's a quick thought. I'm bringing it in as an image file. The code you see above is in ChartExample.php. Is this the issue?

    i.e., img class="img-responsive" src="ChartExample.php"

     

    Last edit: Eric 2015-03-27
  • lbayuk

    lbayuk - 2015-03-27

    You just uncomment $data = array(...) and it starts working, correct? Looping over the session data, converting the date interval, all that says in there either way?

    That is a puzzle.

    Using "img src=scriptname.php" is the correct way to embed a plot image inside an HTML page. In the browser, if the script fails for some reason, you should see either a broken image icon, or perhaps the "alt=..." text instead of the image. If you have no "alt" text on the image, you might see nothing at all.

    If you access your ChartExample.php directly in the browser, bypassing the HTML page (I don't know if this will work, because of the session data), what happens? When I try test cases like this, the browser displays "The image ... cannot be displayed because it contains errors". That is a good sign that there is some "junk" before the image data that PHPlot is producing.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.