2016-04-22 68 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。 –