diff --git a/lib/daru/view/adapters/googlecharts.rb b/lib/daru/view/adapters/googlecharts.rb
index b750960..a9d8282 100644
--- a/lib/daru/view/adapters/googlecharts.rb
+++ b/lib/daru/view/adapters/googlecharts.rb
@@ -1,6 +1,6 @@
require 'google_visualr'
require_relative 'googlecharts/iruby_notebook'
-require_relative 'googlecharts/display'
+require_relative 'googlecharts/google_visualr'
require 'daru'
require 'bigdecimal'
require 'daru/view/constants'
@@ -58,6 +58,16 @@ module GooglechartsAdapter
# options = {type: :area}
# chart = Daru::View::Plot.new(data, options)
#
+ # @example ChartEditor
+ # data = [
+ # ['Year', 'Sales', 'Expenses'],
+ # ['2013', 1000, 400],
+ # ['2014', 1170, 460],
+ # ['2015', 660, 1120],
+ # ['2016', 1030, 540]
+ # ]
+ # plot = Daru::View::Plot.new(data, {}, chart_class: 'Charteditor')
+ #
# @example Multiple Charts in a row
# Draw the Daru::View::PlotList object with the data as an array of
# Daru::View::Plots(s) or Daru::View::Table(s) or both
@@ -115,6 +125,16 @@ def init(data=[], options={}, user_options={})
# query = 'SELECT A, H, O, Q, R, U LIMIT 5 OFFSET 8'
# data << query
# chart = Daru::View::Table.new(data)
+ #
+ # @example ChartEditor
+ # data = [
+ # ['Year', 'Sales', 'Expenses'],
+ # ['2013', 1000, 400],
+ # ['2014', 1170, 460],
+ # ['2015', 660, 1120],
+ # ['2016', 1030, 540]
+ # ]
+ # table = Daru::View::Table.new(data, {}, chart_class: 'Charteditor')
def init_table(data=[], options={}, user_options={})
# if `options` is something like this :
# {
@@ -141,23 +161,6 @@ def init_table(data=[], options={}, user_options={})
@table
end
- # @param data [Array, Daru::DataFrame, Daru::Vector, Daru::View::Table]
- # The data provided by the user to generate the google datatable.
- # Data in String format represents the URL of the google spreadsheet
- # from which data has to invoked
- # @return [GoogleVisualr::DataTable] the table object will the data
- # filled
- def get_table(data)
- if data.is_a?(Daru::View::Table) &&
- data.table.is_a?(GoogleVisualr::DataTable)
- data.table
- elsif data.is_a?(GoogleVisualr::DataTable)
- data
- else
- add_data_in_table(data)
- end
- end
-
# @param data [String] URL of the google spreadsheet from which data
# has to invoked
# @return [Boolean, void] returns true for valid URL and raises error
@@ -216,6 +219,23 @@ def add_series(plot, opts={})
private
+ # @param data [Array, Daru::DataFrame, Daru::Vector, Daru::View::Table]
+ # The data provided by the user to generate the google datatable.
+ # Data in String format represents the URL of the google spreadsheet
+ # from which data has to invoked
+ # @return [GoogleVisualr::DataTable] the table object with the data
+ # filled
+ def get_table(data)
+ if data.is_a?(Daru::View::Table) &&
+ data.table.is_a?(GoogleVisualr::DataTable)
+ data.table
+ elsif data.is_a?(GoogleVisualr::DataTable)
+ data
+ else
+ add_data_in_table(data)
+ end
+ end
+
def extract_chart_type(options)
# TODO: Imprvoe this method.
chart_type = options[:type].nil? ? 'Line' : options.delete(:type)
diff --git a/lib/daru/view/adapters/googlecharts/base_chart.rb b/lib/daru/view/adapters/googlecharts/base_chart.rb
index bbdb8f2..9226957 100644
--- a/lib/daru/view/adapters/googlecharts/base_chart.rb
+++ b/lib/daru/view/adapters/googlecharts/base_chart.rb
@@ -18,6 +18,18 @@ def extract_option_view
'\'\''
end
+ # @param element_id [String] The ID of the DIV element that the Google
+ # ChartEditor should be rendered in
+ # @return [String] Generates JavaScript for loading the charteditor package,
+ # with callback to render ChartEditor
+ def load_js_chart_editor(element_id)
+ js = ''
+ js << "\n google.load('visualization', '#{version}', "
+ js << " {packages: ['charteditor'], callback:"
+ js << " #{chart_function_name(element_id)}});"
+ js
+ end
+
# Generates JavaScript function for rendering the chart when data is URL of
# the google spreadsheet
#
diff --git a/lib/daru/view/adapters/googlecharts/data_table_iruby.rb b/lib/daru/view/adapters/googlecharts/data_table_iruby.rb
index 91a5367..1db145c 100644
--- a/lib/daru/view/adapters/googlecharts/data_table_iruby.rb
+++ b/lib/daru/view/adapters/googlecharts/data_table_iruby.rb
@@ -72,7 +72,9 @@ def google_table_version
end
def package_name
- 'table'
+ return 'table' unless
+ user_options && user_options[:chart_class].to_s.capitalize == 'Charteditor'
+ 'charteditor'
end
# @return [String] Returns value of the view option provided by the user
@@ -91,8 +93,8 @@ def extract_option_view
def load_js(element_id)
js = ''
js << "\n google.load('visualization', #{google_table_version}, "
- js << "\n {packages: ['#{package_name}'], callback:"
- js << "\n #{chart_function_name(element_id)}});"
+ js << " {packages: ['#{package_name}'], callback:"
+ js << " #{chart_function_name(element_id)}});"
js
end
diff --git a/lib/daru/view/adapters/googlecharts/display.rb b/lib/daru/view/adapters/googlecharts/display.rb
index badac85..39209af 100644
--- a/lib/daru/view/adapters/googlecharts/display.rb
+++ b/lib/daru/view/adapters/googlecharts/display.rb
@@ -2,356 +2,259 @@
require 'erb'
require_relative 'data_table_iruby'
require_relative 'base_chart'
-require 'daru/view/constants'
+require_relative 'generate_javascript'
require_relative 'formatters'
-module GoogleVisualr
- def self.init_script(
- dependent_js=GOOGLECHARTS_DEPENDENCIES_WEB
- )
- js = ''
- js << "\n"
- js
- end
-
- module Display
- # Holds a value only when to_html method is invoked
- # @return [String] The ID of the DIV element that the Google Chart or
- # Google DataTable should be rendered in
- attr_accessor :html_id
- attr_accessor :formatters
-
- def show_script(dom=SecureRandom.uuid, options={})
- script_tag = options.fetch(:script_tag) { true }
- # To understand different formatters see:
- # https://developers.google.com/chart/interactive/docs/reference#formatters
- apply_formatters if is_a?(GoogleVisualr::DataTable)
- if script_tag
- show_script_with_script_tag(dom)
- # Without checking for user_options, data as hash was not working!
- elsif user_options &&
- user_options[:chart_class].to_s.capitalize == 'Chartwrapper'
- get_html_chart_wrapper(data, dom)
- elsif data.is_a?(String)
- get_html_spreadsheet(data, dom)
- else
- get_html(dom)
- end
- end
-
- # @param dom [String] The ID of the DIV element that the Google
- # Chart should be rendered in
- # @return [String] js code to render the chart with script tag
- def show_script_with_script_tag(dom=SecureRandom.uuid)
- # if it is data table
- if user_options &&
- user_options[:chart_class].to_s.capitalize == 'Chartwrapper'
- to_js_chart_wrapper(data, dom)
- elsif data.is_a?(String)
- to_js_spreadsheet(data, dom)
- elsif is_a?(GoogleVisualr::DataTable)
- to_js_full_script(dom)
- else
- to_js(dom)
- end
- end
-
- # @param dom [String] The ID of the DIV element that the Google
- # Chart should be rendered in
- # @return [String] js code to render the chart
- def get_html(dom)
- html = ''
- html << load_js(dom)
- html << if is_a?(GoogleVisualr::DataTable)
- draw_js(dom)
- else
- draw_chart_js(dom)
- end
- html
- end
-
- # @param data [String] URL of the google spreadsheet in the specified
- # format: https://developers.google.com/chart/interactive/docs
- # /spreadsheets
- # Query string can be appended to retrieve the data accordingly
- # @param dom [String] The ID of the DIV element that the Google
- # Chart should be rendered in when data is the the URL of the google
- # spreadsheet
- # @return [String] js code to render the chart when data is the URL of
- # the google spreadsheet
- def get_html_spreadsheet(data, dom)
- html = ''
- html << load_js(dom)
- html << draw_js_spreadsheet(data, dom)
- html
- end
+module Display
+ # Holds a value only when to_html method is invoked
+ # @return [String] The ID of the DIV element that the Google Chart or
+ # Google DataTable should be rendered in
+ attr_accessor :html_id
+ attr_accessor :formatters
- def get_html_chart_wrapper(data, dom)
- html = ''
- html << load_js(dom)
- html << draw_js_chart_wrapper(data, dom)
- html
- end
-
- def to_html(id=nil, options={})
- path = File.expand_path(
- '../../templates/googlecharts/chart_div.erb', __dir__
- )
- template = File.read(path)
- id ||= SecureRandom.uuid
- @html_id = id
- add_listener_to_chart
- chart_script = show_script(id, script_tag: false)
- ERB.new(template).result(binding)
- end
-
- def show_in_iruby(dom=SecureRandom.uuid)
- IRuby.html to_html(dom)
+ def show_script(dom=SecureRandom.uuid, options={})
+ script_tag = options.fetch(:script_tag) { true }
+ # To understand different formatters see:
+ # https://developers.google.com/chart/interactive/docs/reference#formatters
+ apply_formatters if is_a?(GoogleVisualr::DataTable)
+ if script_tag
+ show_script_with_script_tag(dom)
+ # Without checking for user_options, data as hash was not working!
+ elsif user_options && user_options[:chart_class]
+ extract_chart_wrapper_editor_js(dom)
+ elsif data.is_a?(String)
+ get_html_spreadsheet(data, dom)
+ else
+ get_html(dom)
end
+ end
- # @return [void] Adds listener to the chart from the
- # user_options[:listeners]
- def add_listener_to_chart
- return unless user_options && user_options[:listeners]
- user_options[:listeners].each do |event, callback|
- add_listener(event.to_s.downcase, callback)
- end
+ # @param dom [String] The ID of the DIV element that the Google
+ # Chart should be rendered in
+ # @return [String] js code to render the chart with script tag
+ def show_script_with_script_tag(dom=SecureRandom.uuid)
+ # if it is data table
+ if user_options &&
+ user_options[:chart_class].to_s.capitalize == 'Chartwrapper'
+ to_js_chart_wrapper(data, dom)
+ elsif data.is_a?(String)
+ to_js_spreadsheet(data, dom)
+ elsif is_a?(GoogleVisualr::DataTable)
+ to_js_full_script(dom)
+ else
+ to_js(dom)
end
+ end
- # @return [String] js function to add the listener to the chart
- def add_listeners_js(type)
- js = ''
- @listeners.each do |listener|
- js << "\n google.visualization.events.addListener("
- js << "#{type}, '#{listener[:event]}', function (e) {"
- js << "\n #{listener[:callback]}"
- js << "\n });"
- end
- js
+ # ChartEditor and ChartWrapper works for both Google Charts and DataTables
+ # @see dummy_rails examples for ChartEditor in shekharrajak/demo_daru-view
+ #
+ # @param dom [String] The ID of the DIV element that the Google
+ # Chart should be rendered in
+ # @return [String] JS to draw ChartWrapper or ChartEditor
+ def extract_chart_wrapper_editor_js(dom)
+ case user_options[:chart_class].to_s.capitalize
+ when 'Chartwrapper'
+ get_html_chart_wrapper(data, dom)
+ when 'Charteditor'
+ get_html_chart_editor(data, dom)
end
+ end
- # @param (see #draw_js_chart_editor)
- # @return [String] options of the ChartWrapper
- def extract_chart_wrapper_options(data, element_id)
- js = ''
- js << if is_a?(GoogleVisualr::DataTable)
- "\n \t\tchartType: 'Table',"
+ # @param dom [String] The ID of the DIV element that the Google
+ # Chart should be rendered in
+ # @return [String] js code to render the chart
+ def get_html(dom)
+ html = ''
+ html << load_js(dom)
+ html << if is_a?(GoogleVisualr::DataTable)
+ draw_js(dom)
else
- "\n \t\tchartType: '#{chart_name}',"
+ draw_chart_js(dom)
end
- js << append_data(data)
- js << "\n \t\toptions: #{js_parameters(@options)},"
- js << "\n \t\tcontainerId: '#{element_id}',"
- js << "\n \t\tview: #{extract_option_view}"
- js
- end
+ html
+ end
- # @param element_id [String] The ID of the DIV element that the Google
- # Chart/DataTable should be rendered in
- # @return [String] unique function name to handle query response
- def query_response_function_name(element_id)
- "handleQueryResponse_#{element_id.tr('-', '_')}"
- end
+ # @param data [Array, Daru::DataFrame, Daru::Vector, Daru::View::Table, String]
+ # Data of GoogleVisualr Chart or GoogleVisualr DataTable
+ # @param dom [String] The ID of the DIV element that the Google
+ # Chart should be rendered in
+ # @return [String] js code to render the chart
+ def get_html_chart_wrapper(data, dom)
+ html = ''
+ html << load_js(dom)
+ html << draw_js_chart_wrapper(data, dom)
+ html
+ end
- # @param data [Array, Daru::DataFrame, Daru::Vector, Daru::View::Table, String]
- # Data of GoogleVisualr DataTable/Chart
- # @return [String] Data option (dataSourceUrl or dataTable) required to
- # draw the Chartwrapper based upon the data provided.
- def append_data(data)
- return "\n \t\tdataSourceUrl: '#{data}'," if data.is_a? String
- "\n \t\tdataTable: data_table,"
- end
+ # @param data [Array, Daru::DataFrame, Daru::Vector, Daru::View::Table, String]
+ # Data of GoogleVisualr Chart or GoogleVisualr DataTable
+ # @param dom [String] The ID of the DIV element that the Google
+ # Chart should be rendered in
+ # @return [String] js code to render the charteditor
+ def get_html_chart_editor(data, dom)
+ html = ''
+ html << if is_a?(GoogleVisualr::DataTable)
+ load_js(dom)
+ else
+ load_js_chart_editor(dom)
+ end
+ html << draw_js_chart_editor(data, dom)
+ html
+ end
- # So that it can be used in ChartEditor also
- #
- # @return [String] Returns string to draw the Chartwrapper and '' otherwise
- def draw_wrapper
- return "\n \twrapper.draw();" if
- user_options[:chart_class].to_s.capitalize == 'Chartwrapper'
- ''
- end
+ # @param data [String] URL of the google spreadsheet in the specified
+ # format: https://developers.google.com/chart/interactive/docs
+ # /spreadsheets
+ # Query string can be appended to retrieve the data accordingly
+ # @param dom [String] The ID of the DIV element that the Google
+ # Chart should be rendered in when data is the the URL of the google
+ # spreadsheet
+ # @return [String] js code to render the chart when data is the URL of
+ # the google spreadsheet
+ def get_html_spreadsheet(data, dom)
+ html = ''
+ html << load_js(dom)
+ html << draw_js_spreadsheet(data, dom)
+ html
+ end
- # Generates JavaScript and renders the Google Chartwrapper in the
- # final HTML output.
- #
- # @param data [Array, Daru::DataFrame, Daru::Vector, Daru::View::Table, String]
- # Data of GoogleVisualr Chart/DataTable
- # @param element_id [String] The ID of the DIV element that the Google
- # Chartwrapper should be rendered in
- # @return [String] Javascript code to render the Google Chartwrapper
- def to_js_chart_wrapper(data, element_id=SecureRandom.uuid)
- js = ''
- js << "\n"
- js
- end
+ def to_html(id=nil, options={})
+ path = File.expand_path(
+ '../../templates/googlecharts/chart_div.erb', __dir__
+ )
+ template = File.read(path)
+ id ||= SecureRandom.uuid
+ @html_id = id
+ add_listener_to_chart
+ chart_script = show_script(id, script_tag: false)
+ ERB.new(template).result(binding)
+ end
- # Generates JavaScript when data is imported from spreadsheet and renders
- # the Google Chart/Table in the final HTML output when data is URL of the
- # google spreadsheet
- #
- # @param data [String] URL of the google spreadsheet in the specified
- # format: https://developers.google.com/chart/interactive/docs
- # /spreadsheets
- # Query string can be appended to retrieve the data accordingly
- # @param element_id [String] The ID of the DIV element that the Google
- # Chart/Table should be rendered in
- # @return [String] Javascript code to render the Google Chart/Table when
- # data is given as the URL of the google spreadsheet
- def to_js_spreadsheet(data, element_id=SecureRandom.uuid)
- js = ''
- js << "\n"
- js
- end
+ def show_in_iruby(dom=SecureRandom.uuid)
+ IRuby.html to_html(dom)
+ end
- # Generates JavaScript function for rendering the chartwrapper
- #
- # @param (see #to_js_chart_wrapper)
- # @return [String] JS function to render the chartwrapper
- def draw_js_chart_wrapper(data, element_id)
- js = ''
- js << "\n function #{chart_function_name(element_id)}() {"
- js << if is_a?(GoogleVisualr::DataTable)
- "\n \t#{to_js}"
- else
- "\n \t#{@data_table.to_js}"
- end
- js << "\n \tvar wrapper = new google.visualization.ChartWrapper({"
- js << extract_chart_wrapper_options(data, element_id)
- js << "\n \t});"
- js << draw_wrapper
- js << add_listeners_js('wrapper')
- js << "\n };"
- js
+ # @return [void] Adds listener to the chart from the
+ # user_options[:listeners]
+ def add_listener_to_chart
+ return unless user_options && user_options[:listeners]
+ user_options[:listeners].each do |event, callback|
+ add_listener(event.to_s.downcase, callback)
end
+ end
- private
-
- # @return [void] applies formatters provided by the user in the
- # third parameter
- def apply_formatters
- return unless user_options && user_options[:formatters]
- @formatters = []
- user_options[:formatters].each_value do |formatter|
- frmttr = case formatter[:type].to_s.capitalize
- when 'Color'
- apply_color_formatter(formatter)
- when 'Pattern'
- apply_pattern_formatter(formatter)
- else
- apply_date_arrow_bar_number_formatter(formatter)
- end
- @formatters << frmttr
- end
- end
+ private
- # @param formatter [Hash] formatter hash provided by the user
- # @return [GoogleVisualr::ColorFormat] adds the ColorFormat to
- # defined columns
- # @example Color Formatter
- # df = Daru::DataFrame.new({b: [11,12,13,14,15], a: [1,2,3,4,5],
- # c: [11,22,33,44,55]},
- # order: [:a, :b, :c],
- # index: [:one, :two, :three, :four, :five])
- #
- # user_options = {
- # formatters: {
- # formatter: {
- # type: 'Color',
- # range: [[1, 3, 'yellow', 'red'],
- # [20, 50, 'green', 'pink']],
- # columns: [0,2]
- # },
- # formatter2: {
- # type: 'Color',
- # range: [[14, 15, 'blue', 'orange']],
- # columns: 1
- # }
- # }
- # }
- #
- # # option `allowHtml: true` is necessary to make this work
- # table = Daru::View::Table.new(df, {allowHtml: true}, user_options)
- def apply_color_formatter(formatter)
- initialize_default_values(formatter)
- frmttr = GoogleVisualr::ColorFormat.new
- formatter[:range].each do |range|
- # add_range parameters: (from, to, color, bgcolor)
- frmttr.add_range(range[0], range[1], range[2], range[3])
- end
- formatter[:gradient_range].each do |gr|
- # add_range parameters: (from, to, color, fromBgColor, toBgColor)
- frmttr.add_gradient_range(gr[0], gr[1], gr[2], gr[3], gr[4])
- end
- frmttr.columns(formatter[:columns])
- frmttr
+ # @return [void] applies formatters provided by the user in the
+ # third parameter
+ def apply_formatters
+ return unless user_options && user_options[:formatters]
+ @formatters = []
+ user_options[:formatters].each_value do |formatter|
+ frmttr = case formatter[:type].to_s.capitalize
+ when 'Color'
+ apply_color_formatter(formatter)
+ when 'Pattern'
+ apply_pattern_formatter(formatter)
+ else
+ apply_date_arrow_bar_number_formatter(formatter)
+ end
+ @formatters << frmttr
end
+ end
- # @param formatter [Hash] formatter hash provided by the user
- # @return [void] initializes default values for the color format
- def initialize_default_values(formatter)
- formatter[:range] ||= []
- formatter[:gradient_range] ||= []
- formatter[:columns] ||= 0
+ # @param formatter [Hash] formatter hash provided by the user
+ # @return [GoogleVisualr::ColorFormat] adds the ColorFormat to
+ # defined columns
+ # @example Color Formatter
+ # df = Daru::DataFrame.new({b: [11,12,13,14,15], a: [1,2,3,4,5],
+ # c: [11,22,33,44,55]},
+ # order: [:a, :b, :c],
+ # index: [:one, :two, :three, :four, :five])
+ #
+ # user_options = {
+ # formatters: {
+ # formatter: {
+ # type: 'Color',
+ # range: [[1, 3, 'yellow', 'red'],
+ # [20, 50, 'green', 'pink']],
+ # columns: [0,2]
+ # },
+ # formatter2: {
+ # type: 'Color',
+ # range: [[14, 15, 'blue', 'orange']],
+ # columns: 1
+ # }
+ # }
+ # }
+ #
+ # # option `allowHtml: true` is necessary to make this work
+ # table = Daru::View::Table.new(df, {allowHtml: true}, user_options)
+ def apply_color_formatter(formatter)
+ initialize_default_values(formatter)
+ frmttr = GoogleVisualr::ColorFormat.new
+ formatter[:range].each do |range|
+ # add_range parameters: (from, to, color, bgcolor)
+ frmttr.add_range(range[0], range[1], range[2], range[3])
end
-
- # @param formatter [Hash] formatter hash provided by the user
- # @return [GoogleVisualr::PatternFormat] adds the PatternFormat to
- # destined columns
- def apply_pattern_formatter(formatter)
- formatter[:format_string] ||= ''
- formatter[:src_cols] ||= 0
- formatter[:des_col] ||= 0
- frmttr = GoogleVisualr::PatternFormat.new(formatter[:format_string].to_s)
- frmttr.src_cols = formatter[:src_cols]
- frmttr.des_col = formatter[:des_col]
- frmttr
+ formatter[:gradient_range].each do |gr|
+ # add_range parameters: (from, to, color, fromBgColor, toBgColor)
+ frmttr.add_gradient_range(gr[0], gr[1], gr[2], gr[3], gr[4])
end
+ frmttr.columns(formatter[:columns])
+ frmttr
+ end
- # @param formatter [Hash] formatter hash provided by the user
- # @return [GoogleVisualr::DateFormat, GoogleVisualr::NumberFormat,
- # GoogleVisualr::BarFormat, GoogleVisualr::ArrowFormat] adds the required
- # format to the destined columns
- # @example
- # df = Daru::DataFrame.new({b: [11,-12,13,14,-15], a: [-1,2,3,4,5],
- # c: [11,22,-33,-44,55]},
- # order: [:a, :b, :c],
- # index: [:one, :two, :three, :four, :five])
- #
- # user_options = {
- # formatters: {
- # formatter: {
- # type: 'Number',
- # options: {negativeParens: true},
- # columns: [0,1,2]
- # }
- # }
- # }
- #
- # table = Daru::View::Table.new(df, {}, user_options)
- def apply_date_arrow_bar_number_formatter(formatter)
- formatter[:options] ||= {}
- formatter[:columns] ||= 0
- frmttr = GoogleVisualr.const_get(
- formatter[:type].to_s.capitalize + 'Format'
- ).new(formatter[:options])
- frmttr.columns(formatter[:columns])
- frmttr
- end
+ # @param formatter [Hash] formatter hash provided by the user
+ # @return [void] initializes default values for the color format
+ def initialize_default_values(formatter)
+ formatter[:range] ||= []
+ formatter[:gradient_range] ||= []
+ formatter[:columns] ||= 0
end
- class DataTable
- include Display
+ # @param formatter [Hash] formatter hash provided by the user
+ # @return [GoogleVisualr::PatternFormat] adds the PatternFormat to
+ # destined columns
+ def apply_pattern_formatter(formatter)
+ formatter[:format_string] ||= ''
+ formatter[:src_cols] ||= 0
+ formatter[:des_col] ||= 0
+ frmttr = GoogleVisualr::PatternFormat.new(formatter[:format_string].to_s)
+ frmttr.src_cols = formatter[:src_cols]
+ frmttr.des_col = formatter[:des_col]
+ frmttr
end
- class BaseChart
- include Display
+ # @param formatter [Hash] formatter hash provided by the user
+ # @return [GoogleVisualr::DateFormat, GoogleVisualr::NumberFormat,
+ # GoogleVisualr::BarFormat, GoogleVisualr::ArrowFormat] adds the required
+ # format to the destined columns
+ # @example
+ # df = Daru::DataFrame.new({b: [11,-12,13,14,-15], a: [-1,2,3,4,5],
+ # c: [11,22,-33,-44,55]},
+ # order: [:a, :b, :c],
+ # index: [:one, :two, :three, :four, :five])
+ #
+ # user_options = {
+ # formatters: {
+ # formatter: {
+ # type: 'Number',
+ # options: {negativeParens: true},
+ # columns: [0,1,2]
+ # }
+ # }
+ # }
+ #
+ # table = Daru::View::Table.new(df, {}, user_options)
+ def apply_date_arrow_bar_number_formatter(formatter)
+ formatter[:options] ||= {}
+ formatter[:columns] ||= 0
+ frmttr = GoogleVisualr.const_get(
+ formatter[:type].to_s.capitalize + 'Format'
+ ).new(formatter[:options])
+ frmttr.columns(formatter[:columns])
+ frmttr
end
end
diff --git a/lib/daru/view/adapters/googlecharts/generate_javascript.rb b/lib/daru/view/adapters/googlecharts/generate_javascript.rb
new file mode 100644
index 0000000..07dbcd9
--- /dev/null
+++ b/lib/daru/view/adapters/googlecharts/generate_javascript.rb
@@ -0,0 +1,148 @@
+module GenerateJavascript
+ # @return [String] js function to add the listener to the chart
+ def add_listeners_js(type)
+ js = ''
+ @listeners.each do |listener|
+ js << "\n google.visualization.events.addListener("
+ js << "#{type}, '#{listener[:event]}', function (e) {"
+ js << "\n #{listener[:callback]}"
+ js << "\n });"
+ end
+ js
+ end
+
+ # @param element_id [String] The ID of the DIV element that the Google
+ # Chart/DataTable should be rendered in
+ # @return [String] unique function name to handle query response
+ def query_response_function_name(element_id)
+ "handleQueryResponse_#{element_id.tr('-', '_')}"
+ end
+
+ # @param data [Array, Daru::DataFrame, Daru::Vector, Daru::View::Table, String]
+ # Data of GoogleVisualr DataTable/Chart
+ # @return [String] Data option (dataSourceUrl or dataTable) required to
+ # draw the Chartwrapper based upon the data provided.
+ def append_data(data)
+ return "\n \t\tdataSourceUrl: '#{data}'," if data.is_a? String
+ "\n \t\tdataTable: data_table,"
+ end
+
+ # @param element_id [String] The ID of the DIV element that the Google
+ # Chart should be rendered in
+ # @return [String] unique function name to save the chart
+ def save_chart_function_name(element_id)
+ "saveChart_#{element_id.tr('-', '_')}"
+ end
+
+ # @param (see #draw_js_chart_editor)
+ # @return [String] options of the ChartWrapper
+ def extract_chart_wrapper_options(data, element_id)
+ js = ''
+ js << if is_a?(GoogleVisualr::DataTable)
+ "\n \t\tchartType: 'Table',"
+ else
+ "\n \t\tchartType: '#{chart_name}',"
+ end
+ js << append_data(data)
+ js << "\n \t\toptions: #{js_parameters(@options)},"
+ js << "\n \t\tcontainerId: '#{element_id}',"
+ js << "\n \t\tview: #{extract_option_view}"
+ js
+ end
+
+ # So that it can be used in ChartEditor also
+ #
+ # @return [String] Returns string to draw the Chartwrapper and '' otherwise
+ def draw_wrapper(element_id)
+ js = ''
+ js << "\n \twrapper_#{element_id.tr('-', '_')}.draw();"
+ unless user_options[:chart_class].to_s.capitalize == 'Chartwrapper'
+ js << "\n \tchartEditor_#{element_id.tr('-', '_')} = "\
+ 'new google.visualization.ChartEditor();'
+ js << "\n \tgoogle.visualization.events.addListener("\
+ "chartEditor_#{element_id.tr('-', '_')},"\
+ " 'ok', #{save_chart_function_name(element_id)});"
+ end
+ js
+ end
+
+ # Generates JavaScript and renders the Google Chartwrapper in the
+ # final HTML output.
+ #
+ # @param data [Array, Daru::DataFrame, Daru::Vector, Daru::View::Table, String]
+ # Data of GoogleVisualr Chart/DataTable
+ # @param element_id [String] The ID of the DIV element that the Google
+ # Chartwrapper should be rendered in
+ # @return [String] Javascript code to render the Google Chartwrapper
+ def to_js_chart_wrapper(data, element_id=SecureRandom.uuid)
+ js = ''
+ js << "\n"
+ js
+ end
+
+ # Generates JavaScript when data is imported from spreadsheet and renders
+ # the Google Chart/Table in the final HTML output when data is URL of the
+ # google spreadsheet
+ #
+ # @param data [String] URL of the google spreadsheet in the specified
+ # format: https://developers.google.com/chart/interactive/docs
+ # /spreadsheets
+ # Query string can be appended to retrieve the data accordingly
+ # @param element_id [String] The ID of the DIV element that the Google
+ # Chart/Table should be rendered in
+ # @return [String] Javascript code to render the Google Chart/Table when
+ # data is given as the URL of the google spreadsheet
+ def to_js_spreadsheet(data, element_id=SecureRandom.uuid)
+ js = ''
+ js << "\n"
+ js
+ end
+
+ # Generates JavaScript function for rendering the chartwrapper
+ #
+ # @param (see #to_js_chart_wrapper)
+ # @return [String] JS function to render the chartwrapper
+ def draw_js_chart_wrapper(data, element_id)
+ js = "\n var wrapper_#{element_id.tr('-', '_')} = null;"
+ js << "\n function #{chart_function_name(element_id)}() {"
+ js << if is_a?(GoogleVisualr::DataTable)
+ "\n \t#{to_js}"
+ else
+ "\n \t#{@data_table.to_js}"
+ end
+ js << "\n \twrapper_#{element_id.tr('-', '_')} = "\
+ 'new google.visualization.ChartWrapper({'
+ js << extract_chart_wrapper_options(data, element_id)
+ js << "\n \t});"
+ js << draw_wrapper(element_id)
+ js << add_listeners_js("wrapper_#{element_id.tr('-', '_')}")
+ js << "\n };"
+ js
+ end
+
+ # @param data [Array, Daru::DataFrame, Daru::Vector, Daru::View::Table, String]
+ # Data of GoogleVisualr Chart
+ # @param element_id [String] The ID of the DIV element that the Google
+ # ChartEditor should be rendered in
+ # @return [String] JS function to render the ChartEditor
+ def draw_js_chart_editor(data, element_id)
+ js = ''
+ js << "\n var chartEditor_#{element_id.tr('-', '_')} = null;"
+ js << draw_js_chart_wrapper(data, element_id)
+ js << "\n function #{save_chart_function_name(element_id)}(){"
+ js << "\n \tchartEditor_#{element_id.tr('-', '_')}.getChartWrapper()."\
+ "draw(document.getElementById('#{element_id}'));"
+ js << "\n }"
+ js << "\n function loadEditor_#{element_id.tr('-', '_')}(){"
+ js << "\n \tchartEditor_#{element_id.tr('-', '_')}.openDialog("\
+ "wrapper_#{element_id.tr('-', '_')}, {});"
+ js << "\n }"
+ js
+ end
+end
diff --git a/lib/daru/view/adapters/googlecharts/google_visualr.rb b/lib/daru/view/adapters/googlecharts/google_visualr.rb
new file mode 100644
index 0000000..6d5b0f4
--- /dev/null
+++ b/lib/daru/view/adapters/googlecharts/google_visualr.rb
@@ -0,0 +1,27 @@
+require 'securerandom'
+require 'erb'
+require_relative 'display'
+require_relative 'generate_javascript'
+require 'daru/view/constants'
+
+module GoogleVisualr
+ def self.init_script(
+ dependent_js=GOOGLECHARTS_DEPENDENCIES_WEB
+ )
+ js = ''
+ js << "\n"
+ js
+ end
+
+ class DataTable
+ include Display
+ include GenerateJavascript
+ end
+
+ class BaseChart
+ include Display
+ include GenerateJavascript
+ end
+end
diff --git a/lib/daru/view/templates/googlecharts/chart_div.erb b/lib/daru/view/templates/googlecharts/chart_div.erb
index fa856dd..9c7769e 100644
--- a/lib/daru/view/templates/googlecharts/chart_div.erb
+++ b/lib/daru/view/templates/googlecharts/chart_div.erb
@@ -1,3 +1,6 @@
+<% if user_options && user_options[:chart_class].to_s.capitalize == 'Charteditor' %>
+
+<% end %>