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
271 changes: 260 additions & 11 deletions lib/rubyplot/artist/axes.rb

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions lib/rubyplot/artist/axis.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require_relative 'axis/base'
require_relative 'axis/x_axis'
require_relative 'axis/x_dash_axis'
require_relative 'axis/y_axis'
require_relative 'axis/y_dash_axis'
2 changes: 1 addition & 1 deletion lib/rubyplot/artist/axis/x_axis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class XAxis < Axis::Base
def initialize axes
super
@abs_x1 = @axes.origin[0]
@abs_x2 = @axes.abs_x + @axes.width - @axes.y_axis_margin
@abs_x2 = @axes.origin[0] + @axes.width/2 - @axes.y_axis_margin
@major_ticks_distance = (@abs_x2 - @abs_x1) / @major_ticks_count
@length = (@abs_x2 - @abs_x1).abs
configure_axis_line
Expand Down
35 changes: 35 additions & 0 deletions lib/rubyplot/artist/axis/x_dash_axis.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require_relative 'base'

module Rubyplot
module Artist
class XDashAxis < Axis::Base
def initialize axes
super
@abs_x1 = @axes.origin[0]
@abs_x2 = @axes.abs_x + @axes.y_axis_margin
@major_ticks_distance = (@abs_x2 - @abs_x1) / @major_ticks_count
@length = (@abs_x2 - @abs_x1).abs
configure_axis_line
end

private

def configure_axis_line
@lines << Rubyplot::Artist::Line2D.new(
self, abs_x1: @abs_x1, abs_y1: @axes.origin[1], abs_x2: @abs_x2, abs_y2: @axes.origin[1],
stroke_width: @stroke_width
)
end

def configure_title
@texts << Rubyplot::Artist::Text.new(
@title,
self,
pointsize: @axes.marker_font_size,
abs_y: @axes.origin[1] + 20,
abs_x: @axes.origin[0] + (@abs_x2 - @abs_x1)/2
)
end
end # class XAxis
end # class Artist
end # module Rubyplot
2 changes: 1 addition & 1 deletion lib/rubyplot/artist/axis/y_axis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def initialize(*)
@abs_x1 = @axes.origin[0]
@abs_y1 = @axes.origin[1]
@abs_x2 = @axes.origin[0]
@abs_y2 = @axes.origin[1] - (@axes.height - @axes.x_axis_margin)
@abs_y2 = @axes.origin[1] - (@axes.height/2 - @axes.x_axis_margin)
@y_ticks = []
@length = (@abs_y1 - @abs_y2).abs
configure_axis_line
Expand Down
40 changes: 40 additions & 0 deletions lib/rubyplot/artist/axis/y_dash_axis.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module Rubyplot
module Artist
class YDashAxis < Axis::Base
def initialize(*)
super
@abs_x1 = @axes.origin[0]
@abs_y1 = @axes.origin[1]
@abs_x2 = @axes.origin[0]
@abs_y2 = @axes.origin[1] + (@axes.height/2 - @axes.x_axis_margin)
@y_ticks = []
@length = (@abs_y1 - @abs_y2).abs
configure_axis_line
end

private

def configure_axis_line
@lines << Rubyplot::Artist::Line2D.new(
self,
abs_x1: @abs_x1,
abs_y1: @abs_y1,
abs_x2: @abs_x2,
abs_y2: @abs_y2,
stroke_width: @stroke_width
)
end

def configure_title
@texts << Rubyplot::Artist::Text.new(
@title,
self,
rotation: -90.0,
abs_x: @axes.origin[0] - 10,
abs_y: (@abs_y1 - @abs_y2) / 2,
pointsize: @axes.marker_font_size
)
end
end # class YDashAxis
end # class Artist
end # module Rubyplot
5 changes: 2 additions & 3 deletions lib/rubyplot/artist/plot/line.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ def draw_lines
iy = @normalized_data[:y_values][idx_ix]
next if ix.nil? || iy.nil?

new_x = ix * (@axes.x_axis.abs_x2 - @axes.x_axis.abs_x1).abs + @axes.abs_x +
@axes.y_axis_margin
new_y = (@axes.y_axis.length - iy * @axes.y_axis.length) + @axes.abs_y
new_x = ix * (@axes.x_axis.length) + @axes.origin[0]
new_y = @axes.origin[1] - iy * @axes.y_axis.length

