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