| Class | Gruff::Bullet |
| In: |
lib/gruff/bullet.rb
|
| Parent: | Gruff::Base |
# File lib/gruff/bullet.rb, line 5
5: def initialize(target_width="400x40")
6: if not Numeric === target_width
7: geometric_width, geometric_height = target_width.split('x')
8: @columns = geometric_width.to_f
9: @rows = geometric_height.to_f
10: else
11: @columns = target_width.to_f
12: @rows = target_width.to_f / 5.0
13: end
14:
15: initialize_ivars
16:
17: reset_themes
18: theme_greyscale
19: @title_font_size = 20
20: end
# File lib/gruff/bullet.rb, line 22
22: def data(value, maximum_value, options={})
23: @value = value.to_f
24: @maximum_value = maximum_value.to_f
25: @options = options
26: @options.map { |k, v| @options[k] = v.to_f if v === Numeric }
27: end
def setup_drawing
# Maybe should be done in one of the following functions for more granularity.
unless @has_data
draw_no_data()
return
end
normalize()
setup_graph_measurements()
sort_norm_data() if @sort # Sort norm_data with avg largest values set first (for display)
draw_legend()
draw_line_markers()
draw_axis_labels()
draw_title
end
# File lib/gruff/bullet.rb, line 46
46: def draw
47: # TODO Left label
48: # TODO Bottom labels and markers
49: # @graph_bottom
50: # Calculations are off 800x???
51:
52: @colors.reverse!
53:
54: draw_title
55:
56: @margin = 30.0
57: @thickness = @raw_rows / 6.0
58: @right_margin = @margin
59: @graph_left = @title_width * 1.3 rescue @margin # HACK Need to calculate real width
60: @graph_width = @raw_columns - @graph_left - @right_margin
61: @graph_height = @thickness * 3.0
62:
63: # Background
64: @d = @d.fill @colors[0]
65: @d = @d.rectangle(@graph_left, 0, @graph_left + @graph_width, @graph_height)
66:
67: [:high, :low].each_with_index do |indicator, index|
68: next unless @options.has_key?(indicator)
69: @d = @d.fill @colors[index + 1]
70: indicator_width_x = @graph_left + @graph_width * (@options[indicator] / @maximum_value)
71: @d = @d.rectangle(@graph_left, 0, indicator_width_x, @graph_height)
72: end
73:
74: if @options.has_key?(:target)
75: @d = @d.fill @font_color
76: target_x = @graph_left + @graph_width * (@options[:target] / @maximum_value)
77: half_thickness = @thickness / 2.0
78: @d = @d.rectangle(target_x, half_thickness, target_x + half_thickness, @thickness * 2 + half_thickness)
79: end
80:
81: # Value
82: @d = @d.fill @font_color
83: @d = @d.rectangle(@graph_left, @thickness, @graph_left + @graph_width * (@value / @maximum_value), @thickness * 2)
84:
85: @d.draw(@base_image)
86: end
# File lib/gruff/bullet.rb, line 88
88: def draw_title
89: return unless @title
90:
91: @font_height = calculate_caps_height(scale_fontsize(@title_font_size))
92: @title_width = calculate_width(@title_font_size, @title)
93:
94: @d.fill = @font_color
95: @d.font = @font if @font
96: @d.stroke('transparent')
97: @d.font_weight = NormalWeight
98: @d.pointsize = scale_fontsize(@title_font_size)
99: @d.gravity = NorthWestGravity
100: @d = @d.annotate_scaled(*[
101: @base_image,
102: 1.0, 1.0,
103: @font_height/2, @font_height/2,
104: @title,
105: @scale
106: ])
107: end