2015-04-24 39 views
0

你好,我有以下任務:繪圖陣列


  • 檢索數據庫中的數據,並保存到陣列(CHECK)
  • 從控制器發送陣列查看(CHECK )
  • 使用json_encode(CHECK)
  • 繪圖陣列使用引導圖表(問題傳遞數組javascript函數!)

如果我使用Bootstrap圖表(下面的var d)附帶的標準數組,它將起作用。但是,當我嘗試使用我的代碼時,什麼都沒有發生我在哪裏失敗?

var d = [[-373597200000, 315.71], [-370918800000, 317.45], [-368326800000, 317.50], [-363056400000, 315.86]] 

我改用下面的代碼行:

var d = JSON.parse('<?php echo json_encode($my_values) ?>'); 

這裏是和例子是什麼我的數組樣子,如果我打印出來的空圖的中間:http://postimg.org/image/3t3paj3vn/

這裏是我的全碼:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
<html> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
    <title>Flot Examples: Time Axes</title> 
    <link href="../examples.css" rel="stylesheet" type="text/css"> 
    <!--[if lte IE 8]><script language="javascript" type="text/javascript" src="../../excanvas.min.js"></script><![endif]--> 
    <script language="javascript" type="text/javascript" src="../../jquery.js"></script> 
    <script language="javascript" type="text/javascript" src="../../jquery.flot.js"></script> 
    <script language="javascript" type="text/javascript" src="../../jquery.flot.time.js"></script> 

</head> 
<body> 
      <select id="chapter_select"> 
       <option value="0"> Select Patient</option> 
       <?php foreach($patients as $patient):?> 
         <option value= <?php echo $patient->id?> > <?php echo $patient->name ?>       </option> 
       <?php endforeach?> 
      </select><br><br> 
       <?php $i= 0;?> 
       <?php foreach($datas as $row):?> 
        <?php $my_values[$i] = (array($row['data'],$row['total']));?> 
        <?php $i = $i+1;?> 
       <?php endforeach;?> 

    <div id="content"> 

     <div class="demo-container"> 
      <div id="placeholder" class="demo-placeholder"></div> 
     </div> 

     <p>Monthly mean atmospheric CO<sub>2</sub> in PPM at Mauna Loa, Hawaii (source: <a href="http://www.esrl.noaa.gov/gmd/ccgg/trends/">NOAA/ESRL</a>).</p> 

     <p>If you tell Flot that an axis represents time, the data will be interpreted as timestamps and the ticks adjusted and formatted accordingly.</p> 

     <p>Zoom to: <button id="whole">Whole period</button> 
     <button id="nineties">1990-2000</button> 
     <button id="latenineties">1996-2000</button></p> 

     <p>Zoom to: <button id="ninetyninequarters">1999 by quarter</button> 
     <button id="ninetynine">1999 by month</button> 
     <button id="lastweekninetynine">Last week of 1999</button> 
     <button id="lastdayninetynine">Dec. 31, 1999</button></p> 

     <p>The timestamps must be specified as Javascript timestamps, as milliseconds since January 1, 1970 00:00. This is like Unix timestamps, but in milliseconds instead of seconds (remember to multiply with 1000!).</p> 

     <p>As an extra caveat, the timestamps are interpreted according to UTC and, by default, displayed as such. You can set the axis "timezone" option to "browser" to display the timestamps in the user's timezone, or, if you use timezoneJS, you can specify a time zone.</p> 

    </div> 

    <div id="footer"> 
     Copyright &copy; 2007 - 2014 IOLA and Ole Laursen 
    </div> 


    <script type="text/javascript"> 

    $(function() { 

    var d = JSON.parse('<?php echo json_encode($my_values) ?>'); 

     $("#latenineties").click(function() { 
      $.plot("#placeholder", [d], { 
       xaxis: { 
        mode: "time", 
        minTickSize: [1, "year"], 
        min: (new Date(1996, 0, 1)).getTime(), 
        max: (new Date(2000, 0, 1)).getTime() 
       } 
      }); 
     }); 

     $("#ninetyninequarters").click(function() { 
      $.plot("#placeholder", [d], { 
       xaxis: { 
        mode: "time", 
        minTickSize: [1, "quarter"], 
        min: (new Date(1999, 0, 1)).getTime(), 
        max: (new Date(2000, 0, 1)).getTime() 
       } 
      }); 
     }); 

     $("#ninetynine").click(function() { 
      $.plot("#placeholder", [d], { 
       xaxis: { 
        mode: "time", 
        minTickSize: [1, "month"], 
        min: (new Date(1999, 0, 1)).getTime(), 
        max: (new Date(2000, 0, 1)).getTime() 
       } 
      }); 
     }); 

     $("#lastweekninetynine").click(function() { 
      $.plot("#placeholder", [d], { 
       xaxis: { 
        mode: "time", 
        minTickSize: [1, "day"], 
        min: (new Date(1999, 11, 25)).getTime(), 
        max: (new Date(2000, 0, 1)).getTime(), 
        timeformat: "%a" 
       } 
      }); 
     }); 

     $("#lastdayninetynine").click(function() { 
      $.plot("#placeholder", [d], { 
       xaxis: { 
        mode: "time", 
        minTickSize: [1, "hour"], 
        min: (new Date(1999, 11, 31)).getTime(), 
        max: (new Date(2000, 0, 1)).getTime(), 
        twelveHourClock: true 
       } 
      }); 
     }); 

     // Add the Flot version string to the footer 

     $("#footer").prepend("Flot " + $.plot.version + " &ndash; "); 
    }); 

    </script> 

</body> 
</html> 
+0

你不需要的JSON。解析在所有。 'var d = <?php echo json_encode(...); ?>;'是所有必要的。請記住JSON ** IS ** javascript。一旦php吐出編碼的json到頁面中,瀏覽器將只會看到'var d = [這裏有效的javascript數據]' –

+0

我用json.parse,因爲json_enconde沒有工作......效果是一樣的。但仍然沒有圖。 – DMBorges

回答

1

問題是引導程序代碼無法識別我的日期時間。所以,我將它轉換爲UNIX時間戳。這裏是我的查詢

$query = $this->db->query(" \t SELECT exam.id AS exam, UNIX_TIMESTAMP(exam.date) AS data, SUM(choice.value) AS total 
 
\t \t \t \t \t \t \t \t \t FROM exam INNER JOIN choice ON exam.id = choice.exam_id 
 
\t \t \t \t \t \t \t \t \t WHERE exam.patient_id = $patient_id AND exam.id = choice.exam_id GROUP BY exam.date"); 
 
\t \t $data = array(); 
 
\t \t foreach ($query->result() as $row) 
 
\t \t { 
 
\t \t \t $data[] = array(
 
     'exam' => $row->exam, 
 
\t \t 'data' => $row->data, 
 
     'total' => $row->total 
 
\t \t); 
 
} 
 
\t \t return $data;

,然後用於FARAN阿里建議
變種d =

\t 
 
var d = '<?php json_encode($my_values); ?>

+0

忘記回聲 var d ='<?php json_encode($ my_values); ?> –

0

試試這個:

var d = JSON.parse('<?php echo($my_values); ?>'); 
+0

爲什麼OP要「試試這個」?請添加一個解釋,你做了什麼,爲什麼你這樣做,不僅是爲了OP,而且是爲了未來的訪問者。 –

+0

同樣的結果Faran ...能夠打印值,但沒有顯示在圖中。 – DMBorges

+0

var d ='<?php json_encode($ my_values); ?>';將工作,JS圖形與JSON工作。 JSON.PARSE轉換成JSON數組,這就是爲什麼它不工作@DMBorges –