2016-04-22 139 views
1

嗨,我有一個圖表正在呈現,並且鍵顯示以及列標題。AMcharts來自JSON的數據填充

我的問題是我無法得到從我傳遞的值生成的實際欄。

我的php代碼很好,我在返回數據時使用了函數json_encode。下面是返回的數據傳遞到圖表代碼:

[{"gid":"NickShare","Users":1}, 
{"gid":"admin","Users":2}, 
{"gid":"sharedTest","Users":2}] 

這是我的jQuery代碼圖表:

function supportsSVG() { 
     return !!document.createElementNS && !!document.createElementNS('http://www.w3.org/2000/svg', "svg").createSVGRect; 
    } 
var token = $('#_token').val(); 
$.ajax({ 
    url: 'getShares', 
    type: 'post', 
    // dataType:'json', 
    data: { 
     _token: token, 

    }, 
    success: function (data, textStatus, jQxhr) { 

     var DBData = jQuery.parseJSON(data); 
     UserGroupsBar = new AmCharts.AmSerialChart(); 
     // var Data = [{}]; 

     UserGroupsBar.dataProvider = DBData; 
     UserGroupsBar.startDuration = 1; 
     UserGroupsBar.categoryField = "gid"; 

     $.each(DBData, function (i, item) { 
      // alert(item['Users']); 
      if (item['Users'] > 0) { 

       // Data[0][item['gid'] = item['Users']]; 
       var UserGroupGraph = new AmCharts.AmGraph(); 
       UserGroupGraph.id = item['gid']; 
       UserGroupGraph.valueField = item['Users']; 
       UserGroupGraph.title = item['gid']; 
       UserGroupGraph.type = "column"; 
       UserGroupGraph.lineAlpha = 0; 
       UserGroupGraph.fillAlphas = 1; 
       UserGroupGraph.balloonText = "[[title]]: [[value]]"; 
       UserGroupsBar.addGraph(UserGroupGraph); 
      } 
     }); 

     UserGroupsBar.write("BarChart"); 

     var GroupValueAxis = new AmCharts.ValueAxis(); 
     GroupValueAxis.axisColor = "#DADADA"; 
     GroupValueAxis.gridAlpha = 0.1; 
     GroupValueAxis.integersOnly = true; 
     UserGroupsBar.addValueAxis(GroupValueAxis); 
     UserGroupsBar.outlineColor = "#FFFFFF"; 
     UserGroupsBar.outlineAlpha = 0.2; 
     UserGroupsBar.outlineThickness = 2; 
     UserGroupsBar.startDuration = supportsSVG() ? 1 : 0; 
     UserGroupsBar.depth3D = supportsSVG() ? 20 : 0; 
     UserGroupsBar.angle = supportsSVG() ? 30 : 0; 
     //Root Causes Legend 
     GroupLegend = new AmCharts.AmLegend(); 
     UserGroupsBar.addLegend(GroupLegend, "Legend"); 
     GroupLegend.valueText = ""; 
     GroupLegend.align = "center"; 
     GroupLegend.position = "absolute"; 
     GroupLegend.marginTop = 30; 
     GroupLegend.markerType = "circle"; 
     GroupLegend.labelWidth = 180; 
     GroupLegend.fontSize = 10; 
     GroupLegend.verticalGap = 20; 
     // WRITE 
     UserGroupsBar.write("BarChart"); 
    } 


}); 

看來非常接近現在的工作,我知道我必須只是失去了一些東西很傻,但我一直盯着它看。任何指導將不勝感激。

乾杯; NE

回答

1

valueField應該指向數據的實際字段的名稱,而您似乎正在分配其值。要解決這個替換行:

UserGroupGraph.valueField = item['Users']; 

進入這個:

奏效謝謝
UserGroupGraph.valueField = 'Users'; 
+0

這就是偉大的Martynasma。 –