Morris Bar无法使用JSON处理动态值

时间:2018-11-13 06:00:04

标签: php jquery json ajax

我正在使用morris bar从json获取动态值

这是莫里斯js代码

$(function() { 
  var str='';
  jQuery.ajax({
  type: "GET",
  url: "today_attendance.php",
  success: function(data)
   {
    //alert(data);
    //console.log(data);
    //uploaddata(data);
    str=data;
   }
});

Morris.Bar({
    element: 'morris-bar',
    data: str,

    //data: 
    // [{x:'B1',y:14,z:0},{x:'B2',y:27,z:5},{x:'B3',y:38,z:100}, 
    // {x:'B4',y:77,z:73},{x:'B5',y:71,z:71},{x:'B6',y:0,z:57}, 
    // {x:'B7',y:0,z:89},{x:'B8',y:71,z:59},{x:'B9',y:0,z:16}, 
    // {x:'B10',y:5,z:63},{x:'B11',y:0,z:0}],
    xkey: 'x',
    ykeys: ['y', 'z'],
    labels: ['A', 'B']
});
});

这是ajax调用页面的php代码

<?php
  require_once("../../assets/db_connect.php");

  //$Today = date('Y-m-d 00:00:00');
     $Today = '2018-08-17 00:00:00';
  //Get Current Session
     $Res_Sess = mysql_query("SELECT sessionid from tbl_session where status=1 ORDER BY sessionid desc limit 1");
     $Row_Sess = mysql_fetch_array($Res_Sess); $Session = $Row_Sess[0];
     $Res_Bat = mysql_query("SELECT batchid,batchname,code FROM tbl_batch where status=1 and batchid!=12 ORDER BY batchid asc");
     $i=1; $Str = array();
     while($Row_Bat = mysql_fetch_array($Res_Bat)){
        $Batch = $Row_Bat['batchid'];  $Bat_Code = $Row_Bat['code'];
       //Get Modules
       $Res_Mod = mysql_query("SELECT distinct module_id from tbl_attendance where batchid = '$Batch' and sessionid = '$Session' LIMIT 2"); 
        $index =1; $Attendance='';
     if(mysql_num_rows($Res_Mod)>0){
        While($Row_Mod = mysql_fetch_array($Res_Mod)){
        $Mod_ID = $Row_Mod[0];
        //Get Total 
         $qatt1 = mysql_query("select count(id) from tbl_attendance where module_id = '$Mod_ID' and batchid = '$Batch' and sessionid = '$Session' and date ='$Today'");
        $qatt2 = mysql_query("select count(id) from tbl_attendance where module_id = '$Mod_ID' and batchid = '$Batch' and sessionid = '$Session' and attend = 1 and date ='$Today'");
        $qatt3 = mysql_query("select count(id) from tbl_attendance where module_id = '$Mod_ID' and batchid = '$Batch' and sessionid = '$Session' and attend = 0 and date ='$Today'");
        $qatt4 = mysql_query("select count(id) from tbl_attendance where module_id = '$Mod_ID' and batchid = '$Batch' and sessionid = '$Session' and attend = 2 and date ='$Today'");
        $Row_T = mysql_fetch_array($qatt1);
        $totalatt = $Row_T[0];
        $Row_P = mysql_fetch_array($qatt2); $tpresent = $Row_P[0];; 
        $Row_A = mysql_fetch_array($qatt3); $tabsent = $Row_A[0];; 
        $Row_L = mysql_fetch_array($qatt4); $tleave = $Row_L[0];; 
        $tattp = ($tpresent + $tleave) / $totalatt * 100;
        $Attendance[$index] = round($tattp,0);  
        $index++;
    }
} else {
            $Attendance[1] = 0; 
            $Attendance[2] = 0; 
        }   
$string = array(
x => $Bat_Code,
y => $Attendance[1],
z => $Attendance[2]
 );
 $Str[] = $string;
$i++;
  }
 echo json_encode($Str);
 ?>

问题是在ajax成功功能之后,当警报数据时,值显示如下

  

[{{x:'B1',y:14,z:0},{x:'B2',y:27,z:5},{x:'B3',y:38,z:100 },{x:'B4',y:77,z:73},{x:'B5',y:71,z:71},{x:'B6',y:0,z:57}, {x:'B7',y:0,z:89},{x:'B8',y:71,z:59},{x:'B9',y:0,z:16},{x :'B10',y:5,z:63},{x:'B11',y:0,z:0}]

如何在数据中传递morris条?当我在morris条数据中提供str变量时,morris条未显示。

1 个答案:

答案 0 :(得分:0)

像这样的正确答案

str