Rails将JSON数据收集并呈现到Highcharts图表中 [英] Rails collecting and rendering JSON data in to a Highcharts graph
问题描述
目前,图表的工作原理是呈现数据,而x轴标签显示0,1,2,3 ... etc这是默认情况下,当没有什么可呈现的时候。
notes_controller:
def dashboard
@data = Note.getData()
end
note.rb
def self.getData
data = [ ]
self.subject_types.each do | type |
数据<< 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
dashboard.html.erb javascript
...系列:[{
name:'按类模块提供的笔记数量',
data:<%= @data%>
}] ...
上面的作品很棒,但我怎样才能获得标签显示?他们应该来自我的表中名为subject_type的列。我已经尝试了以下内容:
note.rb:
def self.getSubjects
respond_to do | format |
render:json => @ note.to_json(:only => [:subject_type])
end
( )
notes_controller.rb:
def subject
@subject = Note.getSubjects()
end
dashboard.html.erb:
...系列:[{
name:'Notes By Class模块',
数据:<%= @data%>,<%= @subject%>
}] ...
谢谢。任何帮助,我真的很感激。 > x-axis ?图表的轴
通常用于测量。一个轴可以是线性的,对数的,日期时间或类别。
根据我的理解,您希望显示每个 subject_type
作为系列名称
。
如果没有
试试这个
xAxis:{
$ p $如果
categories:'<%= raw @ subjects.to_json%>'
}
是
试试这个
Highcharts系列对象是一个数组,意味着它可以包含多个系列。
名称
属性为该系列命名。data
属性表示系列值。
它表示我们的
系列$ c
系列:[
{name:'Subject Type 1',data :[5]},
{name:'Subject Type 2',data:[6]},
{name:'Subject Type n',data:[n]},
]
您有一个
注意
subject_type
列。每个note
必须包含subject_type
。你只需要找出系列的
成为subject_type 名字
code>系列和subject_type 的总数,它们成为 系列的
。数据
你可以这样做:
def dashboard
@series_data = []
@notes = Note.select(id,subject_type,COUNT(*)AS total)。group(id,subject_type)
#收集将显示
@ notes.group_by(&:subject_type).each do | k,v |
@series_data<< {name:k.titleize,data:[v.size]}
end
end
在您的 dashboard.html.erb
系列中:<%= raw @ series_data.to_json%>
正如我上面所看到的,您还需要图表
title
。
title:{
text:< strong> Notes的数量By Class Module< / strong>
}
我希望这会有帮助。
I am trying to pass labels in to a Highcharts column chart on the x axis.
Currently the chart works in that it renders the data but the x axis labels show 0,1,2,3...etc which is the default when there is nothing to render.
notes_controller:
def dashboard @data = Note.getData() end
note.rb
def self.getData 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
dashboard.html.erb javascript
...series: [{ name: 'Number of Notes By Class Module', data: <%= @data %> }]...
The above works which is great but how do I get the labels to show? They should be coming from a column called subject_type from my table. I have tried the following:
note.rb:
def self.getSubjects respond_to do |format| render :json => @note.to_json(:only => [:subject_type]) end
(I'm probably doing this totally wrong!)
notes_controller.rb:
def subject @subject = Note.getSubjects() end
dashboard.html.erb:
...series: [{ name: 'Number of Notes By Class Module', data: <%= @data %>, <%= @subject %> }]...
Thanks. Any help here i'd really appreciate.
解决方案Why you want to print
subject_type
inx-axis
? Theaxes
of charts are generally used for measurement. An axis can be either, linear, logarithmic, datetime or categories.As per my understanding, you want to show each
subject_type
as aseries name
.If
No
try thisxAxis: { categories: '<%=raw @subjects.to_json %>' }
If
Yes
try thisHighcharts series object is an array, meaning it can contain several series. The
name
attribute gives the series a name. Thedata
attribute represent the series values.It means our
series
must be something like this.series: [ {name: 'Subject Type 1', data: [5] }, {name: 'Subject Type 2', data: [6] }, {name: 'Subject Type n', data: [n] }, ]
You have a
Note
table which hassubject_type
column. Eachnote
must contain asubject_type
. You just need to find out the uniqsubject_type
that become thename
of theseries
and total count of thatsubject_type
that become thedata
of theseries
.You can get it by doing:
def dashboard @series_data = [] @notes = Note.select("id, subject_type, COUNT(*) AS total").group("id, subject_type") # Collect series data that will be show @notes.group_by(&:subject_type).each do |k, v| @series_data << { name: k.titleize, data: [v.size] } end end
In your dashboard.html.erb
series: <%=raw @series_data.to_json %>
As I can see above, you also need chart
title
. You can do it in this way.title: { text: "<strong>Number of Notes By Class Module</strong>" }
I hope this will be helpful.
这篇关于Rails将JSON数据收集并呈现到Highcharts图表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!