2010-04-01 81 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