Class: Rubyplot::SubPlot

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyplot/artist/subplots.rb

Instance Method Summary collapse

Constructor Details

#initialize(m, n) ⇒ SubPlot

Makes a grid of M x N dimensions. M -> Num of vertical images. N -> Num of horizontal images.


6
7
8
9
10
# File 'lib/rubyplot/artist/subplots.rb', line 6

def initialize(m, n)
  @m = m
  @n = n
  @grid_array = Array.new((m * n), nil)
end

Instance Method Details

#addimage(index, image) ⇒ Object

Adds a image at a given index in the subplot. NOTE: The image here is any standard image read by RMagick read function


23
24
25
26
# File 'lib/rubyplot/artist/subplots.rb', line 23

def addimage(index, image)
  index -= 1
  @grid_array[index] = image if @grid_array[index].nil?
end

#addplot(plot, index) ⇒ Object

Adds a plot at a given index in the subplot.


13
14
15
16
17
18
19
# File 'lib/rubyplot/artist/subplots.rb', line 13

def addplot(plot, index)
  index -= 1
  if @grid_array[index].nil?
    plot.draw
    @grid_array[index] = plot.base_image
  end
end

#max(a, b) ⇒ Object


28
29
30
# File 'lib/rubyplot/artist/subplots.rb', line 28

def max(a, b)
  a > b ? a : b
end

#write(filename) ⇒ Object

Writes subplot to a file.


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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rubyplot/artist/subplots.rb', line 33

def write(filename)
  @max_x_pixels = 0
  @max_y_pixels = 0
  # Calculating the maximum value of the x pixels in the subplot.
  for i in 0..(@m - 1)
    x_pixel_sum = 10
    for j in 0..(@n - 1)
      index = i * @n + j
      x_pixel_sum += @grid_array[index].columns unless @grid_array[index].nil?
      x_pixel_sum += 10
    end
    @max_x_pixels = max(@max_x_pixels, x_pixel_sum)
  end

  # Calculating the maximum value of the y pixels in the subplot.
  for j in 0..(@n - 1)
    y_pixel_sum = 10
    for i in 0..(@m - 1)
      index = i * @n + j
      y_pixel_sum += @grid_array[index].rows unless @grid_array[index].nil?
      y_pixel_sum += 10
    end
    @max_y_pixels = max(@max_y_pixels, y_pixel_sum)
  end

  # Making the empty base image(white background) on which the plots are overlayed
  base_image = Magick::Image.new(@max_x_pixels, @max_y_pixels) { self.background_color = 'white' }

  y_pixel = 10
  for i in 0..(@m - 1)
    x_pixel_sum = 10
    y_pixel_max = 0
    for j in 0..(@n - 1)
      index = i * @n + j
      next if @grid_array[index].nil?
      # Using Composite function to overlay the plots on the base image
      base_image = base_image.composite(@grid_array[index], Magick::ForgetGravity, x_pixel_sum, y_pixel, Magick::OverCompositeOp)
      x_pixel_sum += @grid_array[index].columns
      x_pixel_sum += 10
      y_pixel_max = max(y_pixel_max, @grid_array[index].rows)
    end
    y_pixel += y_pixel_max + 10
  end
  # Writing the subplot to to a file.
  base_image.write(filename)
end