Class: Charting::Highcharts::Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/ar_to_chart/charting/highcharts.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  :container_height      => '200px',
  :type                  => :area,
}
DEFAULT_CHARTING_OBJECT =
'arToChart'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data_source, category_column, data_columns, options = {}) ⇒ Renderer

Generate Highchart based charts. CSS is used for the colouring. See the file ar_to_chart.css included in the files directory of the gem.

Parameters

data_source:        The active record result set
category_column:    The column used for the x-axis
data_columns:       one column name or an array of column names to be charted or a hash
                    with pairs of :column_names => :series_type
options:            options hash

Options

:type               Chart type to render. Defauly is :area.  Other options
                    are :pie and :funnel.  Subclass Base to create other
                    chart types available in Highcharts. The name of the subclass
                    becomes the chart type.
:container          DOM id of the container to render to.  Default is to
                    generate a name.
:container_height   Requested height of the container.  Defaults to 200px
:charting_object    The javascript object that is instantiated as the 
                    browser renderer. Defaults to arToChar (supplied in 
                    the files directory of the gem)


38
39
40
41
42
43
44
# File 'lib/ar_to_chart/charting/highcharts.rb', line 38

def initialize(data_source, category_column, data_columns, options = {})
  @options = DEFAULT_OPTIONS.merge(options)
  @options[:container] ||= generate_container_name
  @options[:charting_object] ||= self.class.charting_object || DEFAULT_CHARTING_OBJECT
  @data_columns = data_columns.respond_to?(:each) ? data_columns : [data_columns]
  @chart = chart_class.new(data_source, category_column, @data_columns, @options)
end

Instance Attribute Details

#categoriesObject

Returns the value of attribute categories.



10
11
12
# File 'lib/ar_to_chart/charting/highcharts.rb', line 10

def categories
  @categories
end

#chartObject

Returns the value of attribute chart.



10
11
12
# File 'lib/ar_to_chart/charting/highcharts.rb', line 10

def chart
  @chart
end

#optionsObject

Returns the value of attribute options.



10
11
12
# File 'lib/ar_to_chart/charting/highcharts.rb', line 10

def options
  @options
end

#seriesObject

Returns the value of attribute series.



10
11
12
# File 'lib/ar_to_chart/charting/highcharts.rb', line 10

def series
  @series
end

Class Method Details

.configure {|_self| ... } ⇒ Object

Set configuration options. Currently no options are available.

Yields:

  • (_self)

Yield Parameters:



82
83
84
# File 'lib/ar_to_chart/charting/highcharts.rb', line 82

def self.configure
  yield self
end

Instance Method Details

#containerObject

Returns the <div> into which the chart will be rendered.



47
48
49
50
51
# File 'lib/ar_to_chart/charting/highcharts.rb', line 47

def container
  <<-EOF
    <div id='#{container_id}' #{styles}"></div>
  EOF
end

#scriptObject

Returns the javscript (without <script> tag) that is sent to the browser to render the chart. Note that there is a dependency on the ar_to_chart.js javascript library being loaded in the <head> of the document.

See the ar_to_chart.js file included in the files directory of the gem.



60
61
62
63
64
65
66
67
# File 'lib/ar_to_chart/charting/highcharts.rb', line 60

def script
  <<-EOF
    $(document).ready(function() {
      chart = new #{charting_object};
      #{chart.to_js}
    });     
  EOF
end

#to_htmlObject

Return the HTML of the container <div> and the script to render the chart.



71
72
73
74
75
76
77
78
# File 'lib/ar_to_chart/charting/highcharts.rb', line 71

def to_html
  <<-EOF
    #{container}
    <script type="text/javascript">
      #{script}
    </script>
  EOF
end