unless prev_x.nil? && prev_y.nil?
Rubyplot::Artist::Line2D.new(
Expand Down
4 changes: 2 additions & 2 deletions lib/rubyplot/artist/plot/multi_bars.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def configure_x_ticks
@axes.x_ticks = labels.map.with_index do |label, i|
Rubyplot::Artist::XTick.new(
@axes,
abs_x: @axes.abs_x + @axes.y_axis_margin + i * @max_slot_width + @max_slot_width / 2,
abs_x: @axes.abs_x + @axes.y_axis_margin*9 + i * @max_slot_width + @max_slot_width / 2,
abs_y: @axes.origin[1],
label: label,
length: 6,
Expand All @@ -64,7 +64,7 @@ def configure_x_ticks
def set_bar_dims bar_plot, index
bar_plot.bar_width = @max_bars_width / @bars_per_slot
@num_max_slots.times do |i|
bar_plot.abs_x_left[i] = @axes.abs_x + @axes.y_axis_margin +
bar_plot.abs_x_left[i] = @axes.abs_x + @axes.y_axis_margin*9 +
i * @max_slot_width + @padding / 2 + index * bar_plot.bar_width
bar_plot.abs_y_left[i] = @axes.origin[1] - @axes.x_axis.stroke_width
end
Expand Down
6 changes: 6 additions & 0 deletions lib/rubyplot/artist/tick.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
require_relative 'tick/base'
require_relative 'tick/x_tick'
require_relative 'tick/x_dash_tick'
require_relative 'tick/y_tick'
require_relative 'tick/y_dash_tick'
require_relative 'tick/minor_x_tick'
require_relative 'tick/minor_x_dash_tick'
require_relative 'tick/minor_y_tick'
require_relative 'tick/minor_y_dash_tick'
27 changes: 27 additions & 0 deletions lib/rubyplot/artist/tick/minor_x_dash_tick.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Rubyplot
module Artist
class MinorXDashTick < 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
)
if @label_text != '-0.00'
@label.draw
end
end
end # class MinorXDashTick
end # class Artist
end # module Rubyplot
25 changes: 25 additions & 0 deletions lib/rubyplot/artist/tick/minor_x_tick.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module Rubyplot
module Artist
class MinorXTick < 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 MinorXTick
end # class Artist
end # module Rubyplot
26 changes: 26 additions & 0 deletions lib/rubyplot/artist/tick/minor_y_dash_tick.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module Rubyplot
module Artist
class MinorYDashTick < Tick::Base
def initialize(*)
super
@label = Rubyplot::Artist::Text.new(
@label_text.to_s,
@owner,
abs_x: @abs_x - 5 - @label_distance,
abs_y: @abs_y + @length,
pointsize: @owner.marker_font_size,
)
end

def draw
@backend.draw_line(
x1: @abs_x, y1: @abs_y, x2: @abs_x - @length, y2: @abs_y,
stroke_opacity: @tick_opacity,
stroke_width: @tick_width)
if @label_text !='-0.00'
@label.draw
end
end
end # class MinorYTick
end # module Artist
end # module Rubyplot
26 changes: 26 additions & 0 deletions lib/rubyplot/artist/tick/minor_y_tick.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module Rubyplot
module Artist
class MinorYTick < Tick::Base
def initialize(*)
super
@label = Rubyplot::Artist::Text.new(
@label_text.to_s,
@owner,
abs_x: @abs_x - 5 - @label_distance,
abs_y: @abs_y + @length,
pointsize: @owner.marker_font_size,
)
end

def draw
@backend.draw_line(
x1: @abs_x, y1: @abs_y, x2: @abs_x - @length, y2: @abs_y,
stroke_opacity: @tick_opacity,
stroke_width: @tick_width)
if @label_text != '0.00'
@label.draw
end
end
end # class MinorYTick
end # module Artist
end # module Rubyplot
27 changes: 27 additions & 0 deletions lib/rubyplot/artist/tick/x_dash_tick.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Rubyplot
module Artist
class XDashTick < 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
)
if @label_text != '-0.00'
@label.draw
end
end
end # class XDashTick
end # class Artist
end # module Rubyplot
26 changes: 26 additions & 0 deletions lib/rubyplot/artist/tick/y_dash_tick.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module Rubyplot
module Artist
class YDashTick < Tick::Base
def initialize(*)
super
@label = Rubyplot::Artist::Text.new(
@label_text.to_s,
@owner,
abs_x: @abs_x - 5 - @label_distance,
abs_y: @abs_y + @length,
pointsize: @owner.marker_font_size,
)
end

def draw
@backend.draw_line(
x1: @abs_x, y1: @abs_y, x2: @abs_x - @length, y2: @abs_y,
stroke_opacity: @tick_opacity,
stroke_width: @tick_width)
if @label_text !='-0.00'
@label.draw
end
end
end # class YTick
end # module Artist
end # module Rubyplot
4 changes: 3 additions & 1 deletion lib/rubyplot/artist/tick/y_tick.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ def draw
x1: @abs_x, y1: @abs_y, x2: @abs_x - @length, y2: @abs_y,
stroke_opacity: @tick_opacity,
stroke_width: @tick_width)
@label.draw
if @label_text != '0.00'
@label.draw
end
end
end # class YTick
end # module Artist
Expand Down
10 changes: 10 additions & 0 deletions lib/rubyplot/backend/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Rubyplot
module Backend
class Base
# Total height and width of the canvas in pixels.
attr_accessor :canvas_height, :canvas_width

attr_accessor :active_axes, :figure
end # class Base
end # module Backend
end # module Rubyplot
Loading