2月 21st, 2010at 20:36

Tags:

RedmineプラグインでOpen Flash Chart(OFC2)を使うとIO ERROR

Redmineのバーンダウンチャートを作りたかったので、Open Flash Chart II Plugin for Ruby on Rails – Graphs (OFC 2)を元に、プラグイン内からサンプルを動かしてみる。
ソースはOpen Flash Chart II – Line Graphのまんま。indexメソッドのところは以下のように書いた。

  def index
@graph = open_flash_chart_object(600,300,"/version_burndown/graph_code")
end

しかし、以下のように怒られます。

Open Flash Chart
IO ERROR
Loading test data
Error #2032
This is the URL that I tried to open:/version_burndown/graph_code

どうも「表示するデータが取れてないよー」ということらしい。
「http://localhost:3000/version_burndown/graph_code」をたたくと404になる。これはコントローラ内で、project_idがない場合に404にしているからだと気がつく。
「http://localhost:3000/version_burndown/graph_code?project_id=fujihara」を叩くとJSONが表示される。
さらにHTMLのソースコードを見てみると以下のようになっていた。

 <script type="text/javascript">
swfobject.embedSWF("/open-flash-chart.swf", "flash_content_DleU0GzY", "800", "450", "9.0.0", "expressInstall.swf",{"data-file":"%2Fversion_burndown%2Fgrapha_code"});
</script>

data-fileというところで「/version_burndown/graph_code」を指定している。
これが404でIO ERRORになっているのだろう。

このプラグインの動きは、

  • Flashのファイル(swfファイル)をHTMLで表示する
  • swfファイルの引数に表示するデータ(JSON)のURL、ここでは/version_burndown/grapha_codeをCallする
  • 受け取ったデータをswfファイルで表示する

という順番でグラフを表示しているらしい。
さらに、READMEを見てみると、urlを指定しているところがurl_forを使っていてちょっと違う。

5) Add the following to the test_it_controller.rb in RAILS_ROOT/app/controllers:
class TestItController < ApplicationController
def index
respond_to do |wants|
wants.html {
@graph = open_flash_chart_object( 600, 300, url_for( :action => 'index', :format => :json ) )
}
wants.json {
chart = OpenFlashChart.new( "MY TITLE" ) do |c|
c << BarGlass.new( :values => (1..10).sort_by{rand} )
end
render :text => chart, :layout => false
}
end
end
end
6) Add the following to index.html.erb in RAILS_ROOT/app/views/test_it/:
<html>
<head>
<script type="text/javascript" src="/javascripts/swfobject.js"></script>
</head>
<body>
<%= @graph %>
</body>
</html>

project_idなどの指定をやめて、READMEどおりやったら動かすことができた。
Controller#respond_toはWeb ApplicationとWeb Serviceの垣根をなくす!?を読んでわかったが、READMEの場合は、JSONやHTMLの呼び出しをわけて処理しているみたいだ。こういうやりかたもあるということか。

今回のケースだと、パラメタにproject_idとversion_idが必要なので以下のように書くと動作する。

  def index
@graph = open_flash_chart_object( 800, 450, url_for( :action => 'graph_code', :project_id => @project.id, :version_id => @version.id ) )
end

OFC2の動きが読めてきた。よく出来ているな。