以下のコードでは、たくさんのチャートを描画しようとしています。私のデータベースには、「名前、体重、身長、色、年齢」の属性を持つモデル 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 の例を読みましたが、単純すぎて私のケースとは関係がないようです。何か案は?どうもありがとう
ええ、うまくいきますが、データが表示されません。ログが更新されました。データは抽出されたようですが、表示されませんでした