Class: SideBar

Inherits:
Gruff::Base show all
Defined in:
src/view/ajax/ddd_chart.rb

Constant Summary

LABEL_MARGIN =
LEGEND_MARGIN = TITLE_MARGIN = 4
FACTOR_PTRN =
/^[\s\d.\/]+x\s*/

Instance Method Summary (collapse)

Instance Method Details

- (Object) draw



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
# File 'src/view/ajax/ddd_chart.rb', line 10

def draw
  @has_left_labels = true
  super

  return unless @has_data

  # Setup spacing.
  #
  spacing_factor = 0.8

  @bars_width = @graph_height / @column_count.to_f
  @bar_width = @bars_width * spacing_factor / @norm_data.size
  @d         = @d.stroke_opacity 0.0
  height     = Array.new(@column_count, 0)
  length     = Array.new(@column_count, @graph_left)

  @norm_data.each_with_index do |data_row, row_index|
    @d = @d.fill data_row[DATA_COLOR_INDEX]

    data_row[DATA_VALUES_INDEX].each_with_index do |data_point, point_index|

      if clrs = @theme_options[:custom_colors]
        @d = @d.fill clrs[point_index] || data_row[DATA_COLOR_INDEX]
      end

      # Using the original calcs from the stacked bar chart
      # to get the difference between
      # part of the bart chart we wish to stack.
      temp1      = @graph_left + (@graph_width - data_point * @graph_width - height[point_index])
      temp2      = @graph_left + @graph_width - height[point_index]
      difference = temp2 - temp1

      left_x     = length[point_index] - 1
      left_y     = @graph_top + (@bars_width * point_index) + (@bar_width * row_index)
      right_x    = left_x + difference
      right_y    = left_y + @bar_width

      height[point_index] += (data_point * @graph_width)

      @d           = @d.rectangle(left_x, left_y, right_x, right_y)

      # Calculate center based on bar_width and current row
      label_center = @graph_top + (@bars_width * point_index + @bars_width / 2)
      draw_label(label_center, point_index)
    end

  end

  draw_source

  @d.draw(@base_image)
end

- (Object) draw_label(y_offset, index)



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
# File 'src/view/ajax/ddd_chart.rb', line 63

def draw_label(y_offset, index)
  if (text = @labels[index]) && @labels_seen[index].nil?
    price = text.slice! /[A-Z]{3}\s+[\d.]+$/
    @d.fill             = @font_color
    @d.font             = @font if @font
    @d.stroke           = 'transparent'
    @d.pointsize        = scale_fontsize(@marker_font_size)
    @d.gravity          = WestGravity
    x_offset = @left_margin + LABEL_MARGIN
    if factor = text.slice!(FACTOR_PTRN)
      @d.font_weight    = BoldWeight
      @d                = @d.annotate_scaled(@base_image,
                          1, 1, x_offset, y_offset, factor, @scale)
    end
    @d.font_weight      = NormalWeight
    @d                  = @d.annotate_scaled(@base_image,
                            1, 1, x_offset + @factor_width, y_offset,
                            text, @scale)

    @d.gravity          = EastGravity
    @d                  = @d.annotate_scaled(@base_image,
                            1, 1,
                            -@graph_left + LABEL_MARGIN * 2.0, y_offset,
                            price, @scale)

    @labels_seen[index] = 1
  end
end

- (Object) draw_line_markers



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'src/view/ajax/ddd_chart.rb', line 91

def draw_line_markers

  return if @hide_line_markers

  @d = @d.stroke_antialias false

  # Draw horizontal line markers and annotate with numbers
  @d = @d.stroke(@marker_color)
  @d = @d.stroke_width 1
  number_of_lines = [@maximum_value.to_f / 0.5, 4].min

  # TODO Round maximum marker value to a round number like 100, 0.1, 0.5, etc.
  increment = significant(@maximum_value.to_f / number_of_lines)
  (0..number_of_lines).each do |index|

    line_diff    = (@graph_right - @graph_left) / number_of_lines
    x            = @graph_right - (line_diff * index) - 1
    @d           = @d.line(x, @graph_bottom, x, @graph_top)
    diff         = index - number_of_lines
    marker_label = "%4.2f" % (diff.abs * increment)

    unless @hide_line_numbers
      @d.fill      = @font_color
      @d.font      = @font if @font
      @d.stroke    = 'transparent'
      @d.pointsize = scale_fontsize(@marker_font_size)
      @d.gravity   = CenterGravity
      # TODO Center text over line
      @d           = @d.annotate_scaled( @base_image,
                        0, 0, # Width of box to draw text in
                        x, @graph_bottom + (LABEL_MARGIN * 2.0), # Coordinates of text
                        marker_label, @scale)
    end # unless
    @d = @d.stroke_antialias true
  end
