Rails将JSON数据收集并呈现到Highcharts图表中 [英] Rails collecting and rendering JSON data in to a Highcharts graph

查看:98
本文介绍了Rails将JSON数据收集并呈现到Highcharts图表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



目前,图表的工作原理是呈现数据,而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:{
categories:'<%= raw @ subjects.to_json%>'
}
是试试这个



Highcharts系列对象是一个数组,意味着它可以包含多个系列。 名称属性为该系列命名。 data 属性表示系列值。



它表示我们的系列

 系列:[
{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 in x-axis ? The axes 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 a series name.

If No try this

xAxis: {
    categories: '<%=raw @subjects.to_json %>'
}

If Yes try this

Highcharts series object is an array, meaning it can contain several series. The name attribute gives the series a name. The data 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 has subject_type column. Each note must contain a subject_type. You just need to find out the uniq subject_type that become the name of the series and total count of that subject_type that become the data of the series.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