Class: GraphImageDrawerRasem
- Inherits:
-
Object
- Object
- GraphImageDrawerRasem
- Includes:
- GraphImageDrawerModule
- Defined in:
- lib/technical_graph/graph_image_drawer_rasem.rb
Instance Attribute Summary
Attributes included from GraphImageDrawerModule
Instance Method Summary collapse
-
#axis(x_array, y_array, _options = { :color => 'black', :width => 1 }, render_labels = false, x_labels = [], y_labels = []) ⇒ Object
Draw both array axis.
-
#axis_labels(parameter_label, value_label, _options = { :color => 'black', :width => 1, :size => 20 }) ⇒ Object
Label for parameters and values.
-
#close ⇒ Object
Needed before saving?.
- #closed? ⇒ Boolean
-
#create_blank_image ⇒ Object
Initialize blank image.
- #legend(legend_data) ⇒ Object
- #render_data_layer(l, coords) ⇒ Object
-
#save(file) ⇒ Object
Save to file, convert when needed.
- #to_format(format) ⇒ Object
-
#ugly_convert(format) ⇒ Object
Ugly, save temporary file, convert, read, delete temp file.
Methods included from GraphImageDrawerModule
#deflate_string, #format_from_filename, #height, #initialize, #logger, #options, #random_filename, #to_png, #to_svg, #to_svgz, #truncate_string, #width, #x_axis, #y_axis
Instance Method Details
#axis(x_array, y_array, _options = { :color => 'black', :width => 1 }, render_labels = false, x_labels = [], y_labels = []) ⇒ Object
Draw both array axis
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/technical_graph/graph_image_drawer_rasem.rb', line 17 def axis(x_array, y_array, = { :color => 'black', :width => 1 }, render_labels = false, x_labels = [], y_labels = []) # for single axis x_array = [x_array] if not x_array.kind_of? Array y_array = [y_array] if not y_array.kind_of? Array _s = self @image.group :stroke => [:color], :stroke_width => [:width] do x_array.each_with_index do |x, i| line(x, 0, x, _s.height, { }) # labels label = x_labels[i] if render_labels and not label.nil? label = "#{_s.truncate_string % label}" text(x + 15, _s.height - 15, label, { }) end end y_array.each_with_index do |y, i| line(0, y, _s.width, y, { }) # labels label = y_labels[i] if render_labels and not label.nil? label = "#{_s.truncate_string % label}" text(15, y + 15, label, { }) end end end end |
#axis_labels(parameter_label, value_label, _options = { :color => 'black', :width => 1, :size => 20 }) ⇒ Object
Label for parameters and values
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/technical_graph/graph_image_drawer_rasem.rb', line 50 def axis_labels(parameter_label, value_label, = { :color => 'black', :width => 1, :size => 20 }) _s = self @image.group :stroke => [:color], :stroke_width => [:width] do text( (_s.width / 2).to_i, _s.height - 40, parameter_label, { 'font-size' => "#{[:size]}px" } ) text( (_s.height / 2).to_i, -40, value_label, { :transform => 'rotate(90 0,0)', 'font-size' => "#{[:size]}px" } ) end end |
#close ⇒ Object
Needed before saving?
126 127 128 129 |
# File 'lib/technical_graph/graph_image_drawer_rasem.rb', line 126 def close @image.close if not closed? @closed = true end |
#closed? ⇒ Boolean
131 132 133 |
# File 'lib/technical_graph/graph_image_drawer_rasem.rb', line 131 def closed? @closed end |
#create_blank_image ⇒ Object
Initialize blank image
12 13 14 |
# File 'lib/technical_graph/graph_image_drawer_rasem.rb', line 12 def create_blank_image @image = Rasem::SVGImage.new(drawer.width, drawer.height) end |
#legend(legend_data) ⇒ Object
114 115 116 117 118 119 120 121 122 123 |
# File 'lib/technical_graph/graph_image_drawer_rasem.rb', line 114 def legend(legend_data) _s = self @image.group :stroke_width => 1, :stroke => '' do legend_data.each do |l| circle(l[:x], l[:y], 2, { :stroke => l[:color], :fill => l[:color] }) text(l[:x] + 5, l[:y], l[:label], { :stroke => l[:color], :fill => l[:color] }) end end end |
#render_data_layer(l, coords) ⇒ Object
67 68 69 70 71 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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/technical_graph/graph_image_drawer_rasem.rb', line 67 def render_data_layer(l, coords) _s = self _l = l _coords = coords # value labels if l.value_labels t = Time.now @image.group :stroke => _s.[:axis_color], :stroke_width => 1 do _coords.each do |c| string_label = "#{_s.truncate_string % c[:dy]}" text( c[:ax] + 5, c[:ay], string_label ) end end logger.debug "labels" logger.debug " TIME COST #{Time.now - t}" end t = Time.now # lines and dots @image.group :stroke => l.color, :stroke_width => 1 do _coords.each do |c| # additional circle circle(c[:ax], c[:ay], 2, { :fill => l.color }) circle(c[:bx], c[:by], 2, { :fill => l.color }) # line line( c[:ax], c[:ay], c[:bx], c[:by], { } ) _s.drawer.post_dot_drawn(c[:ax], c[:ay]) _s.drawer.post_dot_drawn(c[:bx], c[:by]) end end logger.debug "dots and lines" logger.debug " TIME COST #{Time.now - t}" end |
#save(file) ⇒ Object
Save to file, convert when needed
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/technical_graph/graph_image_drawer_rasem.rb', line 136 def save(file) close format = format_from_filename(file) case format when 'svg' then string = to_svg when 'svgz' then string = to_svgz else # ugly hack, save to svg and then convert using image magick tmp_file = file.gsub(/#{format}/, 'svg') # save to svg save(tmp_file) # convert `convert "#{tmp_file}" "#{file}"` return end File.open(file, 'w') do |f| f << string end end |
#to_format(format) ⇒ Object
161 162 163 164 165 166 167 168 169 |
# File 'lib/technical_graph/graph_image_drawer_rasem.rb', line 161 def to_format(format) close return @image.output if format == 'svg' return to_svgz if format == 'svgz' #raise 'Not implemented' if not format == 'svg' return ugly_convert(format) end |
#ugly_convert(format) ⇒ Object
Ugly, save temporary file, convert, read, delete temp file
172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/technical_graph/graph_image_drawer_rasem.rb', line 172 def ugly_convert(format) # create temp file tmp_file = File.join(Dir.tmpdir, "#{random_filename}.#{format}") save(tmp_file) # read content contents = open(tmp_file, "rb") { |io| io.read } # remove temp file File.delete(tmp_file) # return content contents end |