open_flash_chart_lazyを試す

Windows Vista 32bitでプロキシがあってMySQLが大好きな環境。

以前導入したopen_flash_chart_2を来年度バージョンに移行したら

Open Flash Chart
IO ERROR
Loading test data
Error #2032

こんなのが出るようになった。

wrong number of arguments (1 for 0)
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/json/encoding.rb

JSON辺りでエラーになっているっぽいけど、よく分からない…問題点が見つからないのに新しいヤツを試して解決できるか分からないけど突っ走る。

何をダウンロードする?

Tutorial: Other libraries Open Flash ChartRubyをクリックする。
githubpeterpunk / open_flash_chart_lazyが見つかる。
 
とりあえず「readme.textile」を読む。
「peterpunk-mhash」というのが必要らしい。

C:\Users\kenmituo>gem install peterpunk-mhash
Successfully installed peterpunk-mhash-0.0.8
1 gem installed
Installing ri documentation for peterpunk-mhash-0.0.8...
Installing RDoc documentation for peterpunk-mhash-0.0.8...

入った。
 
 

git clone git://github.com/peterpunk/open_flash_chart_lazy.git

gitが使えないので、右上の「Download Source」から「Zipでクレ」を押す。
解凍したら「vendor/plugins/open_flash_chart_lazy」にぶち込む

C:\Users\kenmituo\Documents\Aptana Studio\project_home>rake open_flash_chart_lazy:i
nstall
(in C:/Users/kenmituo/Documents/Aptana Studio/project_home)

これで入ったらしい。
 
しかしサンプルコードを実行してもFlashが出ない。
Aptana Studioの「Rake タスク」で「open_flash_chart_lazy:install」を選択して「実行」する。
  
なんでやり直したかというと、「vendor/plugins/open_flash_chart_lazy/tasks/openflash_chart_lazy_tasks.rake」っていうファイルを眺めると、

namespace :open_flash_chart_lazy do
PLUGIN_ROOT = File.dirname(__FILE__) + '/../'

desc 'Installs required swf in public/ and javascript files to the public/javascripts directory.'
task :install do
FileUtils.cp Dir[PLUGIN_ROOT + '/assets/swf/*.swf'], RAILS_ROOT + '/public'
FileUtils.cp Dir[PLUGIN_ROOT + '/assets/javascripts/*.js'], RAILS_ROOT + '/public/javascripts'
end
desc 'Removes the swf and javascripts for the plugin.'
task :remove do
FileUtils.rm %{json2.js swfobject.js}.collect { |f| RAILS_ROOT + "/public/javascripts/" + f }
FileUtils.rm %{open_flash_chart.swf}.collect { |f| RAILS_ROOT + "/public/" + f }
end
end

っていうのを見つけたから。
 
 
さて、適当なコントローラーに

app/controllers/kenmituo/blendy_controller.rb
  def hoge

  end
  
  def progress
    bar_graph = OpenFlashChartLazy::Line.new("The title of the graph")
    first_serie = OpenFlashChartLazy::Serie.new(
    [["2008-1",100],["2008-2",120],["2008-3",130]],
    {:title=>"Argentina",:start_date=>Time.mktime(2008,1,1),:items=>8})
    
    second_serie = OpenFlashChartLazy::Serie.new(
    [["2008-1",50],["2008-2",40],["2008-3",90]],
    {:title=>"Tupungato",:start_date=>Time.mktime(2008,1,1),:items=>8})
  
    bar_graph.add_serie(first_serie)
    bar_graph.add_serie(second_serie)
    pp bar_graph
    render :text=>bar_graph.to_graph_json
  end

ビュー

app/vies/kenmituo/blendy/hoge.html.erb
<h3>hoge</h3>
<%= remote_graph("my_chart",{:route=>"/kenmituo/blendy/progress"}) %>

 
 
で、hogeにアクセスしてみる

<h3>hoge</h3>
<script type="text/javascript">
//<![CDATA[
swfobject.embedSWF('/open-flash-chart.swf','my_chart','300','150','9.0.0','expressInstall.swf',{'data-file':'/kenmituo/blendy/progress'});
//]]>
</script><div id="my_chart"></div>

あれ?
html.erbの「remote_graph」はヘルパーでコードが挿入されたけど、コントローラーに仕込んだ「 pp bar_graph」がコンソールに現れない。
localhost:3002/kenmituo/blendy/progress」にアクセスするとJSONデータが表示される。
 
悩んだので、Open Flash Chart Lazy Rails Pluginソースコードを眺めると決定的に異なるところを発見する。


Open <a class="keyword" href="http://d.hatena.ne.jp/keyword/Flash">Flash</a> Chart Lazy <a class="keyword" href="http://d.hatena.ne.jp/keyword/Rails">Rails</a> Plugin









ヘッダの中身が「hoge」には無い。
 
 
ということで、「hoge.html.erb」に細工を施す。

<% content_for :html_header do -%>
<%= javascript_include_tag "open_flash_chart/json2" %>
<%= javascript_include_tag "open_flash_chart/swfobject" %>
<% end -%>

<h3>hoge</h3>
<%= remote_graph("my_chart",{:route=>"//kenmituo/blendy/progress"}) %>

ついでに、jsファイルも移動しておいた。
 
微妙に不親切な公式サイトだった。

inlineが使えたらいいなーって半日試したが動かない…orz
 
ローカル環境では動くようになったので、サーバーにあげてプロジェクトを再起動したら立ち上がらなくなったので止めることにした。
google-charts-on-railsに手を出そうかと思考中。