JPGARPH Erreur 25121

mont_dani Messages postés 232 Date d'inscription   Statut Membre Dernière intervention   -  
mont_dani Messages postés 232 Date d'inscription   Statut Membre Dernière intervention   -
Bonjour a tous

Après beaucoup de recherhce et de temps passé j ai réussi a preéparér ce script, mais j'ai un problème d'affichage d'un graphique JPGRAPH.

Je suis sous chrome et W8

Le grapique exemple s'affiche très bien, mais quand je passe avec la variable, il affiche un petit carre blanc au milieu d'écran.

Quand je fais un print_r(array_values($ydata)); j'obtient Array ( [0] => 10 [1] => 40 [2] => 5 [3] => 5 [4] => 20 [5] => 91 ).

Merci pour votre coup de main.

Mon script est le suivant.





<?php
session_start ();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<meta charset="UTF-8">
<link rel="stylesheet" href="general.css" type="text/css" media="screen"/>
<link rel="stylesheet" href="print.css" type="text/css" media="print" />
</head>
<style type="text/css" media="all">

</style>

<body>
<?php
require_once ('jpgraph/src/jpgraph.php');
require_once ('jpgraph/src/jpgraph_line.php');
require_once ('connexion.php');

$_SESSION['mes'] = $_POST['mes'];
$mes_opcion = $_SESSION['mes'];

//$_SESSION['an'] = $_POST['annee'];
//$an_opcion = $_SESSION['an'];

$req = $connexion->prepare('SELECT fecha, lluvia FROM meteo WHERE MONTH(fecha) = ? ');
$req->execute(array($mes_opcion[0]));

$ydata = array();
$indexydata = 0; // init l'index de dépot
// Lecture en boucle des résultats du Query
// Le while boucle tant qu'il y a quelque chose
// et le fetch() avance d'une case à chaque lecture
//$ydata2 = array();
while ($row=$req->fetch(PDO::FETCH_ASSOC))
{
//echo '<li>' . $row['fecha'] . ' ' . $row['lluvia'] . '</li>';
$ydata[$indexydata] = $row['lluvia'];
$indexydata++; // Passe à l'index suivant
}


// Some (random) data
//$ydata = array(11, 3, 8, 12, 5, 1, 9, 13, 5, 7);
//$ydata2 = array(1, 19, 15, 7, 22, 14, 5, 9, 21, 13 );

// Size of the overall graph
$width=350;
$height=250;

// Create the graph and set a scale.
// These two calls are always required
$graph = new Graph($width,$height);
$graph->SetScale('intlin');
$graph->SetShadow();

// Setup margin and titles
$graph->SetMargin(40,20,20,40);
$graph->title->Set('Calls per operator (June,July)');
$graph->subtitle->Set('(March 12, 2008)');
$graph->xaxis->title->Set('Operator');
$graph->yaxis->title->Set('# of calls');

$graph->yaxis->title->SetFont( FF_FONT1 , FS_BOLD );
$graph->xaxis->title->SetFont( FF_FONT1 , FS_BOLD );

// Create the first data series
$lineplot=new LinePlot($ydata);
$lineplot->SetWeight( 2 ); // Two pixel wide

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

// Create the second data series
//$lineplot2=new LinePlot($ydata2);
//$lineplot2->SetWeight( 2 ); // Two pixel wide

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

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

$req->closeCursor();
session_unset ();
session_destroy ();

?>

</body>
</html>




A voir également:

2 réponses

jordane45 Messages postés 38486 Date d'inscription   Statut Modérateur Dernière intervention   4 752
 
Bonjour,

essayes ça :
<?php
//Démarrage des sessions
session_start ();

//Affichage des erreurs PHP
error_reporting(E_ALL);
ini_set('display-errors','on');


// REQUIRE
require_once ('jpgraph/src/jpgraph.php');
require_once ('jpgraph/src/jpgraph_line.php');
require_once ('connexion.php');


//Récupération PROPRE des variables AVANT de les utiliser
$mes_opcion  = !empty($_POST['mes']) ? $_POST['mes'] : (!empty($_SESSION['mes']) ? $_SESSION['mes'] : NULL);
$_SESSION['mes'] = $mes_opcion ;


