2016-11-05 29 views
0

我目前在制作水平条形图宽度时遇到了麻烦。目前的问题是我不知道应如何放置我的“”和“”,以便obj.sightingCount可以用作buildGraph函数的宽度。Javascript /图的jquery宽度

CSS:

div.bar { 
     display: inline-block; 
     text-align: right; 
     padding-right: 5px; 
     background-color: blue;    
     } 
    div.year { 
     display: inline-block; 
    } 

的jQuery/JavaScript的

$.ajax({ 
     url: "api/UFOSightings", 
     method: "GET", 
     timeout: 10000,    
     success: buildGraph, 
     error: myError 
    }); 


    function myError(data) { 
    } 

    function buildGraph(data) { 
     $.each(data, function (index, obj) { 
      $('#graph').append('<div><div class="year">' + obj.sightingYear + '</div><div class="bar" style= width: "obj.sightingCount%;" >' + obj.sightingCount + '</div></div>'); 
     })} 

回答

0

我认为这可能为你工作

function buildGraph(data) { 
    $.each(data, function (index, obj) { 
     $('#graph').append('<div><div class="year">' + obj.sightingYear + '</div><div class="bar" style="width:'+ obj.sightingCount + '%;" >' + obj.sightingCount + '</div></div>'); 
    }); 
} 
+0

非常感谢你:) – SpatZan