weixin_33736649 2013-03-24 06:06 采纳率: 0%
浏览 46

将数据插入Flot

I am having a hard time displaying an array of data on a Flot graph. I'm using jQuery Ajax/PHP/MySQL to do this.

I created by array with this PHP/MySQL:

  $result = mysql_query("SELECT * FROM happiness");
  $array = array();
  while($row = mysql_fetch_array($result)) {
    $array[] = $row[3];
    $array[] = $row[2];
  }

  echo json_encode($array);

And pass it through jQuery Ajax to Flot like this:

$.ajax({                                      
      url: 'receive-happiness.php',   
      dataType: 'json',                      
      success: function(data)          
      {
            var graph_data = [data];              
            alert(graph_data);
            $.plot($("#graph"), [graph_data], options);
      }
    });

When I alert graph_data, I get this:

23,8,23,1,24,0,25,0,26,9,27,10,28,9

But when I check my Flot graph, it only displays a single data point at (23,8). What is going wrong?

  • 写回答

1条回答 默认 最新

  • 程序go 2013-03-24 06:11
    关注

    Your graph data is supposed to be an array of grouped point coordinates.

    Try this:

    $result = mysql_query("SELECT * FROM happiness");
    $array = array();
    while($row = mysql_fetch_array($result)) {
      $array[] = array($row[3], $row[2]);
    }
    
    echo json_encode($array);
    

    When you alert your graph data, it should look something like this:

    [ [23, 8], [23, 1], [24, 0], [25, 0], [26, 9], [27, 10], [28, 9] ]
    

    See Flot Data format documentation.

    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序