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

Commit

Permalink
Added specs for subplots and minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
pgtgrly committed Jul 25, 2018
1 parent f5d6a24 commit 05c258e
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 19 deletions.
17 changes: 12 additions & 5 deletions examples/GRFramework/Example.rb
@@ -1,7 +1,14 @@
require "../../lib/grruby.rb"
GR.setviewport(0.1, 0.95, 0.1, 0.95)
GR.setviewport(0, 0.5, 0.1, 0.95)
GR.setwindow(-10, 10, -10, 10)
GR.setspace(-0.5, 0.5, 0, 90)
GR.axes(1, 1, 0.0, 0.0, 1, 1, 0.01)
GR.setviewport(0.5, 0.95, 0.1, 0.95)
GR.setwindow(-20, 50, -20, 50)
GR.axes(5, 5, 0.0, 0.0, 1, 1, 0.01)
GR.updatews()
puts("done")
hold=gets
#GR.setspace(-0.5, 0.5, 0, 90)
GR.setmarkersize(1.0)
GR.setmarkertype(-1)
GR.settextalign(2, 0)
Expand All @@ -18,7 +25,7 @@
GR.setcharheight(0.012)
GR.axes(1, 1, 0.0, 0.0, 1, 1, 0.01)
GR.updatews()
while (i<10) do
while (i<10) do
x.insert(-1,x[-1]+1*(-1)**j)
y.insert(-1,y[-1]*1.5)
if x.size >2 and y.size >2
Expand All @@ -28,7 +35,7 @@
GR.polymarker(x,y)
sleep(0.5)
GR.updatews()
i+=1
i+=1
end
sleep(1)
GR.clearws()
Expand All @@ -38,4 +45,4 @@
end

puts("done")
hold=gets
hold=gets
4 changes: 2 additions & 2 deletions lib/rubyplot/scripting_backends/gr/plots/bar.rb
Expand Up @@ -31,14 +31,14 @@ def call(state)
# tasks wont be pushed, rather they will be instantiated and called directly
(0..@data.size - 1).to_a.each do |i|
if @bar_edge
SetFillColorIndex.new(inqcolorfromrgb(@bar_edge_color)).call
SetFillColorIndex.new(hex_color_to_gr_color_index(@bar_edge_color)).call
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.origin[1], @data[i] + 2 * @bar_edge_width).call
end

