Class: Statsample::Graph::SvgHistogram
- Inherits:
-
SVG::Graph::BarBase
- Object
- SVG::Graph::BarBase
- Statsample::Graph::SvgHistogram
- Includes:
- REXML
- Defined in:
- lib/statsample/graph/svghistogram.rb
Overview
:nodoc:
Instance Attribute Summary collapse
-
#inner_margin ⇒ Object
Returns the value of attribute inner_margin.
-
#mean ⇒ Object
Returns the value of attribute mean.
-
#show_normal ⇒ Object
Returns the value of attribute show_normal.
-
#sigma ⇒ Object
Returns the value of attribute sigma.
Instance Method Summary collapse
- #draw_data ⇒ Object
- #draw_x_label(v) ⇒ Object
- #draw_x_labels ⇒ Object
- #get_css ⇒ Object
- #get_x_labels ⇒ Object
- #get_y_labels ⇒ Object
- #histogram=(h) ⇒ Object
-
#initialize(config) ⇒ SvgHistogram
constructor
A new instance of SvgHistogram.
-
#set_defaults ⇒ Object
In addition to the defaults set in Graph::initialize, sets [inner_margin] 14 [key] false.
- #unit_width ⇒ Object
Constructor Details
#initialize(config) ⇒ SvgHistogram
Returns a new instance of SvgHistogram.
5 6 7 8 9 10 |
# File 'lib/statsample/graph/svghistogram.rb', line 5 def initialize(config) config[:fields]=[:dummy] super(config) @histogram=nil end |
Instance Attribute Details
#inner_margin ⇒ Object
Returns the value of attribute inner_margin.
4 5 6 |
# File 'lib/statsample/graph/svghistogram.rb', line 4 def inner_margin @inner_margin end |
#mean ⇒ Object
Returns the value of attribute mean.
4 5 6 |
# File 'lib/statsample/graph/svghistogram.rb', line 4 def mean @mean end |
#show_normal ⇒ Object
Returns the value of attribute show_normal.
4 5 6 |
# File 'lib/statsample/graph/svghistogram.rb', line 4 def show_normal @show_normal end |
#sigma ⇒ Object
Returns the value of attribute sigma.
4 5 6 |
# File 'lib/statsample/graph/svghistogram.rb', line 4 def sigma @sigma end |
Instance Method Details
#draw_data ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/statsample/graph/svghistogram.rb', line 84 def draw_data minvalue = min_value fieldwidth = field_width unit_size = (@graph_height.to_f - font_size*2*top_font) / (get_y_labels.max - get_y_labels.min) bottom = @graph_height field_count = 0 hist_min=@histogram.min hist_max=@histogram.max range_hist=hist_max-hist_min total=0 (0...@histogram.bins).each do |i| dataset_count = 0 value = @histogram[i] total=total+value range = @histogram.get_range(i) left = (range[0] - hist_min)*unit_width = (range[1] - hist_min)*unit_width - left length = (value.abs - (minvalue > 0 ? minvalue : 0)) * unit_size # top is 0 if value is negative top = bottom - (((value < 0 ? 0 : value) - minvalue) * unit_size) @graph.add_element( "rect", { "x" => (@inner_margin+left).to_s, "y" => top.to_s, "width" => .to_s, "height" => length.to_s, "class" => "fill#{dataset_count+1}" }) make_datapoint_text(left + @inner_margin+ (/2), top - 6, value.to_s) field_count += 1 end if(show_normal) divs=30 path="" 0.upto(divs) do |i| x_abs=hist_min+(range_hist/divs)*i y=GSL::Ran::gaussian_pdf((x_abs-mean) / sigma)*total xg=@inner_margin+((x_abs-hist_min)*unit_width) yg=bottom-(y-minvalue)*unit_size if i==0 path="M#{xg} #{yg} " else path+="L#{xg} #{yg} " end end @graph.add_element("path", { "d"=>path, "style"=>"stroke:black;fill:none" }) end end |
#draw_x_label(v) ⇒ Object
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/statsample/graph/svghistogram.rb', line 61 def draw_x_label(v) left = (v - @histogram.min)*unit_width x=@inner_margin+left text = @graph.add_element( "text" ) text.attributes["class"] = "xAxisLabels" text.text = sprintf("%0.2f",v) y = @graph_height + x_label_font_size + 3 text.attributes["x"] = x.to_s text.attributes["y"] = y.to_s end |
#draw_x_labels ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/statsample/graph/svghistogram.rb', line 72 def draw_x_labels if show_x_labels (0...@histogram.bins).each do |i| value = @histogram[i] range = @histogram.get_range(i) draw_x_label(range[0]) if(i==(@histogram.bins)-1) draw_x_label(range[1]) end end end end |
#get_css ⇒ Object
138 139 140 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 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/statsample/graph/svghistogram.rb', line 138 def get_css return "/* default fill styles for multiple datasets (probably only use a single dataset on this graph though) */\n\n.key1,.fill1{\nfill: #ff0000;\nstroke: black;\nstroke-width: 1px; \n}\n.key2,.fill2{\nfill: #0000ff;\nstroke: black;\nstroke-width: 1px; \n}\n.key3,.fill3{\nfill: #00ff00;\nstroke: none;\nstroke-width: 1px; \n}\n.key4,.fill4{\nfill: #ffcc00;\nstroke: none;\nstroke-width: 1px; \n}\n.key5,.fill5{\nfill: #00ccff;\nstroke: none;\nstroke-width: 1px; \n}\n.key6,.fill6{\nfill: #ff00ff;\nstroke: none;\nstroke-width: 1px; \n}\n.key7,.fill7{\nfill: #00ffff;\nstroke: none;\nstroke-width: 1px; \n}\n.key8,.fill8{\nfill: #ffff00;\nstroke: none;\nstroke-width: 1px; \n}\n.key9,.fill9{\nfill: #cc6666;\nstroke: none;\nstroke-width: 1px; \n}\n.key10,.fill10{\nfill: #663399;\nstroke: none;\nstroke-width: 1px; \n}\n.key11,.fill11{\nfill: #339900;\nstroke: none;\nstroke-width: 1px; \n}\n.key12,.fill12{\nfill: #9966FF;\nstroke: none;\nstroke-width: 1px; \n}\n" end |
#get_x_labels ⇒ Object
33 34 35 |
# File 'lib/statsample/graph/svghistogram.rb', line 33 def get_x_labels [""] end |
#get_y_labels ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/statsample/graph/svghistogram.rb', line 37 def get_y_labels maxvalue = max_value minvalue = min_value range = maxvalue - minvalue top_pad = range == 0 ? 10 : range / 20.0 scale_range = (maxvalue + top_pad) - minvalue scale_division = scale_divisions || (scale_range / 10.0) if scale_integers scale_division = scale_division < 1 ? 1 : scale_division.round end rv = [] maxvalue = maxvalue%scale_division == 0 ? maxvalue : maxvalue + scale_division minvalue.step( maxvalue, scale_division ) {|v| rv << v} rv end |
#histogram=(h) ⇒ Object
27 28 29 30 31 32 |
# File 'lib/statsample/graph/svghistogram.rb', line 27 def histogram=(h) @histogram=h @data=[{:data=>(0...@histogram.bins).to_a.collect {|i| @histogram[i] }}] end |
#set_defaults ⇒ Object
In addition to the defaults set in Graph::initialize, sets
- inner_margin
-
14
- key
-
false
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/statsample/graph/svghistogram.rb', line 16 def set_defaults super self.top_align = self.top_font = 0 init_with({ :inner_margin=>16, :key=>false, :show_normal=>false }) end |
#unit_width ⇒ Object
58 59 60 |
# File 'lib/statsample/graph/svghistogram.rb', line 58 def unit_width (@graph_width-(@inner_margin*2)) / (@histogram.max-@histogram.min).to_f end |