8000 Merge pull request #52 from alishdipani/master · SciRuby/rubyplot@a81b2c4 · GitHub
[go: up one dir, main page]

Skip to content

Commit a81b2c4

Browse files
authored
Merge pull request #52 from alishdipani/master
Added show function for GR and Magick backends
2 parents 1b1f765 + 3624962 commit a81b2c4

File tree

13 files changed

+2828
-80
lines changed

13 files changed

+2828
-80
lines changed

lib/rubyplot.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,23 @@ module Rubyplot
144144
:double_line_single_ended,
145145
:double_line_double_ended
146146
].freeze
147-
148-
class << self
147+
148+
class << self
149+
150+
attr_accessor :iruby_inline
151+
149152
def backend
150153
@backend
151154
end
152155

156+
def inline
157+
@iruby_inline = true
158+
end
159+
160+
def stop_inline
161+
@iruby_inline = false
162+
end
163+
153164
def set_backend b
154165
case b
155166
when :magick

lib/rubyplot/artist/axes.rb

Lines changed: 42 additions & 0 deletions
+
@y_axis.minor_ticks = []
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,27 @@ def assign_x_ticks
256256
)
257257
end
258258
end
259+
260+
@x_axis.minor_ticks = []
261+
@x_axis.major_ticks.each_with_index do |major_tick, i|
262+
minor_tick_value_distance = value_distance / (@x_axis.minor_ticks_count.to_f + 1)
263+
if i < (@x_axis.major_ticks_count-1) # Skip the last tick
264+
for j in 1..@x_axis.minor_ticks_count do
265+
@x_axis.minor_ticks.push(major_tick.coord + j * minor_tick_value_distance)
266+
end
267+
end
268+
end
269+
270+
unless @x_axis.minor_ticks.all? { |t| t.is_a?(Rubyplot::Artist::XTick) }
271+
@x_axis.minor_ticks.map!.with_index do |coord, i|
272+
Rubyplot::Artist::XTick.new(
273+
self,
274+
coord: coord,
275+
label: nil,
276+
tick_size: @x_axis.major_ticks[0].tick_size/2
277+
)
278+
end
279+
end
259280
end
260281

261282
def assign_y_ticks
@@ -273,6 +294,27 @@ de 179F f assign_y_ticks
273294
)
274295
end
275296
end
297+
298
299+
@y_axis.major_ticks.each_with_index do |major_tick, i|
300+
minor_tick_value_distance = value_distance / (@y_axis.minor_ticks_count.to_f + 1)
301+
if i < (@y_axis.major_ticks_count-1) # Skip the last tick
302+
for j in 1..@y_axis.minor_ticks_count do # Skip the 0th index as major tick is already present
303+
@y_axis.minor_ticks.push(major_tick.coord + j * minor_tick_value_distance)
304+
end
305+
end
306+
end
307+
308+
unless @y_axis.minor_ticks.all? { |t| t.is_a?(Rubyplot::Artist::YTick) }
309+
@y_axis.minor_ticks.map!.with_index do |coord, i|
310+
Rubyplot::Artist::XTick.new(
311+
self,
312+
coord: coord,
313+
label: nil,
314+
tick_size: @y_axis.major_ticks[0].tick_size/2
315+
)
316+
end
317+
end
276318
end
277319

278320
# Figure out the co-ordinates of the title text w.r.t Axes.

lib/rubyplot/artist/figure.rb

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,29 +95,27 @@ def add_subplot!(nrow, ncol)
9595
#
9696
# @param file_name [String] File name to output to.
9797
def write(file_name, device: :file)
98-
Rubyplot.backend.canvas_height = @height
99-
Rubyplot.backend.canvas_width = @width
100-
Rubyplot.backend.figure = self
101-
Rubyplot.backend.init_output_device(file_name, device: :file)
102-
@subplots.each { |i| i.each(&:process_data) }
103-
@subplots.each { |i| i.each(&:draw) }
104-
Rubyplot.backend.write
105-
Rubyplot.backend.stop_output_device
98+
print_on_device(file_name, device)
10699
end
107100

108101
def show
102+
Rubyplot.backend.output_device = Rubyplot.iruby_inline ? :iruby : :window
103+
print_on_device(nil, Rubyplot.backend.output_device)
104+
end
105+
106+
private
107+
108+
def print_on_device(file_name, device)
109109
Rubyplot.backend.canvas_height = @height
110110
Rubyplot.backend.canvas_width = @width
111111
Rubyplot.backend.figure = self
112-
Rubyplot.backend.init_output_device(nil, device: nil)
112+
Rubyplot.backend.init_output_device(file_name, device: device)
113113
@subplots.each { |i| i.each(&:process_data) }
114114
@subplots.each { |i| i.each(&:draw) }
115115
Rubyplot.backend.show
116116
Rubyplot.backend.stop_output_device
117117
end
118118

