2015-10-29 29 views
0

我正在使用Highcharts在我的表格中对我的数据进行图形化表示。我可以得到的数据将显示在图表上,但这样做,我发现自己不得不给每个记录一个个像这样的代码:RoR,Javascript - 如何自动更新表格中的图表?

型号:

def self.subject_analysis(subject_type) 
     Note.where(:subject_type => English).count if subject_type == :English 
     Note.where(:subject_type => Geography_Class_C).count if subject_type == :Geography_Class_C 
    Note.where(:subject_type => Maths_Class_B).count if subject_type == :Maths_Class_B 

Highcharts JS

... 
}, 
      series: [{ 
       name: 'Number of notes By Class Module', 
       data: [<%= Note.where(:subject_type => 'English').count %>, <%= Note.where(:subject_type => 'Geography Class C').count %>, <%= Note.where(:subject_type => 'Maths Class B').count %>] 
      }] 
     }); 

这有效,但显然这是远远不理想,不是我所需要的。我只希望图表在表格更改时自动从表格中更新,因为添加了新记录。

希望得到任何指导。谢谢。

回答

0

,您可以利用data attributes

在控制器定义@my_var

@my_var = Note.where(:subject_type => English).count 

上查看<div id='english' data-note='<%= @my_var %>'>

比你的JS文件

eng = $('#english').data('note') 

,并把它传递给highcharts代码

+0

谢谢!但我想知道什么时候向表中添加一个新的类模块,我可以使用该模块更新图表,而无需再执行代码?你明白我的意思吗?因此,例如,当管理员向表中添加新类时,它只会自动显示在图表上。尽管非常感谢。 – Co2

+0

哪个图表您使用@ Co2 – user2322409

+0

这一列。但也饼图和线图。混合物。但是对于这一个,它是一个柱形图。 – Co2