class GraphImageDrawerRasem

Public Instance Methods

axis(x_array, y_array, _options = { :color => 'black', :width => 1 }, render_labels = false, x_labels = [], y_labels = []) click to toggle source

Draw both array axis

# File lib/technical_graph/graph_image_drawer_rasem.rb, line 17
def axis(x_array, y_array, _options = { :color => 'black', :width => 1 }, render_labels = false, x_labels = [], y_labels = [])
  # for single axis
  x_array = [x_array] if not x_array.kind_of? Array
  y_array = [y_array] if not y_array.kind_of? Array

  _s = self

  @image.group :stroke => _options[:color], :stroke_width => _options[:width] do
    x_array.each_with_index do |x, i|
      line(x, 0, x, _s.height, { })
    end

    y_array.each_with_index do |y, i|
      line(0, y, _s.width, y, { })
    end
  end

  # labels
  @image.group :fill => _options[:color] do
    x_array.each_with_index do |x, i|
      label = x_labels[i]
      if render_labels and not label.nil?
        label = "#{_s.truncate_string % label}"
        text(x + 15, _s.height - 15, label, { })
      end
    end

    y_array.each_with_index do |y, i|
      # labels
      label = y_labels[i]
      if render_labels and not label.nil?
        label = "#{_s.truncate_string % label}"
        text(15, y + 15, label, { })
      end
    end
  end
end
axis_labels(parameter_label, value_label, _options = { :color => 'black', :width => 1, :size => 20 }) click to toggle source

Label for parameters and values

# File lib/technical_graph/graph_image_drawer_rasem.rb, line 56
def axis_labels(parameter_label, value_label, _options = { :color => 'black', :width => 1, :size => 20 })
  _s = self
  @image.group :fill => _options[:color] do
    text(
      (_s.width / 2).to_i,
      _s.height - 40,
      parameter_label, { 'font-size' => "#{_options[:size]}px" }
    )

    text(
      (_s.height / 2).to_i,
      -40,
      value_label, { :transform => 'rotate(90 0,0)', 'font-size' => "#{_options[:size]}px" }
    )
  end
end
close() click to toggle source

Needed before saving?

# File lib/technical_graph/graph_image_drawer_rasem.rb, line 133
def close
  @image.close if not closed?
  @closed = true
end
closed?() click to toggle source
# File lib/technical_graph/graph_image_drawer_rasem.rb, line 138
def closed?
  @closed
end
create_blank_image() click to toggle source

Initialize blank image

# File lib/technical_graph/graph_image_drawer_rasem.rb, line 12
def create_blank_image
  @image = Rasem::SVGImage.new(drawer.width, drawer.height)
end
legend(legend_data) click to toggle source
# File lib/technical_graph/graph_image_drawer_rasem.rb, line 120
def legend(legend_data)
  _s = self
  legend_text_offset = (options[:legend_font_size] / 2.0).round - 4

  @image.group  do
    legend_data.each do |l|
      circle(l[:x], l[:y], 2, { :stroke => l[:color], :fill => l[:color], :stroke_width => 1 })
      text(l[:x] + 5, l[:y] + legend_text_offset, l[:label], { :fill => l[:color], 'font-size' => "#{_s.options[:legend_font_size]}px" })
    end
  end
end
render_data_layer(l, coords) click to toggle source
# File lib/technical_graph/graph_image_drawer_rasem.rb, line 73
def render_data_layer(l, coords)
  _s = self
  _l = l
  _coords = coords

  # value labels
  if l.value_labels
    t = Time.now

    @image.group :fill => _s.options[:axis_color] do
      _coords.each do |c|
        string_label = "#{_s.truncate_string % c[:dy]}"
        text(
          c[:ax] + 5, c[:ay],
          string_label, {}
        )
      end
    end

    logger.debug "labels"
    logger.debug " TIME COST #{Time.now - t}"
  end

  t = Time.now

  # lines and dots
  @image.group :stroke => l.color, :stroke_width => 1 do
    _coords.each do |c|
      # additional circle
      circle(c[:ax], c[:ay], 2, { :fill => l.color })
      circle(c[:bx], c[:by], 2, { :fill => l.color })
      # line
      line(
        c[:ax], c[:ay],
        c[:bx], c[:by],
        { }
      )

      _s.drawer.post_dot_drawn(c[:ax], c[:ay])
      _s.drawer.post_dot_drawn(c[:bx], c[:by])
    end
  end

  logger.debug "dots and lines"
  logger.debug " TIME COST #{Time.now - t}"
end
save(file) click to toggle source

Save to file, convert when needed

# File lib/technical_graph/graph_image_drawer_rasem.rb, line 143
def save(file)
  close

  format = format_from_filename(file)
  case format
    when 'svg' then
      string = to_svg
    when 'svgz' then
      string = to_svgz
    else
      # ugly hack, save to svg and then convert using image magick
      tmp_file = file.gsub(/#{format}/, 'svg')
      # change temp filename if it exist
      tmp_file = File.join(Dir.tmpdir, "#{random_filename}.svg") if File.exists?(tmp_file)
      # save to svg
      save(tmp_file)
      # convert
      `convert "#{tmp_file}" "#{file}"`
      return
  end

  File.open(file, 'w') do |f|
    f << string
  end
end
to_format(format) click to toggle source
# File lib/technical_graph/graph_image_drawer_rasem.rb, line 170
def to_format(format)
  close

  return @image.output if format == 'svg'
  return to_svgz if format == 'svgz'

  #raise 'Not implemented' if not format == 'svg'
  return ugly_convert(format)
end
ugly_convert(format) click to toggle source

Ugly, save temporary file, convert, read, delete temp file

# File lib/technical_graph/graph_image_drawer_rasem.rb, line 181
def ugly_convert(format)
  # create temp file
  tmp_file = File.join(Dir.tmpdir, "#{random_filename}.#{format}")
  save(tmp_file)
  # read content
  contents = open(tmp_file, "rb") { |io| io.read }
  # remove temp file
  File.delete(tmp_file)

  # return content
  contents
end