如何将时间序列数据数组交给Float javascript?

时间:2011-12-27 18:43:48

标签: php javascript arrays json flot

我有php-page检索sqlight-data。 下半部分是我为Flot图绘制(unix epoch时间戳作为数组中的时间序列数据)准备的重要部分。 我究竟做错了什么?请指教。

  

在php部分中回显json_encode($ dataset1)

给出:

  

[1320710855000,44] [1320711755000,46] [1320712655000,44] [1320713555000,42] [1320714456000,56] ..   和Chromes Resources展示:

script type =“text / javascript”> var kurve = [1320774758000,72];

<?php
 error_reporting(E_ALL);
 try {
    /*** DB erstellen ***/
    $dbh = new PDO('sqlite:/var/www/zaehler/inc/db/zaehler.sqlite');
    /*** set all errors to excptions ***/
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $startdate=startdate();
    $sql = "SELECT date FROM energielog ORDER BY OID DESC LIMIT 1";
    $result = $dbh->query($sql);
    foreach($result as $row) {
          if ($row['date']!=$startdate) {
              $startdate=$row['date'];
                  }
          }
    $sql = "SELECT kwh, peak, OID, date, time FROM energielog WHERE date >=".$startdate." ORDER BY OID ASC";
    $result = $dbh->query($sql);

    foreach($result as $row) {                
            $oid=$row['OID']-1;
            $sql2 = "SELECT kwh FROM energielog WHERE oid <".$oid." ORDER BY OID DESC LIMIT 1";
            $result2 = $dbh->query($sql2);
            foreach($result2 as $row2) {
                    $letzter_eintrag=str_replace(",",".",$row2['kwh']);
                    }
                    $aktueller_eintrag=str_replace(",",".",$row['kwh']);
                    $eintrag=round(($aktueller_eintrag-$letzter_eintrag)*1000/0.5,0);
                    $peak=$row['peak']*1000;                       
                    $time=str_pad($row['time'],6,'0',STR_PAD_LEFT);
                    $times=substr($time,0,2).":".substr($time,2,2);                       
                    $timeStamp=mktime(
                    substr($time,0,2),
                    substr($time,2,2),
                    substr($time,4,2),
                    substr($startdate,2,2),
                    substr($startdate,4,2),
                    substr($startdate,0,2)
                    );                        
                   $dataset1 = array(($timeStamp)*1000, $eintrag);                       
                   echo json_encode($dataset1);
            }
}
catch(Exception $e)
{
    echo $e->getMessage();
    die();
}   
?>  
<div id="placeholder" style="width:600px;height:300px;"></div>
<script type="text/javascript">

var kurve = <?php echo json_encode($dataset1); ?>;

$(function () {    
$.plot($("#placeholder"), [kurve], { xaxis: { mode: "time",  timeformat: "%y/%m/%d/%H:%M"} });    
});
</script>

1 个答案:

答案 0 :(得分:1)

看着这个真实的快速,我突然想到了一些事情。

一:

var kurve = "<?php echo json_encode($dataset1); ?>";

您不希望PHP周围有"。这将导致kurve var成为字符串而不是数组数组。

二:

[1320710855000,44][1320711755000,46][1320712655000,44][1320713555000,42][1320714456000,56]

您或者回声是否在每个阵列点之间删除了,?这不是有效的JS语法。

<强> EDITS

我现在看到了。在每个循环上重新声明dataset1。你想要:

$dataset1 = array();
foreach($result as $row) {
  <do your processing..?
  array_push($dataset1, array(($timeStamp)*1000, $eintrag));
}