//Traitement
$sql = 'SELECT fecha, lluvia FROM meteo WHERE MONTH(fecha) = ? ';
$datas = array($mes_opcion[0]);
try{
  $req = $connexion->prepare($sql);
  $req->execute($datas);
  $row = $req->fetchAll(PDO::FETCH_ASSOC); //on stocke les données dans un ARRAY
}catch(Exception $e){
  echo "Erreur ! ".$e->getMessage();
}

$ydata = array();
if(!empty($row)){
  //On parcours l'array
  foreach($row as $R)  {
    $ydata[] = (float)$R['lluvia'];
  }
}else{
  echo " La requête n'a pas retournée de données.... ";
}

$req->closeCursor();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" >
  <head>
    <meta http-equiv="Content-Type" content="text/html" />       
    <meta charset="UTF-8">       
    <link rel="stylesheet" href="general.css" type="text/css" media="screen"/>
    <link rel="stylesheet" href="print.css" type="text/css" media="print" />       
    <style type="text/css" media="all">
    </style>
   </head>
   <body>
    <?php

    // Size of the overall graph
    $width=350;
    $height=250;
     
    // Create the graph and set a scale.
    // These two calls are always required
    $graph = new Graph($width,$height);
    $graph->SetScale('intlin');
    $graph->SetShadow();
     
    // Setup margin and titles
    $graph->SetMargin(40,20,20,40);
    $graph->title->Set('Calls per operator (June,July)');
    $graph->subtitle->Set('(March 12, 2008)');
    $graph->xaxis->title->Set('Operator');
    $graph->yaxis->title->Set('# of calls');
     
    $graph->yaxis->title->SetFont( FF_FONT1 , FS_BOLD );
    $graph->xaxis->title->SetFont( FF_FONT1 , FS_BOLD );
     
    // Create the first data series
    $lineplot=new LinePlot($ydata);
    $lineplot->SetWeight( 2 );   // Two pixel wide
     
    // Add the plot to the graph
    $graph->Add($lineplot);
     
    // Display the graph
    $graph->Stroke();
    ?>
  </body>
</html>
 


NB: J'ai placé le maximum de PHP AVANT le HTML.
Cela rend le code plus lisible et plus facile à maintenir.

NB²: Tu avais des erreurs de balises HTML ( mal placées...)


NB3 : Vu que tu sembles utiliser le PDO, penses à activer la gestion des erreurs comme ceci : https://forums.commentcamarche.net/forum/affich-37584941-php-pdo-gerer-les-erreurs


Cordialement, 
Jordane                                                                 
0
mont_dani Messages postés 232 Date d'inscription   Statut Membre Dernière intervention  
 
Bonjour Jordan45.

Merci pour ta réponse et les modif du script.

La gestion des erreurs etait activée dans connexion.php.

