Class: ZTK::GoogleChart::Base

Inherits:
Base
  • Object
show all
Includes:
ArrayToDataTable, DataTable, Dates, Options, Ticks
Defined in:
lib/ztk/google_chart/base.rb,
lib/ztk/google_chart/base/ticks.rb,
lib/ztk/google_chart/base/dates.rb,
lib/ztk/google_chart/base/options.rb,
lib/ztk/google_chart/base/data_table.rb,
lib/ztk/google_chart/base/array_to_data_table.rb

Overview

GoogleChart Base Class

Direct Known Subclasses

Annotation, Bar, Candlestick, Column, Combo, Line, Pie, Sankey

Defined Under Namespace

Modules: ArrayToDataTable, DataTable, Dates, Options, Ticks

Constant Summary

Constant Summary

Constants included from Dates

Dates::DATE_HELPERS

Instance Method Summary (collapse)

Methods included from Ticks

#tick_seed

Methods included from Options

#options, #options=

Methods included from Dates

#date_format, #date_scale, #date_seed, #date_wrapper

Methods included from DataTable

#data_table, #data_table_function, #data_table_render

Methods included from ArrayToDataTable

#array_to_data_table, #array_to_data_table_function, #array_to_data_table_render

Methods inherited from Base

build_config, #config, #direct_log, hash_config, log_and_raise, #log_and_raise

Constructor Details

- (Base) initialize(configuration = {})

Returns a new instance of Base

Parameters:

  • configuration (Hash) (defaults to: {})

    Configuration options hash.



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ztk/google_chart/base.rb', line 25

def initialize(configuration={})
  super({ :id => generate_id }, configuration)

  @id = config.id.to_s.underscore.gsub(/ /, '')
  @chart_name_tag = "chart_#{@id}"
  @chart_data_tag = "#{@chart_name_tag}_data"
  @chart_options_tag = "#{@chart_name_tag}_options"
  @chart_draw_tag = "#{@chart_name_tag}_draw"
  @chart_div_tag = "#{@chart_name_tag}_div"
  @chart_type_tag = config.type
end

Instance Method Details

- (Object) body(&block)



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ztk/google_chart/base.rb', line 48

def body(&block)
  <<-EOCHART
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
  var #{@chart_data_tag};
  var #{@chart_name_tag};

  google.load('visualization', '1', {'packages':['corechart']});
  google.load('visualization', '1.1', {'packages':['annotationchart']});
  google.load('visualization', '1.1', {'packages':['sankey']});
  google.setOnLoadCallback(#{@chart_draw_tag});

#{block.call.chomp}
</script>
<div id="#{@chart_div_tag}"></div>
EOCHART
end

- (Object) generate_id



66
67
68
69
70
71
72
# File 'lib/ztk/google_chart/base.rb', line 66

def generate_id
  generated_id = Array.new

  generated_id << SecureRandom.hex(16)

  generated_id.join('_')
end

- (Object) render(content = nil)



37
38
39
40
41
42
43
44
45
46
# File 'lib/ztk/google_chart/base.rb', line 37

def render(content=nil)
  case @chart_method
  when :data_table
    data_table_render
  when :array_to_data_table
    array_to_data_table_render
  else
    raise "You must supply chart data via DataTable or ArrayToDataTable!"
  end
end