2010-04-01 112 views
0

我無法獲得highcharts插件來呈現Rails應用程序中的圖表: http://github.com/loudpixel/highcharts-rails紅寶石陣列,JavaScript和JSON問題

我相信它有事情做與SQL數據庫查詢放在紅寶石數組,這是JavaScript無法解釋。這是我:如果

def panels 
pass = Student.find_by_sql('SELECT COUNT(*) FROM students WHERE student_state = 1') 
fail = Student.find_by_sql('SELECT COUNT(*) FROM students WHERE student_state = 2') 

student_data = [ 
    {:name => 'Pass', :y => pass}, 
    {:name => 'Fail', :y => fail} 
] 
pie_label_formatter = ' 
    function() { 
    if (this.y > 15) return this.point.name; 
    }' 

pie_tooltip_formatter = ' 
    function() { 
    return "<strong>" + this.point.name + "</strong>: " + this.y + " %"; 
    }' 

@pie_chart = 
    Highchart.pie({ 
    :chart => { 
      :renderTo => "pie-chart-container", 
      :margin => [50, 30, 0, 30] 
     }, 
     :plotOptions => { 
      :pie => { 
      :dataLabels => { 
       :formatter => pie_label_formatter, 
       :style => { 
       :textShadow => '#000000 1px 1px 2px' 
       } 
      } 
      } 
     }, 
     :series => [ 
      { 
       :type => 'pie', 
       :data => student_data 
      } 
     ], 
     :subtitle => { 
      :text => 'April 2010' 
     }, 
     :title => { 
      :text => 'Student Status Chart' 
     }, 
     :tooltip => { 
      :formatter => pie_tooltip_formatter 
     }, 
    }) 

注意我把這個: :數據=> student_data.to_json 它實際上返回我的查詢在瀏覽器文本的JSON字符串。 此外,如果我硬編碼值(例如:y => 1),它會正確渲染圖表。 但是,任何數據庫查詢都不會正確渲染圖表。所以我不確定問題到底是什麼。 有什麼建議嗎?謝謝。

回答

1

你可能想使用count而不是find_by_sql

pass = Student.count(:conditions => ['student_state = ?', 1]) 
fail = Student.count(:conditions => ['student_state = ?', 2]) 

find_by_sql將返回Student對象的數組。 count將返回符合條件的行數。

+0

這樣做。謝謝。 – JohnMerlino 2010-04-02 13:21:04