diff --git a/.travis.yml b/.travis.yml index 14de476..2f6c23f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,6 +16,9 @@ after_script: - bundle exec rake coveralls:push install: - - gem install bundler + - if [ $TRAVIS_RUBY_VERSION == '2.2' ] || [ $TRAVIS_RUBY_VERSION == '2.1' ] || [ $TRAVIS_RUBY_VERSION == '2.0' ]; + then gem install bundler -v '1.17.3'; + else gem install bundler; + fi - gem install rainbow -v '2.2.1' - bundle install diff --git a/README.md b/README.md index a133240..b1d65ce 100644 --- a/README.md +++ b/README.md @@ -115,56 +115,22 @@ HighCharts example : # set the library, to plot charts Daru::View.plotting_library = :highcharts -# options for the charts -opts = { - chart: {defaultSeriesType: 'line'}, - title: { - text: 'Solar Employment Growth by Sector, 2010-2016' - }, - - subtitle: { - text: 'Source: thesolarfoundation.com' - }, - - yAxis: { - title: { - text: 'Number of Employees' - } - }, - legend: { - layout: 'vertical', - align: 'right', - verticalAlign: 'middle' - }, - - plotOptions: { - # this is not working. Find the bug - # series: { - # pointStart: 43934 - # } - }, - } - -# data for the charts -series_dt = ([{ - name: 'Tokyo', - data: [7.0, 6.9, 9.5, 14.5, 18.4, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6] - }, { - name: 'London', - data: [3.9, 4.2, 5.7, 8.5, 11.9, 15.2, 17.0, 16.6, 14.2, 10.3, 6.6, 4.8] - }]) +# Simple line chart +@line_graph = Daru::View::Plot.new( + data=[43934, 52503, 57177, 69658, 97031, 119931, 137133, 154175] +) -# initialize -@line_graph = Daru::View::Plot.new(series_dt, opts) +# to see graph in IRuby noteboook +@line_graph.show_in_iruby +# to see graph in any ruby web application framework # Add this line in your view file, where you want to see you graph in web application. (It will put the html code of the line graph in web page) - <%=raw @line_graph.div %> -# Now refresh the page, you will be able to see your graph. - ``` +![Line Graph](https://github.com/shekharrajak/medium-daru-view-blog/blob/master/GIF_Images/HighChartBlog/lineChart.gif) + Nyaplot example : diff --git a/lib/daru/view/adapters/datatables.rb b/lib/daru/view/adapters/datatables.rb index cec4a2c..e8dc265 100644 --- a/lib/daru/view/adapters/datatables.rb +++ b/lib/daru/view/adapters/datatables.rb @@ -6,8 +6,6 @@ module Daru module View module Adapter module DatatablesAdapter - extend self # rubocop:disable Style/ModuleFunction - # Read : https://datatables.net/ to understand # the datatables option concept. # Along with these options, a user can provide an additional option @@ -57,13 +55,13 @@ module DatatablesAdapter # table = Daru::View::Table.new(df_sale_exp, options1) # table2 = Daru::View::Table.new(df_sale_exp, options2) # table3 = Daru::View::Table.new(df_sale_exp) - def init_table(data=[], options={}, _user_options={}) + def self.init_table(data=[], options={}, _user_options={}) @table = Daru::View::DataTable.new(data, options) @table end # @return [String] returns code of the dependent JS and CSS file(s) - def init_script + def self.init_script Daru::View::DataTables.init_script end @@ -71,7 +69,7 @@ def init_script # daru-data_table methods # @return [String] script and table (containg thead only) tags of the # datatable generated - def generate_body(table) + def self.generate_body(table) table.to_html end @@ -81,7 +79,7 @@ def generate_body(table) # @example # table = Daru::View::Table.new(data, options) # table.export_html_file - def export_html_file(table, path='./table.html') + def self.export_html_file(table, path='./table.html') path = File.expand_path(path, Dir.pwd) str = generate_html(table) File.write(path, str) @@ -93,14 +91,14 @@ def export_html_file(table, path='./table.html') # @example # table = Daru::View::Table.new(data, options) # table.show_in_iruby - def show_in_iruby(table) + def self.show_in_iruby(table) table.show_in_iruby end # @param table [Daru::DataTables::DataTable] table object to access # daru-data_table methods # @return [String] returns html code of the datatable generated - def generate_html(table) + def self.generate_html(table) path = File.expand_path( '../templates/datatables/static_html.erb', __dir__ ) @@ -115,7 +113,7 @@ def generate_html(table) # @example # table = Daru::View::Table.new(data, options) # table.init_iruby - def init_iruby + def self.init_iruby Daru::View::DataTables.init_iruby end end diff --git a/lib/daru/view/adapters/googlecharts.rb b/lib/daru/view/adapters/googlecharts.rb index 30e4574..a2bb0fa 100644 --- a/lib/daru/view/adapters/googlecharts.rb +++ b/lib/daru/view/adapters/googlecharts.rb @@ -9,8 +9,6 @@ module Daru module View module Adapter module GooglechartsAdapter - extend self # rubocop:disable Style/ModuleFunction - # Read : https://developers.google.com/chart/ to understand # the google charts option concept. # and google_visualr : http://googlevisualr.herokuapp.com/ @@ -72,7 +70,7 @@ module GooglechartsAdapter # Draw the Daru::View::PlotList object with the data as an array of # Daru::View::Plots(s) or Daru::View::Table(s) or both # combined = Daru::View::PlotList([line_chart, bar_chart]) - def init(data=[], options={}, user_options={}) + def self.init(data=[], options={}, user_options={}) @table = GoogleVisualr::DataTable.new @table = get_table(data) unless data.is_a?(String) validate_url(data) if data.is_a?(String) @@ -135,7 +133,7 @@ def init(data=[], options={}, user_options={}) # ['2016', 1030, 540] # ] # table = Daru::View::Table.new(data, {}, chart_class: 'Charteditor') - def init_table(data=[], options={}, user_options={}) + def self.init_table(data=[], options={}, user_options={}) # if `options` is something like this : # { # cols: [{id: 'task', label: 'Employee Name', type: 'string'}, @@ -165,7 +163,7 @@ def init_table(data=[], options={}, user_options={}) # has to invoked # @return [Boolean, void] returns true for valid URL and raises error # for invalid URL - def validate_url(data) + def self.validate_url(data) # `PATTERN_URL.match? data` is faster but does not support older ruby # versions # For testing purpose, it is returning true @@ -174,21 +172,21 @@ def validate_url(data) raise 'Invalid URL' end - def init_script + def self.init_script GoogleVisualr.init_script end - def generate_body(plot) + def self.generate_body(plot) plot.to_html end - def export_html_file(plot, path='./plot.html') + def self.export_html_file(plot, path='./plot.html') path = File.expand_path(path, Dir.pwd) str = generate_html(plot) File.write(path, str) end - def show_in_iruby(plot) + def self.show_in_iruby(plot) plot.show_in_iruby end @@ -198,7 +196,7 @@ def show_in_iruby(plot) # IRuby notebook. # # @see #Daru::View::Plot.export - def export(plot, export_type='png', file_name='chart') + def self.export(plot, export_type='png', file_name='chart') raise NotImplementedError, 'Not yet implemented!' unless export_type == 'png' @@ -207,7 +205,7 @@ def export(plot, export_type='png', file_name='chart') plot.export(export_type, file_name) end - def generate_html(plot) + def self.generate_html(plot) path = File.expand_path( '../templates/googlecharts/static_html.erb', __dir__ ) @@ -218,13 +216,13 @@ def generate_html(plot) ERB.new(template).result(binding) end - def init_iruby + def self.init_iruby GoogleVisualr.init_iruby end # Generally, in opts Hash, :name, :type, :data , :center=> [X, Y], # :size=> Integer, :showInLegend=> Bool, etc may present. - def add_series(plot, opts={}) + def self.add_series(plot, opts={}) plot.series(opts) plot end @@ -237,7 +235,7 @@ def add_series(plot, opts={}) # from which data has to invoked # @return [GoogleVisualr::DataTable] the table object with the data # filled - def get_table(data) + def self.get_table(data) if data.is_a?(Daru::View::Table) && data.table.is_a?(GoogleVisualr::DataTable) data.table @@ -248,7 +246,7 @@ def get_table(data) end end - def extract_chart_type(options) + def self.extract_chart_type(options) # TODO: Imprvoe this method. chart_type = options[:type].nil? ? 'Line' : options.delete(:type) chart_type = chart_type.to_s.capitalize @@ -265,7 +263,7 @@ def extract_chart_type(options) # # TODO : Currently I didn't find use case for multi index. # rubocop:disable Metrics/MethodLength - def add_data_in_table(data_set) + def self.add_data_in_table(data_set) case when data_set.is_a?(Daru::DataFrame) return ArgumentError unless data_set.index.is_a?(Daru::Index) @@ -288,7 +286,7 @@ def add_data_in_table(data_set) end # rubocop:enable Metrics/MethodLength - def add_dataframe_data(data_set) + def self.add_dataframe_data(data_set) rows = data_set.access_row_tuples_by_indexs(*data_set.index.to_a) data_set.vectors.to_a.each do |vec| @table.new_column(converted_type_to_js(vec, data_set), vec) @@ -296,7 +294,7 @@ def add_dataframe_data(data_set) rows end - def add_array_data(data_set) + def self.add_array_data(data_set) data_set[0].each_with_index do |col, indx| # TODO: below while loop must be improved. Similar thing for # above 2 cases. @@ -312,7 +310,7 @@ def add_array_data(data_set) data_set end - def add_vector_data(data_set) + def self.add_vector_data(data_set) vec_name = data_set.name.nil? ? 'Series' : data_set.name @table.new_column(return_js_type(data_set[0]), vec_name) rows = [] @@ -320,7 +318,7 @@ def add_vector_data(data_set) rows end - def converted_type_to_js(vec_name, data_set) + def self.converted_type_to_js(vec_name, data_set) # Assuming all the data type is same for all the column values. case when data_set.is_a?(Daru::DataFrame) @@ -330,14 +328,14 @@ def converted_type_to_js(vec_name, data_set) end end - def return_js_type(data) + def self.return_js_type(data) return if data.nil? data = data.is_a?(Hash) ? data[:v] : data extract_js_type(data) end - def extract_js_type(data) + def self.extract_js_type(data) case when data.is_a?(String) 'string' diff --git a/lib/daru/view/adapters/highcharts.rb b/lib/daru/view/adapters/highcharts.rb index 22261aa..7fc1a18 100644 --- a/lib/daru/view/adapters/highcharts.rb +++ b/lib/daru/view/adapters/highcharts.rb @@ -8,8 +8,6 @@ module Daru module View module Adapter module HighchartsAdapter - extend self # rubocop:disable Style/ModuleFunction - # Read : https://www.highcharts.com/docs/chart-concepts to understand # the highcharts option concept. # @@ -28,7 +26,7 @@ module HighchartsAdapter # # @param [Array/Daru::DataFrame/Daru::Vector] data # - def init(data=[], options={}, user_options={}) + def self.init(data=[], options={}, user_options={}) # Alternate way is using `add_series` method. # # There are many options present in Highcharts so it is better to use @@ -57,15 +55,15 @@ def init(data=[], options={}, user_options={}) @chart end - def init_script + def self.init_script LazyHighCharts.init_script end - def generate_body(plot) + def self.generate_body(plot) plot.to_html end - def export_html_file(plot, path='./plot.html') + def self.export_html_file(plot, path='./plot.html') path = File.expand_path(path, Dir.pwd) str = generate_html(plot) File.write(path, str) @@ -80,17 +78,17 @@ def export_html_file(plot, path='./plot.html') # running the cells works fine. # # @see #Daru::View::Plot.export - def export(plot, export_type='png', file_name='chart') + def self.export(plot, export_type='png', file_name='chart') plot.export_iruby(export_type, file_name) if defined? IRuby rescue NameError plot.export(export_type, file_name) end - def show_in_iruby(plot) + def self.show_in_iruby(plot) plot.show_in_iruby end - def generate_html(plot) + def self.generate_html(plot) path = File.expand_path('../templates/highcharts/static_html.erb', __dir__) template = File.read(path) initial_script = init_script @@ -98,20 +96,20 @@ def generate_html(plot) ERB.new(template).result(binding) end - def init_iruby + def self.init_iruby LazyHighCharts.init_iruby end # Generally, in opts Hash, :name, :type, :data , :center=> [X, Y], # :size=> Integer, :showInLegend=> Bool, etc may present. - def add_series(plot, opts={}) + def self.add_series(plot, opts={}) plot.series(opts) plot end private - def guess_data(data_set) + def self.guess_data(data_set) case when data_set.is_a?(Daru::DataFrame) # TODO : Currently I didn't find use case for multi index. diff --git a/lib/daru/view/adapters/nyaplot.rb b/lib/daru/view/adapters/nyaplot.rb index 50422b2..b9e12bd 100644 --- a/lib/daru/view/adapters/nyaplot.rb +++ b/lib/daru/view/adapters/nyaplot.rb @@ -6,43 +6,42 @@ module Daru module View module Adapter module NyaplotAdapter - extend self # rubocop:disable Style/ModuleFunction - def init(data, options, _user_options={}) + def self.init(data, options, _user_options={}) data_new = guess_data(data) data_new.plot(options) end - def export_html_file(plot, path) + def self.export_html_file(plot, path) plot.export_html path end - def export(_plot, _export_type='png', _file_name='chart') + def self.export(_plot, _export_type='png', _file_name='chart') raise 'Not implemented yet' end - def show_in_iruby(plot) + def self.show_in_iruby(plot) plot.show end - def init_script + def self.init_script Nyaplot.init_script end - def generate_body(plot) + def self.generate_body(plot) plot.to_iruby[1] end - def init_iruby + def self.init_iruby Nyaplot.init_iruby end private - def multi_dimension_check(arr) + def self.multi_dimension_check(arr) arr.all? { |a| a.class==Array } end - def guess_data(data_set) + def self.guess_data(data_set) case when [Daru::DataFrame, Daru::Vector].include?(data_set.class) data_set