Method: BarChart#draw_bar_graph

Defined in:
lib/bar_chart.rb

#draw_bar_graph(data, data_description, shadow = false, alpha = 100) ⇒ Object

When creating a bar graph, don’t forget to set the with_margin parameter of the draw_scale function to true. Setting shadow to true will draw a shadow behind each series, this will also slow down a bit the renderer engine.



6
7
8
9
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
# File 'lib/bar_chart.rb', line 6

def draw_bar_graph(data,data_description,shadow=false,alpha=100)
  data_description = self.validate_data_description("drawBarGraph",data_description)
  validate_data("drawBarGraph",data)

 # graph_id      = 0
  series       = (data_description["values"]).count
  series_width  = @division_width / (series+1)
  serie_x_offset = @division_width / 2 - series_width / 2

  y_zero  = @g_area_y2 - ((0-@vmin) * @division_ratio)
  y_zero = @g_area_y2 if ( y_zero> @g_area_y2 )
  serie_id = 0
  color_id =0
  id = 0
  data_description["values"].each do |col_name|
    data_description["description"].each do |key_i,value_i|
      if ( key_i == col_name )
        color_id = id
        id = id+1
      end
    end
    x_pos = @g_area_x1 + @g_area_x_offset - serie_x_offset + series_width * serie_id
  #  x_last = -1
    data.each do |key|
      if ( !key[col_name].nil?)
        if ( key[col_name].is_a?(Numeric) )
          value = key[col_name]
          y_pos = @g_area_y2 - ((value-@vmin) * @division_ratio)
          #  Save point into the image map if option activated */
          if (@build_map )
            #add_to_image_map(x_pos+1,[y_zero,y_pos].min,x_pos+series_width-1,[y_zero,y_pos].max,data_description["description"][col_name],data[key][col_name].data_description["unit"]["y"],"Bar")
          end
          if ( shadow && alpha == 100 )
            draw_rectangle(x_pos+1,y_zero,x_pos+series_width-1,y_pos,25,25,25)
          end
          draw_filled_rectangle(x_pos+1,y_zero,x_pos+series_width-1,y_pos,@palette[color_id]["r"],@palette[color_id]["g"],@palette[color_id]["b"],true,alpha)
        end
        x_pos = x_pos + @division_width
      end
    end
    serie_id = serie_id+1
  end
end