Class: HighChart

Inherits:
Object
  • Object
show all
Defined in:
app/models/high_chart.rb

Constant Summary collapse

CANVAS_DEFAULT_HTML_OPTIONS =
{:style => "height: 300px, width:615px" }
SERIES_OPTIONS =
%w(lines points bars shadowSize colors)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(canvas = nil, html_opts = {}) ⇒ HighChart

Returns a new instance of HighChart.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/models/high_chart.rb', line 11

def initialize(canvas = nil, html_opts = {})

  @collection_filter = nil
  self.tap do |high_chart|
    high_chart.data       ||= []
    high_chart.options    ||= {}
    high_chart.defaults_options
    high_chart.html_options = html_opts.reverse_merge(CANVAS_DEFAULT_HTML_OPTIONS)
    high_chart.canvas       = canvas if canvas
    yield high_chart if block_given?
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, opts = {}) ⇒ Object

Pass other methods through to the javascript high_chart object.

For instance: high_chart.grid(:color => "#699")



46
47
48
# File 'app/models/high_chart.rb', line 46

def method_missing(meth, opts = {})
  merge_options meth, opts
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



6
7
8
# File 'app/models/high_chart.rb', line 6

def data
  @data
end

#html_optionsObject

Returns the value of attribute html_options.



6
7
8
# File 'app/models/high_chart.rb', line 6

def html_options
  @html_options
end

#optionsObject

Returns the value of attribute options.



6
7
8
# File 'app/models/high_chart.rb', line 6

def options
  @options
end

#placeholderObject Also known as: canvas

Returns the value of attribute placeholder.



6
7
8
# File 'app/models/high_chart.rb', line 6

def placeholder
  @placeholder
end

Instance Method Details

#defaults_optionsObject

title: legend: xAxis: yAxis: tooltip: credits: :plotOptions



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/models/high_chart.rb', line 26

def defaults_options
  self.title({ :text=>"example test title from plugin"})
  self.legend({:layout=>"vertical", :style=>{:position=>'absolute', :bottom=>'auto', :left=>'150px', :top=>'150px'}, :borderWidth=> 1, :backgroundColor=>'#FFFFFF'}) 
  self.x_axis({:categories => ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'],
               :plotBands  => [{ :from=> 6.0,:to=> 6.5,:color=> 'rgba(68, 170, 213, .2)'}],
               :labels     =>{ :align=>'right',:rotation=>45 }
  })
  self.y_axis({:title=> {:text=> 'Fruit units'}, :labels=>{:align=>'right'} })
  self.tooltip({ :enabled=>true })
  self.credits({:enabled => false})
  self.plot_options({ :areaspline => {:fillOpacity => 0.5}})
  self.chart({:defaultSeriesType=>"areaspline" , :renderTo => nil})
  self.subtitle({})
end

#series(opts = {}) ⇒ Object

Add a simple series to the graph:

data = [[0,5], [1,5], [2,5]]
@high_chart.series :name=>'Updated', :data=>data
@high_chart.series :name=>'Updated', :data=>[5, 1, 6, 1, 5, 4, 9]


56
57
58
59
60
61
62
63
# File 'app/models/high_chart.rb', line 56

def series(opts = {})
  @data ||= []
  if opts.blank?
    @data << series_options.merge(:name => label, :data => d)
  else
    @data << opts.merge(:name => opts[:name], :data => opts[:data])
  end
end