Added the Gruff library to the lib/ directory of the the rails folder, and the
[selectricity] / lib / gruff-0.2.8 / lib / gruff / mini / legend.rb
1 module Gruff
2   module Mini
3     module Legend
4       
5       ##
6       # The canvas needs to be bigger so we can put the legend beneath it.
7
8       def expand_canvas_for_vertical_legend
9         @original_rows = @raw_rows
10         @rows += @data.length * calculate_caps_height(scale_fontsize(@legend_font_size)) * 1.7
11         render_background
12       end
13       
14       ##
15       # Draw the legend beneath the existing graph.
16
17       def draw_vertical_legend
18                 
19         @legend_labels = @data.collect {|item| item[Gruff::Base::DATA_LABEL_INDEX] }
20         
21         legend_square_width = 40.0 # small square with color of this item
22         legend_square_margin = 10.0
23         @legend_left_margin = 40.0
24         legend_top_margin = 40.0
25
26         # May fix legend drawing problem at small sizes
27         @d.font = @font if @font
28         @d.pointsize = @legend_font_size
29
30         current_x_offset = @graph_left + @legend_left_margin
31         current_y_offset = @original_rows + legend_top_margin
32
33         debug { @d.line 0.0, current_y_offset, @raw_columns, current_y_offset }
34
35         @legend_labels.each_with_index do |legend_label, index|        
36
37           # Draw label
38           @d.fill = @font_color
39           @d.font = @font if @font
40           @d.pointsize = scale_fontsize(@legend_font_size)
41           @d.stroke = 'transparent'
42           @d.font_weight = Magick::NormalWeight
43           @d.gravity = Magick::WestGravity
44           @d = @d.annotate_scaled( @base_image, 
45                             @raw_columns, 1.0,
46                             current_x_offset + (legend_square_width * 1.7), current_y_offset, 
47                             truncate_legend_label(legend_label), @scale)
48
49           # Now draw box with color of this dataset
50           @d = @d.stroke 'transparent'
51           @d = @d.fill @data[index][Gruff::Base::DATA_COLOR_INDEX]
52           @d = @d.rectangle(current_x_offset, 
53                             current_y_offset - legend_square_width / 2.0, 
54                             current_x_offset + legend_square_width, 
55                             current_y_offset + legend_square_width / 2.0)
56           
57           current_y_offset += calculate_caps_height(@legend_font_size) * 1.7
58         end
59         @color_index = 0
60       end
61
62       ##
63       # Shorten long labels so they will fit on the canvas.
64       #
65       #   Department of Hu...
66       
67       def truncate_legend_label(label)
68         truncated_label = label.to_s
69         while calculate_width(scale_fontsize(@legend_font_size), truncated_label) > (@columns - @legend_left_margin - Gruff::Base::RIGHT_MARGIN) && (truncated_label.length > 1)
70           truncated_label = truncated_label[0..truncated_label.length-2]
71         end
72         truncated_label + (truncated_label.length < label.to_s.length ? "…" : '')
73       end
74       
75     end
76   end
77 end

Benjamin Mako Hill || Want to submit a patch?