rubyplot 0.0.1 → 0.1.pre.a1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +11 -0
  3. data/.rspec +3 -0
  4. data/.rubocop.yml +133 -0
  5. data/.travis.yml +18 -0
  6. data/CHANGELOG.md +9 -0
  7. data/CONTRIBUTING.md +48 -0
  8. data/Gemfile +6 -0
  9. data/README.md +47 -0
  10. data/Rakefile +32 -0
  11. data/ext/grruby/extconf.rb +6 -0
  12. data/ext/grruby/grruby.c +1163 -0
  13. data/ext/grruby/grruby.h +135 -0
  14. data/lib/rubyplot.rb +30 -1
  15. data/lib/rubyplot/artist.rb +13 -0
  16. data/lib/rubyplot/artist/axes.rb +328 -0
  17. data/lib/rubyplot/artist/axis.rb +3 -0
  18. data/lib/rubyplot/artist/axis/base.rb +34 -0
  19. data/lib/rubyplot/artist/axis/x_axis.rb +35 -0
  20. data/lib/rubyplot/artist/axis/y_axis.rb +40 -0
  21. data/lib/rubyplot/artist/base.rb +14 -0
  22. data/lib/rubyplot/artist/circle.rb +28 -0
  23. data/lib/rubyplot/artist/figure.rb +90 -0
  24. data/lib/rubyplot/artist/legend.rb +59 -0
  25. data/lib/rubyplot/artist/legend_box.rb +89 -0
  26. data/lib/rubyplot/artist/line2d.rb +24 -0
  27. data/lib/rubyplot/artist/plot.rb +9 -0
  28. data/lib/rubyplot/artist/plot/area.rb +38 -0
  29. data/lib/rubyplot/artist/plot/bar.rb +69 -0
  30. data/lib/rubyplot/artist/plot/bar_type.rb +31 -0
  31. data/lib/rubyplot/artist/plot/base.rb +67 -0
  32. data/lib/rubyplot/artist/plot/bubble.rb +41 -0
  33. data/lib/rubyplot/artist/plot/line.rb +61 -0
  34. data/lib/rubyplot/artist/plot/multi_bars.rb +75 -0
  35. data/lib/rubyplot/artist/plot/multi_stacked_bar.rb +78 -0
  36. data/lib/rubyplot/artist/plot/scatter.rb +29 -0
  37. data/lib/rubyplot/artist/plot/stacked_bar.rb +69 -0
  38. data/lib/rubyplot/artist/polygon.rb +21 -0
  39. data/lib/rubyplot/artist/rectangle.rb +39 -0
  40. data/lib/rubyplot/artist/text.rb +49 -0
  41. data/lib/rubyplot/artist/tick.rb +3 -0
  42. data/lib/rubyplot/artist/tick/base.rb +35 -0
  43. data/lib/rubyplot/artist/tick/x_tick.rb +25 -0
  44. data/lib/rubyplot/artist/tick/y_tick.rb +24 -0
  45. data/lib/rubyplot/backend.rb +2 -0
  46. data/lib/rubyplot/backend/gr_wrapper.rb +7 -0
  47. data/lib/rubyplot/backend/magick_wrapper.rb +141 -0
  48. data/lib/rubyplot/color.rb +992 -0
  49. data/lib/rubyplot/figure.rb +2 -0
  50. data/lib/rubyplot/spi.rb +8 -0
  51. data/lib/rubyplot/subplot.rb +4 -0
  52. data/lib/rubyplot/themes.rb +47 -0
  53. data/lib/rubyplot/utils.rb +14 -0
  54. data/lib/rubyplot/version.rb +1 -1
  55. data/rubyplot.gemspec +10 -0
  56. data/spec/axes_spec.rb +477 -0
  57. data/spec/figure_spec.rb +12 -0
  58. data/spec/spec_helper.rb +62 -0
  59. data/spec/spi/multi_plot_graph_spec.rb +33 -0
  60. data/spec/spi/single_plot_graph_spec.rb +227 -0
  61. data/spec/spi/subplots_spec.rb +55 -0
  62. metadata +166 -8
