百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
C

chartjs-ror

> 编程语言
开源

[不支持 - 请参阅 #53] 简化在 Rails 中使用 Chart.js

184 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

[不支持 - 请参阅 #53] 简化在 Rails 中使用 Chart.js

chartjs-ror

Simplifies using Chart.js in Rails views.

Current Chart.js version

This gem includes Chart.js v4.5.0.

Installation

Add this line to your application's Gemfile:

gem 'chartjs-ror'

And then execute:

$ bundle

After that require Chart:

javascript
//= 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.

erb

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

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

Then you can use these helpers instead:

erb

For example, to render a line chart in Javascript:

…

The Ruby equivalent is:

ruby
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 = { ... }

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

ruby
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 = { ... }

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

xml

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.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言