Class: GraphAxis
- Inherits:
-
Object
- Object
- GraphAxis
- Defined in:
- lib/technical_graph/graph_axis.rb
Overview
Calculate axis (only) and draw them
Constant Summary collapse
- SAFE_ENL_COEFF =
some issues with float precision and rounding
1.1
Instance Attribute Summary collapse
-
#technical_graph ⇒ Object
readonly
Returns the value of attribute technical_graph.
Instance Method Summary collapse
- #adjust_axis_to_zero ⇒ Object
- #antialias ⇒ Object
-
#axis_distance_image_enlarge ⇒ Object
Enlarge image to maintain proper axis density.
-
#calc_axis(from, to, interval, count, fixed_interval) ⇒ Object
Calculate axis using 2 methods.
-
#data_processor ⇒ Object
Calculate everything.
- #drawer ⇒ Object
- #image ⇒ Object
-
#initialize(technical_graph) ⇒ GraphAxis
constructor
A new instance of GraphAxis.
-
#layers ⇒ Object
Accessor for DataLayer Array.
- #logger ⇒ Object
-
#move_axis_to_fit_zero(axis) ⇒ Object
Process axis array, give offset to match zero axis, and remove zero axis from array.
-
#options ⇒ Object
Accessor for options Hash.
-
#parameter_axis ⇒ Object
Where to put axis values.
-
#render_axis ⇒ Object
Render normal axis.
- #render_axis_labels ⇒ Object
-
#render_on_image(image) ⇒ Object
Render axis on image.
-
#render_zero_axis ⇒ Object
Render axis for zeros.
- #truncate_string ⇒ Object
-
#value_axis ⇒ Object
Where to put axis values.
-
#x_axis_distance_image_enlarge ⇒ Object
Enlarge image to maintain proper axis density.
- #x_axis_fixed? ⇒ Boolean
- #x_axis_interval ⇒ Object
-
#y_axis_distance_image_enlarge ⇒ Object
Enlarge image to maintain proper axis density.
-
#y_axis_fixed? ⇒ Boolean
Value axis has fixed count.
- #y_axis_interval ⇒ Object
Constructor Details
#initialize(technical_graph) ⇒ GraphAxis
Returns a new instance of GraphAxis.
43 44 45 |
# File 'lib/technical_graph/graph_axis.rb', line 43 def initialize(technical_graph) @technical_graph = technical_graph end |
Instance Attribute Details
#technical_graph ⇒ Object (readonly)
Returns the value of attribute technical_graph.
7 8 9 |
# File 'lib/technical_graph/graph_axis.rb', line 7 def technical_graph @technical_graph end |
Instance Method Details
#adjust_axis_to_zero ⇒ Object
64 65 66 |
# File 'lib/technical_graph/graph_axis.rb', line 64 def adjust_axis_to_zero [:adjust_axis_to_zero] end |
#antialias ⇒ Object
200 201 202 |
# File 'lib/technical_graph/graph_axis.rb', line 200 def antialias [:antialias] == true end |
#axis_distance_image_enlarge ⇒ Object
Enlarge image to maintain proper axis density
130 131 132 133 134 135 136 137 138 139 |
# File 'lib/technical_graph/graph_axis.rb', line 130 def axis_distance_image_enlarge if [:axis_density_enlarge_image] or [:x_axis_density_enlarge_image] or [:y_axis_density_enlarge_image] t = Time.now x_axis_distance_image_enlarge if [:axis_density_enlarge_image] or [:x_axis_density_enlarge_image] y_axis_distance_image_enlarge if [:axis_density_enlarge_image] or [:y_axis_density_enlarge_image] logger.debug "axis enlarged" logger.debug " TIME COST #{Time.now - t}" end end |
#calc_axis(from, to, interval, count, fixed_interval) ⇒ Object
Calculate axis using 2 methods
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 107 108 |
# File 'lib/technical_graph/graph_axis.rb', line 79 def calc_axis(from, to, interval, count, fixed_interval) t = Time.now axis = Array.new l = to - from current = from if fixed_interval while current < to axis << current current += interval end axis = move_axis_to_fit_zero(axis) if adjust_axis_to_zero logger.debug "fixed interval axis calculation from #{from} to #{to} using int. #{interval}" logger.debug " TIME COST #{Time.now - t}" return axis else (0...count).each do |i| axis << from + (l.to_f * i.to_f) / count.to_f end axis = move_axis_to_fit_zero(axis) if adjust_axis_to_zero logger.debug "fixed count axis calculation from #{from} to #{to} using count #{count}" logger.debug " TIME COST #{Time.now - t}" return axis end end |
#data_processor ⇒ Object
Calculate everything
23 24 25 |
# File 'lib/technical_graph/graph_axis.rb', line 23 def data_processor @technical_graph.data_processor end |
#drawer ⇒ Object
31 32 33 |
# File 'lib/technical_graph/graph_axis.rb', line 31 def drawer image.drawer end |
#image ⇒ Object
27 28 29 |
# File 'lib/technical_graph/graph_axis.rb', line 27 def image @technical_graph.image_drawer end |
#layers ⇒ Object
Accessor for DataLayer Array
18 19 20 |
# File 'lib/technical_graph/graph_axis.rb', line 18 def layers @technical_graph.layers end |
#logger ⇒ Object
35 36 37 |
# File 'lib/technical_graph/graph_axis.rb', line 35 def logger @technical_graph.logger end |
#move_axis_to_fit_zero(axis) ⇒ Object
Process axis array, give offset to match zero axis, and remove zero axis from array
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/technical_graph/graph_axis.rb', line 111 def move_axis_to_fit_zero(axis) # if zero axis is within if axis.min <= 0 and axis.max >= 0 # if there is axis within -1..1 axis.each_with_index do |a, i| if a >= -1.0 and a <= 1.0 # this is the offset, move using found offset return axis.collect { |b| b - a } end end end # TODO when it won't work? # return unmodified return axis end |
#options ⇒ Object
Accessor for options Hash
13 14 15 |
# File 'lib/technical_graph/graph_axis.rb', line 13 def @technical_graph. end |
#parameter_axis ⇒ Object
Where to put axis values
74 75 76 |
# File 'lib/technical_graph/graph_axis.rb', line 74 def parameter_axis return calc_axis(data_processor.x_min, data_processor.x_max, [:x_axis_interval], [:x_axis_count], x_axis_fixed?) end |
#render_axis ⇒ Object
Render normal axis
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/technical_graph/graph_axis.rb', line 205 def render_axis drawer.axis( # X parameter_axis.collect { |x| image.calc_bitmap_x(x).to_i }, # Y value_axis.collect { |y| image.calc_bitmap_y(y).to_i }, # options { :color => [:axis_color], :width => 1 }, # draw labels [:axis_value_and_param_labels], # X axis labels parameter_axis, # Y axis labels value_axis ) end |
#render_axis_labels ⇒ Object
241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/technical_graph/graph_axis.rb', line 241 def render_axis_labels drawer.axis_labels( [:x_axis_label].to_s, [:y_axis_label].to_s, { :color => [:axis_color], :width => 1, :size => [:axis_label_font_size], } ) end |
#render_on_image(image) ⇒ Object
Render axis on image
192 193 194 195 196 197 198 |
# File 'lib/technical_graph/graph_axis.rb', line 192 def render_on_image(image) @image = image render_axis render_zero_axis render_axis_labels end |
#render_zero_axis ⇒ Object
Render axis for zeros
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/technical_graph/graph_axis.rb', line 223 def render_zero_axis drawer.axis( # X - 0 image.calc_bitmap_x(0.0).to_i, # Y - 0 image.calc_bitmap_y(0.0).to_i, # options, slightly wider { :color => [:axis_color], :width => 2 }, # draw label [:axis_zero_labels], # X label, [0.0], # Y label [0.0] ) end |
#truncate_string ⇒ Object
39 40 41 |
# File 'lib/technical_graph/graph_axis.rb', line 39 def truncate_string [:truncate_string] end |
#value_axis ⇒ Object
Where to put axis values
69 70 71 |
# File 'lib/technical_graph/graph_axis.rb', line 69 def value_axis return calc_axis(data_processor.y_min, data_processor.y_max, [:y_axis_interval], [:y_axis_count], y_axis_fixed?) end |
#x_axis_distance_image_enlarge ⇒ Object
Enlarge image to maintain proper axis density
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/technical_graph/graph_axis.rb', line 142 def x_axis_distance_image_enlarge a = parameter_axis # must be at least 2 axis logger.debug "axis enlargement - parameter_axis #{a.inspect}" return if a.size < 2 ax = a[0] ax = image.calc_bitmap_x(ax).round bx = a[1] bx = image.calc_bitmap_x(bx).round axis_distance = (bx - ax).abs logger.debug "axis enlargement - width, axis distance #{axis_distance} should be at least #{[:x_axis_min_distance]}" if axis_distance < [:x_axis_min_distance] # enlarging image [:old_width] = [:width] [:width] = [:width].to_f * ([:x_axis_min_distance].to_f / axis_distance.to_f) [:width] *= SAFE_ENL_COEFF [:width] = [:width].ceil logger.debug "axis enlarged - width modified to #{[:width]}" end end |
#x_axis_fixed? ⇒ Boolean
47 48 49 |
# File 'lib/technical_graph/graph_axis.rb', line 47 def x_axis_fixed? [:x_axis_fixed_interval] == true end |
#x_axis_interval ⇒ Object
60 61 62 |
# File 'lib/technical_graph/graph_axis.rb', line 60 def x_axis_interval [:x_axis_interval] end |
#y_axis_distance_image_enlarge ⇒ Object
Enlarge image to maintain proper axis density
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/technical_graph/graph_axis.rb', line 167 def y_axis_distance_image_enlarge a = value_axis # must be at least 2 axis logger.debug "axis enlargement - value_axis #{a.inspect}" return if a.size < 2 ay = a[0] ay = image.calc_bitmap_y(ay).round by = a[1] by = image.calc_bitmap_y(by).round axis_distance = (by - ay).abs logger.debug "axis enlargement - height, axis distance #{axis_distance} should be at least #{[:y_axis_min_distance]}" if axis_distance < [:y_axis_min_distance] # enlarging image [:old_height] = [:height] [:height] = [:height].to_f * ([:y_axis_min_distance].to_f / axis_distance.to_f) [:height] *= SAFE_ENL_COEFF [:height] = [:height].ceil logger.debug "axis enlarged - height modified from #{[:old_height]} to #{[:height]}" end end |
#y_axis_fixed? ⇒ Boolean
Value axis has fixed count
52 53 54 |
# File 'lib/technical_graph/graph_axis.rb', line 52 def y_axis_fixed? [:y_axis_fixed_interval] == true end |
#y_axis_interval ⇒ Object
56 57 58 |
# File 'lib/technical_graph/graph_axis.rb', line 56 def y_axis_interval [:y_axis_interval] end |