2017-10-06 67 views
0

我以前已經使用ChartJS(通過引用在線示例)成功繪製了一個簡單的線圖,並從MySQL數據庫中提取了所需的數據。以前只有一行在圖中,因爲我只從表中抽取了一個字段(寫入速度)。無法使用ChartJS繪製圖表

現在我已經更新它從數據庫中拉出三個字段(test_id,write_speed,read_speed),並在圖表中顯示兩個線圖。但現在它不起作用。

對於JavaScript來說,這是一個相當新穎的東西,我無法理解我出錯的地方。任何幫助將不勝感激。

這裏是代碼的數據拉從DB:

<?php 
/** 
* filename: data.php 
*/ 

//setting header to json 
header('Content-Type: application/json'); 

//database 
define('DB_HOST', 'localhost'); 
define('DB_USERNAME', 'root'); 
define('DB_PASSWORD', ''); 
define('DB_NAME', 'simpletest'); 

//get connection 
$mysqli = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME); 

if(!$mysqli){ 
    die("Connection failed: " . $mysqli->error); 
} 

//query to get data from the table 
$query = "SELECT test_id, read_speed, write_speed FROM log2 ORDER BY test_id ASC"; 

//execute query 
$result = $mysqli->query($query); 

// All good? 
if (!$result) { 
    // Nope 
    $message = 'Invalid query: ' . $link->error . "n"; 
    $message .= 'Whole query: ' . $query; 
    die($message); 
} 

//loop through the returned data 
$data = array(); 
foreach ($result as $row) { 
    $data[] = $row; 
} 


//free memory associated with result 
$result->close(); 

//close connection 
$mysqli->close(); 

//now print the data 
print json_encode($data); 

以下是HTML文件調用的腳本:

<!DOCTYPE html> 
<html> 
    <head> 
     <title>Test Tesults New</title> 
     <style> 
      .chart-container { 
       width: 640px; 
       height: auto; 
      } 
     </style> 
    </head> 
    <body> 
     <div class="chart-container"> 
      <canvas id="mycanvas"></canvas> 
     </div> 

     <!-- javascript --> 
     <script type="text/javascript" src="js/jquery.min.js"></script> 
     <script type="text/javascript" src="js/Chart.min.js"></script> 
     <script type="text/javascript" src="js/linegraph.js"></script> 
    </body> 
</html> 

及以下圖表腳本:

$(document).ready(function() { 

    /** 
    * call the data.php file to fetch the result from db table. 
    */ 
    $.ajax({ 
     url : "http://localhost/test/chartjs2/api/data.php", 
     type : "GET", 
     success : function(data) 
     { 
      console.log(data); 

      var testid = []; 
      var readspeed = []; 
      var writespeed = []; 

      for (var i in data) 
      { 
       testid.push("TestID " + data[i].test_id); 
       readspeed.push(data[i].read_speed); 
       writespeed.push(data[i].write_speed); 
      } 

      //get canvas 

      var chartdata = { 
       labels : testid, 
       datasets : [ 
        { 
         label : "Read Speed in Gbps", 
         data : readspeed, 
         backgroundColor : "blue", 
         borderColor : "lightblue", 
         fill : false, 
         lineTension : 0, 
         pointRadius : 5 
        } 

        { 
         label : "Write Speed in Mbps", 
         data : writespeed, 
         backgroundColor : "blue", 
         borderColor : "darkblue", 
         fill : false, 
         lineTension : 0, 
         pointRadius : 5 
        } 
       ] 
      }; 

      var options = { 
       title : { 
        display : true, 
        position : "top", 
        text : "New Test Results", 
        fontSize : 18, 
        fontColor : "#111" 
       }, 
       legend : { 
        display : true, 
        position : "bottom" 
       } 
      }; 

      if (chart) { 
       chart.destroy(); 
      } 

      //var ctx = document.getElementById('myChart').getContext('2d'); 
      var ctx = $("#mycanvas"); 
      var chart = new Chart(ctx, { 
       type : "line", 
       data : chartdata, 
       options : options 
      }); 

     }, 
     error : function(data) { 
      console.log(data); 
     } 
    }); 

}); 

我可能犯了一個非常愚蠢的錯誤,可能完全躲避我,所以請求如果真的做了一些愚蠢的事情,你就要指出。

謝謝。

+0

發送錯誤的JSON你可以試試在控制檯中查看(F12的開發工具,控制檯選項卡)腳本的輸出是什麼。在腳本中添加'debugger;'語句。當你打開devtools時,它會在該行暫停,你可以檢查數據以確保它是正確的。 – HMR

+0

我相信你在聲明之前破壞了圖表,可能會導致錯誤。 –

+0

更新了我的答案;它可能是語法錯誤,錯誤的chartjs版本(沒有包)或非有效的json。我建議你使用chrome開發工具來查看代碼有什麼問題。 – HMR

回答

0

你有一個語法錯誤的位置:

{ 
     label : "Write Speed in Mbps", 

應該是(添加逗號):

,{ 
     label : "Write Speed in Mbps", 

請檢查您的控制檯輸出,也許使用vscode或原子編輯您的JavaScript代碼爲編輯器也會告訴你有關語法錯誤的信息。

確保您使用chart.js(2.7.0捆綁或依賴關係)的最新版本。

我無法與固定數據重現錯誤,因此無論是在你的代碼中的語法錯誤,通過PHP或錯誤版本chart.js之的

var testid = [1,2,3,4,5]; 
 
var readspeed = [6,7,8,9,10]; 
 
var writespeed = [4,5,6,7,8]; 
 

 
var chartdata = { 
 
    labels : testid, 
 
    datasets : [ 
 
     { 
 
      label : "Read Speed in Gbps", 
 
      data : readspeed, 
 
      backgroundColor : "blue", 
 
      borderColor : "lightblue", 
 
      fill : false, 
 
      lineTension : 0, 
 
      pointRadius : 5 
 
     } 
 

 
     ,{ 
 
      label : "Write Speed in Mbps", 
 
      data : writespeed, 
 
      backgroundColor : "blue", 
 
      borderColor : "darkblue", 
 
      fill : false, 
 
      lineTension : 0, 
 
      pointRadius : 5 
 
     } 
 
    ] 
 
}; 
 

 
var options = { 
 
    title : { 
 
     display : true, 
 
     position : "top", 
 
     text : "New Test Results", 
 
     fontSize : 18, 
 
     fontColor : "#111" 
 
    }, 
 
    legend : { 
 
     display : true, 
 
     position : "bottom" 
 
    } 
 
}; 
 

 
if (chart) { 
 
    chart.destroy(); 
 
} 
 
var ctx = $('#mycanvas'); 
 
var chart = new Chart(ctx, { 
 
    type : "line", 
 
    data : chartdata, 
 
    options : options 
 
});
<canvas id="mycanvas" width="400" height="400"></canvas> 
 

 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.0/Chart.bundle.js"></script>

+1

你的建議確實有幫助。我進入了開發者工具,並發現畫布聲明與基本HTML文件中定義的不匹配。正如你所指出的那樣,有另一個愚蠢的語法錯誤:缺少逗號(這使我昏迷不醒)。修復它們,並按預期工作。謝謝。 :) –