Overview

ReportsKit uses Chart.js to render charts. It uses sensible defaults, but you can optionally set any other Chart.js options in the YAML file, so that all of Chart.js's features are also available in ReportsKit.

If you want to render a table instead of a chart, simply set format: table in the root level of the YAML file. See Tables for details.

Here's an example of how to use custom Chart.js options in a YAML file:


To create the chart above, simply add the following YAML file and call render_report in any view:

YAML

config/reports_kit/reports/visualization_overview.yml

measure: post
dimensions:
- author
chart:
  type: horizontalBar
  options:
    scales:
      xAxes:
      - scaleLabel:
          display: true
          labelString: Posts
      yAxes:
      - scaleLabel:
          display: true
          labelString: Author

Note: The "chart.type" and "chart.options" options are passed to Chart.js, so any values supported by Chart.js are supported here, too.


View

app/views/my_controller/my_view.html.haml

= render_report 'visualization_overview'

Model

app/models/post.rb

class Post < ApplicationRecord
  belongs_to :author
  has_many :post_views, dependent: :destroy
  has_many :posts_tags, dependent: :destroy
  has_many :tags, through: :posts_tags

  include ReportsKit::Model
  reports_kit do
    aggregation :average_time_to_publish, [:average, 'posts.published_at - posts.created_at']
    contextual_filter :for_author, ->(relation, context_params) { relation.where(author_id: context_params[:author_id]) }
    dimension :approximate_views_count, group: 'ROUND(posts.views_count, -1)'
    filter :is_published, :boolean, conditions: ->(relation) { relation.where(status: 'published') }
  end

  STATUSES = %w(draft private published).freeze

  def to_s
    title
  end
end

Model's Columns

id              integer
author_id       integer
title           string
status          string
published_at    datetime
is_featured     boolean
views_count     integer
created_at      datetime
updated_at      datetime