Skip to content

Commit 3a3828e

Browse files
committed
simple area graph
1 parent 155b52d commit 3a3828e

File tree

8 files changed

+72
-24
lines changed

8 files changed

+72
-24
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,5 @@ g++
5959
gcc
6060
mpicc
6161
mpicxx
62+
examples/rect.png
63+
examples/gr_wrapper/*.png

examples/gr_wrapper/polygon.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Filled polygon GR example.
2+
3+
require_relative '../../lib/grruby.so'
4+
5+
Rubyplot::GR.beginprint("polygon.png")
6+
Rubyplot::GR.setviewport(0,1,0,1)
7+
Rubyplot::GR.setwindow(0,100,0,100)
8+
Rubyplot::GR.setfillintstyle(1)
9+
Rubyplot::GR.settransparency(0.5)
10+
Rubyplot::GR.fillarea([1, 5, 25, 30, 50, 30], [1, 5, 25, 30, 25, 5])
11+
Rubyplot::GR.endprint

ext/grruby/grruby.c

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,22 @@ static VALUE inqtext(VALUE self,VALUE a,VALUE b,VALUE c,VALUE d,VALUE e) {
244244
return Qtrue;
245245
}
246246

247+
/**
248+
* call-seq:
249+
* Rubyplot::GR.fillarea([1,2,3], [1,2,3]) -> true
250+
*
251+
* Allows you to specify a polygonal shape of an area to be filled.
252+
*
253+
* **Parameters:**
254+
*
255+
* `x` :
256+
* A list containing the X coordinates
257+
* `y` :
258+
* A list containing the Y coordinates
259+
*
260+
* The attributes that control the appearance of fill areas are fill area interior
261+
* style, fill area style index and fill area color index.
262+
*/
247263
static VALUE fillarea(VALUE self,VALUE x, VALUE y){
248264
int x_size = RARRAY_LEN(x);
249265
int y_size = RARRAY_LEN(y);
@@ -1720,9 +1736,21 @@ static VALUE setshadow(VALUE self,VALUE offsetx,VALUE offsety,VALUE blur){
17201736
return Qtrue;
17211737
}
17221738

1739+
1740+
/**
1741+
* call-seq:
1742+
* Rubyplot::GR.settransparency(alpha) -> true
1743+
*
1744+
* Set the value of the alpha component associated with GR colors
1745+
*
1746+
* **Parameters:**
1747+
*
1748+
* `alpha` :
1749+
* An alpha value (0.0 - 1.0)
1750+
*/
17231751
static VALUE settransparency(VALUE self,VALUE alpha){
1724-
double alphac = NUM2DBL(alpha);
1725-
gr_settransparency(alphac);
1752+
gr_settransparency(NUM2DBL(alpha));
1753+
17261754
return Qtrue;
17271755
}
17281756

lib/rubyplot/artist/plot/area.rb

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,25 @@ module Rubyplot
22
module Artist
33
module Plot
44
class Area < Artist::Plot::Base
5-
attr_accessor :sorted_data
5+
attr_accessor :sort_data
66

77
def initialize(*)
88
super
9-
@sorted_data = true
9+
@sort_data = true
1010
end
1111

1212
def data x_values, y_values=[]
1313
y_values = Array.new(x_values.size) { |i| i } if y_values.empty?
14-
x_values.sort! if @sorted_data
14+
x_values.sort! if @sort_data
1515
super(x_values, y_values)
1616
end
1717

1818
def draw
19-
poly_points = []
20-
@normalized_data[:y_values].each_with_index do |iy, idx_y|
21-
ix = @normalized_data[:x_values][idx_y]
22-
abs_x = ix * @axes.x_axis.length + @axes.origin[0]
23-
abs_y = iy * @axes.y_axis.length + @axes.origin[1]
24-
poly_points << [abs_x, abs_y]
25-
end
26-
poly_points << [@axes.x_axis.abs_x2, @axes.origin[1] - @axes.x_axis.stroke_width]
27-
poly_points << [@axes.origin[0], @axes.origin[1] - @axes.x_axis.stroke_width]
19+
x_poly_points = @data[:x_values].concat([@axes.x_axis.max_val, @axes.x_axis.min_val])
20+
y_poly_points = @data[:y_values].concat([@axes.y_axis.min_val, @axes.y_axis.min_val])
2821
Rubyplot::Artist::Polygon.new(
29-
coords: poly_points,
22+
x: x_poly_points,
23+
y: y_poly_points,
3024
color: @data[:color],
3125
fill_opacity: 0.3
3226
).draw

lib/rubyplot/artist/polygon.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
module Rubyplot
22
module Artist
33
class Polygon < Base
4-
def initialize(coords:, fill_opacity: 1.0, color: :default, stroke: 'transparent')
5-
@coords = coords
4+
def initialize(x:, y:, fill_opacity: 1.0, color: :default, stroke: 'transparent')
5+
@x = x
6+
@y = y
67
@fill_opacity = fill_opacity
78
@color = color
89
@stroke = stroke
910
end
1011

1112
def draw
1213
Rubyplot.backend.draw_polygon(
13-
coords: @coords,
14+
x: @x,
15+
y: @y,
1416
border_color: @color,
1517
border_width: 1.0,
1618
border_type: :solid,

lib/rubyplot/backend/base.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,18 @@ def draw_markers(x:, y:, marker_type:, marker_color:, marker_size:)
4848
raise NotImplementedError, "not implemented for #{self}."
4949
end
5050

51-
# Draw a circle.
51+
# Draw a circle.n
5252
def draw_circle(x:, y:, radius:, border_color:, fill_color:, border_width:)
5353
raise NotImplementedError, "not implemented for #{self}."
5454
end
5555

5656
# Draw a polygon and fill it with color. Co-ordinates are specified in (x,y)
5757
# pairs in the coords Array.
5858
#
59-
# @param coords [Array] Array containing pairs of co-ordinates in (x,y) pairs.
59+
# @param x [Array] Array containing X co-ordinates.
60+
# @param y [Array] Array containting Y co-ordinates.
6061
# @param border_width [Numeric] Widht of the border.
61-
def draw_polygon(coords:, border_width:, border_type:, border_color:, fill_color:,
62+
def draw_polygon(x:, y:, border_width:, border_type:, border_color:, fill_color:,
6263
fill_opacity:)
6364
end
6465
end # class Base

lib/rubyplot/backend/gr_wrapper.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,18 @@ def draw_lines(x:, y:, width:, type:, color:)
188188
end
189189
end
190190

191-
def draw_polygon(coords:, border_width:, border_type:, border_color:, fill_color:,
191+
def draw_polygon(x:, y:, border_width:, border_type:, border_color:, fill_color:,
192192
fill_opacity:)
193+
within_window do
194+
draw_lines(x: x, y: y, width: border_width, color: border_color,
195+
type: border_type)
196+
if fill_color
197+
GR.settransparency(fill_opacity)
198+
GR.setfillintstyle(1)
199+
GR.setfillcolorind(to_gr_color(fill_color))
200+
GR.fillarea(x, y)
201+
end
202+
end
193203
end
194204

195205
# Draw text on the canvas. Unlike other functions, this function does not
@@ -342,7 +352,7 @@ def within_window(abs=false, &block)
342352
@active_axes.x_range[1],
343353
@active_axes.y_range[0],
344354
@active_axes.y_range[1]
345-
)
355+
)
346356
end
347357

348358
block.call
@@ -353,6 +363,7 @@ def draw_axes
353363
axes = v[:axes]
354364
tick_length = transform_avg_ndc(axes.x_axis.major_ticks[0].length)
355365
within_window do
366+
GR.settransparency(1)
356367
GR.setcharheight(0.018)
357368
GR.setlinecolorind(to_gr_color(:black))
358369
GR.axes(

spec/axes_spec.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@
119119
p.label = "Jimmy"
120120
end
121121
axes.title = "Visual simple area graph test."
122-
axes.num_x_ticks = 5
123122
axes.x_ticks = ['0', '22', '44', '66', '88']
124123
end
125124

0 commit comments

Comments
 (0)