Skip to content

Commit 71b99fa

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

18 files changed

+31
-57
lines changed

Diff for: lib/rubyplot.rb

+9-3
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

Diff for: lib/rubyplot/artist/axes.rb

-23
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,

Diff for: lib/rubyplot/artist/axis/base.rb

-1
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 = []

Diff for: lib/rubyplot/artist/base.rb

+1-4
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

Diff for: lib/rubyplot/artist/circle.rb

+2-2
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,

Diff for: lib/rubyplot/artist/figure.rb

+3-3
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

Diff for: lib/rubyplot/artist/legend.rb

+1-1
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

Diff for: lib/rubyplot/artist/legend_box.rb

+1-1
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 = []

Diff for: lib/rubyplot/artist/line2d.rb

+1-2
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

Diff for: lib/rubyplot/artist/plot/base.rb

+1-2
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

Diff for: lib/rubyplot/artist/polygon.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def initialize(owner, coords:, fill_opacity: 0.0, color: :default, stroke: 'tran
1010
end
1111

1212
def draw
13-
@backend.draw_polygon(
13+
Rubyplot.backend.draw_polygon(
1414
coords: @coords,
1515
stroke: @stroke,
1616
color: Rubyplot::Color::COLOR_INDEX[@color],

Diff for: lib/rubyplot/artist/rectangle.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Rectangle < Base
1616

1717
# rubocop:disable Metrics/ParameterLists
1818
def initialize(owner,abs_x:,abs_y:,width:,height:,border_color:,fill_color: nil)
19-
super(owner.backend, abs_x, abs_y)
19+
super(abs_x, abs_y)
2020
@height = height
2121
@width = width
2222
@border_color = Rubyplot::Color::COLOR_INDEX[border_color]
@@ -25,7 +25,7 @@ def initialize(owner,abs_x:,abs_y:,width:,height:,border_color:,fill_color: nil)
2525
# rubocop:enable Metrics/ParameterLists
2626

2727
def draw
28-
@backend.draw_rectangle(
28+
Rubyplot.backend.draw_rectangle(
2929
x1: @abs_x,
3030
y1: @abs_y,
3131
x2: @abs_x + @width,

Diff for: lib/rubyplot/artist/text.rb

+5-8
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,10 @@ class Text < Artist::Base
77

88
# rubocop:disable Metrics/ParameterLists
99
def initialize(text, owner, abs_x:, abs_y:,font: nil,
10-
1110
color: '#000000',pointsize:,stroke: 'transparent',
1211
internal_label: '', rotation: nil,
13-
weight: nil, gravity: nil
14-
)
15-
super(owner.backend, abs_x, abs_y)
16-
12+
weight: nil, gravity: nil)
13+
super(abs_x, abs_y)
1714
@text = text
1815
@owner = owner
1916
@font = font
@@ -27,16 +24,16 @@ def initialize(text, owner, abs_x:, abs_y:,font: nil,
2724

2825
# Height in pixels of this text
2926
def height
30-
@backend.text_height(@text, @font, @font_size)
27+
Rubyplot.backend.text_height(@text, @font, @font_size)
3128
end
3229

3330
# Width in pixels of this text
3431
def width
35-
@backend.text_width(@text, @font, @font_size)
32+
Rubyplot.backend.text_width(@text, @font, @font_size)
3633
end
3734

3835
def draw
39-
@backend.draw_text(
36+
Rubyplot.backend.draw_text(
4037
@text,
4138
font_color: @color,
4239
font: @font,

Diff for: lib/rubyplot/artist/tick/base.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Base < Artist::Base
2020
# rubocop:disable Metrics/ParameterLists
2121
def initialize(owner,abs_x:,abs_y:,length:,label:,label_distance:,
2222
tick_opacity: 0.0,tick_width: 1.0)
23-
super(owner.backend, abs_x, abs_y)
23+
super(abs_x, abs_y)
2424
@owner = owner
2525
@length = length
2626
@label_text = label # Rubyplot::Utils.format_label label

Diff for: lib/rubyplot/artist/tick/x_tick.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def initialize(*)
1313
end
1414

1515
def draw
16-
@backend.draw_line(
16+
Rubyplot.backend.draw_line(
1717
x1: @abs_x, y1: @abs_y, x2: @abs_x, y2: @abs_y + @length,
1818
stroke_opacity: @tick_opacity,
1919
stroke_width: @tick_width

Diff for: lib/rubyplot/artist/tick/y_tick.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def initialize(*)
1313
end
1414

1515
def draw
16-
@backend.draw_line(
16+
Rubyplot.backend.draw_line(
1717
x1: @abs_x, y1: @abs_y, x2: @abs_x - @length, y2: @abs_y,
1818
stroke_opacity: @tick_opacity,
1919
stroke_width: @tick_width)

Diff for: spec/figure_spec.rb

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
context "#add_subplot" do
55
it "creates a singular subplot inside the Figure" do
66
fig = Rubyplot::Figure.new
7-
fig.add_subplots 0,0
87
axes = fig.add_subplot 0,0
98

109
expect(axes).to be_a(Rubyplot::Axes)

Diff for: spec/spec_helper.rb

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def compare_with_reference(test_image, reference_image)
4747
base_image = TEMP_DIR + plot_name
4848
other_image = FIXTURES_DIR + plot_name
4949
@figure.write(other_image)
50+
Rubyplot.set_backend_magick
5051
@figure.write(base_image)
5152

5253
expect(base_image).to eq_image(other_image, 10)

0 commit comments

Comments
 (0)