Module: IRuby::Output::HTML

Defined in:
lib/iruby/output/html.rb

Defined Under Namespace

Modules: Gmaps, WordCloud

Class Method Summary collapse

Class Method Details

.chart_bar(o) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/iruby/output/html.rb', line 51

def self.chart_bar(o)
  data=o.delete(:data)
  title=o.delete(:title)
  size=o.delete(:size) || 300

  klass = o.delete(:stacked) ? Gruff::StackedBar : Gruff::Bar
  g = klass.new(size)

  if labels=o.delete(:labels)
    if ! labels.respond_to?(:keys)
      labels = Hash[labels.map.with_index{|v,k| [k,v]}]
    end
    g.labels = labels
  end

  g.title = title if title
  data.each do |data|
    g.data(data[0], data[1])
  end
  image(g.to_blob)

end

.chart_pie(o) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/iruby/output/html.rb', line 39

def self.chart_pie(o)
  data=o.delete(:data)
  title=o.delete(:title)
  size=o.delete(:size) || 300
  g = Gruff::Pie.new(size)
  g.title = title if title
  data.each do |data|
    g.data(data[0], data[1])
  end
  image(g.to_blob)
end

.image(image) ⇒ Object



34
35
36
37
# File 'lib/iruby/output/html.rb', line 34

def self.image(image)
  data = image.respond_to?(:to_blob) ? image.to_blob : image
  "<img src='data:image/png;base64,#{Base64.encode64(data)}'>"
end

.table(data) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/iruby/output/html.rb', line 6

def self.table(data)
  #
  # data = {a: 1, b:2}

  if data.respond_to?(:keys)
    d = data
  else
    d = data
  end

  r = "<table>"
  if d.respond_to?(:keys) # hash
    columns = [0,1]
  else
    columns = d.first.keys
    r << "<tr>#{columns.map{|c| "<th>#{c}</th>"}.join("\n")}</tr>"
  end
  d.each{|row|
    r << "<tr>"
    columns.each{|column|
      r << "<td>#{row[column]}</td>"
    }
    r << "</tr>"
  }
  r << "</table>"
  r
end