Class: GoogleChart::BarChart

Inherits:
Base
  • Object
show all
Defined in:
lib/google_chart/bar_chart.rb

Overview

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'

Constant Summary

Constants inherited from Base

GoogleChart::Base::BASE_URL, GoogleChart::Base::COMPLEX_ENCODING_ALPHABET, GoogleChart::Base::DEFAULT_LINE_STYLE, GoogleChart::Base::SHAPE_MARKERS, GoogleChart::Base::SIMPLE_ENCODING

Instance Attribute Summary collapse

Attributes inherited from Base

#chart_size, #chart_title, #chart_type, #data_encoding, #params, #show_legend, #title_color, #title_font_size

Instance Method Summary collapse

Methods inherited from Base

#axis, #data, #fill, #fill_area, #grid, #max_value, #range_marker, #shape_marker, #to_escaped_url, #to_url

Constructor Details

#initialize(chart_size = '300x200', chart_title = nil, alignment = :vertical, stacked = false) {|_self| ... } ⇒ BarChart

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

Yields:

  • (_self)

Yield Parameters:



16
17
18
19
20
21
22
23
# 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

Instance Attribute Details

#alignmentObject

Returns the value of attribute alignment.



9
10
11
# File 'lib/google_chart/bar_chart.rb', line 9

def alignment
  @alignment
end

#stackedObject

Returns the value of attribute stacked.



9
10
11
# File 'lib/google_chart/bar_chart.rb', line 9

def stacked
  @stacked
end

Instance Method Details

#process_dataObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# 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

#width_spacing_options(options = {}) ⇒ Object

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



44
45
46
47
48
49
# 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