class XYP::Plot

Attributes

data_set[RW]
moving[RW]
name[RW]
ratio[RW]

Public Class Methods

new(name) click to toggle source
# File lib/xyp/plot.rb, line 53
def initialize name
  @name=name
end

Public Instance Methods

draw_line(ctx,start_,end_) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/xyp/plot.rb, line 69
def set_background_rgba color
  @color_rbga=color
end
set_data_set(hash) click to toggle source
# File lib/xyp/plot.rb, line 57
def set_data_set hash
  @data_set=hash
end
set_view(view) click to toggle source
# File lib/xyp/plot.rb, line 61
def set_view view
  @view=view
end
set_window_size(size) click to toggle source
# File lib/xyp/plot.rb, line 65
def set_window_size size
  @window_size=size
end
window_coord(p) click to toggle source
# 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