• Stars
    star
    184
  • Rank 209,187 (Top 5 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 11 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

[Not supported - see #53] Simplifies using Chart.js in Rails

chartjs-ror

Simplifies using Chart.js in Rails views.

Current Chart.js version

This gem includes Chart.js v3.7.1.

Installation

Add this line to your application's Gemfile:

gem 'chartjs-ror'

And then execute:

$ bundle

After that require Chart:

//= require Chart.min

Please note Chart.js no longer supports IE8 and below.

Usage

Each chart type has a corresponding helper for your views. The helper methods take the same arguments as their Javascript counterparts. The options are optional.

<%= line_chart           data, options %>
<%= bar_chart            data, options %>
<%= horizontal_bar_chart data, options %>
<%= radar_chart          data, options %>
<%= polar_area_chart     data, options %>
<%= pie_chart            data, options %>
<%= doughnut_chart       data, options %>
<%= bubble_chart         data, options %>
<%= scatter_chart        data, options %>

If you don't want these helpers – perhaps they clash with other methods in your views – add this initializer to your app:

# config/initializers/chartjs.rb
Chartjs.no_conflict!

Then you can use these helpers instead:

<%= chartjs_line_chart           data, options %>
<%= chartjs_bar_chart            data, options %>
<%= chartjs_horizontal_bar_chart data, options %>
<%= chartjs_radar_chart          data, options %>
<%= chartjs_polar_area_chart     data, options %>
<%= chartjs_pie_chart            data, options %>
<%= chartjs_doughnut_chart       data, options %>
<%= chartjs_bubble_chart         data, options %>
<%= chartjs_scatter_chart        data, options %>

For example, to render a line chart in Javascript:

var data = {
    labels: ["January", "February", "March", "April", "May", "June", "July"],
    datasets: [
        {
            label: "My First dataset",
            backgroundColor: "rgba(220,220,220,0.2)",
            borderColor: "rgba(220,220,220,1)",
            data: [65, 59, 80, 81, 56, 55, 40]
        },
        {
            label: "My Second dataset",
            backgroundColor: "rgba(151,187,205,0.2)",
            borderColor: "rgba(151,187,205,1)",
            data: [28, 48, 40, 19, 86, 27, 90]
        }
    ]
};
var options = { ... };
new Chart(ctx, {
    type: 'Line',
    data: data,
    options: options
});

The Ruby equivalent is:

data = {
  labels: ["January", "February", "March", "April", "May", "June", "July"],
  datasets: [
    {
        label: "My First dataset",
        backgroundColor: "rgba(220,220,220,0.2)",
        borderColor: "rgba(220,220,220,1)",
        data: [65, 59, 80, 81, 56, 55, 40]
    },
    {
        label: "My Second dataset",
        backgroundColor: "rgba(151,187,205,0.2)",
        borderColor: "rgba(151,187,205,1)",
        data: [28, 48, 40, 19, 86, 27, 90]
    }
  ]
}
options = { ... }
<%= line_chart data, options %>

You can also use underscored symbols for keys, instead of the camelcase versions. They will be converted to their lower camelcase counterparts on output.

data = {
  labels: ["January", "February", "March", "April", "May", "June", "July"],
  datasets: [
    {
        label: "My First dataset",
        background_color: "rgba(220,220,220,0.2)",
        border_color: "rgba(220,220,220,1)",
        data: [65, 59, 80, 81, 56, 55, 40]
    },
    {
        label: "My Second dataset",
        background_color: "rgba(151,187,205,0.2)",
        border_color: "rgba(151,187,205,1)",
        data: [28, 48, 40, 19, 86, 27, 90]
    }
  ]
}
options = { ... }
<%= line_chart data, options %>

Options

You can put anything in the options hash that Chart.js recognises. To pass a JavaScript function as an option value, wrap it in quotation marks to make it a string.

You can also use these non-Chart.js settings:

  • :class - class of the DOM canvas element - default is chart.
  • :id - id of the DOM canvas element - default is chart-n where n is the 0-based index of the chart on the page.
  • :width - width of the canvas in px - default is 400.
  • :height - height of the canvas in px - default is 400.

Sample output

<canvas id="chart-0" class="chart" width=400 height=400></canvas>
<script type="text/javascript">
  //<![CDATA[
  (function() {
    var initChart = function() {
      var ctx = document.getElementById("chart-0");
      var chart = new Chart(ctx, {
        type: "Line",
        data = { ... };
        options = { ... };
      });
    };

    if (typeof Chart !== "undefined" && Chart !== null) {
      initChart();
    }
    else {
      if (window.addEventListener) {
        window.addEventListener("load", initChart, false);
      }
      else if (window.attachEvent) {
        window.attachEvent("onload", initChart);
      }
    }
  })();
  //]]>
</script>

The Javascript is actually written out on a single line but you get the idea.

Intellectual Property

Copyright Andrew Stewart, AirBlade Software. Released under the MIT licence.

More Repositories

1

vim-gitgutter

A Vim plugin which shows git diff markers in the sign column and stages/previews/undoes hunks and partial hunks.
Vim Script
8,327
star
2

vim-rooter

Changes Vim working directory to project root.
Vim Script
1,209
star
3

acts_as_enterprisey

Rails make life easy for us but -- and it's a big but -- we don't want it to look easy. ActsAsEnterprisey makes it look hard.
Ruby
75
star
4

stimulus-datepicker

Stimulus-powered accessible datepicker
JavaScript
60
star
5

dotvim

How I configure the one true text editor.
Vim Script
41
star
6

air_budd_form_builder

[Deprecated] A form builder that generates semantic HTML as advocated by Andy Budd in CSS Mastery.
Ruby
34
star
7

quo_vadis

Multifactor authentication for Rails (7 and 6).
Ruby
32
star
8

vim-matchquote

A Vim plugin to provide %-style motion for single / double quotation marks, backticks and pipe.
Vim Script
31
star
9

vim-localorie

A Vim plugin for easy look-up of translations for Rails i18n YAML keys.
Vim Script
29
star
10

voom

A simplest-thing-that-works Vim plugin manager. Use with Vim 8 or Pathogen.
Shell
28
star
11

css_dryer

[Deprecated] Please use https://github.com/neopoly/css_dryer instead.
Ruby
27
star
12

vim-system-escape

Aims to become the definitive reference for escaping commands for system()
21
star
13

dotfiles

How one rolls.
Shell
18
star
14

geo_tools

Easier latitudes and longitudes on forms (and validation in model) for Rails 2.3.
Ruby
14
star
15

bin

It's not rubbish.
Perl
13
star
16

mys3ql

Simple backup of your MySQL database onto Amazon S3.
Ruby
12
star
17

fyi

Find out what cron is doing.
Ruby
11
star
18

vim-interdental

Indent guides that span empty lines
Vim Script
9
star
19

headlessui-stimulus

Stimulus components for Headless UI
JavaScript
9
star
20

vim-accent

A Vim plugin for typing accented characters without remembering their pseudo versions.
Vim Script
8
star
21

git-stager

Easily stage and unstage files.
Ruby
8
star
22

tcs

Tailwind class sorter - sorts the classes in your HTML into Tailwind's recommended class order, without Prettier
Ruby
7
star
23

vim-helptab

Ensure Vim's help docs always open in their own, single tab.
Vim Script
7
star
24

template_form

Rails form builder using templates to define the HTML
Ruby
6
star
25

brocade

[Deprecated] Generates barcodes for Rails ActiveRecord models.
Ruby
5
star
26

vim-current-search-match

Highlights the current search match.
Vim Script
5
star
27

entitlement

Simple page titles in Rails 3 and 4.
Ruby
4
star
28

gem-versions

(Gem command plugin to) list all published versions of a gem.
Ruby
3
star
29

base_look_and_feel

Generates a basic, neat look and feel for a Rails app.
Ruby
3
star
30

vim-highline

A Vim plugin which lets you toggle a line highlight for any line
Vim Script
3
star
31

vim-tailwind

A Vim plugin to autocomplete Tailwind classes without LSP. Also looks up Tailwind classes' CSS properties.
Vim Script
3
star
32

vim-tag-closer

A Vim plugin to close HTML tags on demand.
Vim Script
2
star
33

acts_as_parent_of

Implements the parent model code from Advanced Rails Recipe 13.
Ruby
2
star
34

flippeur

Simple feature flipping.
Ruby
2
star
35

air_blade_tools

[DEPRECATED] Foreign key support in Rails 2.x migrations.
Ruby
2
star
36

jquery-rhythm

Fit images to the vertical baseline rhythm without distortion.
JavaScript
2
star
37

vim-contest

A runner for Vimscript tests.
Vim Script
2
star
38

right_aws

RightScale's RightAws (they don't seem to have a Git repository)
Ruby
2
star
39

vim-tcs

Integrates tcs (Tailwind class sorter) with Vim
Vim Script
2
star
40

respecta

Measures how well an abbreviation matches a string.
Ruby
1
star
41

label_definitions

An open source list of label dimensions.
Ruby
1
star
42

plugin_routing

Rails plugin which is a port of James Adam's RailsEngine routing extension.
Ruby
1
star
43

pubmed

A few hacky scripts to load PubMed baseline files into a database.
Ruby
1
star
44

sinter

Sinter checks files for syntax errors.
Shell
1
star
45

pdf-print

How to print a PDF at 100%
Ruby
1
star