Skip to content

Commit 71b99fa

Browse files
committed
move backend to module level class variable
1 parent f1d5c38 commit 71b99fa

File tree

18 files changed

+31
-57
lines changed

18 files changed

+31
-57
lines changed

lib/rubyplot.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@
1414

1515
module Rubyplot
1616
@@backend = Rubyplot::Backend::MagickWrapper.new
17-
def self.backend
18-
@@backend
17+
class << self
18+
def backend
19+
@@backend
20+
end
21+
22+
def set_backend_magick
23+
@@backed = Rubyplot::Backend::MagickWrapper.new
24+
end
1925
end
20-
end
26+
end # module Rubyplot

lib/rubyplot/artist/axes.rb

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ def initialize(figure)
7272
@legend_font_size = 20.0
7373
@legend_margin = LEGEND_MARGIN
7474
@title_font_size = 25.0
75-
@backend = @figure.backend
7675
@plot_colors = []
7776
@legends = []
7877
@lines = []
@@ -147,10 +146,6 @@ def bubble!(*_args)
147146
@plots << plot
148147
end
149148

150-
def dot!(*args, &block)
151-
add_plot 'Dot', *args, &block
152-
end
153-
154149
def stacked_bar!(*_args)
155150
plot = Rubyplot::Artist::Plot::StackedBar.new self
156151
yield(plot) if block_given?
@@ -252,26 +247,8 @@ def assign_y_ticks
252247
end
253248
end
254249

255-
def add_plot plot_type, *args, &block
256-
plot = with_backend plot_type, *args
257-
yield(plot) if block_given?
258-
@plots << plot
259-
end
260-
261-
def with_backend(plot_type, *args)
262-
plot =
263-
case Rubyplot.backend
264-
when :magick
265-
Kernel.const_get("Rubyplot::MagickWrapper::Plot::#{plot_type}").new self, *args
266-
when :gr
267-
Kernel.const_get("Rubyplot::GRWrapper::Plot::#{plot_type}").new self, *args
268-
end
269-
plot
270-
end
271-
272250
# Figure out the co-ordinates of the title text w.r.t Axes.
273251
def configure_title
274-
275252
@texts << Rubyplot::Artist::Text.new(
276253
@title, self, abs_x: abs_x + width / 2, abs_y: abs_y + @title_margin,
277254
font: @font, color: @font_color,

lib/rubyplot/artist/axis/base.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ def initialize axes
1616
@min_val = nil
1717
@max_val = nil
1818
@stroke_width = 1.0
19-
@backend = @axes.backend
2019
@major_ticks_count = 5
2120
@x_ticks = nil
2221
@texts = []

lib/rubyplot/artist/base.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
module Rubyplot
22
module Artist
33
class Base
4-
# Artist backend. ImageMagick or whatever.
5-
attr_reader :backend
64
# Absolute X co-ordinate of this Aritist on the canvas.
75
attr_reader :abs_x
86
# Absolute Y co-ordinate of this Aritist on the canvas.
97
attr_reader :abs_y
10-
def initialize(backend, abs_x, abs_y)
11-
@backend = backend
8+
def initialize(abs_x, abs_y)
129
@abs_x = abs_x
1310
@abs_y = abs_y
1411
end

lib/rubyplot/artist/circle.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class Circle < Base
44
# rubocop:disable Metrics/ParameterLists
55
def initialize(owner, abs_x:, abs_y:, radius:, stroke_opacity: 0.0,
66
color: :default, stroke_width:)
7-
super(owner.backend, abs_x, abs_y)
7+
super(abs_x, abs_y)
88
@owner = owner
99
@radius = radius
1010
@stroke_width = stroke_width
@@ -14,7 +14,7 @@ def initialize(owner, abs_x:, abs_y:, radius:, stroke_opacity: 0.0,
1414
# rubocop:enable Metrics/ParameterLists
1515

1616
def draw
17-
@backend.draw_circle(
17+
Rubyplot.backend.draw_circle(
1818
x: @abs_x,
1919
y: @abs_y,
2020
radius: @radius,

lib/rubyplot/artist/figure.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class Figure < Artist::Base
3333
# @param height [Integer] nil Height in pixels of the complete Figure.
3434
# @param width [Integer] nil Width in pixels of the complete Figure.
3535
def initialize(height: nil, width: nil)
36-
super(Rubyplot::Backend::MagickWrapper.new, 0, 0)
36+
super(0, 0)
3737
@title = ''
3838
@nrows = 1
3939
@ncols = 1
@@ -62,15 +62,15 @@ def add_subplot(nrow, ncol)
6262
#
6363
# @param output [TrueClass, FalseClass] true Whether to output to file or not.
6464
def write(file_name, output: true)
65-
@backend.set_base_image_gradient(
65+
Rubyplot.backend.set_base_image_gradient(
6666
Rubyplot::Color::COLOR_INDEX[@theme_options[:background_colors][0]],
6767
Rubyplot::Color::COLOR_INDEX[@theme_options[:background_colors][1]],
6868
@width,
6969
@height,
7070
@theme_options[:background_direction]
7171
)
7272
@subplots.each { |i| i.each(&:draw) }
73-
@backend.write(file_name) if output
73+
Rubyplot.backend.write(file_name) if output
7474
end
7575

7676
private

lib/rubyplot/artist/legend.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Legend < Artist::Base
99

1010
# rubocop:disable Metrics/ParameterLists
1111
def initialize(legend_box, axes, text:, color:,abs_x:,abs_y:)
12-
super(legend_box.backend, abs_x, abs_y)
12+
super(abs_x, abs_y)
1313
@legend_box = legend_box
1414
@axes = axes
1515
@text = text

lib/rubyplot/artist/legend_box.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class LegendBox < Base
1313
attr_accessor :border_color
1414

1515
def initialize(axes, abs_x:, abs_y:)
16-
super(axes.backend, abs_x, abs_y)
16+
super(abs_x, abs_y)
1717
@axes = axes
1818
@border_color = :black
1919
@legends = []

lib/rubyplot/artist/line2d.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@ def initialize(owner,abs_x1:,abs_y1:,abs_x2:,abs_y2:,color: '#000000',
1212
@color = color
1313
@stroke_opacity = stroke_opacity
1414
@stroke_width = stroke_width
15-
@backend = @owner.backend
1615
end
1716
# rubocop:enable Metrics/ParameterLists
1817

1918
def draw
20-
@backend.draw_line(x1: @abs_x1, y1: @abs_y1, x2: @abs_x2, y2: @abs_y2,
19+
Rubyplot.backend.draw_line(x1: @abs_x1, y1: @abs_y1, x2: @abs_x2, y2: @abs_y2,
2120
stroke_width: @stroke_width)
2221
end
2322
end # class Line2D

lib/rubyplot/artist/plot/base.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ class Base < Artist::Base
66
attr_writer :stroke_width, :stroke_opacity
77

88
def initialize axes
9-
super(axes.backend, axes.abs_x, axes.abs_y)
9+
super(axes.abs_x, axes.abs_y)
1010
@axes = axes
11-
@backend = @axes.backend
1211
@data = {
1312
label: '',
1413
color: :default

0 commit comments

Comments
 (0)