9
Go to list of users who liked
8
Share on X(Twitter)
Share on Facebook
More than 5 years have passed since last update.
Ruby | Gvizでインデント構造のテキストをマインドマップに変換する
概要
Gvizでインデント構造のテキストをマインドマップに変換します。
利用ライブラリ
利用ライブラリについては、下記記事参照
サンプルコード
require'familyable'require'gottani_core'require"gviz"text=<<-EOSRuby スクリプト言語 影響 From Perl Lisp Smalltalk To CoffeeScript Elixir Author まつもとゆきひろ Matz 松江 Heroku Web Ruby on Rails Author DHH Products Redmine GitLab Sinatra DevOps Vagrant Author M.Hashimoto Provider Virtual Box VMWare Workstation Fusion AWS Chef Puppet 特徴 楽しい すべてがオブジェクト 可読性の高さ 動的言語 強力なメタプログラミング DSL作成の容易さEOSsp2=Gottani::Space2.newindent_text=sp2.space2_to_common(text)indexed_indent_text=indent_text.reverse.map.with_index{|e,i|e.merge({id:i})}moduleFamilyableclassPersonattr_accessor:value,:levelendendi=0persons=indexed_indent_text.reduce([])do|persons,person|parent=indexed_indent_text[(i+1)..-1].find{|e|e[:level]==person[:level]-1}parent_ids=parent.nil??[]:[parent[:id]]person=Familyable::Person.new(id:i,parent_ids:parent_ids).tapdo|e|e.value=person[:value]e.level=person[:level]endpersons<<personi+=1personsendsorted_persons=persons.reversefamily=Familyable::Family.new(family:persons)Graphdogloballayout:'twopi',overlap:false,splines: :curvednodesfontname:'MS GOTHIC',shape: :none,style:'filled'edgesdir: :none# ノードの定義sorted_persons.each{|person|node:"person#{person.id}",{label:person.value}}# エッジの定義sorted_persons.eachdo|person|parents=family.get_parents(person)nextifparents.empty?parent=parents.firstoptions={}options[:penwidth]=6ifparent.level==0edge:"person#{parent.id}_person#{person.id}",optionsend# *** 根のランク, スタイル定義 ***min_id=persons.find{|e|e.level==0}.idmin_person_key=:"person#{min_id}"rank:min,[min_person_key]nodemin_person_key,{shape:'pentagon',fontsize:80,fillcolor: :red}# *** 根以外のノードのランク定義 ***ranked_persons=sorted_persons.group_by{|e|e.level}.select{|key,value|key.nonzero?}ranked_persons.eachdo|rank,each_rank_persons|each_rank_persons.map{|e|node:"person#{e.id}",{fontsize:40}}ifrank==1persons=each_rank_persons.map{|e|"person#{e.id}".to_sym}rank(:same,persons)endsave:"givz_mindmap",:pngend出力
参照
- Gviz gem
https://github.com/melborne/Gviz - tbpgr_utils gem
https://github.com/tbpgr/tbpgr_utils - gottani gem
https://github.com/tbpgr/gottani
関連記事
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme
