image

  • フォト Amazonギフト券
    ※この時計の時刻は、閲覧しているパソコンのものであり、必ずしも正確な時間とは限りません

検索

最近のトラックバック

無料ブログはココログ

« 2008-05-16 | トップページ | 2008-05-19 »

2008-05-18

2008-05-18

バランスシートの簡易グラフ表示

財務諸表の中の1つのバランスシートのおおまかな様子を図示する
プログラムを試作しました。
これは [[流動資産、固定資産], [流動負債、固定負債、資金]]をパーセント表示するものです。
出力書式は png とsvg です。
Bs_3

画像生成には cairo ライブラリーを利用しました。(sudo gem install cairo でインストールすること)

ファイナンス系の web ページ, web-service や EDINET から バランスシートの上記の値を取得することは簡単だ。それらを図示するのに使おうと思っています。

以下の ruby でのソースコードを示します。

$ cat graph_bs.rb
# See http://jp.rubyist.net/magazine/?0019-cairo
#     http://d.hatena.ne.jp/miyamuko/20070518/p1
#     http://ashitani.jp/wiki/index.rb?p=rcairo

# 2008-05-18 katoy

require "rubygems"
require 'cairo'

def draw(surface, graph_data)
  context = Cairo::Context.new(surface)

  width = graph_data[:width] * 1.0
  height = graph_data[:height] * 1.0
  font_size = graph_data[:font_size] * 1.0
  offset_x = font_size * 1.5
  offset_y = font_size * 0.5
  context.set_font_size(font_size)
  context.select_font_face("Sans")

  colors = [
    [[0.0, 0.9, 0.0], [0.0, 0.9, 0.9], [0.9, 0.0, 0.0]],
    [[0.9, 0.0, 0.0], [0.9, 0.9, 0.0], [0.9, 0.9, 0.9]],
    [[0.0, 0.9, 0.0], [0.5, 0.9, 0.5], [0.5, 0.9, 0.0]],
  ]
  col_num = graph_data[:data].size

  graph_data[:data].each_with_index do |col, i|
    total = col.to_a.inject(0){|t, a| t += a}
    sub_total = 0
    col.each_with_index do |val, j|

      d0 = 1.0 * sub_total / total * height
      d1= 1.0 * (sub_total + val) / total * height
      w0 = 1.0 * width / col_num * i
      w1 = 1.0 * width / col_num * (i + 1)

      context.fill do
        color = colors[ i % 3][j]
        context.set_source_rgb(color[0],color[1],color[2])
        context.rectangle(w0.round, d0.round, w1.round, d1.round)
      end

      context.stroke do
        context.set_source_rgb(0, 0, 0)  # black
        context.set_line_width(1)
        context.move_to(w0, d1)
        context.line_to(w1, d1)
      end
      # text
      context.set_source_rgb(0, 0, 0)  # black
      p = 100.0 * val / total
      c = height * 0.5 * (sub_total + (sub_total + val)) / total
      w = 0.5 * (w0 + w1)
      context.move_to(w.round - offset_x, c + offset_y)
      text = "#{"%.1f" % p} %"
      context.show_text(text)

      sub_total += val
    end
  end

  context.set_source_rgb(0, 0, 0)  # black
  context.set_line_width(1)
  # vertical line
  context.stroke do
    context.move_to(0, 0)
    context.line_to(0, height)

    (1..col_num).each do |i|
      w0 = 1.0 * width / col_num * i
      context.move_to(w0.round - 1, 0)
      context.line_to(w0.round - 1, height)
    end
  end

  # horizontal line
  context.stroke do
    context.move_to(0, 0)
    context.line_to(width, 0)
  end
end

def output(graph_data, file_name)
  width = graph_data[:width] * 1.0
  height = graph_data[:height] * 1.0

  # PNG 出力
  format = Cairo::FORMAT_ARGB32
  Cairo::ImageSurface.new(format, width, height) do |surface|
    draw(surface, graph_data)
    surface.write_to_png("#{file_name}.png")
  end

  # SVG 出力
  Cairo::SVGSurface.new("#{file_name}.svg", width, height) do |surface|
    draw(surface, graph_data)
  end
end

width = 100
height = 120
font_size = 10
rs = 100  # 流動資産の金額
ks =  60  # 固定試算の金額
rf =  70  # 流動負債の金額
kf =  50  # 固定負債の金額
si = (rs + ks) - (rf + kf)  # 残りは資金

graph_data = {:width => width, :height => height, :font_size => 12,
              :data => [[rs, ks], [rf, kf, si]]}

output(graph_data, "bs")

ピックアップ:Scalaでスタック指向言語をサクッと実装, 足跡機能への抵抗感増大, etc...

- http://www.ibm.com/developerworks/jp/xml/library/x-xstream/
> > XStream を使って Java オブジェクトを XML にシリアライズする

xfy/xvcd で利用するとしたら、どんな利用法があるだろう...

- http://inforno.net/articles/2008/05/17/simple-stack-oriented-language-implemented-using-scala
> > inforno :: Scalaでスタック指向言語をサクッと実装する

- http://www.kanasansoft.com/weblab/2008/05/rubyistrubyrubyist_magazinegre.html
> > RubyistによるRubyのオンラインマガジン「Rubyist Magazine(通称るびま)」の通読を支援するGreasemonkey (Kanasansoft Web Lab.)

- http://www.itmedia.co.jp/news/articles/0805/16/news015.html
> > Yahoo!、「SearchMonkey」を一般公開 - ITmedia News

- http://blogs.itmedia.co.jp/knowledge/2008/05/itme-cd32.html
> > ナレッジ!?情報共有・・・永遠の課題への挑戦 > 足跡機能への抵抗感増大はSNSでの新たなイノベーションの兆し : ITmedia オルタナティブ・ブログ

- http://www.nicovideo.jp/watch/sm3331128
> > 1日6分で”割れた腹筋”を手に入れる腹筋法‐ニコニコ動画(SP1)

- http://d.hatena.ne.jp/codemaniax/20080516/1210948653
> > では東京三菱UFJは何をすべきだったのか・2 - codemaniaxの脱・公務員宣言

- http://blog.livedoor.jp/dqnplus/archives/1128576.html
> > 痛いニュース(ノ∀`):ネットで見た「メントスコーラ」を胃袋で実験した女子大生、鼻と口からコーラが吹き出し病院に担ぎ込まれる…中国

- http://www.atmarkit.co.jp/fsecurity/special/112mac/mac01.html
> > セキュリティベンダに聞く「Macって安全ですか?」 − @IT

- http://www.asahi.com/life/update/0516/TKY200805160263.html
> > asahi.com:1位は「空気読め!!それより部下の気持ち読め!!」 - 暮らし

- http://ameblo.jp/yahooseo/entry-10097220470.html
> > 更新頻度は1日数回でも普通。それを異様に思うのは異常|web2.0

« 2008-05-16 | トップページ | 2008-05-19 »

mokuji

2013年12月
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        

google

  • twitter
  • __
  • _
    Googleボットチェッカー

合わせて読む

  • 合わせて読む
    フィードメーター - katoy: cocolog あわせて読みたい

リンク