Chartkick: data visualization made easy with Ruby

gem “chartkick” Then you have to choose your charting library. In case, if more than one charting library is loaded, choose between them with the following code:

<%= line_chart data, adapter: "google" %> <!-- or highcharts -->
P.S:- highcharts is the default adapter is nothing else is defined. Using Chartkick Once it’s installed, you can start playing around with chartkick. For example:
<%= line_chart User.group_by_day(:created_at).count %>

In the above example, I have just shown you how we can make a line chart.Similarly, you can create Pie chart, Column chart, Bar chart, Area chart, Scatter chart, Geo charts, Timeline(Google Charts)etc. All you need is that one line of Ruby code. Cool, isn’t it!!

Chartkick gives you a variety of options to make your charts interesting. You can also set a Global option for each chart by using initializers and even customize the HTML. Data can be passed as a Hash or as an Array. You can find more details about using Chartkick options, sending data in Chartkick documentation. Now, let’s have some real fun by using Chartkick to create some interactive Graphs, using Dynamic data. To create a graph which drives data from the ajax request, you just need to define a method in the controller and pass the JSON data. For example app/controllers/views_controller.rb:
class ViewsController < ApplicationController
  def show
    @view = View.all
  def views_by_day
    render json: View.group_by_day(:viewed_at, format: "%B %d, %Y").count
Next, add in the code listed below to your views/show view. app/views/views/show.html.erb:
<div class="container">
  <h3>Views By Day</h3>
  <%= line_chart views_by_day_view_path %>
We can also create graphs with Multiple Series with the below codes
<%= line_chart { |view|
 {name:, data: view.feats.group_by_week(:created_at).count}
} %>
<%= line_chart Feat.view(:view_id).group_by_week(:created_at).count %>
Voila! there you have it how you want. Here is an example charts I got for you Chartkick So, next time if you want to create charts and graphs easily in your web application, don’t forget to use Chartkick and let me know in the comment section if you know any such tool that made your coding a bliss! Happy Coding!