2016-11-05 25 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