Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions lib/rubyplot/artist/axes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,12 @@ class Axes < Base
attr_reader :y_axis
# Array of X ticks.
attr_reader :x_ticks
# Array of Y ticks.
attr_reader :y_ticks
# Number of X ticks.
attr_accessor :num_x_ticks
# Number of Y ticks.
attr_accessor :num_y_ticks

# @param figure [Rubyplot::Figure] Figure object to which this Axes belongs.
def initialize figure
Expand Down Expand Up @@ -89,7 +93,9 @@ def initialize figure
@x_axis = Rubyplot::Artist::XAxis.new(self)
@y_axis = Rubyplot::Artist::YAxis.new(self)
@x_ticks = nil
@y_ticks = nil
@num_x_ticks = 5
@num_y_ticks = 4

@legend_box_position = :top
end
Expand Down Expand Up @@ -125,6 +131,7 @@ def draw
configure_title
configure_legends
assign_x_ticks
assign_y_ticks
actually_draw
end

Expand Down Expand Up @@ -197,6 +204,10 @@ def x_ticks= x_ticks
@x_ticks = x_ticks
end

def y_ticks= y_ticks
@y_ticks = y_ticks
end

def x_title= x_title
@x_axis.title = x_title
end
Expand Down Expand Up @@ -236,6 +247,26 @@ def assign_x_ticks
end
end

def assign_y_ticks
unless @y_ticks
val_distance = (@y_range[1] - @y_range[0]).abs / @num_y_ticks.to_f
@y_ticks = (@y_range[0]..@y_range[1]).step(val_distance).map { |i| i }
end
unless @y_ticks.all? { |t| t.is_a?(Rubyplot::Artist::YTick) }
inter_ticks_distance = @y_axis.length / (@num_y_ticks - 1)
@y_ticks.map!.with_index do |tick_label, i|
Rubyplot::Artist::YTick.new(
self,
abs_x: @origin[0] - @x_axis_margin,
abs_y: @y_axis.abs_y1 - (i * inter_ticks_distance),
label: Rubyplot::Utils.format_label(tick_label),
length: 6,
label_distance: 10
)
end
end
end

def add_plot plot_type, *args, &block
plot = with_backend plot_type, *args
yield(plot) if block_given?
Expand Down Expand Up @@ -291,6 +322,7 @@ def normalize_plotting_data
def actually_draw
@x_axis.draw
@x_ticks.each(&:draw)
@y_ticks.each(&:draw)
@y_axis.draw
@title.draw
@legend_box.draw
Expand Down
18 changes: 18 additions & 0 deletions lib/rubyplot/artist/tick/y_tick.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
module Rubyplot
module Artist
class YTick < Tick::Base
def initialize(*)
super
@label = Rubyplot::Artist::Text.new(
@label_text.to_s,
@owner,
abs_x: @abs_x - 5,
abs_y: @abs_y + @length + @label_distance,
pointsize: @owner.marker_font_size,
)
end

def draw
@backend.draw_line(
x1: @abs_x, y1: @abs_y, x2: @abs_x, y2: @abs_y + @length,
stroke_opacity: @tick_opacity,
stroke_width: @tick_width)
@label.draw
end
end # class YTick
end # module Artist
end # module Rubyplot
5 changes: 3 additions & 2 deletions spec/axes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@
axes.stacked_bar! do |p|
p.data data
p.label = label
end
end
end
axes.title = "net earnings in different months."
axes.x_ticks = ['Jan', 'Feb', 'March', 'April', 'May', 'June', 'July',
'August', 'September', 'October', 'November', 'December']

axes.y_ticks = ['5', '10', '15', '20', '25', '30']

file = "/#{Rubyplot.backend}_multiple_stacked_bar.png"
fig.write(@temp_dir + file)

Expand Down