プログラムで生成した画像を rails で表示する
gruff, cairo をつかって画像生成し、それを rails で表示させることができた。
view では次のようにする。
<img src="<%= url_for :controller=>'graphview', :action=>'graph_foo' %>">
controller では次のようにする。
class GraphviewController < ApplicationController
def graph_foo
# case: using gruff
#----------------------
# @company = Company.find(params[:company_dbid])
#
# g = Gruff::Line.new 500
# g.labels = {0 => 'Day 0', 1 => 'Day 2',
# 2 => 'Day 4', 3 => 'Day 10'}
# g.data("xx", [30, 20, 60, 50])
# g.minimum_value = 0
#
# # :disposition => "inline"
# send_data(g.to_blob, :type => 'image/png')
# case: using rcairo
#----------------------
format = Cairo::FORMAT_ARGB32
width = 300
height = 200
radius = height / 3
# 日の丸を描く
surface = Cairo::ImageSurface.new(format, width, height)
context = Cairo::Context.new(surface)
context.set_source_rgb(1, 1, 1)
context.rectangle(0, 0, width, height)
context.fill
context.set_source_rgb(1, 0, 0)
context.arc(width / 2, height / 2, radius, 0, 2 * Math::PI)
context.fill
# surface.write_to_png("hinomaru.png") # ファイルへ出力
# 文字列として出力
s = StringIO.new
surface.write_to_png(s)
send_data(s.string, :type => 'image/png')
end
environment.rb では次を追加しておく。
require 'gruff'
require 'cairo'
require 'stringio'
« "喜劇 俺たちに品格はない" を観た | トップページ | ピックアップ:『初めてのRuby』余った切れ端, 秋葉原で職務質問に素直に応じないと こーなる, etc... »
この記事へのコメントは終了しました。
« "喜劇 俺たちに品格はない" を観た | トップページ | ピックアップ:『初めてのRuby』余った切れ端, 秋葉原で職務質問に素直に応じないと こーなる, etc... »
コメント