douhuo1738 2015-09-25 07:48
浏览 103

从数据库中获取并使用flot绘制实时图表

I'm fetching data from database using php and storing data into an array and then storing that array into JavaScript array and then display the chart using flot. and My goal is that in graphic move to the left as new data comes in.

x.html

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<?php 
 $i=0;
while($row = oci_fetch_array($stid,OCI_ASSOC)){

  // add each row returned into an array

  $arr[] = array($i++, (float)$row['DATA']);


}
echo json_encode($arr);
?>

Fetching from database I'm getting following output=>

[[0,15.739993],[1,13.698263],[2,13.214383],[3,15.393282],[4,14.356073],[5,13.364647],...........]

And

<script type="text/javascript" src="jquery-1.11.3.min.js"></script>
<script type="text/javascript" src="jquery.flot.js"></script>
<script type="text/javascript" src="jquery.flot.axislabels.js"></script>

<script type="text/javascript">

var data=[];
function f(){
data= <?php echo json_encode($arr) ?>;
}

var options={
                series: {
                        lines: {
                                show: true,
                                lineWidth: 2,
                                //  fill: true
                                },
                        points:{
                                show: true
                                }
                        },
               legend: {        
                            labelBoxBorderColor: "#B0D5FF"
                        },
                    grid: {
                            hoverable: true, 
                            clickable: true,
                            backgroundColor: { 
                                                colors: ["#B0D5FF", "#5CA8FF"] 
                                            }
                            }
};
$(document).ready(function () {
f();
var dataset=[
              { 
                label: "Data", 
                data: data, 
                points: { 
                            symbol: true
                        } 
             }
            ];

     $.plot($("#flot-container"), dataset , options);
function update() {
f();
if(data.length>10){
data.shift();
   }

$.plot($("#flot-container"), dataset, options);
setTimeout(update, 5000);
      }
      update();
});

</script>

</head>

<body>
<div id="flot-container" style="width:450px;height:300px;margin:0 auto"></div>

</body>
</html>

I'm getting following output.enter image description here

Now when I'm inserting new data into database , first data is not shifting/moving. New data comes and adding to the chart.
enter image description here

I want to update the graph in realtime.like this example http://jsfiddle.net/UMt7d/

How can i fix it? Please Help

UPDATED PART

<?php
include("mydb.php");
// run query
$sql = "select DATA from xet where to_char(workdate,'dd/mm')='25/02'";
$stid=oci_parse($conn, $sql);
// set array
$arr = array(0,0);
if(!$stid){
$e=oci_error($conn);
trigger_error(htmlentities($e[message],ENT_QUOTES),E_USER_ERROR);
}

$r=oci_execute($stid);

if(!$r){
$e=oci_error($stid);
trigger_error(htmlentities($e[message],ENT_QUOTES),E_USER_ERROR);
}


// look through query
while($row = oci_fetch_array($stid,OCI_ASSOC)){

  // add each row returned into an array
  $arr=array_slice($arr,1,9);
  $arr[] = array((strtotime($row['WD'])*1000) , (float)$row['DATA']);
 echo json_encode($arr);
 echo "</br>";
}
?> 

getting output like as follows=>

[0,[0,15.739993]]
[[0,15.739993],[1,13.698263]]
[[1,13.698263],[2,13.214383]]
[[2,13.214383],[3,15.393282]]
[[3,15.393282],[4,14.356073]]
[[4,14.356073],[5,13.364647]]
[[5,13.364647],[6,15.040561]]
[[6,15.040561],[7,12.138517]]
...........................
[[16,13.734816],[17,15.6194315]]

getting following chart enter image description here

only taking last value .

  • 写回答

1条回答 默认 最新

  • dongshi8038 2015-09-25 08:57
    关注

    Change your php script so that it outputs a fixed amount of data points. This leads to data points on the left "moving" out of the graph.

    You can use array_slice() to reduce an array which is too big (and array_fill() and array_merge() to fill an array which is too small if you want to start with a partial filled array).

    For your new php code, try something like this:

    while($row = oci_fetch_array($stid,OCI_ASSOC)){
        // add each row returned into an array
        $arr[] = array((strtotime($row['WD'])*1000) , (float)$row['DATA']);
    }
    $arr = array_slice($arr, count($arr) - 10, 9);
    echo json_encode($arr);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