Skip to content

Commit 468f0b6

Browse files
284kmv0dro
authored andcommitted
Implement Y ticks (#33)
1 parent f9cde9a commit 468f0b6

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

lib/rubyplot/artist/axes.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,14 @@ class Axes < Base
2323
attr_reader :y_axis
2424
# Array of X ticks.
2525
attr_reader :x_ticks
26+
# Array of Y ticks.
27+
attr_reader :y_ticks
2628
# Array denoting co-ordinates in pixels of the origin of X and Y axes.
2729
attr_reader :origin
2830
# Number of X ticks.
2931
attr_accessor :num_x_ticks
32+
# Number of Y ticks.
33+
attr_accessor :num_y_ticks
3034
# Position of the legend box.
3135
attr_accessor :legend_box_position
3236
# Set true if title is to be hidden.
@@ -78,7 +82,9 @@ def initialize(figure)
7882
@x_axis = Rubyplot::Artist::XAxis.new(self)
7983
@y_axis = Rubyplot::Artist::YAxis.new(self)
8084
@x_ticks = nil
85+
@y_ticks = nil
8186
@num_x_ticks = 5
87+
@num_y_ticks = 4
8288
@legend_box_position = :top
8389
end
8490

@@ -107,6 +113,7 @@ def draw
107113
configure_title
108114
configure_legends
109115
assign_x_ticks
116+
assign_y_ticks
110117
actually_draw
111118
end
112119

@@ -179,6 +186,10 @@ def x_ticks= x_ticks
179186
@x_ticks = x_ticks
180187
end
181188

189+
def y_ticks= y_ticks
190+
@y_ticks = y_ticks
191+
end
192+
182193
def x_title= x_title
183194
@x_axis.title = x_title
184195
end
@@ -221,6 +232,26 @@ def assign_x_ticks
221232
end
222233
end
223234

235+
def assign_y_ticks
236+
unless @y_ticks
237+
val_distance = (@y_range[1] - @y_range[0]).abs / @num_y_ticks.to_f
238+
@y_ticks = (@y_range[0]..@y_range[1]).step(val_distance).map { |i| i }
239+
end
240+
unless @y_ticks.all? { |t| t.is_a?(Rubyplot::Artist::YTick) }
241+
inter_ticks_distance = @y_axis.length / (@num_y_ticks - 1)
242+
@y_ticks.map!.with_index do |tick_label, i|
243+
Rubyplot::Artist::YTick.new(
244+
self,
245+
abs_x: @origin[0],
246+
abs_y: @y_axis.abs_y1 - (i * inter_ticks_distance),
247+
label: Rubyplot::Utils.format_label(tick_label),
248+
length: 6,
249+
label_distance: 50
250+
)
251+
end
252+
end
253+
end
254+
224255
def add_plot plot_type, *args, &block
225256
plot = with_backend plot_type, *args
226257
yield(plot) if block_given?
@@ -271,6 +302,7 @@ def normalize_plotting_data
271302
def actually_draw
272303
@x_axis.draw
273304
@x_ticks.each(&:draw)
305+
@y_ticks.each(&:draw)
274306
@y_axis.draw
275307
@texts.each(&:draw)
276308
@legend_box.draw

lib/rubyplot/artist/tick/y_tick.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
11
module Rubyplot
22
module Artist
33
class YTick < Tick::Base
4+
def initialize(*)
5+
super
6+
@label = Rubyplot::Artist::Text.new(
7+
@label_text.to_s,
8+
@owner,
9+
abs_x: @abs_x - 5 - @label_distance,
10+
abs_y: @abs_y + @length,
11+
pointsize: @owner.marker_font_size,
12+
)
13+
end
14+
15+
def draw
16+
@backend.draw_line(
17+
x1: @abs_x, y1: @abs_y, x2: @abs_x - @length, y2: @abs_y,
18+
stroke_opacity: @tick_opacity,
19+
stroke_width: @tick_width)
20+
@label.draw
21+
end
422
end
523
# class YTick
624
end

spec/axes_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
axes.y_title = "Y title"
3333
axes.x_ticks = ['Jan', 'Feb', 'March', 'April', 'May', 'June', 'July',
3434
'August', 'September', 'October', 'November', 'December']
35+
axes.y_ticks = ['5', '10', '15', '20', '25', '30']
3536
end
3637

3738
it "plots stacked bar in a small size" do

0 commit comments

Comments
 (0)