@@ -0,0 +1,135 @@
1
+ #ifndef GRRUBY_H
2
+ #define GRRUBY_H
3
+
4
+ #include <ruby.h>
5
+ #include <gr.h>
6
+
7
+ double* rb_ar_2_dbl_ar(VALUE);
8
+ int* rb_ar_2_int_ar(VALUE);
9
+ static VALUE opengks(VALUE);
10
+ static VALUE closegks(VALUE);
11
+ static VALUE inqdspsize(VALUE,VALUE,VALUE,VALUE,VALUE);
12
+ static VALUE openws(VALUE,VALUE,VALUE,VALUE);
13
+ static VALUE closews(VALUE,VALUE);
14
+ static VALUE activatews(VALUE,VALUE);
15
+ static VALUE deactivatews(VALUE,VALUE);
16
+ static VALUE clearws(VALUE);
17
+ static VALUE updatews(VALUE);
18
+ static VALUE polyline(VALUE,VALUE,VALUE);
19
+ static VALUE polymarker(VALUE,VALUE,VALUE);
20
+ static VALUE text(VALUE,VALUE,VALUE,VALUE);
21
+ static VALUE inqtext(VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
22
+ static VALUE fillarea(VALUE,VALUE,VALUE);
23
+ static VALUE cellarray(VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
24
+ static VALUE gdp(VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
25
+ static VALUE spline(VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
26
+ static VALUE gridit(VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
27
+ static VALUE setlinetype(VALUE,VALUE);
28
+ static VALUE inqlinetype(VALUE,VALUE);
29
+ static VALUE setlinewidth(VALUE,VALUE);
30
+ static VALUE inqlinewidth(VALUE,VALUE);
31
+ static VALUE setlinecolorind(VALUE,VALUE);
32
+ static VALUE inqlinecolorind(VALUE,VALUE);
33
+ static VALUE setmarkertype(VALUE,VALUE);
34
+ static VALUE inqmarkertype(VALUE,VALUE);
35
+ static VALUE setmarkersize(VALUE,VALUE);
36
+ static VALUE setmarkercolorind(VALUE,VALUE);
37
+ static VALUE inqmarkercolorind(VALUE,VALUE);
38
+ static VALUE settextfontprec(VALUE,VALUE,VALUE);
39
+ static VALUE setcharexpan(VALUE,VALUE);
40
+ static VALUE setcharspace(VALUE,VALUE);
41
+ static VALUE settextcolorind(VALUE,VALUE);
42
+ static VALUE setcharheight(VALUE,VALUE);
43
+ static VALUE setcharup(VALUE,VALUE,VALUE);
44
+ static VALUE settextpath(VALUE,VALUE);
45
+ static VALUE settextalign(VALUE,VALUE,VALUE);
46
+ static VALUE setfillintstyle(VALUE,VALUE);
47
+ static VALUE setfillstyle(VALUE,VALUE);
48
+ static VALUE setfillcolorind(VALUE,VALUE);
49
+ static VALUE setcolorrep(VALUE,VALUE,VALUE,VALUE,VALUE);
50
+ static VALUE setwindow(VALUE,VALUE,VALUE,VALUE,VALUE);
51
+ static VALUE inqwindow(VALUE,VALUE,VALUE,VALUE,VALUE);
52
+ static VALUE setviewport(VALUE,VALUE,VALUE,VALUE,VALUE);
53
+ static VALUE inqviewport(VALUE,VALUE,VALUE,VALUE,VALUE);
54
+ static VALUE selntran(VALUE,VALUE);
55
+ static VALUE setclip(VALUE,VALUE);
56
+ static VALUE setwswindow(VALUE,VALUE,VALUE,VALUE,VALUE);
57
+ static VALUE setwsviewport(VALUE,VALUE,VALUE,VALUE,VALUE);
58
+ static VALUE createseg(VALUE,VALUE);
59
+ static VALUE copysegws(VALUE,VALUE);
60
+ static VALUE redrawsegws(VALUE);
61
+ static VALUE setsegtran(VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
62
+ static VALUE closeseg(VALUE);
63
+ static VALUE emergencyclosegks(VALUE);
64
+ static VALUE updategks(VALUE);
65
+ static VALUE setspace(VALUE,VALUE,VALUE,VALUE,VALUE);
66
+ static VALUE inqspace(VALUE,VALUE,VALUE,VALUE,VALUE);
67
+ static VALUE setscale(VALUE,VALUE);
68
+ static VALUE inqscale(VALUE,VALUE);
69
+ static VALUE textext(VALUE,VALUE,VALUE,VALUE);
70
+ static VALUE inqtextext(VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
71
+ static VALUE axes(VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
72
+ static VALUE grid(VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
73
+ static VALUE grid3d(VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
74
+ static VALUE verrorbars(VALUE,VALUE,VALUE,VALUE,VALUE);
75
+ static VALUE herrorbars(VALUE,VALUE,VALUE,VALUE,VALUE);
76
+ static VALUE polyline3d(VALUE,VALUE,VALUE,VALUE);
77
+ static VALUE polymarker3d(VALUE,VALUE,VALUE,VALUE);
78
+ static VALUE axes3d(VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
79
+ static VALUE titles3d(VALUE,VALUE,VALUE,VALUE);
80
+ static VALUE surface(VALUE,VALUE,VALUE,VALUE,VALUE);
81
+ static VALUE contour(VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
82
+ static VALUE tricontour(VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
83
+ static VALUE hexbin(VALUE,VALUE,VALUE,VALUE,VALUE);
84
+ static VALUE setcolormap(VALUE,VALUE);
85
+ static VALUE inqcolormap(VALUE,VALUE);
86
+ static VALUE colorbar(VALUE);
87
+ static VALUE inqcolor(VALUE,VALUE,VALUE);
88
+ static VALUE inqcolorfromrgb(VALUE,VALUE,VALUE,VALUE);
89
+ static VALUE hsvtorgb(VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
90
+ static VALUE tick(VALUE,VALUE,VALUE);
91
+ static VALUE validaterange(VALUE,VALUE,VALUE);
92
+ static VALUE adjustlimits(VALUE,VALUE,VALUE);
93
+ static VALUE adjustrange(VALUE,VALUE,VALUE);
94
+ static VALUE beginprint(VALUE,VALUE);
95
+ static VALUE beginprintext(VALUE,VALUE,VALUE,VALUE,VALUE);
96
+ static VALUE endprint(VALUE);
97
+ static VALUE ndctowc(VALUE,VALUE,VALUE);
98
+ static VALUE wctondc(VALUE,VALUE,VALUE);
99
+ static VALUE wc3towc(VALUE,VALUE,VALUE,VALUE);
100
+ static VALUE drawrect(VALUE,VALUE,VALUE,VALUE,VALUE);
101
+ static VALUE fillrect(VALUE,VALUE,VALUE,VALUE,VALUE);
102
+ static VALUE drawarc(VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
103
+ static VALUE fillarc(VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
104
+ static VALUE setarrowstyle(VALUE,VALUE);
105
+ static VALUE setarrowsize(VALUE,VALUE);
106
+ static VALUE drawarrow(VALUE,VALUE,VALUE,VALUE,VALUE);
107
+ static VALUE drawimage(VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
108
+ static VALUE importgraphics(VALUE,VALUE);
109
+ static VALUE setshadow(VALUE,VALUE,VALUE,VALUE);
110
+ static VALUE settransparency(VALUE,VALUE);
111
+ static VALUE begingraphics(VALUE,VALUE);
112
+ static VALUE endgraphics(VALUE);
113
+ static VALUE getgraphics(VALUE);
114
+ static VALUE drawgraphics(VALUE,VALUE);
115
+ static VALUE mathtex(VALUE,VALUE,VALUE,VALUE);
116
+ static VALUE inqmathtex(VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
117
+ static VALUE beginselection(VALUE,VALUE,VALUE);
118
+ static VALUE endselection(VALUE);
119
+ static VALUE moveselection(VALUE,VALUE,VALUE);
120
+ static VALUE resizeselection(VALUE,VALUE,VALUE,VALUE);
121
+ static VALUE inqbbox(VALUE,VALUE,VALUE,VALUE,VALUE);
122
+ static VALUE precision(VALUE);
123
+ static VALUE setregenflags(VALUE,VALUE);
124
+ static VALUE inqregenflags(VALUE);
125
+ static VALUE savestate(VALUE);
126
+ static VALUE restorestate(VALUE);
127
+ static VALUE selectcontext(VALUE,VALUE);
128
+ static VALUE destroycontext(VALUE,VALUE);
129
+ static VALUE uselinespec(VALUE,VALUE);
130
+ static VALUE trisurface(VALUE,VALUE,VALUE,VALUE);
131
+ static VALUE gradient(VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
132
+ static VALUE quiver(VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
133
+ static VALUE version(VALUE);
134
+
135
+ #endif /* GRRUBY_H */
@@ -1 +1,30 @@
1
- # rubyplot
1
+ require 'bigdecimal'
2
+
3
+ require 'rmagick'
4
+
5
+ require 'rubyplot/color'
6
+ require 'rubyplot/utils'
7
+ require 'rubyplot/version'
8
+ require 'rubyplot/themes'
9
+ require 'rubyplot/artist'
10
+ require 'rubyplot/backend'
11
+ require 'rubyplot/figure'
12
+ require 'rubyplot/subplot'
13
+ require 'rubyplot/spi'
14
+
15
+ module Rubyplot
16
+ class << self
17
+ def backend
18
+ @backend
19
+ end
20
+
21
+ def set_backend b
22
+ case b
23
+ when :magick
24
+ @backend = Rubyplot::Backend::MagickWrapper.new
25
+ when :gr
26
+ @backend = Rubyplot::Backend::GRWrapper.new
27
+ end
28
+ end
29
+ end
30
+ end # module Rubyplot
@@ -0,0 +1,13 @@
1
+ require_relative 'artist/base'
2
+ require_relative 'artist/figure'
3
+ require_relative 'artist/legend_box'
4
+ require_relative 'artist/legend'
5
+ require_relative 'artist/line2d'
6
+ require_relative 'artist/tick'
7
+ require_relative 'artist/axis'
8
+ require_relative 'artist/text'
9
+ require_relative 'artist/plot'
10
+ require_relative 'artist/axes'
11
+ require_relative 'artist/rectangle'
12
+ require_relative 'artist/circle'
13
+ require_relative 'artist/polygon'
@@ -0,0 +1,328 @@
1
+ module Rubyplot
2
+ module Artist
3
+ class Axes < Base
4
+ TITLE_MARGIN = 20.0
5
+ # Space around text elements. Mostly used for vertical spacing.
6
+ # This way the vertical text doesn't overlap.
7
+ LEGEND_MARGIN = TITLE_MARGIN = 20.0
8
+ LABEL_MARGIN = 10.0
9
+ DEFAULT_MARGIN = 20.0
10
+ THOUSAND_SEPARATOR = ','.freeze
11
+
12
+ # Rubyplot::Figure object to which this Axes belongs.
13
+ attr_reader :figure
14
+ # Array of plots contained in this Axes.
15
+ attr_reader :plots
16
+ attr_reader :font, :marker_font_size, :legend_font_size,
17
+ :title_font_size, :scale, :font_color, :marker_color, :axes,
18
+ :legend_margin, :backend, :marker_caps_height
19
+ attr_reader :label_stagger_height
20
+ # Rubyplot::Artist::XAxis object.
21
+ attr_reader :x_axis
22
+ # Rubyplot::Artist::YAxis object.
23
+ attr_reader :y_axis
24
+ # Array of X ticks.
25
+ attr_reader :x_ticks
26
+ # Array of Y ticks.
27
+ attr_reader :y_ticks
28
+ # Array denoting co-ordinates in pixels of the origin of X and Y axes.
29
+ attr_reader :origin
30
+ # Number of X ticks.
31
+ attr_accessor :num_x_ticks
32
+ # Number of Y ticks.
33
+ attr_accessor :num_y_ticks
34
+ # Position of the legend box.
35
+ attr_accessor :legend_box_position
36
+ # Set true if title is to be hidden.
37
+ attr_accessor :hide_title
38
+ # Margin between the X axis and the bottom of the Axes artist.
39
+ attr_accessor :x_axis_margin
40
+ # Margin between the Y axis and the left of the Axes artist.
41
+ attr_accessor :y_axis_margin
42
+ # Range of X axis.
43
+ attr_accessor :x_range
44
+ # Range of Y axis.
45
+ attr_accessor :y_range, :grid, :bounding_box, :title_shift
46
+ # Main title for this Axes.
47
+ attr_accessor :title
48
+
49
+ # @param figure [Rubyplot::Figure] Figure object to which this Axes belongs.
50
+ def initialize(figure)
51
+ @figure = figure
52
+
53
+ @x_title = ''
54
+ @y_title = ''
55
+ @x_axis_margin = 40.0
56
+ @y_axis_margin = 40.0
57
+ @x_range = [nil, nil]
58
+ @y_range = [nil, nil]
59
+ @title = ''
60
+ @title_shift = 0
61
+ @title_margin = TITLE_MARGIN
62
+ @text_font = :default
63
+ @grid = true
64
+ @bounding_box = true
65
+ @plots = []
66
+ @raw_rows = width * (height/width)
67
+ @theme = Rubyplot::Themes::CLASSIC_WHITE
68
+ vera_font_path = File.expand_path('Vera.ttf', ENV['MAGICK_FONT_PATH'])
69
+ @font = File.exist?(vera_font_path) ? vera_font_path : nil
70
+ @font_color = '#000000'
71
+ @marker_font_size = 15.0
72
+ @legend_font_size = 20.0
73
+ @legend_margin = LEGEND_MARGIN
74
+ @title_font_size = 25.0
75
+ @plot_colors = []
76
+ @legends = []
77
+ @lines = []
78
+ @texts = []
79
+ @origin = [nil, nil]
80
+ calculate_xy_axes_origin
81
+ @x_axis = Rubyplot::Artist::XAxis.new(self)
82
+ @y_axis = Rubyplot::Artist::YAxis.new(self)
83
+ @x_ticks = nil
84
+ @y_ticks = nil
85
+ @num_x_ticks = 5
86
+ @num_y_ticks = 4
87
+ @legend_box_position = :top
88
+ end
89
+
90
+ # X co-ordinate of the legend box depending on value of @legend_box_position.
91
+ def legend_box_ix
92
+ case @legend_box_position
93
+ when :top
94
+ abs_y + width / 2
95
+ end
96
+ end
97
+
98
+ # Y co-ordinate of the legend box depending on value of @legend_box_position.
99
+ def legend_box_iy
100
+ case @legend_box_position
101
+ when :top
102
+ abs_x + @x_axis_margin + @legend_margin
103
+ end
104
+ end
105
+
106
+ # Write an image to a file by communicating with the backend.
107
+ def draw
108
+ set_axes_ranges
109
+ normalize_plotting_data
110
+ assign_default_label_colors
111
+ consolidate_plots
112
+ configure_title
113
+ configure_legends
114
+ assign_x_ticks
115
+ assign_y_ticks
116
+ actually_draw
117
+ end
118
+
119
+ def scatter!(*_args)
120
+ plot = Rubyplot::Artist::Plot::Scatter.new self
121
+ yield(plot) if block_given?
122
+ @plots << plot
123
+ end
124
+
125
+ def bar!(*_args)
126
+ plot = Rubyplot::Artist::Plot::Bar.new self
127
+ yield(plot) if block_given?
128
+ @plots << plot
129
+ end
130
+
131
+ def line!(*_args)
132
+ plot = Rubyplot::Artist::Plot::Line.new self
133
+ yield(plot) if block_given?
134
+ @plots << plot
135
+ end
136
+
137
+ def area!(*_args)
138
+ plot = Rubyplot::Artist::Plot::Area.new self
139
+ yield(plot) if block_given?
140
+ @plots << plot
141
+ end
142
+
143
+ def bubble!(*_args)
144
+ plot = Rubyplot::Artist::Plot::Bubble.new self
145
+ yield(plot) if block_given?
146
+ @plots << plot
147
+ end
148
+
149
+ def stacked_bar!(*_args)
150
+ plot = Rubyplot::Artist::Plot::StackedBar.new self
151
+ yield(plot) if block_given?
152
+ @plots << plot
153
+ end
154
+
155
+ def write(file_name)
156
+ @plots[0].write file_name
157
+ end
158
+
159
+ # Absolute X co-ordinate of the Axes. Top left corner.
160
+ def abs_x
161
+ @figure.top_spacing * @figure.height + @figure.abs_x
162
+ end
163
+
164
+ # Absolute Y co-ordinate of the Axes. Top left corner.
165
+ def abs_y
166
+ @figure.top_spacing * @figure.height + @figure.abs_y
167
+ end
168
+
169
+ # Absolute width of the Axes in pixels.
170
+ def width
171
+ (1 - (@figure.left_spacing + @figure.right_spacing)) * @figure.width
172
+ end
173
+
174
+ # Absolute height of the Axes in pixels.
175
+ # FIXME: expand for multiple axes on same figure. width too.
176
+ def height
177
+ (1 - (@figure.top_spacing + @figure.bottom_spacing)) * @figure.height
178
+ end
179
+
180
+ def x_ticks= x_ticks
181
+ @x_ticks = x_ticks
182
+ end
183
+
184
+ def y_ticks= y_ticks
185
+ @y_ticks = y_ticks
186
+ end
187
+
188
+ def x_title= x_title
189
+ @x_axis.title = x_title
190
+ end
191
+
192
+ def y_title= y_title
193
+ @y_axis.title = y_title
194
+ end
195
+
196
+ private
197
+
198
+ def assign_default_label_colors
199
+ @plots.each_with_index do |p, i|
200
+ if p.color == :default
201
+ p.color = @figure.theme_options[:label_colors][
202
+ i % @figure.theme_options[:label_colors].size]
203
+ end
204
+ end
205
+ end
206
+
207
+ def assign_x_ticks
208
+ @inter_x_ticks_distance = @x_axis.length / (@num_x_ticks.to_f-1)
209
+ unless @x_ticks
210
+ value_distance = (@x_range[1] - @x_range[0]) / (@num_x_ticks.to_f - 1)
211
+ @x_ticks = @num_x_ticks.times.map do |i|
212
+ @x_range[0] + i * value_distance
213
+ end
214
+ end
215
+
216
+ unless @x_ticks.all? { |t| t.is_a?(Rubyplot::Artist::XTick) }
217
+ @x_ticks.map!.with_index do |tick_label, i|
218
+ Rubyplot::Artist::XTick.new(
219
+ self,
220
+ abs_x: i * @inter_x_ticks_distance + @x_axis.abs_x1,
221
+ abs_y: @origin[1],
222
+ label: Rubyplot::Utils.format_label(tick_label),
223
+ length: 6,
224
+ label_distance: 10
225
+ )
226
+ end
227
+ end
228
+ end
229
+
230
+ def assign_y_ticks
231
+ unless @y_ticks
232
+ val_distance = (@y_range[1] - @y_range[0]).abs / @num_y_ticks.to_f
233
+ @y_ticks = (@y_range[0]..@y_range[1]).step(val_distance).map { |i| i }
234
+ end
235
+ unless @y_ticks.all? { |t| t.is_a?(Rubyplot::Artist::YTick) }
236
+ inter_ticks_distance = @y_axis.length / (@num_y_ticks - 1)
237
+ @y_ticks.map!.with_index do |tick_label, i|
238
+ Rubyplot::Artist::YTick.new(
239
+ self,
240
+ abs_x: @origin[0],
241
+ abs_y: @y_axis.abs_y1 - (i * inter_ticks_distance),
242
+ label: Rubyplot::Utils.format_label(tick_label),
243
+ length: 6,
244
+ label_distance: 50
245
+ )
246
+ end
247
+ end
248
+ end
249
+
250
+ # Figure out the co-ordinates of the title text w.r.t Axes.
251
+ def configure_title
252
+ @texts << Rubyplot::Artist::Text.new(
253
+ @title, self, abs_x: abs_x + width / 2, abs_y: abs_y + @title_margin,
254
+ font: @font, color: @font_color,
255
+ pointsize: @title_font_size, internal_label: 'axes title.')
256
+ end
257
+
258
+ def calculate_xy_axes_origin
259
+ @origin[0] = abs_x + @x_axis_margin
260
+ @origin[1] = abs_y + height - @y_axis_margin
261
+ end
262
+
263
+ # Figure out co-ordinates of the legends
264
+ def configure_legends
265
+ @legend_box = Rubyplot::Artist::LegendBox.new(
266
+ self, abs_x: legend_box_ix, abs_y: legend_box_iy
267
+ )
268
+ end
269
+
270
+ # Make adjustments to the data that will be plotted. Maps the data
271
+ # contained in the plot to actual pixel values.
272
+ def normalize_plotting_data
273
+ @plots.each do |plot|
274
+ plot.normalize
275
+ end
276
+ end
277
+
278
+ # Call the respective draw methods on each of the elements of this Axes.
279
+ def actually_draw
280
+ @x_axis.draw
281
+ @x_ticks.each(&:draw)
282
+ @y_ticks.each(&:draw)
283
+ @y_axis.draw
284
+ @texts.each(&:draw)
285
+ @legend_box.draw
286
+ @plots.each(&:draw)
287
+ end
288
+
289
+ def consolidate_plots
290
+ bars = @plots.grep(Rubyplot::Artist::Plot::Bar)
291
+ unless bars.empty?
292
+ @plots.delete_if { |p| p.is_a?(Rubyplot::Artist::Plot::Bar) }
293
+ @plots << Rubyplot::Artist::Plot::MultiBars.new(self, bar_plots: bars)
294
+ end
295
+
296
+ stacked_bars = @plots.grep(Rubyplot::Artist::Plot::StackedBar)
297
+ unless stacked_bars.empty?
298
+ @plots.delete_if { |p| p.is_a?(Rubyplot::Artist::Plot::StackedBar) }
299
+ @plots << Rubyplot::Artist::Plot::MultiStackedBar.new(self, stacked_bars: stacked_bars)
300
+ end
301
+ end
302
+
303
+ # FIXME: replace x_range and y_range with XAxis::max/min_value and YAxis::max/min_value.
304
+ def set_axes_ranges
305
+ set_xrange
306
+ set_yrange
307
+ end
308
+
309
+ def set_xrange
310
+ if @x_range[0].nil? && @x_range[1].nil?
311
+ @x_range[0] = @plots.map(&:x_min).min
312
+ @x_range[1] = @plots.map(&:x_max).max
313
+ end
314
+ @x_axis.min_val = @x_range[0]
315
+ @x_axis.max_val = @x_range[1]
316
+ end
317
+
318
+ def set_yrange
319
+ if @y_range[0].nil? && @y_range[1].nil?
320
+ @y_range[0] = @plots.map { |p| p.y_min }.min
321
+ @y_range[1] = @plots.map { |p| p.y_max }.max
322
+ end
323
+ @y_axis.min_val = @y_range[0]
324
+ @y_axis.max_val = @y_range[1]
325
+ end
326
+ end # class Axes
327
+ end # moudle Artist
328
+ end # module Rubyplot