Class: Gruff::Line

Inherits:
Base
  • Object
show all
Defined in:
lib/gruff/line.rb

Overview

Here’s how to make a Line graph:

g = Gruff::Line.new
g.title = "A Line Graph"
g.data 'Fries', [20, 23, 19, 8]
g.data 'Hamburgers', [50, 19, 99, 29]
g.write("test/output/line.png")

There are also other options described below, such as #baseline_value, #baseline_color, #hide_dots, and #hide_lines.

Constant Summary

Constants inherited from Base

Base::DATA_COLOR_INDEX, Base::DATA_LABEL_INDEX, Base::DATA_VALUES_INDEX, Base::DEBUG, Base::DEFAULT_MARGIN, Base::DEFAULT_TARGET_WIDTH, Base::LABEL_MARGIN, Base::LEGEND_MARGIN, Base::THOUSAND_SEPARATOR

Instance Attribute Summary collapse

Attributes inherited from Base

#additional_line_values, #bottom_margin, #center_labels_over_point, #colors, #font, #font_color, #has_left_labels, #hide_legend, #hide_line_markers, #hide_line_numbers, #hide_title, #labels, #left_margin, #legend_box_size, #legend_font_size, #legend_margin, #marker_color, #marker_count, #marker_font_size, #maximum_value, #minimum_value, #no_data_message, #right_margin, #sort, #stacked, #title, #title_font_size, #title_margin, #top_margin, #x_axis_label, #y_axis_increment, #y_axis_label

Instance Method Summary collapse

Methods inherited from Base

#add_color, #data, #initialize_ivars, #margins=, #replace_colors, #theme=, #theme_37signals, #theme_greyscale, #theme_keynote, #theme_odeo, #theme_pastel, #theme_rails_keynote, #to_blob, #write

Methods included from Deprecated

#graph_height, #graph_left, #graph_top, #graph_width, #scale_measurements, #total_height

Constructor Details

#initialize(*args) ⇒ Line

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 (for backwards compatibility)

g = Gruff::Line.new(false) # Defaults to 800px wide, no lines (for backwards compatibility)

The preferred way is to call hide_dots or hide_lines instead.

Raises:

  • (ArgumentError)


39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/gruff/line.rb', line 39

def initialize(*args)
  raise ArgumentError, "Wrong number of arguments" if args.length > 2
  if args.empty? or ((not Numeric === args.first) && (not String === args.first)) then
    super()
  else
    super args.shift
  end

  @hide_dots = @hide_lines = false
  @baseline_color = 'red'
  @baseline_value = nil
end

Instance Attribute Details

#baseline_colorObject

Color of the baseline



21
22
23
# File 'lib/gruff/line.rb', line 21

def baseline_color
  @baseline_color
end

#baseline_valueObject

Draw a dashed line at the given value



18
19
20
# File 'lib/gruff/line.rb', line 18

def baseline_value
  @baseline_value
end

#dot_radiusObject

Returns the value of attribute dot_radius.



25
26
27
# File 'lib/gruff/line.rb', line 25

def dot_radius
  @dot_radius
end

#hide_dotsObject

Hide parts of the graph to fit more datapoints, or for a different appearance.



28
29
30
# File 'lib/gruff/line.rb', line 28

def hide_dots
  @hide_dots
end

#hide_linesObject

Hide parts of the graph to fit more datapoints, or for a different appearance.



28
29
30
# File 'lib/gruff/line.rb', line 28

def hide_lines
  @hide_lines
end

#line_widthObject

Dimensions of lines and dots; calculated based on dataset size if left unspecified



24
25
26
# File 'lib/gruff/line.rb', line 24

def line_width
  @line_width
end

Instance Method Details

#contains_one_point_only?(data_row) ⇒ Boolean

Returns:

  • (Boolean)


118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/gruff/line.rb', line 118

def contains_one_point_only?(data_row)
  # Spin through data to determine if there is just one_value present.
  one_point = false
  data_row[DATA_VALUES_INDEX].each do |data_point|
    if !data_point.nil?
      if one_point
        # more than one point, bail
        return false
      else
        # there is at least one data point
        return true
      end
    end
  end
  return one_point
end

#drawObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
109
110
# File 'lib/gruff/line.rb', line 52

def draw
  super

  return unless @has_data

  # Check to see if more than one datapoint was given. NaN can result otherwise.
  @x_increment = (@column_count > 1) ? (@graph_width / (@column_count - 1).to_f) : @graph_width

  if (defined?(@norm_baseline)) then
    level = @graph_top + (@graph_height - @norm_baseline * @graph_height)
    @d = @d.push
    @d.stroke_color @baseline_color
    @d.fill_opacity 0.0
    @d.stroke_dasharray(10, 20)
    @d.stroke_width 5
    @d.line(@graph_left, level, @graph_left + @graph_width, level)
    @d = @d.pop
  end

  @norm_data.each do |data_row|
    prev_x = prev_y = nil

    @one_point = contains_one_point_only?(data_row)

    data_row[DATA_VALUES_INDEX].each_with_index do |data_point, index|
      new_x = @graph_left + (@x_increment * index)
      next if data_point.nil?

      draw_label(new_x, index)

      new_y = @graph_top + (@graph_height - data_point * @graph_height)

      # Reset each time to avoid thin-line errors
      @d = @d.stroke data_row[DATA_COLOR_INDEX]
      @d = @d.fill data_row[DATA_COLOR_INDEX]
      @d = @d.stroke_opacity 1.0
      @d = @d.stroke_width line_width ||
        clip_value_if_greater_than(@columns / (@norm_data.first[DATA_VALUES_INDEX].size * 4), 5.0)


      circle_radius = dot_radius ||
        clip_value_if_greater_than(@columns / (@norm_data.first[DATA_VALUES_INDEX].size * 2.5), 5.0)

      if !@hide_lines and !prev_x.nil? and !prev_y.nil? then
        @d = @d.line(prev_x, prev_y, new_x, new_y)
      elsif @one_point
        # Show a circle if there's just one_point
        @d = @d.circle(new_x, new_y, new_x - circle_radius, new_y)
      end
      @d = @d.circle(new_x, new_y, new_x - circle_radius, new_y) unless @hide_dots

      prev_x = new_x
      prev_y = new_y
    end

  end

  @d.draw(@base_image)
end

#normalizeObject



112
113
114
115
116
# File 'lib/gruff/line.rb', line 112

def normalize
  @maximum_value = [@maximum_value.to_f, @baseline_value.to_f].max
  super
  @norm_baseline = (@baseline_value.to_f / @maximum_value.to_f) if @baseline_value
end