Skip to content

Commit

Permalink
move backend to module level class variable
Browse files Browse the repository at this point in the history
  • Loading branch information
v0dro committed Jan 8, 2019
1 parent f1d5c38 commit 71b99fa
Show file tree
Hide file tree
Showing 18 changed files with 31 additions and 57 deletions.
12 changes: 9 additions & 3 deletions lib/rubyplot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@

module Rubyplot
@@backend = Rubyplot::Backend::MagickWrapper.new
def self.backend
@@backend
class << self
def backend
@@backend
end

def set_backend_magick
@@backed = Rubyplot::Backend::MagickWrapper.new
end
end
end
end # module Rubyplot
23 changes: 0 additions & 23 deletions lib/rubyplot/artist/axes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ def initialize(figure)
@legend_font_size = 20.0
@legend_margin = LEGEND_MARGIN
@title_font_size = 25.0
@backend = @figure.backend
@plot_colors = []
@legends = []
@lines = []
Expand Down Expand Up @@ -147,10 +146,6 @@ def bubble!(*_args)
@plots << plot
end

def dot!(*args, &block)
add_plot 'Dot', *args, &block
end

def stacked_bar!(*_args)
plot = Rubyplot::Artist::Plot::StackedBar.new self
yield(plot) if block_given?
Expand Down Expand Up @@ -252,26 +247,8 @@ def assign_y_ticks
end
end

def add_plot plot_type, *args, &block
plot = with_backend plot_type, *args
yield(plot) if block_given?
@plots << plot
end

def with_backend(plot_type, *args)
plot =
case Rubyplot.backend
when :magick
Kernel.const_get("Rubyplot::MagickWrapper::Plot::#{plot_type}").new self, *args
when :gr
Kernel.const_get("Rubyplot::GRWrapper::Plot::#{plot_type}").new self, *args
end
plot
end

# Figure out the co-ordinates of the title text w.r.t Axes.
def configure_title

