| Class | Gruff::Bar |
| In: |
lib/gruff/bar.rb
|
| Parent: | Gruff::Base |
# File lib/gruff/bar.rb, line 6
6: def draw
7: # Labels will be centered over the left of the bar if
8: # there are more labels than columns. This is basically the same
9: # as where it would be for a line graph.
10: @center_labels_over_point = (@labels.keys.length > @column_count ? true : false)
11:
12: super
13: return unless @has_data
14:
15: draw_bars
16: end
# File lib/gruff/bar.rb, line 20
20: def draw_bars
21: # Setup spacing.
22: #
23: # Columns sit side-by-side.
24: spacing_factor = 0.9 # space between the bars
25: @bar_width = @graph_width / (@column_count * @data.length).to_f
26:
27: @d = @d.stroke_opacity 0.0
28:
29: # Setup the BarConversion Object
30: conversion = Gruff::BarConversion.new()
31: conversion.graph_height = @graph_height
32: conversion.graph_top = @graph_top
33:
34: # Set up the right mode [1,2,3] see BarConversion for further explanation
35: if @minimum_value >= 0 then
36: # all bars go from zero to positiv
37: conversion.mode = 1
38: else
39: # all bars go from 0 to negativ
40: if @maximum_value <= 0 then
41: conversion.mode = 2
42: else
43: # bars either go from zero to negativ or to positiv
44: conversion.mode = 3
45: conversion.spread = @spread
46: conversion.minimum_value = @minimum_value
47: conversion.zero = -@minimum_value/@spread
48: end
49: end
50:
51: # iterate over all normalised data
52: @norm_data.each_with_index do |data_row, row_index|
53:
54: data_row[1].each_with_index do |data_point, point_index|
55: # Use incremented x and scaled y
56: # x
57: left_x = @graph_left + (@bar_width * (row_index + point_index + ((@data.length - 1) * point_index)))
58: right_x = left_x + @bar_width * spacing_factor
59: # y
60: conv = []
61: conversion.getLeftYRightYscaled( data_point, conv )
62:
63: # create new bar
64: @d = @d.fill data_row[DATA_COLOR_INDEX]
65: @d = @d.rectangle(left_x, conv[0], right_x, conv[1])
66:
67: # Calculate center based on bar_width and current row
68: label_center = @graph_left +
69: (@data.length * @bar_width * point_index) +
70: (@data.length * @bar_width / 2.0)
71: # Subtract half a bar width to center left if requested
72: draw_label(label_center - (@center_labels_over_point ? @bar_width / 2.0 : 0.0), point_index)
73: end
74:
75: end
76:
77: # Draw the last label if requested
78: draw_label(@graph_right, @column_count) if @center_labels_over_point
79:
80: @d.draw(@base_image)
81: end