Class: Highcharts

Inherits:
ActionView::Base
  • Object
show all
Includes:
ActionView::Helpers
Defined in:
lib/highcharts.rb,
lib/highcharts/axis.rb,
lib/highcharts/base.rb,
lib/highcharts/chart.rb,
lib/highcharts/point.rb,
lib/highcharts/title.rb,
lib/highcharts/axis/x.rb,
lib/highcharts/axis/y.rb,
lib/highcharts/colors.rb,
lib/highcharts/engine.rb,
lib/highcharts/labels.rb,
lib/highcharts/legend.rb,
lib/highcharts/series.rb,
lib/highcharts/credits.rb,
lib/highcharts/tooltip.rb,
lib/highcharts/axis/events.rb,
lib/highcharts/plot_options.rb,
lib/highcharts/point/events.rb,
lib/highcharts/axis/plot_bands.rb,
lib/highcharts/axis/plot_lines.rb,
lib/highcharts/axis/stack_labels.rb,
lib/highcharts/plot_options/plot_type.rb,
lib/highcharts/plot_options/plot_type/events.rb,
lib/highcharts/plot_options/plot_type/marker.rb,
lib/highcharts/plot_options/plot_type/states.rb,
lib/highcharts/plot_options/plot_type/states/hover.rb,
lib/highcharts/plot_options/plot_type/marker/states.rb

Defined Under Namespace

Classes: Axis, Base, Chart, Colors, Credits, Engine, Labels, Legend, PlotOptions, Point, Series, Title, Tooltip

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Highcharts

Returns a new instance of Highcharts.

Yields:

  • (_self)

Yield Parameters:

  • _self (Highcharts)

    the object that the method was called on



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/highcharts.rb', line 9

def initialize
  @options = {}

  @base_options = %w(global labels lang loading navigation pane)
  @default_options = %w(chart colors credits legend series title tooltip)
  @custom_options = {
    'plotOptions' => 'PlotOptions',
    'subtitle' => 'Title',
    'xAxis' => 'Axis::X',
    'yAxis' => 'Axis::Y'
  }

  yield self if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/highcharts.rb', line 28

def method_missing(method, *args)
  # We only want to allow the base + custom options to be constantized and instantiated
  if (base_options + default_options + custom_options.keys).include?(method.to_s)
    # If there are arguments passed to this method, we want to set the option.
    if args.length > 0
      # For xAxis, yAxis, and series, we need to take the array that is passed as the option
      # and for each value, instantiate a new class.
      @options[method] = if %w(xAxis yAxis series).include?(method.to_s)
        Array.wrap(args.first).collect {|v| determine_method_class(method).constantize.new(v)}
      # For all others, just instantiate the class with the original arguments.
      else
        determine_method_class(method).constantize.new(*args)
      end
    # Otherwise, just return the option's value.
    else
      options[method]
    end
  else
    raise NoMethodError
  end
end

Instance Attribute Details

#base_optionsObject (readonly)

Returns the value of attribute base_options.



7
8
9
# File 'lib/highcharts.rb', line 7

def base_options
  @base_options
end

#custom_optionsObject (readonly)

Returns the value of attribute custom_options.



7
8
9
# File 'lib/highcharts.rb', line 7

def custom_options
  @custom_options
end

#default_optionsObject (readonly)

Returns the value of attribute default_options.



7
8
9
# File 'lib/highcharts.rb', line 7

def default_options
  @default_options
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/highcharts.rb', line 7

def options
  @options
end

Instance Method Details

#inspectObject



24
25
26
# File 'lib/highcharts.rb', line 24

def inspect
  "#<#{self.class}:0x#{object_id} #{options.inspect}>"
end

#to_sObject

Called by default when you output the final chart. Spits out jQuery-encapsulated Highcharts JavaScript. <% chart = Highcharts.new -%> <%= chart %>



53
54
55
56
57
58
59
# File 'lib/highcharts.rb', line 53

def to_s
  javascript_tag "$(function(){" +
    "new Highcharts.Chart({" +
      to_json +
    "})" +
  "});"
end