class GraphAxis

Calculate axis (only) and draw them

Constants

SAFE_ENL_COEFF

some issues with float precision and rounding

Attributes

technical_graph[R]

Public Class Methods

new(technical_graph) click to toggle source
# File lib/technical_graph/graph_axis.rb, line 43
def initialize(technical_graph)
  @technical_graph = technical_graph
end

Public Instance Methods

adjust_axis_to_zero() click to toggle source
# File lib/technical_graph/graph_axis.rb, line 64
def adjust_axis_to_zero
  options[:adjust_axis_to_zero]
end
antialias() click to toggle source
# File lib/technical_graph/graph_axis.rb, line 200
def antialias
  options[:antialias] == true
end
axis_distance_image_enlarge() click to toggle source

Enlarge image to maintain proper axis density

# File lib/technical_graph/graph_axis.rb, line 130
def axis_distance_image_enlarge
  if options[:axis_density_enlarge_image] or options[:x_axis_density_enlarge_image] or options[:y_axis_density_enlarge_image]
    t = Time.now
    x_axis_distance_image_enlarge if options[:axis_density_enlarge_image] or options[:x_axis_density_enlarge_image]
    y_axis_distance_image_enlarge if options[:axis_density_enlarge_image] or options[: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) click to toggle source

Calculate axis using 2 methods

# 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() click to toggle source

Calculate everything

# File lib/technical_graph/graph_axis.rb, line 23
def data_processor
  @technical_graph.data_processor
end
drawer() click to toggle source
# File lib/technical_graph/graph_axis.rb, line 31
def drawer
  image.drawer
end
image() click to toggle source
# File lib/technical_graph/graph_axis.rb, line 27
def image
  @technical_graph.image_drawer
end
layers() click to toggle source

Accessor for DataLayer Array

# File lib/technical_graph/graph_axis.rb, line 18
def layers
  @technical_graph.layers
end
logger() click to toggle source
# File lib/technical_graph/graph_axis.rb, line 35
def logger
  @technical_graph.logger
end
move_axis_to_fit_zero(axis) click to toggle source

Process axis array, give offset to match zero axis, and remove zero axis from array

# 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() click to toggle source

Accessor for options Hash

# File lib/technical_graph/graph_axis.rb, line 13
def options
  @technical_graph.options
end
parameter_axis() click to toggle source

Where to put axis values

# File lib/technical_graph/graph_axis.rb, line 74
def parameter_axis
  return calc_axis(data_processor.x_min, data_processor.x_max, options[:x_axis_interval], options[:x_axis_count], x_axis_fixed?)
end
render_axis() click to toggle source

Render normal axis

# 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 => options[:axis_color], :width => 1 },
    # draw labels
    options[:axis_value_and_param_labels],
    # X axis labels
    parameter_axis,
    # Y axis labels
    value_axis
  )
end
render_axis_labels() click to toggle source
# File lib/technical_graph/graph_axis.rb, line 241
def render_axis_labels
  drawer.axis_labels(
    options[:x_axis_label].to_s,
    options[:y_axis_label].to_s,
    {
      :color => options[:axis_color],
      :width => 1,
      :size => options[:axis_label_font_size],
    }
  )
end
render_on_image(image) click to toggle source

Render axis on image

# 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() click to toggle source

Render axis for zeros

# 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 => options[:axis_color], :width => 2 },
    # draw label
    options[:axis_zero_labels],
    # X label,
    [0.0],
    # Y label
    [0.0]
  )
end
truncate_string() click to toggle source
# File lib/technical_graph/graph_axis.rb, line 39
def truncate_string
  options[:truncate_string]
end
value_axis() click to toggle source

Where to put axis values

# File lib/technical_graph/graph_axis.rb, line 69
def value_axis
  return calc_axis(data_processor.y_min, data_processor.y_max, options[:y_axis_interval], options[:y_axis_count], y_axis_fixed?)
end
x_axis_distance_image_enlarge() click to toggle source

Enlarge image to maintain proper axis density

# 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 #{options[:x_axis_min_distance]}"
  if axis_distance < options[:x_axis_min_distance]
    # enlarging image
    options[:old_width] = options[:width]
    options[:width] = options[:width].to_f * (options[:x_axis_min_distance].to_f / axis_distance.to_f)
    options[:width] *= SAFE_ENL_COEFF
    options[:width] = options[:width].ceil
    logger.debug "axis enlarged - width modified to #{options[:width]}"
  end
end
x_axis_fixed?() click to toggle source
# File lib/technical_graph/graph_axis.rb, line 47
def x_axis_fixed?
  options[:x_axis_fixed_interval] == true
end
x_axis_interval() click to toggle source
# File lib/technical_graph/graph_axis.rb, line 60
def x_axis_interval
  options[:x_axis_interval]
end
y_axis_distance_image_enlarge() click to toggle source

Enlarge image to maintain proper axis density

# 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 #{options[:y_axis_min_distance]}"
  if axis_distance < options[:y_axis_min_distance]
    # enlarging image
    options[:old_height] = options[:height]
    options[:height] = options[:height].to_f * (options[:y_axis_min_distance].to_f / axis_distance.to_f)
    options[:height] *= SAFE_ENL_COEFF
    options[:height] = options[:height].ceil
    logger.debug "axis enlarged - height modified from #{options[:old_height]} to #{options[:height]}"
  end
end
y_axis_fixed?() click to toggle source

Value axis has fixed count

# File lib/technical_graph/graph_axis.rb, line 52
def y_axis_fixed?
  options[:y_axis_fixed_interval] == true
end
y_axis_interval() click to toggle source
# File lib/technical_graph/graph_axis.rb, line 56
def y_axis_interval
  options[:y_axis_interval]
end