Class: TechnicalGraph

Inherits:
Object
  • Object
show all
Defined in:
lib/technical_graph.rb

Overview

options parameters: :width - width of image :height - height of image :x_min, :x_max, :y_min, :y_max - default or fixed ranges :xy_behaviour:

  • :default - use them as default ranges

  • :fixed - ranges will not be changed during addition of layers

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = { }) ⇒ TechnicalGraph

Returns a new instance of TechnicalGraph.



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/technical_graph.rb', line 23

def initialize(options = { })
  @options = options

  @log_device = options[:log_device] || STDOUT
  @logger = Logger.new(@log_device)
  @logger.level = options[:log_level] || Logger::INFO

  @data_processor = GraphDataProcessor.new(self)
  @image_drawer = GraphImageDrawer.new(self)
  @axis = GraphAxis.new(self)
  @layers = Array.new
end

Instance Attribute Details

#axisObject (readonly)

Returns the value of attribute axis.



36
37
38
# File 'lib/technical_graph.rb', line 36

def axis
  @axis
end

#data_processorObject (readonly)

Returns the value of attribute data_processor.



36
37
38
# File 'lib/technical_graph.rb', line 36

def data_processor
  @data_processor
end

#image_drawerObject (readonly)

Returns the value of attribute image_drawer.



36
37
38
# File 'lib/technical_graph.rb', line 36

def image_drawer
  @image_drawer
end

#layersObject (readonly)

Returns the value of attribute layers.



36
37
38
# File 'lib/technical_graph.rb', line 36

def layers
  @layers
end

#loggerObject (readonly)

Returns the value of attribute logger.



36
37
38
# File 'lib/technical_graph.rb', line 36

def logger
  @logger
end

#optionsObject (readonly)

Returns the value of attribute options.



36
37
38
# File 'lib/technical_graph.rb', line 36

def options
  @options
end

Instance Method Details

#add_layer(data = [], options = { }) ⇒ Object

Add new data layer to layer array



49
50
51
52
53
54
# File 'lib/technical_graph.rb', line 49

def add_layer(data = [], options = { })
  t = Time.now
  @layers << DataLayer.new(data, options, self)
  logger.debug "layer added, size #{data.size}"
  logger.debug " TIME COST #{Time.now - t}"
end

#best_output_formatObject

Best output image format, used for testing



39
40
41
42
43
44
45
46
# File 'lib/technical_graph.rb', line 39

def best_output_format
  if options[:drawer_class] == :rasem
    return 'svg'
  end
  if options[:drawer_class] == :rmagick
    return 'png'
  end
end

#pre_render(ext) ⇒ Object

You don’t have to run this



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
116
117
# File 'lib/technical_graph.rb', line 90

def pre_render(ext)
  case ext
    when 'svg', 'svgz' then
      @options[:drawer_class] = :rasem
      render

    when 'png' then
      if gem_available?('rmagick')
        # rmagick is at the moment the best solution
        @options[:drawer_class] = :rmagick
      else
        @options[:drawer_class] = :chunky_png
      end
      render

    when 'jpeg', 'jpg', 'bmp', 'gif' then
      if rmagick_installed?
        @options[:drawer_class] = :rmagick
        render
      else
        raise Gem::LoadError
      end

    else
      raise ArgumentError

  end
end

#renderObject

Create graph



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/technical_graph.rb', line 57

def render
  @layers.each do |l|
    @data_processor.process_data_layer(l)
  end

  # recalculate ranges if needed
  @image = @image_drawer.crate_blank_graph_image

  # draw axis
  @axis.render_on_image(@image)
  # draw layers
  @layers.each do |l|
    # drawing
    @image_drawer.render_data_layer(l)
  end
  # draw legend
  @image_drawer.render_data_legend
end

#save_to_file(filename) ⇒ Object

Render and save graph to a file



77
78
79
80
81
# File 'lib/technical_graph.rb', line 77

def save_to_file(filename)
  ext = File.extname(filename).gsub(/^\./, '')
  pre_render(ext)
  @image_drawer.save_to_file(filename)
end

#to_format(ext) ⇒ Object

Render and return graph string



84
85
86
87
# File 'lib/technical_graph.rb', line 84

def to_format(ext)
  pre_render(ext)
  @image_drawer.to_format(ext)
end