Pie Charts in Rails Admin

 With Rails Admin

If you are using RailsAdmin for managing you web-application from backend, you get reports for your data out of the box. In addition you can edit/update in no time with ease. You will not have charts and graph at that instant though.

How can I get Charts?

Yes there are some Rails-Admin plugins to help you out. My favorite is RailsAdmin Charts.

With Rails Admin Charts

The default chart is Line Chart. For Pie charts the data format. Since this gem uses ‘Lazy High charts‘ for chart generation, we can find chart specific data arrangement technique  from there.

For pie data format should be like

class Contractor < ActiveRecord::Base
  include RailsAdminCharts
  ...
  
  def self.graph_data(since = 30.days.ago)
    Contractor.group(:state).count.to_a
    # Output
    # [["Bagmati", 3], ["Gandaki", 3], ["Janakpur", 5]]
  end

  def self.chart_type
    'pie'
  end
end

In this example I wanted to show all the contractors count by States.

Charts By States : Nepal

Charts By States : Nepal

 

Useful links

http://www.highcharts.com/demo/?example=pie-basic&theme=default

https://github.com/michelson/lazy_high_charts/wiki/Pie-Chart

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s