3
3

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.

Ruby | Gviz gem で、 テキスト内容・形状・色をランダムに選択してグラフを生成する

Posted at

Ruby | Gviz gem で、 テキスト内容・形状・色をランダムに選択してグラフを生成する

概要

Gviz gem で、 テキスト内容・形状・色をランダムに選択してグラフを生成してみます。
この行為に意味はありません。

テキストは Lorem ipsum を利用します。

Lorem ipsum とは?

下記参照。
http://ja.wikipedia.org/wiki/Lorem_ipsum

Loerm ipsum のテキスト

Sublime Text2 の Emmet plugin を利用して取得しました。

この Emmet の命令を Ctrl + E で展開すると

lorem10.item*5

こうなります。これで、 loerm ipsum の 10単語 × 5 行 で 50 単語を取得します。

<div class="item">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Suscipit, nam.</div>
<div class="item">Aliquam, hic tempora iure quibusdam voluptatibus natus perferendis maiores veritatis.</div>
<div class="item">Doloribus dignissimos odio deleniti dolorum unde doloremque ex eaque voluptates.</div>
<div class="item">Ea placeat dolor quam provident distinctio voluptatibus, fuga ut incidunt.</div>
<div class="item">Ea, reiciendis repudiandae magnam aliquam eum debitis aliquid atque maiores.</div>

サンプルコード

require 'gviz'
require 'date'

lorem_ipsum = <<-EOS
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Suscipit, nam.
Aliquam, hic tempora iure quibusdam voluptatibus natus perferendis maiores veritatis.
Doloribus dignissimos odio deleniti dolorum unde doloremque ex eaque voluptates.
Ea placeat dolor quam provident distinctio voluptatibus, fuga ut incidunt.
Ea, reiciendis repudiandae magnam aliquam eum debitis aliquid atque maiores.
EOS

lorem_ipsum = lorem_ipsum.gsub(/[\n,\.]/, '').split(' ')
loerm_samples = lorem_ipsum.sample(9).map(&:to_sym)

shapes = %w(box polygon ellipse oval circle point egg triangle plaintext diamond trapezium parallelogram house pentagon hexagon septagon octagon doublecircle doubleoctagon tripleoctagon invtriangle invtrapezium invhouse Mdiamond Msquare Mcircle rect rectangle square star none underline note tab folder box3d component promoter cds terminator utr primersite restrictionsite fivepoverhang threepoverhang noverhang assembly signature insulator ribosite rnastab proteasesite proteinstab rpromoter rarrow larrow lpromoter)
shape_samples = shapes.sample(18)

colorschemes = %i(paired10 brbg10 piyg10 prgn10 puor10)
colorscheme = colorschemes.sample

Graph do
  global layout:'neato', overlap:false
  loerm_samples.each_cons(2).with_index(1) do |loerms, i|
    route loerms.first => loerms.last
    loerms.each_with_index do |loerm, j|
      node loerm, {:shape => shape_samples[2 * (i + j - 1)], :colorscheme=> colorscheme,:style => :filled, fillcolor: i%11 }
    end
  end
  route loerm_samples.last => loerm_samples.first
  date_sufix = DateTime.now.strftime("%Y%m%d_%H%M%S")
  save("loerm_#{date_sufix}".to_sym, :png)
end

出力1回目

loerm_20140820_222718.png

出力2回目

loerm_20140820_222719.png

出力3回目

loerm_20140820_222720.png

3
3
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
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?