Class: PRRD::Graph

Inherits:
Object
  • Object
show all
Defined in:
lib/prrd/graph.rb,
lib/prrd/graph/area.rb,
lib/prrd/graph/line.rb,
lib/prrd/graph/hrule.rb,
lib/prrd/graph/print.rb,
lib/prrd/graph/shift.rb,
lib/prrd/graph/vrule.rb,
lib/prrd/graph/colors.rb,
lib/prrd/graph/comment.rb,
lib/prrd/graph/textalign.rb,
lib/prrd/graph/definition.rb

Overview

PRRD Graph class

Defined Under Namespace

Classes: Area, Color, Comment, Definition, Hrule, Line, Print, Shift, Textalign, Vrule

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values = nil) ⇒ Graph

Constructor



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/prrd/graph.rb', line 21

def initialize(values = nil)
  @entities = {
    colors: [],
    definitions: [],
    areas: [],
    lines: [],
    prints: [],
    comments: [],
    hrules: [],
    vrules: [],
    shift: [],
    textalign: []
  }

  unless values.nil?
    values.each do |k, v|
      m = "#{k}=".to_sym
      next unless respond_to? m
      send m, v
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/prrd/graph.rb', line 54

def method_missing(m, *args, &block)
  entities = @entities.keys.map(&:to_s) + @entities.keys.map! { |e| e.to_s[0..-2] }
  ms = m.to_s.gsub 'add_', ''
  if entities.include? ms
    if ms[-1] == 's'
      @entities[ms.to_sym] = args[0]
    else
      @entities["#{ms}s".to_sym] << args[0]
    end

    return true
  end

  super
end

Instance Attribute Details

#baseObject

Returns the value of attribute base.



17
18
19
# File 'lib/prrd/graph.rb', line 17

def base
  @base
end

#borderObject

Returns the value of attribute border.



17
18
19
# File 'lib/prrd/graph.rb', line 17

def border
  @border
end

#databaseObject

Accessors



10
11
12
# File 'lib/prrd/graph.rb', line 10

def database
  @database
end

#endObject

Returns the value of attribute end.



12
13
14
# File 'lib/prrd/graph.rb', line 12

def end
  @end
end

#entitiesObject

Returns the value of attribute entities.



18
19
20
# File 'lib/prrd/graph.rb', line 18

def entities
  @entities
end

#full_size_modeObject

Returns the value of attribute full_size_mode.



14
15
16
# File 'lib/prrd/graph.rb', line 14

def full_size_mode
  @full_size_mode
end

#heightObject

Returns the value of attribute height.



14
15
16
# File 'lib/prrd/graph.rb', line 14

def height
  @height
end

#imgformatObject

Returns the value of attribute imgformat.



17
18
19
# File 'lib/prrd/graph.rb', line 17

def imgformat
  @imgformat
end

#legend_directionObject

Returns the value of attribute legend_direction.



16
17
18
# File 'lib/prrd/graph.rb', line 16

def legend_direction
  @legend_direction
end

#legend_positionObject

Returns the value of attribute legend_position.



16
17
18
# File 'lib/prrd/graph.rb', line 16

def legend_position
  @legend_position
end

#lower_limitObject

Returns the value of attribute lower_limit.



15
16
17
# File 'lib/prrd/graph.rb', line 15

def lower_limit
  @lower_limit
end

#no_legendObject

Returns the value of attribute no_legend.



16
17
18
# File 'lib/prrd/graph.rb', line 16

def no_legend
  @no_legend
end

#only_graphObject

Returns the value of attribute only_graph.



14
15
16
# File 'lib/prrd/graph.rb', line 14

def only_graph
  @only_graph
end

#pathObject

Accessors



10
11
12
# File 'lib/prrd/graph.rb', line 10

def path
  @path
end

#rigidObject

Returns the value of attribute rigid.



15
16
17
# File 'lib/prrd/graph.rb', line 15

def rigid
  @rigid
end

#startObject

Returns the value of attribute start.



12
13
14
# File 'lib/prrd/graph.rb', line 12

def start
  @start
end

#stepObject

Returns the value of attribute step.



12
13
14
# File 'lib/prrd/graph.rb', line 12

def step
  @step
end

#titleObject

Returns the value of attribute title.



13
14
15
# File 'lib/prrd/graph.rb', line 13

