class Charty::Backends::Gruff

Public Class Methods

new() click to toggle source
# File lib/charty/backends/gruff.rb, line 14
def initialize
  @plot = ::Gruff
end
prepare() click to toggle source
# File lib/charty/backends/gruff.rb, line 9
def prepare
  require 'gruff'
end

Public Instance Methods

label(x, y) click to toggle source
# File lib/charty/backends/gruff.rb, line 18
def label(x, y)
end
old_style_render(context, filename="") click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/charty/backends/gruff.rb, line 25
def render_layout(layout)
  raise NotImplementedError
end
series=(series) click to toggle source
# File lib/charty/backends/gruff.rb, line 21
def series=(series)
  @series = series
end