Class: Gruff::Bullet

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

Overview

Constant Summary

Constants inherited from Base

Gruff::Base::DEFAULT_MARGIN, Gruff::Base::DEFAULT_TARGET_WIDTH, Gruff::Base::LABEL_MARGIN, Gruff::Base::LEGEND_MARGIN, Gruff::Base::THOUSAND_SEPARATOR

Instance Attribute Summary

Attributes inherited from Base

#additional_line_values, #bold_title, #bottom_margin, #center_labels_over_point, #colors, #font, #font_color, #has_left_labels, #hide_legend, #hide_line_markers, #hide_line_numbers, #hide_title, #label_max_size, #label_stagger_height, #label_truncation_style, #labels, #left_margin, #legend_at_bottom, #legend_box_size, #legend_font_size, #legend_margin, #marker_color, #marker_count, #marker_font_size, #marker_shadow_color, #maximum_value, #minimum_value, #no_data_message, #right_margin, #sort, #sorted_drawing, #title, #title_font, #title_font_size, #title_margin, #top_margin, #use_data_label, #x_axis_increment, #x_axis_label, #y_axis_increment, #y_axis_label

Instance Method Summary collapse

Methods inherited from Base

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

Constructor Details

#initialize(target_width = '400x40') ⇒ Bullet

Returns a new instance of Bullet.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/gruff/bullet.rb', line 8

def initialize(target_width = '400x40')
  if target_width.is_a?(String)
    geometric_width, geometric_height = target_width.split('x')
    @columns = geometric_width.to_f
    @rows = geometric_height.to_f
  else
    @columns = target_width.to_f
    @rows = target_width.to_f / 5.0
  end
  @columns.freeze
  @rows.freeze

  initialize_ivars

  reset_themes
  self.theme = Gruff::Themes::GREYSCALE
  @title_font_size = 20
end

Instance Method Details

#data(value, maximum_value, options = {}) ⇒ Object



27
28
29
30
31
32
# File 'lib/gruff/bullet.rb', line 27

def data(value, maximum_value, options = {})
  @value = value.to_f
  self.maximum_value = maximum_value.to_f
  @options = options
  @options.map { |k, v| @options[k] = v.to_f if v.is_a?(Numeric) }
end

#drawObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
# File 'lib/gruff/bullet.rb', line 34

def draw
  # TODO: Left label
  # TODO Bottom labels and markers
  # @graph_bottom
  # Calculations are off 800x???

  @colors.reverse!

  draw_title

  title_width  = calculate_width(@title_font_size, @title)
  margin       = 30.0
  thickness    = @raw_rows / 6.0
  right_margin = margin
  graph_left   = (@title && (title_width * 1.3)) || margin
  graph_width  = @raw_columns - graph_left - right_margin
  graph_height = thickness * 3.0

  # Background
  rect_renderer = Gruff::Renderer::Rectangle.new(color: @colors[0])
  rect_renderer.render(graph_left, 0, graph_left + graph_width, graph_height)

  [:high, :low].each_with_index do |indicator, index|
    next unless @options.key?(indicator)

    indicator_width_x = graph_left + graph_width * (@options[indicator] / maximum_value)

    rect_renderer = Gruff::Renderer::Rectangle.new(color: @colors[index + 1])
    rect_renderer.render(graph_left, 0, indicator_width_x, graph_height)
  end

  if @options.key?(:target)
    target_x = graph_left + graph_width * (@options[:target] / maximum_value)
    half_thickness = thickness / 2.0

    rect_renderer = Gruff::Renderer::Rectangle.new(color: @font_color)
    rect_renderer.render(target_x, half_thickness, target_x + half_thickness, thickness * 2 + half_thickness)
  end

  # Value
  rect_renderer = Gruff::Renderer::Rectangle.new(color: @font_color)
  rect_renderer.render(graph_left, thickness, graph_left + graph_width * (@value / maximum_value), thickness * 2)

  Gruff::Renderer.finish
end