Class: Charty::Backends::Gruff

Inherits:
Object
  • Object
show all
Defined in:
lib/charty/backends/gruff.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGruff

Returns a new instance of Gruff.



14
15
16
# File 'lib/charty/backends/gruff.rb', line 14

def initialize
  @plot = ::Gruff
end

Class Method Details

.prepareObject



9
10
11
# File 'lib/charty/backends/gruff.rb', line 9

def prepare
  require 'gruff'
end

Instance Method Details

#label(x, y) ⇒ Object



18
19
# File 'lib/charty/backends/gruff.rb', line 18

def label(x, y)
end

#old_style_render(context, filename = "") ⇒ Object



29
30
31
32
# File 'lib/charty/backends/gruff.rb', line 29

def old_style_render(context, filename="")
  FileUtils.mkdir_p(File.dirname(filename))
  plot(@plot, context).write(filename)
end

#plot(plot, context) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/charty/backends/gruff.rb', line 34

def plot(plot, context)
  # case
  # when plot.respond_to?(:xlim)
  #   plot.xlim(context.range_x.begin, context.range_x.end)
  #   plot.ylim(context.range_y.begin, context.range_y.end)
  # when plot.respond_to?(:set_xlim)
  #   plot.set_xlim(context.range_x.begin, context.range_x.end)
  #   plot.set_ylim(context.range_y.begin, context.range_y.end)
  # end

  case context.method
  when :bar
    p = plot::Bar.new
    p.title = context.title if context.title
    p.x_axis_label = context.xlabel if context.xlabel
    p.y_axis_label = context.ylabel if context.ylabel
    context.series.each do |data|
      p.data(data.label, data.xs.to_a)
    end
    p
  when :barh
    p = plot::SideBar.new
    p.title = context.title if context.title
    p.x_axis_label = context.xlabel if context.xlabel
    p.y_axis_label = context.ylabel if context.ylabel
    labels = context.series.map {|data| data.xs.to_a}.flatten.uniq
    labels.each do |label|
      data_ys = context.series.map do |data|
        if data.xs.to_a.index(label)
          data.ys.to_a[data.xs.to_a.index(label)]
        else
          0
        end
      end
      p.data(label, data_ys)
    end
    p.labels = context.series.each_with_index.inject({}) do |attr, (data, i)|
      attr[i] = data.label
      attr
    end
    p
  when :box_plot
    # refs. https://github.com/topfunky/gruff/issues/155
    raise NotImplementedError
  when :bubble
    raise NotImplementedError
  when :curve
    p = plot::Line.new
    p.title = context.title if context.title
    p.x_axis_label = context.xlabel if context.xlabel
    p.y_axis_label = context.ylabel if context.ylabel
    context.series.each do |data|
      p.dataxy(data.label, data.xs.to_a, data.ys.to_a)
    end
    p
  when :scatter
    p = plot::Scatter.new
    p.title = context.title if context.title
    p.x_axis_label = context.xlabel if context.xlabel
    p.y_axis_label = context.ylabel if context.ylabel
    context.series.each do |data|
      p.data(data.label, data.xs.to_a, data.ys.to_a)
    end
    p
  when :error_bar
    # refs. https://github.com/topfunky/gruff/issues/163
    raise NotImplementedError
  when :hist
    p = plot::Histogram.new
    p.title = context.title if context.title
    p.x_axis_label = context.xlabel if context.xlabel
    p.y_axis_label = context.ylabel if context.ylabel
    if context.range_x
      p.minimum_bin = context.range_x.first
      p.maximum_bin = context.range_x.last
    end
    context.data.each do |data|
      p.data('', data.to_a)
    end
    p
  end
end

#render_layout(layout) ⇒ Object

Raises:

  • (NotImplementedError)


25
26
27
# File 'lib/charty/backends/gruff.rb', line 25

def render_layout(layout)
  raise NotImplementedError
end

#series=(series) ⇒ Object



21
22
23
# File 'lib/charty/backends/gruff.rb', line 21

def series=(series)
  @series = series
end