end

- (Object) draw_source



127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'src/view/ajax/ddd_chart.rb', line 127

def draw_source
    @d.fill             = @font_color
    @d.font             = @font if @font
    @d.stroke           = 'transparent'
    @d.font_weight      = NormalWeight
    @d.pointsize        = scale_fontsize(@theme_options[:source_font_size])
    @d.gravity          = WestGravity
    @d                  = @d.annotate_scaled(@base_image,
                            1, 1,
                            @left_margin + LABEL_MARGIN,
                            @graph_bottom + (LABEL_MARGIN * 2.0),
                            @theme_options[:source], @scale)
end

- (Object) draw_title



140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'src/view/ajax/ddd_chart.rb', line 140

def draw_title
  return if (@hide_title || @title.nil?)

  @d.fill = @font_color
  @d.font = @font if @font
  @d.stroke('transparent')
  @d.pointsize = scale_fontsize(@title_font_size)
  @d.font_weight = BoldWeight
  @d.gravity = NorthWestGravity
  @d = @d.annotate_scaled( @base_image,
  @raw_columns, 1.0,
  TITLE_MARGIN, @top_margin,
  @title, @scale)
end

- (Object) setup_graph_measurements



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'src/view/ajax/ddd_chart.rb', line 154

def setup_graph_measurements
  @marker_caps_height = @hide_line_markers ? 0 :
  calculate_caps_height(@marker_font_size)
  @title_caps_height = @hide_title ? 0 :
  calculate_caps_height(@title_font_size)
  @legend_caps_height = @hide_legend ? 0 :
  calculate_caps_height(@legend_font_size)

  @factor_width = 0
  label_kerning = 1.2

  if @hide_line_markers
    (@graph_left,
    @graph_right_margin,
    @graph_bottom_margin) = [@left_margin, @right_margin, @bottom_margin]
  else
    longest_left_label_width = 0
    if @has_left_labels
      longest_factor = longest_label = ''
      labels.values.each do |label|
        if label.to_s.length > longest_label.length
          longest_label = label.to_s
        end
        if (factor = label[FACTOR_PTRN]) \
          && factor.length > longest_factor.length
          longest_factor = factor
        end
      end
      unless longest_label.empty?
        longest_left_label_width = calculate_width(@marker_font_size,
                                                   longest_label) * label_kerning
      end
      unless longest_factor.empty?
        @factor_width = calculate_width(@marker_font_size,
                                        longest_factor) * label_kerning
      end
    else
      longest_left_label_width = calculate_width(@marker_font_size,
      label(@maximum_value.to_f))
    end

    # Shift graph if left line numbers are hidden
    line_number_width = @hide_line_numbers && !@has_left_labels ?
    0.0 :
    (longest_left_label_width + LABEL_MARGIN)

    @graph_left = @left_margin +
    line_number_width +
    (@y_axis_label.nil? ? 0.0 : @marker_caps_height + LABEL_MARGIN * 2)

    # Make space for half the width of the rightmost column label.
    # Might be greater than the number of columns if between-style bar markers are used.
    last_label = "%4.2f" % @maximum_value
    extra_room_for_long_label = calculate_width(@marker_font_size, last_label) / 2.0
    @graph_right_margin = @right_margin + extra_room_for_long_label

    @graph_bottom_margin = @bottom_margin +
    @marker_caps_height + LABEL_MARGIN
  end

  @graph_right = @raw_columns - @graph_right_margin
  @graph_width = @raw_columns - @graph_left - @graph_right_margin

  # When @hide title, leave a TITLE_MARGIN space for aesthetics.
  # Same with @hide_legend
  @graph_top = @top_margin +
  (@hide_title  ? TITLE_MARGIN  : @title_caps_height  + TITLE_MARGIN  * 2) +
  (@hide_legend ? LEGEND_MARGIN : @legend_caps_height + LEGEND_MARGIN * 2)

  x_axis_label_height = @x_axis_label.nil? ? 0.0 :
  @marker_caps_height + LABEL_MARGIN
  @graph_bottom = @raw_rows - @graph_bottom_margin - x_axis_label_height
  @graph_height = @graph_bottom - @graph_top
end