119-
private
120-
121119
def set_rubyplot_artist_coords!
122120
@max_x = 100.0
123121
@max_y = 100.0

lib/rubyplot/artist/plot/area.rb

100644100755
Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@ class Area < Artist::Plot::Base
66

77
def initialize(*)
88
super
9-
@sort_data = false
9+
@sort_data = true
10+
@fill_opacity = 0.3
1011
end
1112

12-
def data y_values, x_values=[]
13-
x_values = Array.new(y_values.size) { |i| i } if x_values.empty?
14-
y_values.sort! if @sort_data
13+
def stacked(bool)
14+
@fill_opacity = 1 if bool
15+
end
16+
17+
def data x_values, y_values
18+
x_values, y_values = x_values.zip(y_values).sort.transpose if @sort_data
1519
super(x_values, y_values)
1620
end
1721

@@ -22,7 +26,7 @@ def draw
2226
x: x_poly_points,
2327
y: y_poly_points,
2428
color: @data[:color],
25-
fill_opacity: 0.3
29+
fill_opacity: @fill_opacity
2630
).draw
2731
end
2832
end # class Area

lib/rubyplot/artist/plot/basic_plot.rb

Lines changed: 111 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,125 @@ module Rubyplot
22
module Artist
33
module Plot
44
class BasicPlot < Artist::Plot::Base
5-
attr_accessor :marker
5+
attr_accessor :marker_type
66
attr_accessor :marker_size
7-
7+
attr_accessor :marker_border_color
8+
attr_accessor :marker_fill_color
9+
attr_accessor :line_color
10+
attr_accessor :line_type
11+
attr_accessor :line_width
12+
13+
COLOR_TYPES_FMT = {
14+
'b' => :blue,
15+
'g' => :green,
16+
'r' => :red,
17+
'c' => :cyan,
18+
'm' => :magenta,
19+
'y' => :yellow,
20+
' D411 k' => :black,
21+
'w' => :white
22+
}.freeze
23+
24+
MARKER_TYPES_FMT = {
25+
'.' => :dot,
26+
',' => :omark,
27+
'o' => :circle,
28+
'v' => :traingle_down,
29+
'^' => :traingle_up,
30+
'<' => :solid_tri_left,
31+
'>' => :solid_tri_right,
32+
'1' => :solid_triangle_down,
33+
'2' => :solid_triangle_up,
34+
'3' => :solid_tri_left,
35+
'4' => :solid_tri_right,
36+
's' => :square,
37+
'p' => :pentagon,
38+
'*' => :star,
39+
'h' => :hexagon,
40+
'H' => :heptagon,
41+
'+' => :plus,
42+
'x' => :diagonal_cross,
43+
'D' => :solid_diamond,
44+
'd' => :diamond,
45+
'|' => :vline,
46+
'_' => :hline
47+
}.freeze
48+
49+
LINE_TYPES_FMT ={
50+
'--' => :dashed,
51+
'-.' => :dashed_dotted,
52+
'-' => :solid,
53+
':' => :dotted
54+
}.freeze
55+
856
def initialize(*)
957
super
10-
@marker = :dot
58+
@marker_type = nil
1159
@marker_size = 1.0
60+
@marker_border_color = :default
61+
# set fill to nil for the benefit of hollow markers so that legend
62+
# color defaults to :black in case user does not specify.
63+
@marker_fill_color = nil
64+
@line_color = :default
65+
@line_type = nil
66+
@line_width = 1.0
1267
end
1368

