Class: XYP::Plot

Inherits:
Object
  • Object
show all
Defined in:
lib/xyp/plot.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Plot

Returns a new instance of Plot.



53
54
55
# File 'lib/xyp/plot.rb', line 53

def initialize name
  @name=name
end

Instance Attribute Details

#data_setObject

Returns the value of attribute data_set.



50
51
52
# File 'lib/xyp/plot.rb', line 50

def data_set
  @data_set
end

#movingObject

Returns the value of attribute moving.



51
52
53
# File 'lib/xyp/plot.rb', line 51

def moving
  @moving
end

#nameObject

Returns the value of attribute name.



50
51
52
# File 'lib/xyp/plot.rb', line 50

def name
  @name
end

#ratioObject

Returns the value of attribute ratio.



49
50
51
# File 'lib/xyp/plot.rb', line 49

def ratio
  @ratio
end

Instance Method Details

#draw_line(ctx, start_, end_) ⇒ Object

true abstract coord



92
93
94
95
96
97
98
# File 'lib/xyp/plot.rb', line 92

def draw_line(ctx,start_,end_) # true abstract coord
  p1=window_coord(start_)
  ctx.move_to(p1.x,p1.y)
  p2=window_coord(end_)
  ctx.line_to(p2.x,p2.y)
  ctx.stroke
end

#draw_point(ctx, center, radius) ⇒ Object

true abstract coord



100
101
102
103
104
# File 'lib/xyp/plot.rb', line 100

def draw_point ctx,center,radius # true abstract coord
  p=window_coord(center)
  ctx.arc p.x,p.y,1,0,2*Math::PI
  ctx.fill
end

#paint_axis(ctx) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/xyp/plot.rb', line 141

def paint_axis ctx
  ctx.select_font_face "Monospace"
  ctx.set_font_size 13

  x_1=(@view.center.x-@view.dims.x/2)
  x_2=(@view.center.x+@view.dims.x/2)
  x_1-=1
  x_2+=1
  y  = 0
  p1=Point.new(x_1,y)
  p2=Point.new(x_2,y)
  ctx.set_source *ORANGE
  draw_line(ctx,p1,p2)

  size_tick=@view.dims.y.to_f/60
  range=x_1.to_i..x_2.to_i
  transparency=10.0/range.size
  if transparency>0.2
    for x in range
      p1=Point.new(x,-size_tick/2)
      p2=Point.new(x,size_tick/2)
      draw_line(ctx,p1,p2)
      paint_text_axis(ctx,"#{x}",p1,:x)
    end
  end


  y_1=(@view.center.y-@view.dims.y/2)
  y_2=(@view.center.y+@view.dims.y/2)
  x  = 0
  y_1-=3
  y_2+=3
  p3=Point.new(x,y_1)
  p4=Point.new(x,y_2)
  ctx.set_source *ORANGE
  draw_line(ctx,p3,p4)

  size_tick=@view.dims.x.to_f/80
  range=y_1.to_i..y_2.to_i
  transparency=10.0/range.size

  if transparency>0.2
    for y in range
      ctx.set_source ORANGE.red,ORANGE.green,ORANGE.blue,transparency
      p1=Point.new(-size_tick/2,y)
      p2=Point.new(size_tick/2,y)
      draw_line(ctx,p1,p2)
      paint_text_axis(ctx,"#{y}",p1,:y)
    end
  end
end

#paint_data_set_line(ctx) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/xyp/plot.rb', line 106

def paint_data_set_line ctx
  ctx.set_source *YELLOW
  @points||=@data_set.each.collect{|a,b| Point.new(a,b)}
  @points.each_cons(2) do |start,end_|
    draw_line(ctx,start,end_)
  end
end

#paint_grid(ctx) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/xyp/plot.rb', line 114

def paint_grid ctx
  x_1=(@view.center.x-@view.dims.x/2-2).to_i
  x_2=(@view.center.x+@view.dims.x/2+2).to_i
  y_1=(@view.center.y-@view.dims.y/2).to_i
  y_2=(@view.center.y+@view.dims.y/2).to_i
  x_range=x_1..x_2
  y_range=y_1..y_2
  transparency=400.0/(x_range.size*y_range.size)
  if transparency>0.08
    for x in x_range
      for y in y_range
        ctx.set_source ORANGE.red,ORANGE.green,ORANGE.blue,transparency
        center=Point.new(x,y)
        draw_point(ctx,center,1)
      end
    end
  end
end

#paint_text_axis(ctx, txt, point, axis_sym) ⇒ Object



133
134
135
136
137
138
139
# File 'lib/xyp/plot.rb', line 133

def paint_text_axis ctx,txt,point,axis_sym
  coord=*window_coord(point).to_a
  coord.x+=20 if axis_sym==:y
  coord.y-=20 if axis_sym==:x
  ctx.move_to *coord
  ctx.show_text txt
end

#plot(cairo) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/xyp/plot.rb', line 73

def plot cairo
  wx,wy=*@window_size.map(&:to_f)
  vx,vy=*(@view.dims||[1,1]).map(&:to_f)
  @ratio=[wx/vx,wy/vy]
  cairo.set_source_rgba @color_rbga
  cairo.paint
  paint_grid(cairo)
  paint_axis(cairo)
  paint_data_set_line(cairo)
end

#set_background_rgba(color) ⇒ Object



69
70
71
# File 'lib/xyp/plot.rb', line 69

def set_background_rgba color
  @color_rbga=color
end

#set_data_set(hash) ⇒ Object



57
58
59
# File 'lib/xyp/plot.rb', line 57

def set_data_set hash
  @data_set=hash
end

#set_view(view) ⇒ Object



61
62
63
# File 'lib/xyp/plot.rb', line 61

def set_view view
  @view=view
end

#set_window_size(size) ⇒ Object



65
66
67
# File 'lib/xyp/plot.rb', line 65

def set_window_size size
  @window_size=size
end

#window_coord(p) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/xyp/plot.rb', line 84

def window_coord p
  cx=@view.dims.x/2.0-@view.center.x
  cy=@view.dims.y/2.0-@view.center.y
  xx=(p.x+cx)*@ratio.x
  yy=@window_size.y-(p.y+cy)*@ratio.y
  Point.new xx,yy
end