2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

RailsでC3.jsを使う

Posted at

RailsでC3.jsというグラフ描画機能を使う。こんな感じ。

ss 2017-10-16 20.12.48.png

Gemfileに以下を追加

gem 'c3-rails'
gem 'd3-rails', '~> 3.5.17'

※ はじめ、d3のバージョンを指定せずにインストールしたら動かなかった。以下によるとD3のバージョンを3系するとうまくいくとのこと。
https://stackoverflow.com/questions/38344643/c3js-cannot-read-property-category10-of-undefined

applicaton.jsに以下を追加

//= require d3
//= require c3

application.cssに以下を追加

 *= require c3

描画したいviewに以下を追加

<div class="well">
  <span>グラフ</span>
  <div id="chart"></div>
</div>

<script type="text/javascript">
var chart = c3.generate({
    bindto: '#chart',
    data: {
      columns: [
        ['data1', 30, 200, 100, 400, 150, 250],
        ['data2', 50, 20, 10, 40, 15, 25]
      ]
    }
});
</script>

以上。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?