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