2014-11-24 60 views
-1
$http.post(galileoServer + "actions.php", { 
     "action": "get-attendance-graph", 
     "user": window.localStorage.getItem("username") 
    }).success(function(result){ 
     //console.log(result) 
     busyIndicator("hide"); 
     $('#attendance-graph').highcharts({ 
      credits: 0, 
      tooltip:{ 
       enabled: false 
      }, 
      chart: { 
       type: 'bar' 
      }, 
      title: { 
       text: '', 
       style: { 
        display: "none" 
       } 
      }, 
      xAxis: { 
       categories: ['June', 'July', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec', 'Jan', 'Feb', 'Mar', 'Apr', 'May'] 
      }, 
      yAxis: { 
       min: 0, 
       title: { 
        text: 'No. of days' 
       }, 
       gridLineWidth: 0, 
       minorGridLineWidth: 0, 
       labels: { //to disable points displayed 
        enabled: false 
       } 
      }, 
      legend: { 
       reversed: true 
      }, 
      plotOptions: { 
       series: { 
        stacking: 'normal', 
        dataLabels: { 
         enabled: true, 
         color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white', 
         formatter: function() { //function to avoid displaying zero values 
          if (this.y != 0) { 
           return this.y; 
          } else { 
           return null; 
          } 
         } 
         /*style: { 
         textShadow: '0 0 3px black, 0 0 3px black' 
         }*/ 
        } 
       } 
      }, 
      series: [{ 
       name: 'Absent', 
       data: [result.absentData] 
      }, { 
       name: 'Present', 
       data: [result.presentData] 
      }] 

     }); 



    }).error(function(result, status){ 
     alert(status + "\nCouldn't connect to Galileo server due to network problem") 
    }); 

我想通过ajax加载数据,但没有得到加载图加载的图是空白的。 提供了编码片段。 我也尝试了getJSON部分,但它也没有奏效。 请让我知道解决方案,因为我无法从最近两天获取图表。

控制台输出{"absentData":"0,0,2,0,0,1,0,0,0,0,0,0","presentData":"30,31,29,30,31,29,31,31,28,31,30,31"}

+0

空白图形表示您的数据格式不正确。首先,检查你的控制台是否有错误。其次,用'console.log(result)'的输出更新你的问题。在你这样做之前,我们无法帮助你。 – Mark 2014-11-24 16:03:25

+0

控制台显示完美结果。我将附上输出 – 2014-11-24 17:03:43

+0

@Mark 控制台输出为http://prntscr.com/59p18w – 2014-11-24 17:29:40

回答

1

您的JSON不能正常形成Highcharts。你想要的数字阵列,你给它一个元素的字符串数组:

data: ["0,0,2,0,0,1,0,0,0,0,0,0"] // an array of a single string... 

这是更好,你在你的PHP代码解决这个问题。您需要构建一个ints的php数组(不要构建连接的字符串),然后使用json_encode

如果你不能在PHP修复它,你可以这样做:

data: $.parseJSON("["+result.absentData+"]") 

但这是有点丑陋。

+0

它的工作!找到了一个包含你的代码片段的工作。 – 2014-11-25 07:13:04