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/color.rb,
lib/highcharts/point.rb,
lib/highcharts/title.rb,
lib/highcharts/axis/x.rb,
lib/highcharts/axis/y.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

#initializeHighcharts

Returns a new instance of Highcharts.



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

def initialize
  @options = {}

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



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

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)
        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.



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

def base_options
  @base_options
end

#custom_optionsObject (readonly)

Returns the value of attribute custom_options.



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

def custom_options
  @custom_options
end

#default_optionsObject (readonly)

Returns the value of attribute default_options.



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

def default_options
  @default_options
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Instance Method Details

#inspectObject



21
22
23
# File 'lib/highcharts.rb', line 21

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 %>



50
51
52
53
54
55
56
# File 'lib/highcharts.rb', line 50

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