如果不刷新页面,图表将不会返回动态数据

时间:2019-06-15 15:04:51

标签: javascript php ajax

我有一个用于在足球比赛期间记录统计信息的应用程序。在我的应用程序中,我有一个模态,它将以条形图的形式显示从游戏到用户的统计数据的细分。但是,由于统计信息被记录且统计信息固定为零,因此该图不会更新,我希望该图形随着统计信息被记录而更新。

这是JavaScript代码

  <script type="text/javascript">
$(document).ready(function(){
  $.ajax({
    url: "statsgraph.php",
    type: "GET",
    success: function(data) {
      console.log(data);
      var dataJS=data;
      var goal = [];
      var point = [];
      var wide = [];
      var kickout = [];
      var turnover = [];
      var foul = [];
      var fixture=[];

      for(var i in dataJS) {
        fixture.push(data[i].fixture);
        goal.push(data[i].goalCountHome);
        point.push(data[i].pointCountHome);
        wide.push(data[i].wideCountHome);
        kickout.push(data[i].kickoutWonCountHome);
        turnover.push(data[i].turnoverCountHome);
        foul.push(data[i].foulCountHome);
      }
      var chartdata = {
        labels: ['Goals','points','Wides', 'Kickouts won', 'Turnovers won', 'Fouls conceded'],
        datasets : [
          {
            label: 'Home team',
            backgroundColor: 'rgba(200, 200, 200, 0.75)',
            borderColor: 'rgba(200, 200, 200, 0.75)',
            hoverBackgroundColor: 'rgba(200, 200, 200, 1)',
            hoverBorderColor: 'rgba(200, 200, 200, 1)',
            data: [goal,point,wide,kickout,turnover,foul]
          },

        ]
      };
      var ctx = $("#myCanvas");

      var barGraph = new Chart(ctx, {
        type: 'bar',
        data: chartdata
      });
    },
    error: function(data) {
      console.log(data);
    }
  });
});
</script>

这是statsgraph页面上的代码:

    <?php

session_start();

if(!isset($_SESSION['STATtrackStatMan'])){
    header('Location:index.php');
}

include ("conn.php");
$fixture_id= $_SESSION['fixture_id'];
$query="SELECT fixture,(SELECT COUNT(*) FROM fixtures WHERE goal='1'AND fixture='$fixture_id') AS goalCountHome,
(SELECT COUNT(*) FROM fixtures WHERE point='1'AND fixture='$fixture_id') AS pointCountHome,
(SELECT COUNT(*) FROM fixtures WHERE wide='1'AND fixture='$fixture_id') AS wideCountHome,
(SELECT COUNT(*) FROM fixtures WHERE kickout='1'AND fixture='$fixture_id') AS kickoutWonCountHome,
(SELECT COUNT(*) FROM fixtures WHERE turnover='1'AND fixture='$fixture_id') AS turnoverCountHome,
(SELECT COUNT(*) FROM fixtures WHERE fouls='1'AND fixture='$fixture_id') AS foulCountHome
FROM fixtures limit 1";

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

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

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

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

//now print the data
header('Content-type:application/json');
echo json_encode($data,JSON_PRETTY_PRINT);
?>

0 个答案:

没有答案