LoginSignup
2
2

More than 5 years have passed since last update.

Ruby | Gviz gem の Draw Module を活用する

Posted at

Ruby | Gviz gem の Draw Module を活用する

概要

Gviz gem の Draw Module を活用します。

ellipse(id, x:0, y:0, **attrs)

ellipse = 楕円
x, y Ruby2から導入されているキーワード引数。
attrs はオプション引数(キーワード引数と併用する場合は、アスタリスクを二つつける)。

id : ID
x : X軸位置
y : Y軸位置
attrs : その他の設定

circle(id, x:0, y:0, r:0.5, **attrs)

circle = 円

id : ID
x : X軸位置
y : Y軸位置
r : 半径
attrs : その他の設定

square(id, x:0, y:0, **attrs)

square = 四角

id : ID
x : X軸位置
y : Y軸位置
attrs : その他の設定

point(id, x:0, y:0, **attrs)

point = 点

id : ID
x : X軸位置
y : Y軸位置
attrs : その他の設定

line(id, from:[0,0], **attrs)

id : ID
from: 始点の x/y 座標
attrs : その他の設定(toはこれに含まれる)

attrs に to を設定しないとエラーが発生します。

サンプルコード

require 'gviz'

Graph do
  ellipse :ellipse11, x: 1, y: 1, label: :ellipse11
  ellipse :ellipse33, x: 3, y: 3, label: :ellipse33
  circle :circle13, x: 1, y: 3 , r: 0.75, label: :circle13
  circle :circle31, x: 3, y: 1 , r: 0.75, label: :circle31
  square :square15, x: 1, y: 5 , label: :square15
  square :square35, x: 3, y: 5 , label: :square35
  point :point17, x: 1, y: 7
  point :point37, x: 3, y: 7
  line :line1, from: [1, 1], to: [7, 7]
  line :line2, from: [1, 7], to: [7, 1]
  route ellipse11: :circle31
  route circle31: :ellipse33
  route ellipse33: :square35
  route square35: :square15
  route square15: :circle13
  route circle13: :ellipse11
  save(:draw, :png)
end

出力画像

draw.png

参照

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