タイトルにもある通りで、chart.jsでRailsアプリ上にデータを反映させグラフ化したいです。
やりたいこと。
1、金額を入力してデータを保存。
2、金額をグラフに反映する。
以上の事をやってみたいのですが、グラフ表示ができたのですが。
**2、金額をグラフに反映する。**が上手くいきません。
ソースがこちら▼
app/models/good.rb
class Good < ApplicationRecord validate :price_validate #priceフィールドに数字か入力されているか検証。 validates_numericality_of :price, :message => "が有効な数値ではありません。" #商品の価格が正の数であることを確認する。 #価格フィールドが空でないときだけチェックをする。 protected def price_validate errors.add(:price, "は0より大きくなければなりません。") unless price.nil? || price > 0.0 end end
app/view/goods/index.html.erb
<p id="notice"><%= notice %></p> <h1>Rails貯金グラフ</h1> <%= link_to '商品登録', new_good_path, class: 'btn' %> <table> <thead> <tr> <th>価格</th> <th colspan="3"></th> </tr> </thead> <tbody> <% @goods.each do |good| %> <tr> <td><%= (good.price).to_i %>円</td> <td><%= link_to '詳細', good, class: 'btn' %></td> <td><%= link_to '編集', edit_good_path(good), class: 'btn' %></td> <td><%= link_to '削除', good, method: :delete, data: { confirm: '本当に削除してもよろしいですか?' }, class: 'btn' %></td> </tr> <% end %> </tbody> </table> <br> <h1>売上です。</h1> <canvas id="myChart" width="250" height="100"></canvas> <script>draw_graph();</script> <%= link_to '商品登録', new_good_path, class: 'btn' %>
app/controllers/gooods_controller.rb
class GoodsController < ApplicationController before_action :set_good, only: [:show, :edit, :update, :destroy] # GET /goods # GET /goods.json def index @goods = Good.all end # GET /goods/1 # GET /goods/1.json def show end # GET /goods/new def new @good = Good.new end # GET /goods/1/edit def edit end # POST /goods # POST /goods.json def create @good = Good.new(good_params) respond_to do |format| if @good.save format.html { redirect_to @good, notice: 'Good was successfully created.' } format.json { render :show, status: :created, location: @good } else format.html { render :new } format.json { render json: @good.errors, status: :unprocessable_entity } end end end # PATCH/PUT /goods/1 # PATCH/PUT /goods/1.json def update respond_to do |format| if @good.update(good_params) format.html { redirect_to @good, notice: 'Good was successfully updated.' } format.json { render :show, status: :ok, location: @good } else format.html { render :edit } format.json { render json: @good.errors, status: :unprocessable_entity } end end end # DELETE /goods/1 # DELETE /goods/1.json def destroy @good.destroy respond_to do |format| format.html { redirect_to goods_url, notice: 'Good was successfully destroyed.' } format.json { head :no_content } end end private # Use callbacks to share common setup or constraints between actions. def set_good @good = Good.find(params[:id])t end # Never trust parameters from the scary internet, only allow the white list through. def good_params params.require(:good).permit(:price) end end
app/assets/javascripts/goods.coffee
window.draw_graph = -> ctx = document.getElementById("myChart").getContext('2d') barNum = 6 labels = new Array(barNum) bgColors = new Array(barNum) bdColors = new Array(barNum) for i in [0...barNum] labels[i] = 'data' + i bgColors[i] = 'rgba(75, 192, 192, 0.2)' bdColors[i] = 'rgba(75, 192, 192, 1)' myChart = new Chart(ctx, { type: 'bar', data: { labels: labels datasets: [{ label: '# of Votes', data: [1,2,3] backgroundColor: bgColors, borderColor: bdColors, borderWidth: 1 }] }, options: { scales: { yAxes: [{ ticks: { beginAtZero:true } }] } } })
参考にした記事はこちら▼
あなたの回答
tips
プレビュー