SetFillColorIndex.new(inqcolorfromrgb(@bar_color)).call
SetFillColorIndex.new(hex_color_to_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,
Expand Down
4 changes: 2 additions & 2 deletions lib/rubyplot/scripting_backends/gr/plots/candlestick.rb
Expand Up @@ -25,9 +25,9 @@ def initialize(open_, high, low, close_, bar_gap, bar_width,
line_color = down_line_color
end
x_coord = i * (bar_width + bar_gap) + bar_width.to_f/2
@tasks.push(SetLineColorIndex.new(inqcolorfromrgb(line_color)))
@tasks.push(SetLineColorIndex.new(hex_color_to_gr_color_index(line_color)))
@tasks.push(Polyline.new([x_coord] * 2, [low[i], high[i]]))
@tasks.push(SetFillColorIndex.new(inqcolorfromrgb(bar_color)))
@tasks.push(SetFillColorIndex.new(hex_color_to_gr_color_index(bar_color)))
@tasks.push(SetFillInteriorStyle.new(GR_FILL_INTERIOR_STYLES[:solid]))
@tasks.push(FillRectangle.new(i * (bar_width + bar_gap),
i * (bar_width + bar_gap) + bar_width,
Expand Down
4 changes: 2 additions & 2 deletions lib/rubyplot/scripting_backends/gr/plots/line.rb
Expand Up @@ -13,7 +13,7 @@ def initialize(x_coordinates, y_coordinates, line_width: :default,
marker_color = COLOR_INDEX[:black] if marker_color == :default
marker_color = COLOR_INDEX[marker_color] if marker_color.is_a? Symbol
marker_type = :solid_circle if marker_type == :default
@tasks.push(SetMarkerColorIndex.new(inqcolorfromrgb(marker_color)))
@tasks.push(SetMarkerColorIndex.new(hex_color_to_gr_color_index(marker_color)))
@tasks.push(SetMarkerSize.new(marker_size))
@tasks.push(SetMarkerType.new(GR_MARKER_SHAPES[marker_type]))
@tasks.push(Polymarker.new(x_coordinates, y_coordinates))
Expand All @@ -22,7 +22,7 @@ def initialize(x_coordinates, y_coordinates, line_width: :default,
line_color = COLOR_INDEX[line_color] if line_color.is_a? Symbol
line_width = 1 if line_width == :default
line_type = :solid if line_type == :default
@tasks.push(SetLineColorIndex.new(inqcolorfromrgb(line_color)))
@tasks.push(SetLineColorIndex.new(hex_color_to_gr_color_index(line_color)))
@tasks.push(SetLineWidth.new(line_width))
@tasks.push(SetLineType.new(GR_LINE_TYPES[line_type]))
@tasks.push(Polyline.new(x_coordinates, y_coordinates))
Expand Down
2 changes: 1 addition & 1 deletion lib/rubyplot/scripting_backends/gr/plots/scatter.rb
Expand Up @@ -11,7 +11,7 @@ def initialize(x_coordinates, y_coordinates, marker_size: :default,
marker_color = COLOR_INDEX[marker_color] if marker_color.is_a? Symbol
marker_size = 1 if marker_size == :default
marker_type = :solid_circle if marker_type == :default
@tasks.push(SetMarkerColorIndex.new(inqcolorfromrgb(marker_color)))
@tasks.push(SetMarkerColorIndex.new(hex_color_to_gr_color_index(marker_color)))
@tasks.push(SetMarkerSize.new(marker_size))
@tasks.push(SetMarkerType.new(GR_MARKER_SHAPES[marker_type]))
@tasks.push(Polymarker.new(x_coordinates, y_coordinates))
Expand Down
4 changes: 2 additions & 2 deletions lib/rubyplot/scripting_backends/gr/plots/stacked_bar.rb
Expand Up @@ -35,15 +35,15 @@ def call(state)
bars = @data.map{|row| row[i]}
(0..bars.size-1).to_a.each do |j|
if @bar_edge
SetFillColorIndex.new(inqcolorfromrgb(@bar_edge_color)).call
SetFillColorIndex.new(hex_color_to_gr_color_index(@bar_edge_color)).call
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,
base, base + bars[j] + 2 * @bar_edge_width).call
end
bar_color = @bar_colors[j]
bar_color = COLOR_INDEX[bar_color] if bar_color.is_a? Symbol
SetFillColorIndex.new(inqcolorfromrgb(bar_color)).call
SetFillColorIndex.new(hex_color_to_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,
Expand Down
6 changes: 3 additions & 3 deletions lib/rubyplot/scripting_backends/gr/plots/stacked_bar_z.rb
Expand Up @@ -20,7 +20,7 @@ def initialize(data, bar_colors: :default, bar_width: :default,
@bar_edge = true if @bar_edge == :default
@bar_colors = CONTRASTING_COLORS if @bar_colors == :default
@bar_edge_color = COLOR_INDEX[:black] if @bar_edge_color == :default
@bar_edge_color = COLOR_INDEX[marker_color] if @bar_edge_color.is_a? Symbol
@bar_edge_color = COLOR_INDEX[@bar_edge_color] if @bar_edge_color.is_a? Symbol
@data = data
# All this will be repurposed
end
Expand All @@ -35,15 +35,15 @@ def call(state)
bar_heights = bar_heights.sort.reverse
(0..bar_heights.size-1).to_a.each do |j|
if @bar_edge
SetFillColorIndex.new(inqcolorfromrgb(@bar_edge_color)).call
SetFillColorIndex.new(hex_color_to_gr_color_index(@bar_edge_color)).call
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.origin[1], bar_heights[j] + 2 * @bar_edge_width).call
end
bar_color = @bar_colors[order[j]]
bar_color = COLOR_INDEX[bar_color] if bar_color.is_a? Symbol
SetFillColorIndex.new(inqcolorfromrgb(bar_color)).call
SetFillColorIndex.new(hex_color_to_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,
Expand Down
2 changes: 1 addition & 1 deletion lib/rubyplot/scripting_backends/gr/plotspace.rb
Expand Up @@ -32,7 +32,7 @@ def set_axis! # for internal use before drawing
SetCharHeight.new(0.012).call
@state.y_tick_count = 10 if @state.y_tick_count == :default
@state.x_tick_count = 10 if @state.x_tick_count == :default # 10 ticks by default
SetLineColorIndex.new(inqcolorfromrgb(COLOR_INDEX[:black])).call
SetLineColorIndex.new(hex_color_to_gr_color_index(COLOR_INDEX[:black])).call
SetLineWidth.new(1).call
SetLineType.new(GR_LINE_TYPES[:solid]).call
Grid.new((@state.x_range[1] - @state.x_range[0]).to_f / @state.x_tick_count,
Expand Down
31 changes: 30 additions & 1 deletion spec/scripting_layer/single_plot_graph_spec.rb
Expand Up @@ -123,10 +123,39 @@
end
end

context '#stacked_bar_z!' do
before do
@bars_data = [[12, 4, 53, 24],
[4, 34, 8, 25],
[20, 9, 31, 2],
[56, 12, 84, 30]]
end

it 'creates a stacked bar Z graph' do
a = Rubyplot::Figure.new
a.stacked_bar_z! @bars_data
a.save 'spec/reference_images/file_name.bmp'

expect(compare_with_reference?('file_name.bmp', 'single_plot_graph/' \
'stacked_bar_z_graph.bmp',
10)).to eq(true)
end

it 'creates a stacked bar Z graph with user defined colors' do
a = Rubyplot::Figure.new
a.stacked_bar! @bars_data,bar_colors: [:black, :red, :green, :blue]
a.save 'spec/reference_images/file_name.bmp'

expect(compare_with_reference?('file_name.bmp', 'single_plot_graph/' \
'user_color_stacked_bar_z_graph.bmp',
10)).to eq(true)
end
end

context '#line_plot!' do
it 'creates a simple line plot' do
a = Rubyplot::Figure.new
a.line_plot! @freqwise
a.line_plot_z! @freqwise
a.save 'spec/reference_images/file_name.bmp'

expect(compare_with_reference?('file_name.bmp', 'single_plot_graph/' \
Expand Down
55 changes: 55 additions & 0 deletions spec/scripting_layer/subplots_spec.rb
@@ -0,0 +1,55 @@
require 'spec_helper'

describe Rubyplot::Figure do
before do
@x1 = [1, 2, 3, 4, 5]
@y1 = [10, 20, 30, 40, 50]
@x2 = [2, 4, 16]
@y2 = [10, 20, -40]
@values = [0, 24, -12, 48]
@bars_data = [[12, 4, 53, 24],
[4, 34, 8, 25],
[20, 9, 31, 2],
[56, 12, 84, 30]]
@open = [10, 15, 24, 18]
@high = [20, 25, 30, 18]
@low = [5, 13, 15, 3]
@close = [15, 24, 18, 4]
end
after do
File.delete 'spec/reference_images/file_name.bmp'
end

context '#subplot!' do
it 'creates a Figure with 2 Subplots stacked vertically' do
a = Rubyplot::Figure.new
a.subplot!(2, 1, 1)
a.line! @x1, @y1
a.subplot!(2, 1, 2)
a.scatter! @x2, @y2
a.save 'spec/reference_images/file_name.bmp'

expect(compare_with_reference?('file_name.bmp', 'subplots/' \
'two_vertical.bmp', 10)).to eq(true)
end

it 'creates a 2x2 subplots with multiple plots in a figure' do
a = Rubyplot::Figure.new
a.subplot!(2, 2, 1)
a.line! @x1, @y1, marker_size: 1
a.scatter! @x2, @y2
a.subplot!(2, 2, 2)
a.scatter! @x2, @y2
a.subplot!(2, 2, 3)
a.line! @x1, @y1, line_color: :red, line_type: :dashed
a.subplot!(2, 2, 4)
a.candlestick! @open, @high, @low, @close, up_color: :blue, down_color: :black
a.subplot!(2, 2, 3)
a.bar! @values, bar_color: :orange, bar_gap: 1
a.save 'spec/reference_images/file_name.bmp'

expect(compare_with_reference?('file_name.bmp', 'subplots/' \
'two_by_two_grid.bmp', 10)).to eq(true)
end
end
end

0 comments on commit 05c258e

Please sign in to comment.