为什么我的SQL数据导致我的图表的Y轴跳到无穷远? [英] Why does my SQL data cause my chart's Y axis to jump to infinity?

查看:104
本文介绍了为什么我的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:

preview-1 http: //oi43.tinypic.com/2hcfgqp.jpg

所以,它与本地声明的数组完美匹配。但是我需要使用SQL中的数组。因此,我取消注释和更改 $ canvas-> c_labels $ canvas->系列

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:

preview-2 http://oi41.tinypic.com/ spb42g.jpg

我们可以看到,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->系列,但我尝试了以下代码,并且工作正常:

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.

我的SQL的print_r($ data)如下:

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

以下是一些其他组合我试过$ canvas->系列,失败了...

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..:)

这篇关于为什么我的SQL数据导致我的图表的Y轴跳到无穷远?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