phphtmlmysqljpgraph

How to fix " Uncaught TypeError: Argument 1 passed to JpGraphException::defaultHandler() " problem in php 7.1.3 and jpgraph 4.2.6?


I'm working on a graph with a Satisfaction form with a database with jpgraph and I've encountered some issues with an handler error.

This is my program:

<?php
    require_once("./src/jpgraph.php");
    require_once("./src/jpgraph_pie.php");
    require_once("./src/jpgraph_pie3d.php");

    $db="questionnaire";
    try
    {
    $bdd=new PDO('mysql:host=localhost;dbname='.$db,'root','');
    }
    catch(Exception $exception)
    {
    die('Erreur :'.$exception->getMessage());
    }
    //var
    $req=$bdd->prepare('SELECT Adaptation1 FROM s3');
    // on execute la requete avec le contenu saisi 'user'

    $req->execute();    
    $valpour = 100;
    $comptage=array(
    "Vide"=>0,
    "Satisfaisante"=>0,
    "Inadaptee"=>0,
    "Incomplete"=>0);   
    /*$vide = cal_pour($comptage["Vide"],$comptage,$valpour);
    $Satisfaisante = cal_pour($comptage["Satisfaisante"],$comptage,$valpour);
    $Inadaptee = cal_pour($comptage["Inadaptee"],$comptage,$valpour);
    $Incomplete = cal_pour($comptage["Incomplete"],$comptage,$valpour);*/
    while ($donnees=$req->fetch(PDO::FETCH_ASSOC))
       {
        foreach ($donnees as $indice=>$valeur){
        echo $valeur."<br/>";
        if ($valeur=="") $comptage["Vide"]++;
        else    $comptage[$valeur]++;
        }
    }

    /*function cal_pour($nombre,$total,$pourcentage)
    { 
     $resultat = ($nombre/$total) * $pourcentage;
    return round($resultat); // Arrondi la valeur
    } */

    $data = array($comptage["Vide"],$comptage["Satisfaisante"],$comptage["Inadaptee"],$comptage["Incomplete"]);
    //print_r($data);
    $graph = new PieGraph(400,300);
    $theme_class= new VividTheme;
    $graph->SetTheme($theme_class);

    $graph->title->Set("Résultat Question PPN");

    $p1 = new PiePlot3D($data);
    $graph = Add($p1);
    $p1->ShowBorder();
    $p1->SetColor("green");
    $p1->ExplodeSlice(1);
    $graph->Stroke();
?>

This is the error I've got:

Fatal error: Uncaught TypeError: Argument 1 passed to JpGraphException::defaultHandler() must be an instance of Exception, instance of Error given in C:\Users\Stric\Documents\projet-tut\projet-tut\src\jpgraph_errhandler.inc.php:158 Stack trace: #0 [internal function]: JpGraphException::defaultHandler(Object(Error)) #1 {main} thrown in C:\Users\Stric\Documents\projet-tut\projet-tut\src\jpgraph_errhandler.inc.php on line 158

here is the respective line 158:

 static public function defaultHandler(Exception $exception) {
        global $__jpg_OldHandler;
        if( $exception instanceof JpGraphException ) {
            $exception->Stroke();
        }
        else {
            // Restore old handler
            if( $__jpg_OldHandler !== NULL ) {
                set_exception_handler($__jpg_OldHandler);
            }
            throw $exception;
        }
    }
}

Solution

  • Simply change $graph = Add($p1); to $graph->Add($p1); and your graph will work.

    $p1 = new PiePlot3D($data);
    $graph->Add($p1);  // this is where the change is
    $p1->ShowBorder();
    $p1->SetColor("green");
    $p1->ExplodeSlice(1);
    $graph->Stroke();