14-
def draw
15-
line_style = @marker.to_s.match /(.*)_line\z/
16-
if line_style
17-
Rubyplot::Artist::Line2D.new(
18-
self,
19-
x: @data[:x_values],
20-
y: @data[:y_values],
21-
type: line_style[1].to_sym,
22-
color: @data[:color]
23-
).draw
24-
else
25-
# FIXME: this should probably be inside a 'Collections' class that will
26-
# allow the user to customise individual parameters.
27-
Rubyplot.backend.draw_markers(
28-
x: @data[:x_values],
29-
y: @data[:y_values],
30-
type: @marker,
31-
fill_color: @data[:color],
32-
size: [@marker_size] * @data[:x_values].size
33-
)
69+
def color
70+
@line_color || @marker_fill_color || @marker_border_color || :default
71+
end
72+
73+
def fmt=(fmt)
74+
unless fmt.is_a? String
75+
raise TypeError, 'fmt argument takes a String input'
76+
end
77+
78+
COLOR_TYPES_FMT.each do |symbol, color|
79+
if fmt.include? symbol
80+
@marker_fill_color = color
81+
@marker_border_color = color
82+
@line_color = color
83+
break
84+
end
85+
end
86+
87+
MARKER_TYPES_FMT.each do |symbol, marker_type|
88+
if fmt.include? symbol
89+
@marker_type = marker_type
90+
break
91+
end
92+
end
93+
94+
LINE_TYPES_FMT.each do |symbol, line_type|
95+
if fmt.include? symbol
96+
@line_type = line_type
97+
break
98+
end
3499
end
35100
end
101+
102+
def draw
103+
# Default marker fill color
104+
@marker_fill_color = :default if @marker_fill_color.nil?
105+
# defualt type of plot is solid line
106+
@line_type = :solid if @line_type.nil? && @marker_type.nil?
107+
Rubyplot::Artist::Line2D.new(
108+
self,
109+
x: @data[:x_values],
110+
y: @data[:y_values],
111+
type: @line_type,
112+
color: @line_color,
113+
width: @line_width
114+
).draw if @line_type
115+
Rubyplot.backend.draw_markers(
116+
x: @data[:x_values],
117+
y: @data[:y_values],
118+
type: @marker_type,
119+
fill_color: @marker_fill_color,
120+
border_color: @marker_border_color,
121+
size: [@marker_size] * @data[:x_values].size
122+
) if @marker_type
123+
end
36124
end # class BasicPlot
37125
end # module Plot
38126
end # module Artist

lib/rubyplot/artist/plot/bubble.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@ class Bubble < Artist::Plot::Base
55
# Width in pixels of the border of each bubble.
66
attr_reader :border_width
77
attr_reader :z_max, :z_min
8+
# Opacity of the circles
9+
attr_accessor :fill_opacity
10+
811
def initialize(*)
912
super
1013
@bubbles = []
1114
@border_width = 1.0
15+
@fill_opacity = 0.5
1216
end
1317

1418
def data x_values, y_values, z_values
@@ -23,9 +27,9 @@ def draw
2327
x: @data[:x_values][idx],
2428
y: @data[:y_values][idx],
2529
radius: @data[:z_values][idx],
26-
fill_opacity: 0.5,
30+
fill_opacity: @fill_opacity,
2731
color: @data[:color],
28-
border_width: 1,
32+
border_width: @border_width,
2933
abs: false
3034
).draw
3135
end

lib/rubyplot/artist/plot/histogram.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@ class Histogram < Artist::Plot::Base
88
attr_accessor :bins
99
# Width of each bar.
1010
attr_accessor :bar_width
11-
11+
1212
def initialize(*)
13-
super
13+
super
14+
end
15+
16+
def data x_values
17+
@x = x_values
1418
end
1519

1620
def process_data

lib/rubyplot/artist/plot/line.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ class Line < Artist::Plot::Base
66
attr_writer :line_type
77
# The number of times that you want the width to be of the graphic device. Default 1.0.
88
attr_writer :line_width
9-
# Color of the line. Default black.
10-
attr_writer :line_color
9+
10+
def line_color=(color)
11+
@line_color = color
12+
@data[:color] = color
13+
end
1114

1215
def initialize(*)
1316
super
@@ -16,8 +19,7 @@ def initialize(*)
1619
@line_color = :black
1720
end
1821

19-
def data(y_values, x_values=[])
20-
x_values = Array.new(y_values.size) { |i| i } if x_values.empty?
22+
def data(x_values, y_values)
2123
super x_values, y_values
2224
end
2325

@@ -34,4 +36,3 @@ def draw
3436
end # module Plot
3537
end # module Artist
3638
end # module Rubyplot
37-

lib/rubyplot/backend/base.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class Base
55
# Total height and width of the canvas in pixels.
66
attr_accessor :canvas_height, :canvas_width
77

8-
attr_accessor :active_axes, :figure
8+
attr_accessor :active_axes, :figure, :output_device
99

1010
# Write text anywhere on the canvas. abs_x and abs_y should be specified in terms
1111
# of Rubyplot Artist Co-ordinates.

0 commit comments

Comments
 (0)
0