[英]Ruby on Rails, JSON and Highcharts
I'm trying to get highcharts graphs to update automatically from a table.我试图让 highcharts 图表从表格中自动更新。 Currently i'm coding each record in one by one as follows:
目前我正在对每条记录进行如下编码:
<script>
...
},
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 %>]
}]
});
});
</script>
And in the model notes.rb:在模型 notes.rb 中:
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
end
Classmodules schema类模块模式
t.integer "student_id"
t.string "subject"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
Clearly this is not ideal.显然这并不理想。 What I want is for the column chart to update automatically when a new record is put in the table.
我想要的是柱形图在表中放入新记录时自动更新。 To do this I think I need to use JSON in my controller and pass it to Highcharts.
为此,我想我需要在控制器中使用 JSON 并将其传递给 Highcharts。 Only I don't know how to do this.
只是我不知道该怎么做。 Any guidance?
任何指导? Thanks.
谢谢。 If more info required I will provide.
如果需要更多信息,我会提供。
You need 3 steps to set it up:您需要 3 个步骤来设置它:
Create a route for the JSON API in your routes file like:在您的路由文件中为 JSON API 创建一个路由,例如:
get 'highchart-data', to: 'controller_name#action_name'
Create an action in a controller (match the route you just created):在控制器中创建一个动作(匹配你刚刚创建的路由):
def action_name @data = [Note.where(:subject_type => 'English').count, Note.where(:subject_type => 'Geography Class C').count, Note.where(:subject_type => 'Maths Class B').count] render json: @data end
In your js file, assuming you are using jQuery
and run on your localhost with port 3000, get the data from the route created above.在您的 js 文件中,假设您正在使用
jQuery
并在您的本地主机上使用端口 3000 运行,从上面创建的路由中获取数据。 Add this line before you create the series
:在创建
series
之前添加此行:
$.getJSON('http://localhost:3000/highchart-data', function(data) { var highChartData = data; });
And in series
replace the data
line with:并
series
替换data
线:
data: highChartData
If by 'update automatically' you mean that a chart is always up to date when the browser is refreshed, you don't need to use JSON and your implementation can be relatively simple.如果“自动更新”是指在刷新浏览器时图表始终是最新的,则您不需要使用 JSON,并且您的实现可以相对简单。
First, I would move the actual data compilation into the model itself.首先,我会将实际的数据编译移动到模型本身中。 Something like this would construct the three-element array you require:
像这样的东西会构造你需要的三元素数组:
class Note < ActiveRecord::Base
...
def self.highchart_data
data = []
self.subject_types.each do |type|
data << self.type_count(type)
end
data
end
private
def self.subject_types
pluck(:subject_type).uniq
end
def self.type_count(type)
where(subject_type: type).count
end
end
Then your controller is extremely simple:那么你的控制器非常简单:
class BarChartController < ApplicationController
def show
@data = Note.highchart_data
end
...
end
As far as getting the @data instance variable into your JS, there are several different approaches.至于将 @data 实例变量放入 JS 中,有几种不同的方法。 One of them might look like:
其中之一可能如下所示:
In your show view:在您的节目视图中:
<%= javascript_tag do %>
window.highchartDATA = '<%= @data %>';
<% end %>
In your JS:在你的 JS 中:
...
series: [{
name: 'Number of Notes By Class Module',
data: highchartDATA
}]
...
Note that this code is to illustrate concepts and not meant to be copy and pasted.请注意,此代码是为了说明概念,而不是复制和粘贴。
Based on your comment, I've updated the example code to use the unique subject_types added by users.根据您的评论,我已更新示例代码以使用用户添加的唯一 subject_types。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.