Class: GraphImageDrawerRmagick

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

Instance Attribute Summary

Attributes included from GraphImageDrawerModule

#drawer

Instance Method Summary collapse

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



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
113
114
115
116
117
118
119
120
121
# File 'lib/technical_graph/graph_image_drawer_rmagick.rb', line 86

def axis(x_array, y_array, _options = { :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

  plot_axis = axis_draw_object
  plot_axis.stroke(_options[:color])
  plot_axis.stroke_width(_options[:width])
  plot_axis_text = layer_no_stroke(plot_axis)
  plot_axis_text.fill(_options[:color])

  x_array.each_with_index do |x, i|
    plot_axis.line(x, 0, x, height)

    # labels
    label = x_labels[i]
    if render_labels and not label.nil?
      label = "#{truncate_string % label}"
      plot_axis_text.text(x + 15, height - 15, label)
    end
  end

  y_array.each_with_index do |y, i|
    plot_axis.line(0, y, width, y)

    # labels
    label = y_labels[i]
    if render_labels and not label.nil?
      label = "#{truncate_string % label}"
      plot_axis_text.text(15, y + 15, label)
    end
  end

  plot_axis.draw(@image)
  plot_axis_text.draw(@image)
end

#axis_draw_objectObject

Layer used for drawing axis



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

def axis_draw_object
  plot_axis = Magick::Draw.new

  plot_axis.stroke_antialias(options[:antialias])
  plot_axis.text_antialias(options[:antialias])
  plot_axis.fill_opacity(1.0)
  plot_axis.stroke(options[:axis_color])
  plot_axis.stroke_opacity(1.0)
  plot_axis.stroke_width(1.0)
  plot_axis.stroke_linecap('square')
  plot_axis.stroke_linejoin('miter')

  plot_axis.pointsize(options[:axis_font_size])
  plot_axis.font_family('helvetica')
  plot_axis.font_style(Magick::NormalStyle)
  plot_axis.text_align(Magick::LeftAlign)
  plot_axis.text_undercolor(options[:background_color])

  return plot_axis
end

#axis_labels(parameter_label, value_label, _options = { :color => 'black', :width => 1, :size => 20 }) ⇒ Object

Label for parameters and values



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/technical_graph/graph_image_drawer_rmagick.rb', line 124

def axis_labels(parameter_label, value_label, _options = { :color => 'black', :width => 1, :size => 20 })
  if options[:x_axis_label].to_s.size > 0
    plot = axis_labels_draw_object
    plot.stroke(_options[:color])
    plot.stroke_width(0.0)
    plot.fill_opacity(1.0)
    plot.stroke_opacity(0.0)

    plot.text(
      (width / 2).to_i,
      height - 40,
      options[:x_axis_label].to_s
    )
    plot.draw(@image)
  end


  if options[:y_axis_label].to_s.size > 0
    plot = axis_labels_draw_object
    plot.stroke(_options[:color])
    plot.stroke_width(0.0)
    plot.fill_opacity(1.0)
    plot.stroke_opacity(0.0)
    plot = plot.rotate(90)

    plot.text(
      (height / 2).to_i,
      -40,
      options[:y_axis_label].to_s
    )
    plot.draw(@image)
  end
end

#axis_labels_draw_objectObject

Layer used for drawing main labels (parameter, value)



45
46
47
48
49
50
51
52
# File 'lib/technical_graph/graph_image_drawer_rmagick.rb', line 45

def axis_labels_draw_object
  draw = axis_draw_object
  draw.pointsize(options[:axis_label_font_size])
  draw.font_style(Magick::NormalStyle)
  draw.text_align(Magick::CenterAlign)

  return draw
end

#closeObject



212
213
214
215
# File 'lib/technical_graph/graph_image_drawer_rmagick.rb', line 212

def close
  # only for compatibility
  @closed = true
end

#closed?Boolean

Returns:

  • (Boolean)


217
218
219
# File 'lib/technical_graph/graph_image_drawer_rmagick.rb', line 217

def closed?
  @closed
end

#create_blank_imageObject



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/technical_graph/graph_image_drawer_rmagick.rb', line 8

def create_blank_image
  @image = Magick::ImageList.new
  @image.new_image(
    width,
    height,
    Magick::HatchFill.new(
      options[:background_color],
      options[:background_hatch_color]
    )
  )

  return @image
end

#layer_draw_object(layer) ⇒ Object

Layer used for drawing chart, lines and dots



62
63
64
65
66
67
# File 'lib/technical_graph/graph_image_drawer_rmagick.rb', line 62

def layer_draw_object(layer)
  draw = axis_draw_object
  draw.fill(layer.color)
  draw.stroke(layer.color)
  return draw
end

#layer_no_fill(_draw) ⇒ Object

Create no-fill draw object



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

def layer_no_fill(_draw)
  draw = _draw.clone
  draw.fill_opacity(0.0)
  return draw
end

#layer_no_stroke(_draw) ⇒ Object

Create no-stroke draw object



70
71
72
73
74
75
# File 'lib/technical_graph/graph_image_drawer_rmagick.rb', line 70

def layer_no_stroke(_draw)
  draw = _draw.clone
  draw.stroke_opacity(0.0)
  draw.stroke_width(0.0)
  return draw
end

#layer_value_labels_draw_object(layer) ⇒ Object

Layer used for drawing value labels



55
56
57
58
59
# File 'lib/technical_graph/graph_image_drawer_rmagick.rb', line 55

def layer_value_labels_draw_object(layer)
  draw = axis_draw_object
  draw.pointsize(options[:layers_font_size])
  return draw
end

#legend(legend_data) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/technical_graph/graph_image_drawer_rmagick.rb', line 192

def legend(legend_data)
  legend_text_offset = (options[:legend_font_size] / 2.0).round - 4

  legend_data.each do |l|
    plot = axis_draw_object
    plot_text = layer_no_stroke(plot)

    plot.fill(l[:color])
    plot.stroke(l[:color])
    plot_text.fill(l[:color])
    plot_text.pointsize(options[:legend_font_size])

    plot.circle(l[:x], l[:y], l[:x] + 2, l[:y])
    plot_text.text(l[:x] + 5, l[:y] + legend_text_offset, l[:label])

    plot.draw(@image)
    plot_text.draw(@image)
  end
end

#render_data_layer(l, coords) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/technical_graph/graph_image_drawer_rmagick.rb', line 158

def render_data_layer(l, coords)
  # value labels
  if l.value_labels
    plot = layer_no_stroke(layer_value_labels_draw_object(l))
    coords.each do |c|
      string_label = "#{truncate_string % c[:dy]}"
      plot.text(
        c[:ax] + 5, c[:ay],
        string_label
      )
    end
    plot.draw(@image)
  end

  # lines and dots
  plot = layer_draw_object(l)
  coords.each do |c|
    # additional circle
    plot.circle(c[:ax], c[:ay], c[:ax] + 2, c[:ay])
    plot.circle(c[:bx], c[:by], c[:bx] + 2, c[:by])

    # line
    plot.line(
      c[:ax], c[:ay],
      c[:bx], c[:by]
    )

    drawer.post_dot_drawn(c[:ax], c[:ay])
    drawer.post_dot_drawn(c[:bx], c[:by])
  end
  plot.draw(@image)
end

#save(file) ⇒ Object

Save output to file



222
223
224
225
226
227
228
229
# File 'lib/technical_graph/graph_image_drawer_rmagick.rb', line 222

def save(file)
  t = Time.now

  @image.write(file)

  logger.debug "saving image"
  logger.debug " TIME COST #{Time.now - t}"
end

#to_format(format) ⇒ Object

Export image



232
233
234
235
236
237
238
239
240
241
242
# File 'lib/technical_graph/graph_image_drawer_rmagick.rb', line 232

def to_format(format)
  t = Time.now
  i = @image.flatten_images
  i.format = format
  blob = i.to_blob

  logger.debug "exporting image as string"
  logger.debug " TIME COST #{Time.now - t}"

  return blob
end