Skip to content
This repository has been archived by the owner on Dec 24, 2022. It is now read-only.

Commit

Permalink
Bar Plot Debugged
Browse files Browse the repository at this point in the history
  • Loading branch information
pgtgrly committed Jul 14, 2018
1 parent 21cc735 commit 25b251d
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 15 deletions.
15 changes: 12 additions & 3 deletions examples/RubyPlot/BarPlotSpace.rb
@@ -1,5 +1,14 @@
require_relative '../../lib/grruby'
values = [0, 24, 12, 48]
values = [0, 24, -12, 48]
RM = 1e4
CM = 1e-3
V = [50]
t = [1]
(1..1000).to_a.each do
V << (V[-1] - V[-1] * 0.1 / (RM * CM))
t << (t[-1] + 0.1)
end
a = Rubyplot::Figure.new
a.bar! values, bar_color: :blue, bar_gap: 1
a.view
a.bar! values, bar_color: :green, bar_gap: 1
a.line!(t, V)
a.view
2 changes: 1 addition & 1 deletion lib/rubyplot/figure.rb
Expand Up @@ -12,7 +12,7 @@ def initialize(backend: :default)
@tasks = []
@x_range = [0, 0]
@y_range = [0, 0]
@origin = [0, 0]
@origin = [:default, :default]
@x_tick_count = :default
@y_tick_count = :default
@title = nil
Expand Down
15 changes: 10 additions & 5 deletions lib/rubyplot/scripting_backends/gr/gr_backend.rb
Expand Up @@ -41,20 +41,25 @@ def line!(x_coordinates, y_coordinates, line_width: :default,
end

def bar!(data, bar_color: :default, bar_width: :default,
bar_gap: :default, bar_edge: :default, bar_edge_color: :default,
bar_edge_width: :default)
bar_gap: :default, bar_edge: :default, bar_edge_color: :default,
bar_edge_width: :default)

@x_range[0] = 0 if @x_range[0].nil?
@x_range[1] = data.length if @x_range[0].nil?
@x_range[1] = data.length if data.length > @x_range[1]
bar_gap = 0 if bar_gap == :default
bar_width = 1 if bar_width == :default
bar_edge_width = 0.03 if bar_edge_width == :default
x_length = data.length * (bar_width + bar_gap) + bar_width + bar_edge_width
@x_range[1] = x_length if x_length > @x_range[1]

@y_range[0] = data.min if @y_range[0].nil?
@y_range[1] = data.max if @y_range[1].nil?
@y_range[0] = data.min if data.min < @y_range[0]
@y_range[1] = data.max if data.max > @y_range[1]
@tasks.push(Bar.new(data, bar_color: bar_color, bar_width: bar_width,
bar_gap: bar_gap, bar_edge: bar_edge,
bar_edge_color: bar_edge_color, bar_edge_width: bar_edge_width))
bar_gap: bar_gap, bar_edge: bar_edge,
bar_edge_color: bar_edge_color,
bar_edge_width: bar_edge_width))
end

def view
Expand Down
6 changes: 3 additions & 3 deletions lib/rubyplot/scripting_backends/gr/plots/bar.rb
Expand Up @@ -17,9 +17,9 @@ def initialize(data, bar_color: :default, bar_width: :default,
@bar_color = :easy_blue if @bar_color == :default
@bar_gap = 0 if @bar_gap == :default
@bar_width = 1 if @bar_width == :default
@bar_edge_width = 0.053 if @bar_edge_width == :default
@bar_edge = true if @bar_edge == :default
@bar_edge_color = :black if @bar_edge_color == :default
@bar_edge_width = 0.02 if @bar_edge_width == :default
@data = data
# All this will be repurposed
end
Expand All @@ -34,14 +34,14 @@ def call(state)
SetFillInteriorStyle.new(GR_FILL_INTERIOR_STYLES[:solid]).call
FillRectangle.new(i * (@bar_width + @bar_gap) - @bar_edge_width,
i * (@bar_width + @bar_gap) + @bar_width + @bar_edge_width,
state.y_range[0] - state.y_axis_padding, @data[i] + @bar_edge_width).call
state.origin[1], @data[i] + 2 * @bar_edge_width).call
end

SetFillColorIndex.new(GR_COLOR_INDEX[@bar_color]).call
SetFillInteriorStyle.new(GR_FILL_INTERIOR_STYLES[:solid]).call
FillRectangle.new(i * (@bar_width + @bar_gap),
i * (@bar_width + @bar_gap) + @bar_width,
state.y_range[0] - state.y_axis_padding, @data[i]).call
state.origin[1], @data[i]).call
end
end
end
Expand Down
13 changes: 10 additions & 3 deletions lib/rubyplot/scripting_backends/gr/plotspace.rb
Expand Up @@ -9,10 +9,17 @@ def set_axis! # for internal use before drawing
#Automate tick sizes so that it is not too conjested
@state.title_shift = 0.1 unless @state.title.nil?

if @state.x_axis_padding == :default
@state.x_axis_padding = Math.log((@state.x_range[1] - @state.x_range[0]), 10).round
end
@state.x_axis_padding = Math.log((@state.x_range[1] - @state.x_range[0]), 10).round
@state.y_axis_padding = Math.log((@state.y_range[1] - @state.y_range[0]), 10).round

if @state.origin[0] == :default
@state.origin[0] = @state.x_range[0] - @state.x_axis_padding
end

if @state.origin[1] == :default
@state.origin[1] = @state.y_range[0] - @state.y_axis_padding
end

SetViewPort.new(0.05, 0.99, 0.05, 0.99 - @state.title_shift).call
SetWindow.new(@state.x_range[0] - @state.x_axis_padding, @state.x_range[1] + @state.x_axis_padding,
@state.y_range[0] - @state.y_axis_padding, @state.y_range[1] + @state.y_axis_padding).call
Expand Down

0 comments on commit 25b251d

Please sign in to comment.