问题
I have been trying to build a graph using open Flash Chart 2 in Drupal (Well, the problem isn't related to Drupal or the graphs and chart module)
For the graph I am fetching the data from a MySQL database. Below is the function which gets the data and generates the graph:
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:
So, it works perfectly with a locally declared array. But I need to use the array from SQL. Hence I uncomment and change $canvas->c_labels
and $canvas->series
:
$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:
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:
$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...
$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.!
Thus I tried everything I can think of with no luck.
回答1:
wow.. Finally I found the answer, thanks to drupal forums..:)
As it turns out its a type issue..! i.e, if I use the following code, it works..!!
<?php
$data[] = (int) $row[total_calorie];
?>
I have no idea why is it a type issue. or the reason why this works, but it does solve my problem...! Thanks to everybody for helping me solve it..:)
回答2:
Try insert
$data = array();
Before the while($row = db_fetch_array($result))
And use just
'Page Views' => $data
来源:https://stackoverflow.com/questions/9180502/why-does-my-sql-data-cause-my-charts-y-axis-to-jump-to-infinity