@texts << Rubyplot::Artist::Text.new(
@title, self, abs_x: abs_x + width / 2, abs_y: abs_y + @title_margin,
font: @font, color: @font_color,
Expand Down
1 change: 0 additions & 1 deletion lib/rubyplot/artist/axis/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ def initialize axes
@min_val = nil
@max_val = nil
@stroke_width = 1.0
@backend = @axes.backend
@major_ticks_count = 5
@x_ticks = nil
@texts = []
Expand Down
5 changes: 1 addition & 4 deletions lib/rubyplot/artist/base.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
module Rubyplot
module Artist
class Base
# Artist backend. ImageMagick or whatever.
attr_reader :backend
# Absolute X co-ordinate of this Aritist on the canvas.
attr_reader :abs_x
# Absolute Y co-ordinate of this Aritist on the canvas.
attr_reader :abs_y
def initialize(backend, abs_x, abs_y)
@backend = backend
def initialize(abs_x, abs_y)
@abs_x = abs_x
@abs_y = abs_y
end
Expand Down
4 changes: 2 additions & 2 deletions lib/rubyplot/artist/circle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Circle < Base
# rubocop:disable Metrics/ParameterLists
def initialize(owner, abs_x:, abs_y:, radius:, stroke_opacity: 0.0,
color: :default, stroke_width:)
super(owner.backend, abs_x, abs_y)
super(abs_x, abs_y)
@owner = owner
@radius = radius
@stroke_width = stroke_width
Expand All @@ -14,7 +14,7 @@ def initialize(owner, abs_x:, abs_y:, radius:, stroke_opacity: 0.0,
# rubocop:enable Metrics/ParameterLists

def draw
@backend.draw_circle(
Rubyplot.backend.draw_circle(
x: @abs_x,
y: @abs_y,
radius: @radius,
Expand Down
6 changes: 3 additions & 3 deletions lib/rubyplot/artist/figure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Figure < Artist::Base
# @param height [Integer] nil Height in pixels of the complete Figure.
# @param width [Integer] nil Width in pixels of the complete Figure.
def initialize(height: nil, width: nil)
super(Rubyplot::Backend::MagickWrapper.new, 0, 0)
super(0, 0)
@title = ''
@nrows = 1
@ncols = 1
Expand Down Expand Up @@ -62,15 +62,15 @@ def add_subplot(nrow, ncol)
#
# @param output [TrueClass, FalseClass] true Whether to output to file or not.
def write(file_name, output: true)
@backend.set_base_image_gradient(
Rubyplot.backend.set_base_image_gradient(
Rubyplot::Color::COLOR_INDEX[@theme_options[:background_colors][0]],
Rubyplot::Color::COLOR_INDEX[@theme_options[:background_colors][1]],
@width,
@height,
@theme_options[:background_direction]
)
@subplots.each { |i| i.each(&:draw) }
@backend.write(file_name) if output
Rubyplot.backend.write(file_name) if output
end

private
Expand Down
2 changes: 1 addition & 1 deletion lib/rubyplot/artist/legend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Legend < Artist::Base

# rubocop:disable Metrics/ParameterLists
def initialize(legend_box, axes, text:, color:,abs_x:,abs_y:)
super(legend_box.backend, abs_x, abs_y)
super(abs_x, abs_y)
@legend_box = legend_box
@axes = axes
@text = text
Expand Down
2 changes: 1 addition & 1 deletion lib/rubyplot/artist/legend_box.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class LegendBox < Base
attr_accessor :border_color

def initialize(axes, abs_x:, abs_y:)
super(axes.backend, abs_x, abs_y)
super(abs_x, abs_y)
@axes = axes
@border_color = :black
@legends = []
Expand Down
3 changes: 1 addition & 2 deletions lib/rubyplot/artist/line2d.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ def initialize(owner,abs_x1:,abs_y1:,abs_x2:,abs_y2:,color: '#000000',
@color = color
@stroke_opacity = stroke_opacity
@stroke_width = stroke_width
@backend = @owner.backend
end
# rubocop:enable Metrics/ParameterLists

def draw
@backend.draw_line(x1: @abs_x1, y1: @abs_y1, x2: @abs_x2, y2: @abs_y2,
Rubyplot.backend.draw_line(x1: @abs_x1, y1: @abs_y1, x2: @abs_x2, y2: @abs_y2,
stroke_width: @stroke_width)
end
end # class Line2D
Expand Down
3 changes: 1 addition & 2 deletions lib/rubyplot/artist/plot/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ class Base < Artist::Base
attr_writer :stroke_width, :stroke_opacity

def initialize axes
super(axes.backend, axes.abs_x, axes.abs_y)
super(axes.abs_x, axes.abs_y)
@axes = axes
@backend = @axes.backend
@data = {
label: '',
color: :default
Expand Down
2 changes: 1 addition & 1 deletion lib/rubyplot/artist/polygon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def initialize(owner, coords:, fill_opacity: 0.0, color: :default, stroke: 'tran
end

def draw
@backend.draw_polygon(
Rubyplot.backend.draw_polygon(
coords: @coords,
stroke: @stroke,
color: Rubyplot::Color::COLOR_INDEX[@color],
Expand Down
4 changes: 2 additions & 2 deletions lib/rubyplot/artist/rectangle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Rectangle < Base

# rubocop:disable Metrics/ParameterLists
def initialize(owner,abs_x:,abs_y:,width:,height:,border_color:,fill_color: nil)
super(owner.backend, abs_x, abs_y)
super(abs_x, abs_y)
@height = height
@width = width
@border_color = Rubyplot::Color::COLOR_INDEX[border_color]
Expand All @@ -25,7 +25,7 @@ def initialize(owner,abs_x:,abs_y:,width:,height:,border_color:,fill_color: nil)
# rubocop:enable Metrics/ParameterLists

def draw
@backend.draw_rectangle(
Rubyplot.backend.draw_rectangle(
x1: @abs_x,
y1: @abs_y,
x2: @abs_x + @width,
Expand Down
13 changes: 5 additions & 8 deletions lib/rubyplot/artist/text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@ class Text < Artist::Base

# rubocop:disable Metrics/ParameterLists
def initialize(text, owner, abs_x:, abs_y:,font: nil,

color: '#000000',pointsize:,stroke: 'transparent',
internal_label: '', rotation: nil,
weight: nil, gravity: nil
)
super(owner.backend, abs_x, abs_y)

weight: nil, gravity: nil)
super(abs_x, abs_y)
@text = text
@owner = owner
@font = font
Expand All @@ -27,16 +24,16 @@ def initialize(text, owner, abs_x:, abs_y:,font: nil,

# Height in pixels of this text
def height
@backend.text_height(@text, @font, @font_size)
Rubyplot.backend.text_height(@text, @font, @font_size)
end

# Width in pixels of this text
def width
@backend.text_width(@text, @font, @font_size)
Rubyplot.backend.text_width(@text, @font, @font_size)
end

def draw
@backend.draw_text(
Rubyplot.backend.draw_text(
@text,
font_color: @color,
font: @font,
Expand Down
2 changes: 1 addition & 1 deletion lib/rubyplot/artist/tick/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Base < Artist::Base
# rubocop:disable Metrics/ParameterLists
def initialize(owner,abs_x:,abs_y:,length:,label:,label_distance:,
tick_opacity: 0.0,tick_width: 1.0)
super(owner.backend, abs_x, abs_y)
super(abs_x, abs_y)
@owner = owner
@length = length
@label_text = label # Rubyplot::Utils.format_label label
Expand Down
2 changes: 1 addition & 1 deletion lib/rubyplot/artist/tick/x_tick.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def initialize(*)
end

def draw
@backend.draw_line(
Rubyplot.backend.draw_line(
x1: @abs_x, y1: @abs_y, x2: @abs_x, y2: @abs_y + @length,
stroke_opacity: @tick_opacity,
stroke_width: @tick_width
Expand Down
2 changes: 1 addition & 1 deletion lib/rubyplot/artist/tick/y_tick.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def initialize(*)
end

def draw
@backend.draw_line(
Rubyplot.backend.draw_line(
x1: @abs_x, y1: @abs_y, x2: @abs_x - @length, y2: @abs_y,
stroke_opacity: @tick_opacity,
stroke_width: @tick_width)
Expand Down
1 change: 0 additions & 1 deletion spec/figure_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
context "#add_subplot" do
it "creates a singular subplot inside the Figure" do
fig = Rubyplot::Figure.new
fig.add_subplots 0,0
axes = fig.add_subplot 0,0

expect(axes).to be_a(Rubyplot::Axes)
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def compare_with_reference(test_image, reference_image)
base_image = TEMP_DIR + plot_name
other_image = FIXTURES_DIR + plot_name
@figure.write(other_image)
Rubyplot.set_backend_magick
@figure.write(base_image)

expect(base_image).to eq_image(other_image, 10)
Expand Down

0 comments on commit 71b99fa

Please sign in to comment.