class GoogleChart::BarChart

Generates a Bar Chart. You can specify the alignment(horizontal or vertical) and whether you want the bars to be grouped or stacked

Examples

bc = GoogleChart::BarChart.new('800x200', "Bar Chart", :vertical, false)
bc.data "Trend 1", [5,4,3,1,3,5], '0000ff'

Attributes

alignment[RW]
stacked[RW]

Public Class Methods

new(chart_size='300x200', chart_title=nil, alignment=:vertical, stacked=false) { |self| ... } click to toggle source

Specify the

  • chart_size in WIDTHxHEIGHT format

  • chart_title as a string

  • alignment as either :vertical or :horizontal

  • stacked should be true if you want the bars to be stacked, false otherwise

# File lib/google_chart/bar_chart.rb, line 16
def initialize(chart_size='300x200', chart_title=nil, alignment=:vertical, stacked=false) # :yield: self
  super(chart_size, chart_title)
  @alignment = alignment
  @stacked = stacked
  set_chart_type
  self.show_legend = true
  yield self if block_given?
end

Public Instance Methods

alignment=(value) click to toggle source

Set the alignment to either :vertical or :horizontal

# File lib/google_chart/bar_chart.rb, line 26
def alignment=(value)
  @alignment = value
  set_chart_type
end
process_data() click to toggle source
# File lib/google_chart/bar_chart.rb, line 51
def process_data
  if @stacked # Special handling of max value for stacked
    unless @max_data # Unless max_data is explicitly set
      @max_data = @data.inject([]) do |sum_arr, series| 
        series.each_with_index do |v,i| 
          if sum_arr[i] == nil
            sum_arr[i] = v
          else
            sum_arr[i] += v
          end
        end
        sum_arr
      end.max
    end
  end

  if @data.size > 1              
    join_encoded_data(@data.collect { |series|
                        encode_data(series, max_data_value)
                      })
  else
    encode_data(@data.flatten,max_data_value)
  end
end
stacked=(value) click to toggle source

If you want the bar chart to be stacked, set the value to true, otherwise set the value to false to group it.

# File lib/google_chart/bar_chart.rb, line 32
def stacked=(value)
  @stacked = value
  set_chart_type
end
width_spacing_options(options={}) click to toggle source

Defines options for bar width, spacing between bars and between groups of bars. Applicable for bar charts.

options

: Options for the style, specifying things like line thickness and lengths of the line segment and blank portions

Options

  • :bar_width, Bar width in pixels

  • :bar_spacing (optional), space between bars in a group

  • :group_spacing (optional), space between groups

# File lib/google_chart/bar_chart.rb, line 44
def width_spacing_options(options={})
  options_str = "#{options[:bar_width]}"
  options_str += ",#{options[:bar_spacing]}" if options[:bar_spacing]
  options_str += ",#{options[:group_spacing]}" if options[:bar_spacing] and options[:group_spacing]
  @bar_width_spacing_options = options_str
end