Hi

I have been trying to build a graph using "Charts and graphs " module with open Flash Chart 2 in Drupal 6.

For the graph I am fetching the data from a MySQL database. Below is the function which gets the data and generates the graph:

<?php
function my_module_charts_graphs_test() {

 global $user;
$uname = $user->name;
$sql = "Select total_calorie from health_calorie_consumed where name = '%s'";
$result = db_query($sql,$uname);


  while($row = db_fetch_array($result))
{

    $data[] = $row[total_calorie];

}

$canvas = charts_graphs_get_graph('open-flash');

  $canvas->title = "OpenFlashCharts Chart";
  $canvas->type = "bar_3d";
  $canvas->y_legend = "Y Legend";
  $canvas->colour = '#808000';
  $canvas->width = 700;
  $canvas->height = 300;
  $canvas->y_max=1000;
  $canvas->y_min=0;
  $canvas->y_step=100;
  $canvas->series = array(
    'Some Value' => array(923,623,73,92,5,722,643,156,345),
    //'Page Views' => array_values($data),
  );
  $canvas->x_labels = array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine');

  // $canvas->x_labels = array_values($data);

  $out = $canvas->get_chart();

  return $out;


} 
?>

When I run the code, I get the below graph:

http://oi43.tinypic.com/2hcfgqp.jpg

(Sorry i coudn't insert the image, because.."Only local images are allowed" )

So, it works perfectly with a locally declared array. But I need to use the array from SQL. Hence I uncomment and change $canvas->x_labels and $canvas->series:

<?php
$canvas->x_labels = array_values($data); //$data is array from sql query

'Page Views' => array_values($data)
?>

Now, to my surprise, I get the below graph:

http://oi41.tinypic.com/spb42g.jpg

As we can see, the x_axis values are right as per the query but y_axis says the same values as infinity. Why does it do this?

There is another face to this strange problem. Initially I thought that this approach may not work with $canvas->series, but I tried the below code and it worked perfectly:

<?php
$array = array(0,0,117,207,130,260,207); //these values are that are fetched from sql
$canvas->series = array(
   'some values'=>array_values($array),
   );
?>

So this "infinity" problem only appears for arrays fetched from SQL queries and only for the Y axis.

print_r($data) of my SQL is as follows:

Array ( [0] => 0 [1] => 0 [2] => 117 [3] => 207 [4] => 130 [5] => 260 [6] => 207 )

Below are some other combinations I tried for $canvas->series and failed...

<?php

$test = implode(",",$data); // $data is the array fetched from sql
$abcd = "array(".$test.")"; // array(0,0,117,207,130,260,207)

$canvas->series=>array(
      'some value'=>print($abcd), //i tried to print those values in standard format..:p
      );

?>

'some values'=>$abcd, // doesn't work..!

'some values'=>$data, // doesn't work.!

Tried inserting
$data = array();
before

while($row = db_fetch_array($result))

And use just

'Page Views' => $data

So i have no other method to think of to solve it. So please help me..:)
Thanks in advance.

Comments

jaypan’s picture

I've never used the API in question, but I read through it, and I can follow your logic and debugging, and it all looks good to me. Definitely confusing.

Maybe change this:

'Page Views' => array_values($data)

To this:

'Page Views' => $data

It really shouldn't matter, as you have added the array elements by pushing them onto the end of the stack, which means that both array_values($data) and $data should be identical, but conversely, because they should be identical, it means you don't need to be doing it. Something to try at least.

Contact me to contract me for D7 -> D10/11 migrations.

akshaynhegde’s picture

Thanks for the reply... But I already tried that n it didn't work...!

jaypan’s picture

Try these as well:

$data[] = (int) $row[total_calorie];
$data[] = (string) $row[total_calorie];

I'm wondering if it's not a type issue. It really shouldn't be the second one, and if it is, it's sloppy on the part of the API, but the first one is a possibility.

Contact me to contract me for D7 -> D10/11 migrations.

akshaynhegde’s picture

WOW...... Thank you so much man...:) That worked..!!!

<?php
$data[] = (int) $row[total_calorie];
?>

oh god.. I can finally breath now...!! I was so frustrated..:)

Anyways, why was the type causing an issue...?? Why does it work for x_axis but not for y_axis..??

jaypan’s picture

The x-axis, is actually a series of labels for the various vertical bars. As this is a label/title, it's expected to be a string. Generally, when you try to use an integer as a string, it works, because integers are included within strings, so there aren't as strong checks. That's why it was working.

However, when a script is checking for integers, it will often use functions like is_int() to check if the number is an integer. The numbers you were pulling from the database to use as the y-axis values were coming out as strings, even though they were numbers. So you were passing a string to the graphing script, when it wanted an integer. Even though 5 and "5" look the same to us, to a computer the first is an integer, the second is a string, and they are not equal, or at least, not equal when you are doing strict checking of type and value.

So by converting the values to an integer right away when pulling them from the mysql result, it solved the problem apparently.

Contact me to contract me for D7 -> D10/11 migrations.