def title
  @title
end

#upper_limitObject

Returns the value of attribute upper_limit.



15
16
17
# File 'lib/prrd/graph.rb', line 15

def upper_limit
  @upper_limit
end

#vertical_labelObject

Returns the value of attribute vertical_label.



13
14
15
# File 'lib/prrd/graph.rb', line 13

def vertical_label
  @vertical_label
end

#watermarkObject

Returns the value of attribute watermark.



17
18
19
# File 'lib/prrd/graph.rb', line 17

def watermark
  @watermark
end

#widthObject

Returns the value of attribute width.



14
15
16
# File 'lib/prrd/graph.rb', line 14

def width
  @width
end

#zoomObject

Returns the value of attribute zoom.



17
18
19
# File 'lib/prrd/graph.rb', line 17

def zoom
  @zoom
end

Instance Method Details

#<<(object) ⇒ Object

Add an object

Parameters:

  • object (Object)


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
# File 'lib/prrd/graph.rb', line 72

def <<(object)
  if object.is_a? PRRD::Graph::Definition
    @entities[:definitions] << object
  elsif object.is_a? PRRD::Graph::Color
    @entities[:colors] << object
  elsif object.is_a? PRRD::Graph::Area
    @entities[:areas] << object
  elsif object.is_a? PRRD::Graph::Line
    @entities[:lines] << object
  elsif object.is_a? PRRD::Graph::Print
    @entities[:prints] << object
  elsif object.is_a? PRRD::Graph::Comment
    @entities[:comments] << object
  elsif object.is_a? PRRD::Graph::Hrule
    @entities[:hrules] << object
  elsif object.is_a? PRRD::Graph::Vrule
    @entities[:vrules] << object
  elsif object.is_a? PRRD::Graph::Shift
    @entities[:shift] << object
  elsif object.is_a? PRRD::Graph::Textalign
    @entities[:textalign] << object
  else
    fail 'Can not add this kind of object in PRRD::Graph'
  end
end

#check_databaseObject

Check database existence



50
51
52
# File 'lib/prrd/graph.rb', line 50

def check_database
  fail 'No database provided' if @database.nil?
end

#check_fileObject

Check image existence



45
46
47
# File 'lib/prrd/graph.rb', line 45

def check_file
  fail 'Image path is missing' if  @path.nil?
end

#generateString

Generate a graph

Returns:

  • (String)


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/prrd/graph.rb', line 100

def generate
  check_file
  check_database

  fail 'Definitions are missing' if @entities[:definitions].empty?

  cmd = []

  cmd << "#{PRRD.bin} graph #{@path}"

  cmd << "--start=#{@start}" unless @start.nil?
  cmd << "--end=#{@end}" unless @end.nil?
  cmd << "--step=#{@step}" unless @step.nil?

  cmd << "--title=\"#{@title}\"" unless @title.nil?
  cmd << "--vertical-label=\"#{@vertical_label}\"" unless @vertical_label.nil?
  cmd << "--width=#{@width}" unless @width.nil?
  cmd << "--height=#{@height}" unless @height.nil?
  cmd << '--only-graph' unless @only_graph.nil?
  cmd << '--full-size-mode' unless @full_size_mode.nil?
  cmd << "--lower-limit=#{@lower_limit}" unless @lower_limit.nil?
  cmd << "--upper-limit=#{@upper_limit}" unless @upper_limit.nil?
  cmd << '--rigid' unless @rigid.nil?
  cmd << "--no-legend" unless @no_legend.nil?
  cmd << "--legend-position=\"#{@legend_position}\"" unless @legend_position.nil?
  cmd << "--legend-direction=\"#{@legend_direction}\"" unless @legend_direction.nil?
  cmd << "--base=#{@base}" unless @base.nil?
  cmd << "--border=#{@border}" unless @border.nil?
  cmd << "--zoom=#{@zoom}" unless @zoom.nil?
  cmd << "--imgformat=\"#{@imgformat}\"" unless @imgformat.nil?
  cmd << "--watermark=\"#{@watermark}\"" unless @watermark.nil?

  @entities.each do |k, v|
    next if v.empty?
    v.each do |e|
      cmd << e.to_s
    end
  end

  # Exectute
  PRRD.execute cmd.join ' '
end