Skip to content

Instantly share code, notes, and snippets.

@melborne
Created October 2, 2012 01:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save melborne/3815566 to your computer and use it in GitHub Desktop.
Save melborne/3815566 to your computer and use it in GitHub Desktop.
Gviz sample: Tokyo Metro with m_station data of 駅.jp
require "gviz"
colors = [[:Ruby, '#C80000'], [:Lisp, '#FF286F'], [:Java, '#FF7F00'], [:Perl, '#C8FF00'], [:"Java\nScript", '#7FC800'], [:Python, '#00FF32'], [:C, '#007FFF'], [:Haskell, '#0A28F0'], [:PHP, '#6F00FF'], [:Erlang, '#B400FF'], [:'C++', '#B46496'], [:Scala, '#FF281E']]
Graph do
nodes shape:'circle', width:1.6, penwidth:20, fontname:'Futura', fontsize:26
colors.each_with_index do |(name, color), i|
subgraph do
global color:'white'
node :"#{i}", label:name, color:color
end
end
save(:lang, :png)
end
require "gviz"
colors = [[:Rb, '#C80000'], [:Lp, '#FF286F'], [:Jv, '#FF7F00'], [:Pl, '#C8FF00'], [:Js, '#7FC800'], [:Py, '#00FF32'], [:C, '#007FFF'], [:Hs, '#0A28F0'], [:Ph, '#6F00FF'], [:Er, '#B400FF'], [:'Cp', '#B46496'], [:Sc, '#FF281E']]
Graph do
nodes shape:'circle', width:0.9, penwidth:16, fontname:'Futura', fontsize:24
colors.each_with_index do |(name, color), i|
subgraph do
global color:'white'
node "#{i}".intern, label:name, color:color
end
end
save(:lang2, :png)
end
# encoding: UTF-8
require "gviz"
colors = [["銀座線", "#f39700"], ["丸ノ内線", "#e60012"], ["日比谷線", "#9caeb7"], ["東西線", "#00a7db"], ["千代田線", "#009944"], ["有楽町線", "#d7c447"], ["半蔵門線", "#9b7cb6"], ["南北線", "#00ada9"], ["副都心線", "#bb641d"], ["浅草線", "#e85298"], ["三田線", "#0079c2"], ["新宿線", "#6cbb5a"], ["大江戸線", "#b6007a"], ["荒川線", "#7aaa16"], ["舎人ライナー", "#999999"]]
marks = %w(G M H T C Y Z N F A I S E).map(&:intern)
logodata = marks.zip(colors).map(&:flatten) # => [[:G, "銀座線", "#f39700"], [:M, "丸ノ内線", "#e60012"], [:H, "日比谷線", "#9caeb7"], [:T, "東西線", "#00a7db"], [:C, "千代田線", "#009944"], [:Y, "有楽町線", "#d7c447"], [:Z, "半蔵門線", "#9b7cb6"], [:N, "南北線", "#00ada9"], [:F, "副都心線", "#bb641d"], [:A, "浅草線", "#e85298"], [:I, "三田線", "#0079c2"], [:S, "新宿線", "#6cbb5a"], [:E, "大江戸線", "#b6007a"]]
Graph do
nodes shape:'circle', penwidth:16, fontname:'Futura', fontsize:24
logodata.each do |id, line, color|
subgraph do
global label:line, fontname:'Hiragino Maru Gothic Pro', labelloc:'b', color:'white'
node id, color:color
end
end
save(:logo, :png)
end
# encoding: UTF-8
require "csv"
require "gviz"
header, *data = CSV.read('m_station.csv')
metrodata = data.select { |d| d[7].match(/東京メトロ|東京都交通局/) }
.group_by { |d| d[8] }
colors = [["銀座線", "#f39700"], ["丸ノ内線", "#e60012"], ["日比谷線", "#9caeb7"], ["東西線", "#00a7db"], ["千代田線", "#009944"], ["有楽町線", "#d7c447"], ["半蔵門線", "#9b7cb6"], ["南北線", "#00ada9"], ["副都心線", "#bb641d"], ["浅草線", "#e85298"], ["三田線", "#0079c2"], ["新宿線", "#6cbb5a"], ["大江戸線", "#b6007a"], ["荒川線", "#7aaa16"], ["舎人ライナー", "#999999"]]
flatdata = metrodata.values.flatten(1)
lon_minmax = flatdata.map { |d| d[11].to_f }.minmax
lat_minmax = flatdata.map { |d| d[12].to_f }.minmax
lon_range = Range.new(*lon_minmax) # => 139.612434..139.958972
lat_range = Range.new(*lat_minmax) # => 35.586859..35.814544
Graph(:Metro) do
global label:'Metro of Tokyo', size:16, layout:'neato'
edges arrowhead:'none', penwidth:3
nodes style:'filled', fontcolor:'white'
metrodata.each do |line, stations|
stlength = stations.length
stations.each.with_index(1) do |st, i|
st_id, st_name, st_seq = st.values_at(2, 9, 4)
st_id = st_id.intern
next_id = "#{st_seq.to_i+1}".intern
color = (c = colors.detect { |ln, c| line.match /#{ln}/ }) ? c[1] : "#999999"
pos_x = st[11].to_f.norm(lon_range, 1000..5000).round # 10..60 for svg
pos_y = st[12].to_f.norm(lat_range, 1000..5000).round
node st_id, label:st_name, fillcolor:color+'aa', pos:"#{pos_x},#{pos_y}!"
case st_id
when :'2800220' # 中野坂上
edge [st_id, :'2800226'].join('_'), color:color # 中野坂上 => 中野新橋
when :'2800225' # 荻窪
next
end
edge [st_id, next_id].join('_'), color:color if i < stlength
end
end
save(:metro, :svg)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment