Skip to content

Commit d194c07

Browse files
committed
adding legend box for further compartelization of attributes and responsibilities
1 parent 1791f3d commit d194c07

13 files changed

Lines changed: 156 additions & 70 deletions

File tree

lib/rubyplot/artist.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require_relative 'artist/base'
22
require_relative 'artist/figure'
3+
require_relative 'artist/legend_box'
34
require_relative 'artist/legend'
45
require_relative 'artist/line2d'
56
require_relative 'artist/tick'

lib/rubyplot/artist/axes.rb

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ class Axes < Base
4040
attr_reader :plots
4141
# data variables for something
4242
attr_reader :raw_rows
43-
attr_reader :backend
4443

4544
attr_reader :geometry, :font, :marker_font_size, :legend_font_size,
4645
:title_font_size, :scale, :font_color, :marker_color, :axes,
@@ -55,6 +54,8 @@ class Axes < Base
5554
attr_accessor :x_axis_margin
5655
# Margin between the Y axis and the left of the Axes artist.
5756
attr_accessor :y_axis_margin
57+
# Position of the legend box.
58+
attr_accessor :legend_box_position
5859

5960
# @param figure [Rubyplot::Figure] Figure object to which this Axes belongs.
6061
def initialize figure
@@ -76,8 +77,6 @@ def initialize figure
7677
@text_font = :default
7778
@grid = true
7879
@bounding_box = true
79-
@x_axis_padding = :default
80-
@y_axis_padding = :default
8180
@x_ticks = {}
8281
@plots = []
8382

@@ -87,28 +86,46 @@ def initialize figure
8786
@geometry = Rubyplot::MagickWrapper::Plot::Scatter::Geometry.new
8887
vera_font_path = File.expand_path('Vera.ttf', ENV['MAGICK_FONT_PATH'])
8988
@font = File.exist?(vera_font_path) ? vera_font_path : nil
89+
@font_color = "#000000"
9090
@marker_font_size = 15.0
9191
@legend_font_size = 20.0
9292
@legend_margin = LEGEND_MARGIN
93-
@title_font_size = 36.0
93+
@title_font_size = 25.0
9494
@backend = @figure.backend
95-
#@backend.scale(@scale)
9695
@plot_colors = []
9796
@legends = []
9897
@lines = []
9998
@texts = []
10099
@x_axis = nil
101100
@y_axis = nil
101+
102+
@legend_box_position = :top
103+
end
104+
105+
# X co-ordinate of the legend box depending on value of @legend_box_position.
106+
def legend_box_ix
107+
case @legend_box_position
108+
when :top
109+
abs_y + width / 2
110+
end
111+
end
112+
113+
# Y co-ordinate of the legend box depending on value of @legend_box_position.
114+
def legend_box_iy
115+
case @legend_box_position
116+
when :top
117+
abs_x + @x_axis_margin + @legend_margin
118+
end
102119
end
103120

104121
# Write an image to a file by communicating with the backend.
105122
def draw
106123
configure_title
107124
calculate_xy_axes_origin
108125
configure_xy_axes
109-
# configure_legends
126+
configure_legends
110127
# configure_plotting_data
111-
# actually_draw
128+
actually_draw
112129
# @plots.each(&:draw)
113130
end
114131

@@ -191,24 +208,6 @@ def with_backend plot_type, *args
191208
plot
192209
end
193210

194-
def prepare_title
195-
return if @geometry.hide_title || @title.nil?
196-
t = Rubyplot::Artist::Text.new(
197-
@title,
198-
self,
199-
x: 0,
200-
y: @geometry.top_margin,
201-
height: 1.0,
202-
width: @geometry.raw_columns,
203-
font: @font,
204-
color: @font_color,
205-
pointsize: @title_font_size,
206-
internal_label: "axes title."
207-
)
208-
@texts << t
209-
@texts.each(&:draw)
210-
end
211-
212211
def prepare_legend
213212
@legends = @plots.map(&:create_legend)
214213
@legends.each { |l| l.draw }
@@ -316,8 +315,8 @@ def configure_title
316315
@title = Rubyplot::Artist::Text.new(
317316
@title,
318317
self,
319-
abs_x: abs_x,
320-
abs_y: abs_y,
318+
abs_x: abs_x + width / 2,
319+
abs_y: abs_y + @title_margin,
321320
font: @font,
322321
color: @font_color,
323322
pointsize: @title_font_size,
@@ -328,22 +327,28 @@ def configure_title
328327
def calculate_xy_axes_origin
329328
@origin[0] = abs_x + @x_axis_margin
330329
@origin[1] = abs_y + height - @y_axis_margin
331-
puts "origin: #{@origin}"
332-
puts "h: #{height}"
333-
puts "w: #{width}"
334330
end
335331

336332
# Figure out co-ordinatees of the XAxis and YAxis
337333
def configure_xy_axes
338334
@x_axis = Rubyplot::Artist::XAxis.new(
339335
self, @x_title, @x_range[0], @x_range[1])
340-
@x_axis.draw
341-
# @y_axis = Rubyplot::Artist::YAxis.new(
342-
# self, @y_title, @y_range[0], @y_range[1])
336+
@y_axis = Rubyplot::Artist::YAxis.new(
337+
self, @y_title, @y_range[0], @y_range[1])
343338
end
344339

345340
# Figure out co-ordinates of the legends
346341
def configure_legends
342+
@legend_box = Rubyplot::Artist::LegendBox.new(
343+
self, abs_x: legend_box_ix, abs_y: legend_box_iy)
344+
end
345+
346+
# Call the respective draw methods on each of the elements of this Axes.
347+
def actually_draw
348+
@x_axis.draw
349+
@y_axis.draw
350+
@title.draw
351+
@legend_box.draw
347352
end
348353

349354
# Return a formatted string representing a number value that should be

lib/rubyplot/artist/axis/base.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@ class Base
55
# Length in pixels of the arrow after the last major tick.
66
FINISH_ARROW_LENGTH = 10.0
77

8-
attr_writer :label, :ticks, :major_ticks_count, :min_val, :max_val, :title
8+
attr_reader :label, :ticks, :major_ticks_count, :min_val, :max_val, :title
99
attr_reader :abs_x1, :abs_x2, :abs_y1, :abs_y2, :backend
10+
attr_reader :stroke_width
1011

1112
def initialize axes, title, min_val, max_val
1213
@axes = axes
1314
@title = title
1415
@min_val = min_val
1516
@max_val = max_val
17+
@stroke_width = 1.0
1618
@backend = @axes.backend
1719
end
1820
end # class Base

lib/rubyplot/artist/axis/x_axis.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def draw
2828
def configure_axis_line
2929
@line = Rubyplot::Artist::Line2D.new(
3030
self, abs_x1: @abs_x1, abs_y1: @abs_y1, abs_x2: @abs_x2, abs_y2: @abs_y2,
31-
stroke_width: 1.0)
31+
)#stroke_width: @stroke_width)
3232
end
3333

3434
def populate_major_x_ticks
@@ -51,9 +51,8 @@ def configure_title
5151
@title,
5252
self,
5353
pointsize: @axes.marker_font_size,
54-
abs_y: @axes.origin[1],
55-
abs_x: @axes.origin[0] + (@axes.abs_x + @axes.abs_y)/2,
56-
gravity: :center
54+
abs_y: @axes.origin[1] + 20,
55+
abs_x: @axes.origin[0] + (@abs_x2 - @abs_x1)/2,
5756
)
5857
end
5958
end # class XAxis

lib/rubyplot/artist/axis/y_axis.rb

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,48 @@ module Artist
33
class YAxis < Axis::Base
44
def initialize(*)
55
super
6-
@x1 = @axes.graph_left
7-
@y1 = @axes.graph_height + @axes.graph_top
8-
@x2 = @axes.graph_left
9-
@y2 = @axes.graph_top
10-
if @title
11-
@title = Rubyplot::Artist::Text.new(
12-
@title,
13-
self,
14-
rotation: -90.0,
15-
gravity: :center,
16-
x: @axes.geometry.left_margin + @axes.marker_caps_height/2.0,
17-
y: @axes.graph_top + @axes.graph_height/2.0,
18-
width: 1.0,
19-
height: @axes.raw_rows,
20-
pointsize: @axes.marker_font_size * @axes.scale
21-
)
22-
end
6+
@abs_x1 = @axes.origin[0]
7+
@abs_y1 = @axes.origin[1]
8+
@abs_x2 = @axes.origin[0]
9+
@abs_y2 = @axes.origin[1] - (@axes.height - @axes.x_axis_margin)
10+
@y_ticks = []
11+
configure_axis_line
12+
configure_title
13+
populate_major_y_ticks
2314
end
2415

2516
def draw
26-
super
2717
@title.draw
18+
@line.draw
19+
#@y_ticks.each(&:draw)
20+
end
21+
22+
private
23+
24+
def configure_axis_line
25+
@line = Rubyplot::Artist::Line2D.new(
26+
self,
27+
abs_x1: @abs_x1,
28+
abs_y1: @abs_y1,
29+
abs_x2: @abs_x2,
30+
abs_y2: @abs_y2,
31+
stroke_width: @stroke_width
32+
)
33+
end
34+
35+
def configure_title
36+
@title = Rubyplot::Artist::Text.new(
37+
@title,
38+
self,
39+
rotation: -90.0,
40+
abs_x: @axes.origin[0] - 10,
41+
abs_y: (@abs_y1 - @abs_y2) / 2,
42+
pointsize: @axes.marker_font_size
43+
)
44+
end
45+
46+
def populate_major_y_ticks
47+
2848
end
2949
end # class YAxis
3050
end # class Artist

lib/rubyplot/artist/base.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
module Rubyplot
22
module Artist
33
class Base
4+
# Artist backend. ImageMagick or whatever.
45
attr_reader :backend
56
# Absolute X co-ordinate of this Aritist on the canvas.
67
attr_reader :abs_x
78
# Absolute Y co-ordinate of this Aritist on the canvas.
89
attr_reader :abs_y
9-
end
10-
end
11-
end
10+
11+
def initialize backend, abs_x, abs_y
12+
@backend = backend
13+
@abs_x = abs_x
14+
@abs_y = abs_y
15+
end
16+
end # class Base
17+
end # module Artist
18+
end # module Rubyplot

lib/rubyplot/artist/legend.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
module Rubyplot
22
module Artist
3-
class Legend
3+
class Legend < Artist::Base
44
attr_reader :legend_box_size, :font, :font_size, :font_color
55

66
# text - [String] String containing name of this legend.
77
# colors - [String] String of corresponding color.
8-
def initialize axes, text, color
8+
def initialize(axes, text:, color:,abs_x:,abs_y:)
9+
super(axes.backend, abs_x, abs_y)
910
@axes = axes
1011
@text = text
1112
@color = color
1213
@legend_box_size = 20.0 # size of the color box of the legend.
1314
@font_size = 20.0
1415
@font = @axes.font
1516
@font_color = @axes.font_color
17+
configure_legend_color_box
18+
configure_legend_text
1619
calculate_legend_size
1720
calculate_offsets
1821
end
@@ -24,6 +27,14 @@ def draw
2427

2528
private
2629

30+
def configure_legend_color_box
31+
32+
end
33+
34+
def configure_legend_text
35+
36+
end
37+
2738
def draw_legend_text
2839
scaled_width = @axes.geometry.raw_columns * @axes.scale
2940
scaled_width = scaled_width >=1 ? scaled_width : 1

lib/rubyplot/artist/legend_box.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module Rubyplot
2+
module Artist
3+
class LegendBox < Base
4+
MAX_WIDTH = 50.0
5+
MAX_HEIGHT = 30.0
6+
7+
def initialize(axes, abs_x:, abs_y:)
8+
super(axes.backend, abs_x, abs_y)
9+
@axes = axes
10+
@legends = []
11+
configure_legends
12+
end
13+
14+
def draw
15+
16+
end
17+
18+
private
19+
20+
def configure_legends
21+
22+
end
23+
end # class LegendBox
24+
end # module Artist
25+
end # module Rubyplot

lib/rubyplot/artist/line2d.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module Rubyplot
22
module Artist
33
class Line2D < Artist::Base
44
def initialize(owner,abs_x1:,abs_y1:,abs_x2:,abs_y2:,color: '#000000',
5-
stroke_opacity: 1.0, stroke_width:)
5+
stroke_opacity: 0.0, stroke_width: 2.0)
66
@owner = owner
77
@abs_x1 = abs_x1
88
@abs_y1 = abs_y1

lib/rubyplot/artist/rectangle.rb

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,24 @@
11
module Rubyplot
22
module Artist
3-
class Rectangle
4-
3+
class Rectangle < Base
4+
attr_reader :width, :height, :color
5+
6+
def initialize(owner,abs_x:,abs_y:,width:,height:,color:)
7+
super(owner.backend, abs_x, abs_y)
8+
@height = height
9+
@width = width
10+
@color = color
11+
end
12+
13+
def draw
14+
@backend.draw_rectangle(
15+
x1: @abs_x,
16+
y1: @abs_y,
17+
x2: @abs_x + @width,
18+
y2: @abs_y + @height,
19+
color: @color
20+
)
21+
end
522
end # class Rectangle
623
end # class Artist
724
end # moduel Rubyplot

0 commit comments

Comments
 (0)