Skip to content

Commit 799ed02

Browse files
committed
streamline tests
1 parent 34a5f3e commit 799ed02

File tree

10 files changed

+123
-151
lines changed

10 files changed

+123
-151
lines changed

lib/rubyplot/artist/axes.rb

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,7 @@ class Axes < Base
88
LABEL_MARGIN = 10.0
99
DEFAULT_MARGIN = 20.0
1010
THOUSAND_SEPARATOR = ','.freeze
11-
# FIXME: most of the below accessors should just be name= methods which
12-
# will access the required Artist and set the variable in there directly.
13-
# Range of X axis.
14-
attr_accessor :x_range
15-
# Range of Y axis.
16-
17-
attr_accessor :y_range, :text_font, :grid, :bounding_box, :origin, :title_shift, :title_margin
18-
19-
# Main title for this Axes.
20-
attr_accessor :title
11+
2112
# Rubyplot::Figure object to which this Axes belongs.
2213
attr_reader :figure
2314
# Array of plots contained in this Axes.
@@ -26,24 +17,30 @@ class Axes < Base
2617
:title_font_size, :scale, :font_color, :marker_color, :axes,
2718
:legend_margin, :backend, :marker_caps_height
2819
attr_reader :label_stagger_height
29-
# FIXME: possibly disposable attrs
30-
attr_reader :title_caps_height
31-
# Set true if title is to be hidden.
32-
attr_accessor :hide_title
33-
# Margin between the X axis and the bottom of the Axes artist.
34-
attr_accessor :x_axis_margin
35-
# Margin between the Y axis and the left of the Axes artist.
36-
attr_accessor :y_axis_margin
37-
# Position of the legend box.
38-
attr_accessor :legend_box_position
3920
# Rubyplot::Artist::XAxis object.
4021
attr_reader :x_axis
4122
# Rubyplot::Artist::YAxis object.
4223
attr_reader :y_axis
4324
# Array of X ticks.
4425
attr_reader :x_ticks
26+
# Array denoting co-ordinates in pixels of the origin of X and Y axes.
27+
attr_reader :origin
4528
# Number of X ticks.
4629
attr_accessor :num_x_ticks
30+
# Position of the legend box.
31+
attr_accessor :legend_box_position
32+
# Set true if title is to be hidden.
33+
attr_accessor :hide_title
34+
# Margin between the X axis and the bottom of the Axes artist.
35+
attr_accessor :x_axis_margin
36+
# Margin between the Y axis and the left of the Axes artist.
37+
attr_accessor :y_axis_margin
38+
# Range of X axis.
39+
attr_accessor :x_range
40+
# Range of Y axis.
41+
attr_accessor :y_range, :grid, :bounding_box, :title_shift
42+
# Main title for this Axes.
43+
attr_accessor :title
4744

4845
# @param figure [Rubyplot::Figure] Figure object to which this Axes belongs.
4946
def initialize(figure)
@@ -208,16 +205,15 @@ def assign_default_label_colors
208205
end
209206

210207
def assign_x_ticks
208+
@inter_x_ticks_distance = @x_axis.length / (@num_x_ticks.to_f-1)
211209
unless @x_ticks
212-
val_distance = (@x_range[1] - @x_range[0]).abs / @num_x_ticks.to_f
213-
@x_ticks = (@x_range[0]..@x_range[1]).step(val_distance).map { |i| i }
210+
@x_ticks = (@x_range[0]..@x_range[1]).step(@inter_x_ticks_distance).map { |i| i }
214211
end
215212
unless @x_ticks.all? { |t| t.is_a?(Rubyplot::Artist::XTick) }
216-
inter_ticks_distance = @x_axis.length / (@num_x_ticks - 1)
217213
@x_ticks.map!.with_index do |tick_label, i|
218214
Rubyplot::Artist::XTick.new(
219215
self,
220-
abs_x: i * inter_ticks_distance + @x_axis.abs_x1,
216+
abs_x: i * @inter_x_ticks_distance + @x_axis.abs_x1,
221217
abs_y: @origin[1],
222218
label: Rubyplot::Utils.format_label(tick_label),
223219
length: 6,
@@ -305,8 +301,8 @@ def set_axes_ranges
305301

306302
def set_xrange
307303
if @x_range[0].nil? && @x_range[1].nil?
308-
@x_range[0] = @plots.map { |p| p.x_min }.min
309-
@x_range[1] = @plots.map { |p| p.x_max }.max
304+
@x_range[0] = @plots.map(&:x_min).min
305+
@x_range[1] = @plots.map(&:x_max).max
310306
end
311307
@x_axis.min_val = @x_range[0]
312308
@x_axis.max_val = @x_range[1]

lib/rubyplot/artist/figure.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ def add_subplot(nrow, ncol)
5757
@subplots[nrow][ncol] = Rubyplot::Artist::Axes.new(self)
5858
end
5959

60-
def write(file_name)
60+
# Draw on a canvas and output to a file.
61+
#
62+
# @param output [TrueClass, FalseClass] true Whether to output to file or not.
63+
def write(file_name, output: true)
6164
@backend.set_base_image_gradient(
6265
Rubyplot::Color::COLOR_INDEX[@theme_options[:background_colors][0]],
6366
Rubyplot::Color::COLOR_INDEX[@theme_options[:background_colors][1]],
@@ -66,7 +69,7 @@ def write(file_name)
6669
@theme_options[:background_direction]
6770
)
6871
@subplots.each { |i| i.each(&:draw) }
69-
@backend.write(file_name)
72+
@backend.write(file_name) if output
7073
end
7174

7275
private

lib/rubyplot/artist/plot/area.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ module Rubyplot
22
module Artist
33
module Plot
44
class Area < Artist::Plot::Base
5-
attr_reader :sorted_data
5+
attr_accessor :sorted_data
6+
67
def initialize(*)
78
super
89
@sorted_data = true

lib/rubyplot/backend/magick_wrapper.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,11 @@ def draw_line(x1:,y1:,x2:,y2:,color: '#000000', stroke: 'transparent',
9191
@draw.line x1, y1, x2, y2
9292
end
9393

94-
def draw_circle(x_pos:,y_pos:,radius:,stroke_opacity:,stroke_width:,color:)
94+
def draw_circle(x:,y:,radius:,stroke_opacity:,stroke_width:,color:)
9595
@draw.stroke_opacity stroke_opacity
9696
@draw.stroke_width stroke_width
9797
@draw.fill color
98-
@draw.circle(x_pos,y_pos,x-radius,y)
98+
@draw.circle(x,y,x-radius,y)
9999
end
100100
# rubocop:enable Metrics/ParameterLists
101101

0 commit comments

Comments
 (0)