class Charty::Backends::Bokeh

Public Class Methods

new() click to toggle source
# File lib/charty/backends/bokeh.rb, line 12
def initialize
  @plot = PyCall.import_module('bokeh.plotting')
end
prepare() click to toggle source
# File lib/charty/backends/bokeh.rb, line 7
def prepare
  require 'pycall'
end

Public Instance Methods

old_style_render(context, filename) click to toggle source
# File lib/charty/backends/bokeh.rb, line 20
def old_style_render(context, filename)
  plot = plot(context)
  save(plot, context, filename)
  PyCall.import_module('bokeh.io').show(plot)
end
old_style_save(plot, context, filename) click to toggle source
# File lib/charty/backends/bokeh.rb, line 26
def old_style_save(plot, context, filename)
  if filename
    PyCall.import_module('bokeh.io').save(plot, filename)
  end
end
plot(context) click to toggle source
# File lib/charty/backends/bokeh.rb, line 32
def plot(context)
  #TODO To implement boxplot, bublle, error_bar, hist.

  plot = @plot.figure(title: context&.title)
  plot.xaxis[0].axis_label = context&.xlabel
  plot.yaxis[0].axis_label = context&.ylabel

  case context.method
  when :bar
    context.series.each do |data|
      diffs = data.xs.to_a.each_cons(2).map {|n, m| (n - m).abs }
      width = diffs.min * 0.8
      plot.vbar(data.xs.to_a, width, data.ys.to_a)
    end

  when :barh
    context.series.each do |data|
      diffs = data.xs.to_a.each_cons(2).map {|n, m| (n - m).abs }
      height = diffs.min * 0.8
      plot.hbar(data.xs.to_a, height, data.ys.to_a)
    end

  when :boxplot
    raise NotImplementedError

  when :bubble
    raise NotImplementedError

  when :curve
    context.series.each do |data|
      plot.line(data.xs.to_a, data.ys.to_a)
    end

  when :scatter
    context.series.each do |data|
      plot.scatter(data.xs.to_a, data.ys.to_a)
    end

  when :error_bar
    raise NotImplementedError

  when :hist
    raise NotImplementedError
  end
  plot
end
series=(series) click to toggle source
# File lib/charty/backends/bokeh.rb, line 16
def series=(series)
  @series = series
end