Problems with passing Data to the graph

90 views
Skip to first unread message

Funskater Friedberg

unread,
Jul 22, 2020, 8:15:03 AM7/22/20
to JpGraph
Dear all,
I generated a lot of graphs with jpgraph before, selecting data from a MySql/MariaDB. Now I try to extract the Data using a cURL acces to an IBM Db2 Database. Whatever I do I don't get a graph.

Here is my php fragment:

$erg=curl_exec($ch);
$json=json_decode($erg,true);
             $datax=array_column($json['ResultSet Output'], 'C1');
             $datay=array_column($json['ResultSet Output'], 'ANZAHL');

 // schließe den cURL-Handle und gib die Systemresourcen frei
 curl_close($ch);
#print_r($json);
#print_r($datax);
#print_r($datay);
// Create the graph. These two calls are always required
$graph = new Graph(1000,600);
$graph->SetScale('intlin');
 
// Add a drop shadow
$graph->SetShadow();
 
// Adjust the margin a bit to make more room for titles
$graph->SetMargin(40,30,20,40);
 
// Create a bar pot
$bplot = new BarPlot($datay);
 
// Adjust fill color
$bplot->SetFillColor('orange');
$graph->Add($bplot);
 
// Setup the titles
$graph->title->Set('A basic bar graph');
$graph->xaxis->title->Set('X-title');
$graph->yaxis->title->Set('Y-title');
 
$graph->title->SetFont(FF_FONT1,FS_BOLD);
$graph->yaxis->title->SetFont(FF_FONT1,FS_BOLD);
$graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD);
 
// Display the graph
$graph->Stroke();

The Bar graph is the demo graph from the website.
If I list the content of the arrays I receive a correct ourput. ($json reflects the result from Db2, $datay should replace the $datay from the example. $datax should reflect the x-axis)

I know it will be a silly thing, but I'm a little confused.

Does anyone has a glue for me?

Thanks in advance.

Fred

Content of the arrays

$json: Array ( [ResultSet Output] => Array ( [0] => Array ( [C1] => C [ANZAHL] => 96 ) [1] => Array ( [C1] => D [ANZAHL] => 34 ) [2] => Array ( [C1] => E [ANZAHL] => 27 ) [3] => Array ( [C1] => F [ANZAHL] => 577 ) ) [StatusCode] => 200 [StatusDescription] => Execution Successful )
$datax: Array ( [0] => C [1] => D [2] => E [3] => F )
$datay: Array ( [0] => 96 [1] => 34 [2] => 27 [3] => 577 )

Jon Taylor

unread,
Jul 23, 2020, 1:53:35 AM7/23/20
to JpGraph
Are you seeing any error messages? A common problem is that some sort of HTML is being sent before the header of the graph, this will normally result in an error message being generated. but sometimes you just get a blank screen. If you see any text at all on the svreen your graph is not going to be rendered, and even whitespace will mess things up.

One way of checking for this is to stroke to a file and look at that file:

$graph->Stroke("test.png");

There are a few issues with your code, but nothing that would stop some kind of graph being generated:
  • You populate $datax but you never make use of it. I am assuming you meant to do something like:
    $graph->xaxis->SetTickLabels($datax);

  • You are using a text scale for x, so you should really use
    $graph->SetScale('textlin');

Have you tried replacing the data retrieved from the database with literal arrays, just to check that there isn't something odd in the data that printing it isn't showing up?

$datax = Array ('C','D','E','F');
$datay
= Array (96,34,27,577) ;


Funskater Friedberg

unread,
Jul 23, 2020, 2:21:53 AM7/23/20
to JpGraph
Thanks Jon,
I'll follow your proposals.
And yes, I know, that I didn't use $datax, I just wanted to show the values. The graph is the simple demo, without the x-Axis Ticks.
That drives me crazy. I did hundreds of graphs before, never had serious problems.
Thanks for your support

Funskater Friedberg

unread,
Jul 23, 2020, 3:59:09 AM7/23/20
to JpGraph
Hi Jon,

test_uid3.png

I now see the rendered Grap in the output png file. If I try to view directly a message appears

I checked for echos, prints etc, but it is all clear. The cURL is working properly and shows the correct results in the stored graph.

I listed the complete code below. Very strange

<?php // content="text/plain; charset=utf-8"
include("include/inc_session.php");


# payload für SQL
#$data = json_encode($postdata);
$auth = base64_encode($cred);
$hdrs = array(
              'Authorization: Basic '.$auth,
              'Content-Type: application/json',
              'Accept: application/json'
              );

# erzeuge einen neuen cURL-Handle
 $ch = curl_init();

# setze die URL und andere Optionen
 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
 curl_setopt($ch, CURLOPT_URL, "http://xxxxxx:yyy/services/RACF_AUDIT/count_user/v1");
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_HTTPHEADER, $hdrs);
 
# führe die Aktion aus und gib die Daten an den Browser weiter

$erg=curl_exec($ch);
$json=json_decode($erg,true);
     $datax=array_column($json['ResultSet Output'], 'C1');
     $datay=array_column($json['ResultSet Output'], 'ANZAHL');


# schließe den cURL-Handle und gib die Systemresourcen frei
 curl_close($ch);

require_once ('jpgraph/src/jpgraph.php');
require_once ('jpgraph/src/jpgraph_bar.php');
# Create the graph. These two calls are always required
$graph = new Graph(1000,600);
$graph->SetScale('textlin');
 
# Add a drop shadow
$graph->SetShadow();
 
# Adjust the margin a bit to make more room for titles
$graph->SetMargin(40,30,20,40);
 
# Create a bar pot
$bplot = new BarPlot($datay);
 
# Adjust fill color

$bplot->SetFillColor('orange');
$graph->Add($bplot);
 
# Setup the titles
$graph->title->Set('Anzahl UIDs nach 1.Buchstaben ');
$graph->xaxis->SetTickLabels($datax);
$graph->xaxis->title->Set('UID-Präfix');
$graph->yaxis->title->Set('');

 
$graph->title->SetFont(FF_FONT1,FS_BOLD);
$graph->yaxis->title->SetFont(FF_FONT1,FS_BOLD);
$graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD);
 
# Display the graph
$graph->Stroke();
?>

But thanks anyway, it was a big step


Reply all
Reply to author
Forward
0 new messages