6

I was trying to use Chartkick to generate graph that shows on rails active admin dashboard. Here is the tutorial I am using: http://www.patrickedelman.com/simple-charting-for-activeadmin/

However, the graph did not show up, instead, it shows "Loading..."

I already include javascript_include_tag "//www.google.com/jsapi", "chartkick" and gem chartkick is installed, but it still does not work.

Please help! Thank you.

Juan Kou
  • 155
  • 5
  • 12

4 Answers4

11

Don't forget to add #= require chartkick to active_admin.js.coffee.

To render a chart in AA dashboard, go with:

div class: 'custom-class' do
  h3 'Your name for a chart'
  @metric = Model.group(:attribute).count # whatever data you pass to chart
  render partial: 'metrics/partial_name', locals: {metric: @metric}
end

This file

partial: 'metrics/partial_name'

lays under app/views/metrics/partial_name directory, and could have the following structure:

_partial_name.html.haml:

= javascript_include_tag "//www.google.com/jsapi", "chartkick"
= pie_chart metric 

Having this you should be ok with using chartkick.

Andrey Deineko
  • 51,333
  • 10
  • 112
  • 145
7

For Active Admin on Rails 6

After add the gem "chartkick", you need to add to app/assets/javascripts/active_admin.js:

//= require chartkick
//= require Chart.bundle
Community
  • 1
  • 1
Pedro Schmitt
  • 150
  • 3
  • 9
  • 3
    This is the actual fix for Rails 6. Tried many other stuff and this is the only one that worked. – crodev Jun 02 '21 at 10:38
0

If you want to include the js in the active admin file without rendering a partial, you can do this:

text_node javascript_include_tag 'https://www.gstatic.com/charts/loader.js', skip_pipeline: true

In my case I have a custom page and I added it in the content block

content do
  text_node javascript_include_tag 'https://www.gstatic.com/charts/loader.js', skip_pipeline: true

  columns do
    column do
      xxxx
    end
  end
end

I got the idea from here https://stackoverflow.com/a/61460749/3984542

vicente.fava
  • 360
  • 3
  • 14
0

For Active Admin on Rails 7

I added the following lines to my active_admin.rb (I'm using Highcharts):

ActiveAdmin.setup do |config|
  config.register_javascript "http://code.highcharts.com/highcharts.js"
  config.register_javascript "chartkick.js"
end