在下面的代码中,我正在尝试绘制一堆图表。我的数据库中的模型 Person 具有“名称、体重、身高、颜色、年龄”的属性所以我的目标是为每一行绘制图表,以体重为 x 轴,高度为 y 轴。颜色将是每个图表的实际颜色,例如第 1 个人的颜色为黄色,那么图表应该是黄色(这很难实现)
无论如何,我正在使用lazy_high_chart 来实现这一点,但是没有运气,我失败了,没有错误。
但为了保持一致,这是我的代码:people_controller.rb
class PeopleController < ApplicationController
#GET /people/index
#GET /people
def index
@people = Person.all
end
#GET /people/show/id
def show
@person = Person.find(params[:id])
end
#GET /people/new
def new
@person = Person.new
end
#POST /people/update
def create
@person = Person.new(person_params)
@person.save
redirect_to :action => :index
end
#GET /people/edit/:id
def edit
@person = Person.find(params[:id])
end
#POST /people/update/:id
def update
@person = Person.find(params[:id])
@person.update(person_params)
redirect_to :action => :show, :id => @person
end
#GET /people/destroy/:id
def destroy
@person = Person.find(params[:id])
@person.destroy
redirect_to :action => :index
end
def display
@people = Person.all
names = []
weights = []
heights = []
colors = []
ages = []
@people.each do |x|
names = x.name
weights = x.weight
heights = x.height
colors = x.color
ages = x.age
end
@chart = LazyHighCharts::HighChart.new('graph') do |x|
x.title(:text => "Display Data")
x.xAxis(:categories => weights, :title => names, :margin => 10)
x.yAxis(:categories => heights)
x.series(:type => 'column', :name => 'showing data', :data => weights, :color => colors)
end
end
private
def person_params
params.require(:person).permit(:name, :weight, :height, :color, :age)
end
end
路线.rb
Rails.application.routes.draw do
root 'people#index'
match ':controller(/:action(/:id(.:format)))', :via => :all
end
index.html.erb:
<h1> People list</h1>
<table>
<thead>
<tr>
<th>Name</th>
<th> Weight</th>
<th> Height</th>
<th> Color</th>
<th> Age</th>
<th colspan="4"></th>
</tr>
</thead>
<tbody>
<% @people.each do |e| %>
<tr>
<td><%= e.name %></td>
<td><%= e.weight %></td>
<td><%= e.height %></td>
<td><%= e.color %></td>
<td><%= e.age %></td>
<td><%= link_to 'Show', :controller => "people", :action => "show", :id => e %></td>
<td><%= link_to 'Edit', :controller => "people", :action => "edit", :id => e %></td>
<td><%= link_to 'Delete', :controller => "people", :action => "destroy", :id => e %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Input', :controller => 'people', :action => 'new' %>
<%= link_to 'Display', :controller => "people", :action => "display" %>
display.html.erb:
<h1> Display Result</h1>
<%= high_chart("display_res", @chart) %>
我相信我的路线是正确的,它应该处理控制器代码块中的显示操作。我已经阅读了lazy_high_chart 示例,但似乎太简单且与我的情况无关。有任何想法吗?非常感谢
是的,它成功了,但没有数据显示怎么来的?日志已更新,似乎数据已提取但未显示