Skip to content

Commit 8aa1f90

Browse files
committed
import code for area plots
1 parent 0923ac4 commit 8aa1f90

5 files changed

Lines changed: 143 additions & 13 deletions

File tree

lib/rubyplot/axes.rb

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,19 @@ def initialize figure, position
5353
end
5454

5555
def scatter! *args, &block
56-
plot = with_backend "Scatter", *args
57-
yield(plot) if block_given?
58-
@plots << plot
56+
add_plot "Scatter", *args, &block
5957
end
6058

6159
def bar! *args, &block
62-
plot = with_backend "Bar", *args
63-
yield(plot) if block_given?
64-
@plots << plot
60+
add_plot "Bar", *args, &block
6561
end
6662

6763
def line! *args, &block
68-
plot = with_backend "Line", *args
69-
yield(plot) if block_given?
70-
@plots << plot
64+
add_plot "Line", *args, &block
65+
end
66+
67+
def area! *args, &block
68+
add_plot "Area", *args, &block
7169
end
7270

7371
def write file_name
@@ -76,6 +74,12 @@ def write file_name
7674

7775
private
7876

77+
def add_plot plot_type, *args, &block
78+
plot = with_backend plot_type, *args
79+
yield(plot) if block_given?
80+
@plots << plot
81+
end
82+
7983
def with_backend plot_type, *args
8084
plot =
8185
case Rubyplot.backend
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
require_relative 'plot/scatter'
22
require_relative 'plot/bar'
33
require_relative 'plot/line'
4+
require_relative 'plot/area'
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
require_relative 'area/geometry'
2+
3+
module Rubyplot
4+
module MagickWrapper
5+
module Plot
6+
class Area < Artist
7+
def initialize(*)
8+
super
9+
@sorted_drawing = true
10+
@all_colors_array = Magick.colors
11+
end
12+
13+
def draw
14+
super
15+
return unless @geometry.has_data
16+
17+
@x_increment = @graph_width / (@geometry.column_count - 1).to_f
18+
@d = @d.stroke 'transparent'
19+
20+
@geometry.norm_data.each_with_index do |data_row, i|
21+
poly_points = []
22+
prev_x = prev_y = 0.0
23+
@d = @d.fill @plot_colors[i]
24+
25+
data_row[DATA_VALUES_INDEX].each_with_index do |data_point, index|
26+
# Use incremented x and scaled y
27+
new_x = @graph_left + (@x_increment * index)
28+
new_y = @graph_top + (@graph_height - data_point * @graph_height)
29+
30+
poly_points << new_x
31+
poly_points << new_y
32+
33+
draw_label(new_x, index)
34+
35+
prev_x = new_x
36+
prev_y = new_y
37+
end
38+
39+
# Add closing points, draw polygon
40+
poly_points << @graph_right
41+
poly_points << @graph_bottom - 1
42+
poly_points << @graph_left
43+
poly_points << @graph_bottom - 1
44+
@d.fill_opacity(0.3)
45+
@d = @d.polyline(*poly_points)
46+
end
47+
48+
@d.draw(@base_image)
49+
end
50+
end
51+
end
52+
end
53+
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module Rubyplot
2+
module MagickWrapper
3+
module Plot
4+
class Area < MagickWrapper::Artist
5+
class Geometry < MagickWrapper::Artist::Geometry
6+
attr_accessor :spacing_factor
7+
def initialize(*)
8+
super
9+
@spacing_factor = 0.9
10+
end
11+
end # class Geometry
12+
end # class Area
13+
end # module Plot
14+
end # module MagickWrapper
15+
end # module Rubyplot

spec/axes_spec.rb

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,63 @@
1212
["Venus", [90, 34, 23, 12, 78, 89, 98, 88]]
1313
]
1414
end
15+
16+
context "#area!", focus: true do
17+
before do
18+
@temp_dir = SPEC_ROOT + "temp/area"
19+
@fix_dir = SPEC_ROOT + "fixtures/area"
20+
FileUtils.mkdir_p @temp_dir
21+
end
22+
23+
it "plots a single simple Area graph" do
24+
fig = Rubyplot::Figure.new
25+
axes = fig.add_subplot 0,0
26+
axes.area! do |p|
27+
p.data [25, 36, 86, 39, 25, 31, 79, 88]
28+
p.label = "Jimmy"
29+
end
30+
axes.title = "Visual simple area graph test."
31+
axes.x_ticks = {
32+
0 => '0',
33+
2 => '2',
34+
4 => '4',
35+
6 => '6'
36+
}
37+
38+
file = "/#{Rubyplot.backend}_simple_area.png"
39+
fig.write(@temp_dir + file)
40+
41+
#expect("temp/area" + file).to eq_image("fixtures/area" + file)
42+
end
43+
44+
it "plots multiple area plots on the same Axes" do
45+
fig = Rubyplot::Figure.new
46+
axes = fig.add_subplot 0,0
47+
[
48+
["Jimmy", [25, 36, 86, 39, 25, 31, 79, 88]],
49+
["Charles", [80, 54, 67, 54, 68, 70, 90, 95]],
50+
["Julie", [22, 29, 35, 38, 36, 40, 46, 57]],
51+
["Jane", [3, 95, 95, 90, 85, 80, 88, 100]]
52+
].each do |n, data|
53+
axes.area! do |p|
54+
p.data data
55+
p.label = n
56+
end
57+
end
58+
axes.title = "Multiple area plots on same axes."
59+
axes.x_ticks = {
60+
0 => '0',
61+
2 => '2',
62+
4 => '4',
63+
6 => '6'
64+
}
65+
66+
file = "/#{Rubyplot.backend}_multiple_area.png"
67+
fig.write(@temp_dir + file)
68+
69+
#expect("temp/area" + file).to eq_image("fixtures/area" + file)
70+
end
71+
end
1572

1673
context "#line!" do
1774
before do
@@ -305,7 +362,7 @@
305362
p.label = "peaches"
306363
end
307364
axes.title = "Pos/neg bar graph test."
308-
axes.labels = {
365+
axes.x_ticks = {
309366
0 => '5/6',
310367
1 => '5/15',
311368
2 => '5/24',
@@ -322,7 +379,7 @@
322379
fig = Rubyplot::Figure.new
323380
axes = fig.add_subplot 0,0
324381
axes.title = "all negative bar graph."
325-
axes.labels = {
382+
axes.x_ticks = {
326383
0 => '5/6',
327384
1 => '5/15',
328385
2 => '5/24',
@@ -357,7 +414,7 @@
357414
p.label = name
358415
end
359416
end
360-
axes.labels = {
417+
axes.x_ticks = {
361418
0 => '5/6',
362419
1 => '5/15',
363420
2 => '5/24',
@@ -385,7 +442,7 @@
385442
p.label = name
386443
end
387444
end
388-
axes.labels = { 0 => '2003', 2 => '2004', 4 => '2005' }
445+
axes.x_ticks = { 0 => '2003', 2 => '2004', 4 => '2005' }
389446

390447
file = "/#{Rubyplot.backend}_adjust_legens.png"
391448
fig.write(@temp_dir + file)

0 commit comments

Comments
 (0)