Skip to content

Commit

Permalink
Merge c093309 into 2d3c49d
Browse files Browse the repository at this point in the history
  • Loading branch information
Shekharrajak committed Jan 8, 2019
2 parents 2d3c49d + c093309 commit 11dfe26
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 85 deletions.
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
52 changes: 9 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 :
Expand Down
16 changes: 7 additions & 9 deletions lib/daru/view/adapters/datatables.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -57,21 +55,21 @@ 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

# @param table [Daru::DataTables::DataTable] table object to access
# 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

Expand All @@ -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)
Expand All @@ -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__
)
Expand All @@ -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
Expand Down
24 changes: 11 additions & 13 deletions lib/daru/view/adapters/googlecharts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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'},
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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'

Expand All @@ -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__
)
Expand All @@ -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
Expand Down
20 changes: 9 additions & 11 deletions lib/daru/view/adapters/highcharts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
#
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -80,31 +78,31 @@ 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
chart_div = generate_body(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
Expand Down
15 changes: 7 additions & 8 deletions lib/daru/view/adapters/nyaplot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,32 @@ 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

Expand Down

0 comments on commit 11dfe26

Please sign in to comment.