Sha256: e523ca82e8ff5449ca367a4521fdcab40aa9bac7f5a7eb2c7098a3cdeac5ac5a

Contents?: true

Size: 1.66 KB

Versions: 1

Compression:

Stored size: 1.66 KB

Contents

require File.dirname(__FILE__) + '/base'

class Gruff::Line < Gruff::Base

  #
  # Call with target pixel width of graph (800, 400, 300), and/or 'false' to omit lines (points only).
  #
  #  g = Gruff::Line.new(400) # 400px wide with lines
  #
  #  g = Gruff::Line.new(400, false) # 400px wide, no lines
  #
  #  g = Gruff::Line.new(false) # Defaults to 800px wide, no lines
  def initialize(*args)
    raise ArgumentError, "Wrong number of arguments" if args.length > 2
    if args.empty? or not Numeric === args.first then
      super()
    else
      super args.shift
    end
    @lines = args.empty? || args.shift # draw lines by default
  end

  def draw
    super

    return unless @has_data
      
    @x_increment = @graph_width / (@column_count - 1).to_f
    circle_radius = clip_value_if_greater_than(@columns / (@norm_data.first[1].size * 2.5), 5.0)

    @d = @d.stroke_opacity 1.0
    @d = @d.stroke_width clip_value_if_greater_than(@columns / (@norm_data.first[1].size * 4), 5.0)
    
    @norm_data.each do |data_row|
      prev_x = prev_y = 0.0
      @d = @d.stroke current_color
      @d = @d.fill current_color

      data_row[1].each_with_index do |data_point, index|
        # Use incremented x and scaled y
        new_x = @graph_left + (@x_increment * index)
        new_y = @graph_top + (@graph_height - data_point * @graph_height)

        if @lines and prev_x > 0 and prev_y > 0 then
          @d = @d.line(prev_x, prev_y, new_x, new_y)
        end
        @d = @d.circle(new_x, new_y, new_x - circle_radius, new_y)

        draw_label(new_x, index)

        prev_x = new_x
        prev_y = new_y
      end

      increment_color()
    end

    @d.draw(@base_image)
  end


end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gruff-0.0.6 lib/gruff/line.rb