Skip to content

Commit

Permalink
Merge 6472eaf into 2d3c49d
Browse files Browse the repository at this point in the history
  • Loading branch information
Shekharrajak committed Jan 8, 2019
2 parents 2d3c49d + 6472eaf commit a31a5a7
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 97 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
42 changes: 20 additions & 22 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 All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -288,15 +286,15 @@ 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)
end
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.
Expand All @@ -312,15 +310,15 @@ 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 = []
data_set.to_a.each { |a| rows << [a] }
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)
Expand All @@ -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'
Expand Down

0 comments on commit a31a5a7

Please sign in to comment.