25
loading...
This website collects cookies to deliver better user experience
rbenv
. It makes it easier to manage different ruby versions.rails new chartjs-example
rails generate controller Home index
routes.rb
.
Rails.application.routes.draw do
root to: 'home#index'
end
rails s
then on your web browser go to localhost:3000
. It should show you an empty page.yarn add chart.js
/app/javascript/packs/application.js
add then following import Chart from 'chart.js/auto';
import Chart from 'chart.js/auto';
as simply including require 'chart.js'
will not work. You can choose to import specific modules by following the instructions here.app/views/home/index.html.erb
and add the following
<canvas id="myChart" width="400px" height="400px"></canvas>
<script>
var myChart = new Chart(ctx, {...});
</script>
application.js
. As you've imported your ChartJS modules there, Chart
can be accessed within that scope.
document.addEventListener('turbolinks:load', () => {
var ctx = document.getElementById('myChart').getContext('2d');
var myChart = new Chart(ctx, {
type: 'line',
data: {
labels: JSON.parse(ctx.canvas.dataset.labels),
datasets: [{
data: JSON.parse(ctx.canvas.dataset.data),
}]
},
});
})
labels
(x axis) and data
(y axis). You need to do that using the data-
attributes in html.index.html.erb
and replace the previous canvas line with
<canvas id="myChart" width="200px" height="100px" data-labels="<%= @data_keys %>" data-data="<%= @data_values %>" ></canvas>
app/controllers/home_controller.rb
class HomeController < ApplicationController
def index
@data_keys = [
'January',
'February',
'March',
'April',
'May',
'June',
]
@data_values = [0, 10, 5, 2, 20, 30, 45]
end
end
data_keys
and data_values
are populated with sample data.http://localhost:3000/
you'll see your newly created chart.data-
attributesRails.application.routes.draw do
root to: 'home#index'
end
<h1>Home#index</h1>
<p>Find me in app/views/home/index.html.erb</p>
<canvas id="myChart" width="200px" height="100px" data-labels="<%= @data_keys %>" data-data="<%= @data_values %>" ></canvas>
class HomeController < ApplicationController
def index
@data_keys = [
'January',
'February',
'March',
'April',
'May',
'June',
]
@data_values = [0, 10, 5, 2, 20, 30, 45]
end
end
// This file is automatically compiled by Webpack, along with any other files
// present in this directory. You're encouraged to place your actual application logic in
// a relevant structure within app/javascript and only use these pack files to reference
// that code so it'll be compiled.
import Rails from "@rails/ujs"
import Turbolinks from "turbolinks"
import * as ActiveStorage from "@rails/activestorage"
import "channels"
import Chart from 'chart.js/auto';
Rails.start()
Turbolinks.start()
ActiveStorage.start()
document.addEventListener('turbolinks:load', () => {
var ctx = document.getElementById('myChart').getContext('2d');
var myChart = new Chart(ctx, {
type: 'line',
data: {
labels: JSON.parse(ctx.canvas.dataset.labels),
datasets: [{
data: JSON.parse(ctx.canvas.dataset.data),
}]
},
});
})