gihyo.jpã®ãå ·ä½ä¾ã§å¦ã¶ï¼æ å ±å¯è¦åã®ãã¯ããã¯ãã®ããã°ã©ã ãåæã«Rubyã§æ¸ãæãã¦ã¿ãã
gihyo.jpã§情報可視化の特集ããã£ã¦ã¦ããããåå¼·ã«ãªã£ã¦ããã£ããã§ããã©ã
ããã°ã©ã ãJavaã§æ¸ããã¦ã¦ãå人çã«Rubyã§ããããã£ããã§ã
åå¼·ã¤ãã§ã«ããã°ã©ã ãRubyã§æ¸ãæãã¦ã¿ã¾ããã
ãã®Rubyã®ããã°ã©ã ã¯Githubã«ããã¦ããã®ã§ããèªç±ã«ã使ããã ããã
以ä¸ã®ãªã³ã¯ã®downloadããzipãã¾ãã¯tarã§åºãããã®ããã¦ã³ãã¼ãã§ãã¾ãã
GitHub - ombran/gihyojp-visualization-ruby: The Ruby version of the information visualization introduced by gihyo.jp.(unofficial)
ã¨ã¾ãããããã«ããã§çµããã®ã¯ã²ã©ãã®ã§ãããã°ã©ã ã®ç°¡åãªèª¬æããã¾ãã
ä»åã¯visualization2ã®ãã©ã«ãã«ããããã°ã©ã ã®èª¬æããã¾ãã
ãã ãããã§Rubyã¯ã¤ã³ã¹ãã¼ã«æ¸ã¿ã§ãããã®ã¨ãã¾ãã
Rubyã®ã¤ã³ã¹ãã¼ã«ã¨ãã®è¨äºã¯ããããªã¨ããã«ãããã§ãã£ã¡ãåèã«ãã¦ãã ããã
Linuxã¨ããªãå
ã
ã¤ã³ã¹ãã¼ã«æ¸ã¿ã ã£ãããã¾ããã
visualization2ã®èª¬æ
å
è¨äºã ã¨第2回ã«ãããå
容ã«ãªãã¾ãã
ãã®ããã°ã©ã ã¯ç°¡åã«è¨ãã°ãè²éåãæçè·é¢æ³ã«åºã¥ãã¦é層çã¯ã©ã¹ã¿ãªã³ã°ãè¡ããã®ã§ãã
詳ãããã¨ã¯å
è¨äºãè¦ã¦ããã£ãã»ããããããããã¨æãã¾ãã
visualization2ãã©ã«ãå 容
visualization2ã®ãã©ã«ãå 容ã¯ä»¥ä¸ã®ããã«ãªã£ã¦ãã¾ãã
$ tree visualization2 visualization2
-- Demo.rb | |
-- Visualization | |
-- Cluster.rb | |
-- ClusterBuilder.rb | |
-- DistanceEvaluator.rb | |
-- Item.rb | |
-- MultiVector.rb | |
-- NearestDistanceEvaluator.rb | |
`-- Node.rb |
æ¡å¼µåã®éãã¯ããã¾ãããå
è¨äºã®ããã°ã©ã ã®ãã¡ã¤ã«åã¨å
容ã対å¿ããã¦ããã¾ãã
Visualization.rbãèªã¿è¾¼ããã¨ã ãã§ãVisualizationãã©ã«ã以ä¸ã®ãã¡ã¤ã«ãå
¨ã¦èªã¿è¾¼ããããã«ãã¦ããã¾ãã
å è¨äºã®ããã°ã©ã ã¨ã®éã
ããã°ã©ã ã®å
容èªä½ã¯ãã»ã©éãã¯ãªããã§ããã©ã
å
ããã°ã©ã ã¨å¤§ããéãã¨ããã¨ãã¦MultiVectorã¯ã©ã¹ã®å¼æ°ãããã·ã¥ã«ãªã£ã¦ãã¾ãã
# MultiVector.rb module Visualization class MultiVector # ãã¯ãã«çæ(å¼æ°ã¯ããã·ã¥) def initialize(hash={}) @data = {} hash.each do |key, value| @data[key] = value end end ... end
ããã¯ãã¯ãã«ã®æ¬¡å
æ°ãéããã®å士ã§ãã¯ã©ã¹ã¿ãªã³ã°ã§ããããã«ããããã§ãã
ã¡ãªã¿ã«ä»åã¯ãã¯ãã«ã®æ¬¡å
æ°ãå
¨ã¦åããªã®ã§ãã®å©ç¹ãå®æã§ãã¾ãããã
visualization6ã§ã¯ããã«ãã£ã¦å
è¨äºã®ããã°ã©ã ã§ç¨ãããã¦ããã¯ã©ã¹ãä¸ã¤åæ¸ã§ããããã«ãªã£ã¦ãã¾ãã
ã¾ãã¨ããããããã·ã¥ã®ã»ãã便å©ã ããã¨ã§ãæã£ã¦ããã¦ãã ããã
ãã®ä»ã®ã¯ã©ã¹ã§ã¯è¨èªã®éãã¯ããã¾ããå
容ã«ã»ã¨ãã©å¤åã¯ããã¾ããã
ãã ããå
¨ã¦ã®ã¯ã©ã¹ã¯Visualizationã¢ã¸ã¥ã¼ã«å
ã«å«ã¾ããããã«ãã¦ããã¾ãã
Javaã§ã®ããã±ã¼ã¸ã®ä»£ããã ã¨èãã¦ãã ããã
ãã¢ããã°ã©ã ã®å®è¡
ããã§ã¯ããã°ã©ã ã®ãã¢ãè¡ãDemoã¯ã©ã¹ã¯ä»¥ä¸ã®ããã«ãªãã¾ãã
# Demo.rb require File.dirname(__FILE__) + '/Visualization' class Demo include Visualization def run # å ¥åãã¼ã¿ä½æ input = [] color = Struct.new(:red, :green, :blue) input << Item.new("BLUE", colorToVector(color.new(0, 0, 255))) input << Item.new("CYAN", colorToVector(color.new(0, 255, 255))) input << Item.new("MAGENTA", colorToVector(color.new(255, 0, 255))) input << Item.new("ORANGE", colorToVector(color.new(255, 200, 0))) input << Item.new("PINK", colorToVector(color.new(255, 175, 175))) input << Item.new("RED", colorToVector(color.new(255, 0, 0))) # æçè·é¢æ³ã«åºã¥ãé層çã¯ã©ã¹ã¿ãªã³ã°ãæºå evaluator = NearestDistanceEvaluator.new builder = ClusterBuilder.new(evaluator) # ã¯ã©ã¹ã¿ãªã³ã°ãå®è¡ result = builder.build(input) # ã¯ã©ã¹ã¿ãªã³ã°çµæã表示 output(result, 0) end def colorToVector(c) # è²æåã3次å ã®ãã¯ãã«ã«å¤æ MultiVector.new({:red => c.red, :green => c.green, :blue => c.blue}) end def output(node, depth) # ã¤ã³ãã³ãã表示 depth.times do print " " end if (node.kind_of? Item) # æ«ç«¯ãã¼ããªãé ç®åã表示 puts node.getName elsif (node.kind_of? Cluster) # ã¯ã©ã¹ã¿ãªã"+"ã表示ããåãã¼ããå帰çã«è¡¨ç¤º puts "+" cluster = node output(cluster.getLeft, depth + 1) output(cluster.getRight, depth + 1) end end end demo = Demo.new demo.run
å
¥åãã¼ã¿ã®è²æ
å ±ã¯RGBã®å¤ããã®ã¾ã¾å
¥åãã¦ããã¾ãã
Demoã¯ã©ã¹ãå®è¡ããã¨ï¼ä»¥ä¸ã®åºåãå¾ããã¾ãã
$ ruby Demo.rb + + RED + MAGENTA + ORANGE PINK + BLUE CYAN
çµæã¯å
è¨äºã¨åãã«ãªã£ã¦ããã§ãããã°ã©ã ã¯åã£ã¦ãã¨æããã§ããã
ããä½ãééãçããã¾ãããæãã¦ããã ããã¨ãããããã§ãã
以ä¸
ä»åã¯ããã¾ã§ã§ãã
ã»ãã¨ã«ç°¡åãªèª¬æã§ç³ã訳ãªãã§ããã©ã
å
è¨äºã®ããã°ã©ã ã¨å¯¾å¿ããã¦è¦ã¦ããããã¨ããããããããªã¨æãã¾ãã
ä»ã®ããã°ã©ã ã«é¢ãã¦ã¯æ¬¡å以éã«èª¬æãã¾ãã