Module: Rabbit::Parser::Ext::Charty

Includes:
GetText
Defined in:
lib/rabbit/parser/ext/charty.rb

Constant Summary

Constants included from GetText

GetText::DOMAIN

Class Method Summary collapse

Methods included from GetText

included

Class Method Details

.make_image(path, prop, logger) ⇒ Object


26
27
28
29
30
31
32
33
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
# File 'lib/rabbit/parser/ext/charty.rb', line 26

def make_image(path, prop, logger)
  require "charty"
  backend = prop["backend"]
  ::Charty::Backends.use(backend) if backend
  font_family = prop["font-family"]
  if font_family and backend == "pyplot"
    default_font_family = ::Matplotlib.rcParams["font.sans-serif"]
    ::Matplotlib.rcParams["font.sans-serif"] =
      [font_family, *default_font_family]
  end
  data = CSV.read(path, headers: true, converters: :all)
  type = prop["type"]
  case type
  when "bar"
    plotter = ::Charty.bar_plot(data: data,
                                x: prop["x"],
                                y: prop["y"],
                                color: prop["color"])
  when "line"
    plotter = ::Charty.line_plot(data: data,
                                 x: prop["x"],
                                 y: prop["y"],
                                 color: prop["color"])
  when "scatter"
    plotter = ::Charty.scatter_plot(data: data,
                                    x: prop["x"],
                                    y: prop["y"],
                                    color: prop["color"])
  else
    raise ArgumentError, "charty: unsupported type: #{type.inspect}"
  end
  image_file = Tempfile.new(["rabbit-image-charty", ".svg"])
  plotter.save(image_file.path)
  if font_family and backend == "pyplot"
    ::Matplotlib.rcParams["font.sans-serif"] = default_font_family
  end
  image_file
end