Skip to content

Commit

Permalink
Resolve conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
Prakriti-nith committed Aug 4, 2018
1 parent 33f4e4d commit 2fb8e29
Show file tree
Hide file tree
Showing 44 changed files with 5,588 additions and 2,229 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ For now, other applications (Rails/Sinatra) you need to run it locally.
daru-view
```
- To update al the JS files:
- To update all the JS files:
```
daru-view update
```
Expand All @@ -297,7 +297,7 @@ For now, other applications (Rails/Sinatra) you need to run it locally.
### 2. Developers
To update to the current highcharts.js directly from http://code.highcharts.com/", you can always run
To update to the current highcharts.js directly from http://code.highcharts.com/", you can always run
rake highcharts:update
Expand All @@ -309,6 +309,14 @@ For now, other applications (Rails/Sinatra) you need to run it locally.
rake update_all
## Creating a new adapter (Developers)
To create a new adapter `Demo`, run
```
rake new:adapter Demo
```
and a file demo.rb will be created in the daru/view/adapters folder with all the necessary methods (init, init_script, init_ruby, generate_body, show_in_iruby and export_html_file) as TODO.
## Development
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Coveralls::RakeTask.new
import 'lib/tasks/high_charts.rake'
import 'lib/tasks/nyaplot.rake'
import 'lib/tasks/google_charts.rake'
import 'lib/tasks/new_adapter.rake'

# TODO: add Nyaplot
task :update_all => ["googlecharts:update", "highcharts:update"]

99 changes: 65 additions & 34 deletions lib/daru/view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require 'daru/view/adapters/highcharts/display'
require 'daru/view/adapters/nyaplot/display'
require 'daru/view/adapters/googlecharts/display'
require 'daru/view/adapters/googlecharts/google_visualr'
require 'daru/view/table'

# needed in load_lib_in_iruby method
Expand Down Expand Up @@ -69,21 +70,75 @@ def table_library=(lib)
end
end

# Load the dependent JS files in IRuby notebook. Those JS will help in
# plotting the charts in IRuby cell.
# dependent script for the library. It must be added in the head tag
# of the web application.
#
# @param lib [String, Symbol] library whose dependencies are to be loaded
# @return [void, String] dependent script for the library
# @example
# dep_js = Daru::View.dependent_script(:highcharts)
# use in Rails app : <%=raw dep_js %>
# @example
# dep_js = Daru::View.dependent_script('highcharts')
# use in Rails app : <%=raw dep_js %>
#
# To load the dependent JS file for Nyaplot library
# plotting system (Nyaplot.js, d3.js):
#
# Daru::View.load_lib_in_iruby('nyaplot')
#
# To load the HighCharts dependent JS
# files (highcharts.js, highcharts-3d.js, highstock.js):
# @example
# To load the dependent JS file for Nyaplot library
# plotting system (Nyaplot.js, d3.js):
#
# Daru::View.load_lib_in_iruby('highcharts')
# Daru::View.dependent_script(:nyaplot)
# @example
# Daru::View.dependent_script('nyaplot')
def dependent_script(lib=:nyaplot)
load_lib_in_iruby(lib.to_s) if defined? IRuby
rescue NameError
case lib.to_s
when 'nyaplot'
Nyaplot.init_script
when 'highcharts'
LazyHighCharts.init_script
when 'googlecharts'
GoogleVisualr.init_script
when 'datatables'
DataTables.init_script
else
raise ArgumentError, "Unsupported library #{lib}"
end
end

# @param libraries [Array] libraries whose dependencies are to be
# loaded
# @return [void, String] dependent script for the libraries
#
# @example
# To load the dependent JS file for Nyaplot and GoogleCharts libraries
# Daru::View.dependent_scripts(['nyaplot', 'googlecharts'])
# @example
# To load the dependent JS file for Nyaplot and GoogleCharts libraries
# Daru::View.dependent_scripts([:nyaplot, :googlecharts])
def dependent_scripts(libraries=[])
load_libs_in_iruby(libraries) if defined? IRuby
rescue NameError
script = ''
libraries.each do |library|
script << dependent_script(library)
end
script
end

private

# @param libraries [Array] Adapters whose JS files will be loaded
# @return [void] load the dependent JS files for the adapter in IRuby
# notebook
def load_libs_in_iruby(libraries=[])
libraries.each do |library|
load_lib_in_iruby(library.to_s)
end
end

# Load the dependent JS files in IRuby notebook. Those JS will help in
# plotting the charts in IRuby cell.
def load_lib_in_iruby(library)
if library.match('highcharts')
library = 'LazyHighCharts'
Expand All @@ -98,30 +153,6 @@ def load_lib_in_iruby(library)
Object.const_get(library.capitalize).init_iruby
end
end

# dependent script for the library. It must be added in the head tag
# of the web application.
#
# @example
#
# dep_js = Daru::View.dependent_script(:highcharts)
#
# use in Rails app : <%=raw dep_js %>
#
def dependent_script(lib=:nyaplot)
case lib
when :nyaplot
Nyaplot.init_script
when :highcharts
LazyHighCharts.init_script
when :googlecharts
GoogleVisualr.init_script
when :datatables
DataTables.init_script
else
raise ArgumentError, "Unsupported library #{lib}"
end
end
end
end
end
56 changes: 38 additions & 18 deletions lib/daru/view/adapters/googlecharts.rb
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 :
# {
Expand All @@ -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
Expand Down Expand Up @@ -226,6 +229,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)
Expand Down
12 changes: 12 additions & 0 deletions lib/daru/view/adapters/googlecharts/base_chart.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand Down
8 changes: 5 additions & 3 deletions lib/daru/view/adapters/googlecharts/data_table_iruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
Loading

0 comments on commit 2fb8e29

Please sign in to comment.