Beautiful charts for Rails app with preconfigured Javascript, AJAX data load, clean HTML and helpers
Add this line to your application's Gemfile:
gem 'rails_chart'
And then execute:
$ bundle
Or install it yourself as:
$ gem install rails_chart
Then add into application.js
//= require chartjs_init
For AJAX request:
<%= chartjs 'test_chart', chart_data_path(name: 'test_data'), height: '200', width: '500', charttype: :line %>
Dataset inside html:
<%= chartjs 'test_chart', @dataset_hash, height: '200', charttype: :pie %>
Load GraphJs data with AJAX for two charts from a controller.
# GET /home/chart_data/:name
def chart_data
if params[:name] == 'content_data'
graphjs = { id: params[:id] }
graphjs[:dataset] = [{value: 300, label: "App"},
{value: 140, label: "Software"},
{value: 200, label: "Laptop"}]
else
graphjs = { id: params[:id], labels: %w(Jan Feb Mar Apr), datasets: [] }
(1..3).each do |line|
d = { label: "label #{line}", data: [] }
(1..4).each do |datapoint|
d[:data] << Random.new.rand(100)
end
graphjs[:datasets] << d
end
end
respond_to do |format|
format.json { render json: graphjs }
end
endSend dataset into view directly with Hash:
def index
@dataset_hash = {dataset: [
{value: 300, label: "App"},
{value: 140, label: "Software"},
{value: 200, label: "Laptop"}
]}
end- Fork it
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create new Pull Request