Class: Statsample::Graph::SvgScatterplot

Inherits:
SVG::Graph::Plot
  • Object
show all
Defined in:
lib/statsample/graph/svgscatterplot.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ds, config = {}) ⇒ SvgScatterplot

Returns a new instance of SvgScatterplot.



5
6
7
8
9
# File 'lib/statsample/graph/svgscatterplot.rb', line 5

def initialize(ds,config={})
    super(config)
    @ds=ds
    set_x(@ds.fields[0])
end

Instance Attribute Details

#draw_pathObject

Returns the value of attribute draw_path.



4
5
6
# File 'lib/statsample/graph/svgscatterplot.rb', line 4

def draw_path
  @draw_path
end

Instance Method Details

#draw_dataObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/statsample/graph/svgscatterplot.rb', line 54

def draw_data
  line = 1
  
  x_min, x_max, x_div = x_range
  y_min, y_max, y_div = y_range
  x_step = (@graph_width.to_f - font_size*2) / (x_max-x_min)
  y_step = (@graph_height.to_f -  font_size*2) / (y_max-y_min)
  
  for data in @data
  x_points = data[:data][X]
  y_points = data[:data][Y]
  
  lpath = "L"
  x_start = 0
  y_start = 0
  x_points.each_index { |idx|
  x = (x_points[idx] -  x_min) * x_step
  y = @graph_height - (y_points[idx] -  y_min) * y_step
  x_start, y_start = x,y if idx == 0
  lpath << "#{x} #{y} "
  }
  
  if area_fill
  @graph.add_element( "path", {
    "d" => "M#{x_start} #@graph_height #{lpath} V#@graph_height Z",
    "class" => "fill#{line}"
  })
  end
  if draw_path
  @graph.add_element( "path", {
  "d" => "M#{x_start} #{y_start} #{lpath}",
  "class" => "line#{line}"
  })
  end
  if show_data_points || show_data_values
  x_points.each_index { |idx|
    x = (x_points[idx] -  x_min) * x_step
    y = @graph_height - (y_points[idx] -  y_min) * y_step
    if show_data_points
      @graph.add_element( "circle", {
        "cx" => x.to_s,
        "cy" => y.to_s,
        "r" => "2.5",
        "class" => "dataPoint#{line}"
      })
      add_popup(x, y, format( x_points[idx], y_points[idx] )) if add_popups
    end
    make_datapoint_text( x, y-6, y_points[idx] ) if show_data_values
  }
  end
  line += 1
  end
end

#get_x_labelsObject



34
35
36
37
38
39
40
41
42
43
# File 'lib/statsample/graph/svgscatterplot.rb', line 34

def get_x_labels
  values=super
  values.collect{|x|
    if x.is_a? Integer
        x 
    else
        sprintf("%0.2f",x).to_f
    end
  }
end

#get_y_labelsObject



44
45
46
47
48
49
50
51
52
53
# File 'lib/statsample/graph/svgscatterplot.rb', line 44

def get_y_labels
  values=super
  values.collect{|x|
    if x.is_a? Integer
        x 
    else
        sprintf("%0.2f",x).to_f
    end
  }
end

#parseObject



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/statsample/graph/svgscatterplot.rb', line 21

def parse
  data=@y.inject({}){|a,v| a[v]=[];a}
  @ds.each{|row|
    @y.each{|y|
        data[y]+=[row[@x],row[y]] unless row[@x].nil? or row[y].nil?
    }
  }
  data.each{|y,d|
    add_data({
            :data=>d, :title=>@ds.vector_label(y)
    })
  }
end

#set_defaultsObject



10
11
12
13
14
15
16
# File 'lib/statsample/graph/svgscatterplot.rb', line 10

def set_defaults
    super
    init_with(
        :show_data_values => false,
        :draw_path => false
        )
end

#set_x(x) ⇒ Object



17
18
19
20
# File 'lib/statsample/graph/svgscatterplot.rb', line 17

def set_x(x)
    @x=x
    @y=@ds.fields - [x]
end