Je continue avec le même probléme. (carré blanc au milieu de l'écran.)
0
jordane45 Messages postés 38486 Date d'inscription   Statut Modérateur Dernière intervention   4 752 > mont_dani Messages postés 232 Date d'inscription   Statut Membre Dernière intervention  
 
tu peux, dans ton navigateur, afficher le code source de la page (donc celui généré à l'affichage par ton navigateur....) et nous le coller ici ?

Regarde si tu n'y vois pas une erreur php
0
mont_dani Messages postés 232 Date d'inscription   Statut Membre Dernière intervention   > jordane45 Messages postés 38486 Date d'inscription   Statut Modérateur Dernière intervention  
 
l'option "afficher le code source n'est pas disponible"

si je bloque l'affichage de la ligne ydata (Create the first data series)
j'ai le code suivant:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" >
<head>
<meta http-equiv="Content-Type" content="text/html" />
<meta charset="UTF-8">
<link rel="stylesheet" href="general.css" type="text/css" media="screen"/>
<link rel="stylesheet" href="print.css" type="text/css" media="print" />
<style type="text/css" media="all">
</style>
</head>
<body>
JpGraph Error: 25026 Can't draw unspecified Y-scale. You have either: 1. Specified an Y axis for auto scaling but have not supplied any plots. 2. Specified a scale manually but have forgot to specify the tick steps


J ai fait un "inspecter" de la page avec le carre blanc et voici le resultat:

Dans HTML - console j ai erreur suivante

Uncaught DOMException: Failed to execute 'getRangeAt' on 'Selection': 0 is not a valid index.
at file:///C:/Users/PC%20Lenovo/AppData/Local/Google/Chrome/User%20Data/Default/Extensions/fbhgckgfljgjkkfngcoeajbgndkeoaaj/1.0_0/script.js:9:26
2jp_graph.php:1 Error in event handler for (unknown): TypeError: Cannot read property 'farewell' of undefined
0
jordane45 Messages postés 38486 Date d'inscription   Statut Modérateur Dernière intervention   4 752 > mont_dani Messages postés 232 Date d'inscription   Statut Membre Dernière intervention  
 
OK.
Change juste ça pour voir et re-teste
  $index=1;
  foreach($row as $R)  {
    $ydata[] = (float)$R['lluvia'];
    $index ++;
  }

0
mont_dani Messages postés 232 Date d'inscription   Statut Membre Dernière intervention   > jordane45 Messages postés 38486 Date d'inscription   Statut Modérateur Dernière intervention  
 
Je suis désolé. Mais cela ne change rien.
0
jordane45 Messages postés 38486 Date d'inscription   Statut Modérateur Dernière intervention   4 752
 
Bon..

déjà :

print_r(array_values($ydata));
Array ( [0] => 10 [1] => 40 [2] => 5 [3] => 5 [4] => 20 [5] => 91 ) qui est parfaitement correct avec la BD.


Tu as sûrement oublié d'initialiser l'index à 1.
  $index=1;
  foreach($row as $R)  {
    $ydata[$index] = (float)$R['lluvia'];
    $index ++;
  }


Ensuite...

Dans
foreach($row as $R) {
$ydata[$index] = (float)$R['lluvia'];
$index ++;
}

j ai fait
foreach($row as $R) {
$ydata[$index] = (float)$R['lluvia'];
$ydata[$index] = (float)$R['fecha'];
$index ++;
}


Oulaaaa !!!!
Qu'est-ce que tu fais ????
Que vient faire cette ligne ici :
$ydata[$index] = (float)$R['fecha']; 

Tu REMPLACES dans $ydata .. les données par les DATES ???
Non!!
Pour afficher les dates sur ton graphes .. c'est le $xdata qu'il faut remplir et non le ydata.
(le Ydata contient les VALEURS en Y ... et le Xdata... c'est l'absice (l'axe des X) )


Donc :
$ydata = array();
$xdata = array();
if(!empty($row)){
  $index=1;
  foreach($row as $R)  {
    $ydata[$index] = (float)$R['lluvia'];
    $xdata[$index] = $R['fecha']; 
    $index ++;
  }
}else{
  echo " La requête n'a pas retournée de données.... ";
  exit();
}


puis faudra donc faire un
$lineplot = new LinePlot($data,$xdata);


Voir l'exemple ici :
https://jpgraph.net/download/manuals/chunkhtml/ch14s10.html

.
0
mont_dani Messages postés 232 Date d'inscription   Statut Membre Dernière intervention  
 
Jordane,

Tout d'abord excuse moi pour ce mélange que j'ai fait.

Bien sur, je suis d'accord avec toi pour mettre les dates sur l'axe x.

Pour le moment je doit arriver a ce que le graph reconnaisse la variable $ydata sans riem faire avec l'axe x, comme dans le modele.. Et ca , ca reste un mystère...
0
jordane45 Messages postés 38486 Date d'inscription   Statut Modérateur Dernière intervention   4 752 > mont_dani Messages postés 232 Date d'inscription   Statut Membre Dernière intervention  
 
juste pour être sûr .... ton fichier est bien en utf8 sans bom ?
Peux tu vérifier (en t'appuyant sur ça : https://forums.commentcamarche.net/forum/affich-37584944-php-html-caracteres-accentues-et-l-utf8#1-verifiez-l-encodage-des-fichiers )
0
mont_dani Messages postés 232 Date d'inscription   Statut Membre Dernière intervention   > jordane45 Messages postés 38486 Date d'inscription   Statut Modérateur Dernière intervention  
 
Je te confirme mon fichier est bien en utf8 sans bom.

connexion.php
<?php
try{
$connexion =new PDO('mysql:host=localhost;
dbname=meteo_test_3; charset=utf8', 'root', '');
// Activation des erreurs PDO
$connexion->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// mode de fetch par défaut : FETCH_ASSOC / FETCH_OBJ / FETCH_BOTH
$connexion->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
} catch(PDOException $e) {
die('Erreur : ' . $e->getMessage());
}
?>
0
jordane45 Messages postés 38486 Date d'inscription   Statut Modérateur Dernière intervention   4 752 > mont_dani Messages postés 232 Date d'inscription   Statut Membre Dernière intervention  
 
Donc le fichier est en utf8.
La connexion à la bdd est en utf8 aussi
Ok.

On peut refaire un teste avec ce code ?
<?php
//Démarrage des sessions
session_start ();

//Affichage des erreurs PHP
error_reporting(E_ALL);
ini_set('display-errors','on');


// REQUIRE
require_once ('jpgraph/src/jpgraph.php');
require_once ('jpgraph/src/jpgraph_line.php');
require_once ('connexion.php');


//Récupération PROPRE des variables AVANT de les utiliser
$mes_opcion  = !empty($_POST['mes']) ? $_POST['mes'] : (!empty($_SESSION['mes']) ? $_SESSION['mes'] : NULL);
$_SESSION['mes'] = $mes_opcion ;

/*
//Traitement
$sql = 'SELECT fecha, lluvia FROM meteo WHERE MONTH(fecha) = ? ';
$datas = array($mes_opcion[0]);
try{
  $req = $connexion->prepare($sql);
  $req->execute($datas);
  $row = $req->fetchAll(PDO::FETCH_ASSOC); //on stocke les données dans un ARRAY
}catch(Exception $e){
  echo "Erreur ! ".$e->getMessage();
}

$ydata = array();
$xdata = array();
if(!empty($row)){
  $index=1;
  foreach($row as $R)  {
    $ydata[$index] = intval($R['lluvia']);
    $xdata[$index] = $R['fecha']; 
    $index ++;
  }
}else{
  echo " La requête n'a pas retournée de données.... ";
  print_r($datas);
  exit();
}

$req->closeCursor();
*/

$ydata = array(11, 3, 8, 12, 5, 1, 9, 13, 5, 7);

?>
<!DOCTYPE>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html" />       
    <meta charset="UTF-8">       
    <link rel="stylesheet" href="general.css" type="text/css" media="screen"/>
    <link rel="stylesheet" href="print.css" type="text/css" media="print" />       
    <style type="text/css" media="all">
    </style>
   </head>
   <body>
    <div id="graphique">
    <?php

    // Size of the overall graph
    $width=350;
    $height=250;
     
    // Create the graph and set a scale.
    // These two calls are always required
    $graph = new Graph($width,$height);
    $graph->SetScale('intlin');
    $graph->SetShadow();
    $graph->img->SetAntiAliasing(); 
    // Setup margin and titles
    $graph->SetMargin(40,20,20,40);
    $graph->title->Set('Calls per operator (June,July)');
    $graph->subtitle->Set('(March 12, 2008)');
    $graph->xaxis->title->Set('Operator');
    $graph->yaxis->title->Set('# of calls');
     
    $graph->yaxis->title->SetFont( FF_FONT1 , FS_BOLD );
    $graph->xaxis->title->SetFont( FF_FONT1 , FS_BOLD );
     
    
     
    // Create the first data series
    $lineplot=new LinePlot($ydata);
    $lineplot->SetWeight(2);   // Two pixel wide
 
    // Add the plot to the graph
    $graph->Add($lineplot);
     
    // Display the graph
    $graph->Stroke();
    ?>
    </div>
  </body>
</html>


Si ça fonctionne... ça vient bien des données transmises par la bdd... et faudra qu'on trouve pourquoi.
Si ça ne fonctionne pas... c'est que ton appel au script n'est pas le bon.
0
mont_dani Messages postés 232 Date d'inscription   Statut Membre Dernière intervention   > jordane45 Messages postés 38486 Date d'inscription   Statut Modérateur Dernière intervention  
 
Désolé cela ne fonctionne pas.
0