为什么我的 SQL 数据会导致图表的 Y 轴跳到无穷大?
我一直在尝试使用 Drupal 中的开放式 Flash Chart 2 构建图表(嗯,问题与 Drupal 或图表和图表模块无关)
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)
对于图表,我从 MySQL 数据库中获取数据.下面是获取数据并生成图表的函数:
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:
因此,它与本地声明的数组完美配合.但我需要使用 SQL 中的数组.因此,我取消注释并更改 $canvas->c_labels
和 $canvas->series
:
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:
正如我们所见,x_axis 值与查询一致,但 y_axis 表示与无穷大相同的值.为什么要这样做?
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?
这个奇怪的问题还有另外一面.最初我认为这种方法可能不适用于 $canvas->series
,但我尝试了以下代码并且效果很好:
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),
);
所以这个无穷大"问题只出现在从 SQL 查询中获取的数组中,并且只出现在 Y 轴上.
So this "infinity" problem only appears for arrays fetched from SQL queries and only for the Y axis.
print_r($data)
我的SQL如下:
Array ( [0] => 0 [1] => 0 [2] => 117 [3] => 207 [4] => 130 [5] => 260 [6] => 207 )
以下是我为 $canvas->series 尝试过的其他一些组合,但失败了...
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.
推荐答案
哇.. 终于我找到了答案,感谢 drupal 论坛..:)
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..:)
相关文章