Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move chart_class, modules and css to third parameter in HighCharts #120

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/daru/view/adapters/highcharts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module HighchartsAdapter
#
# @param [Array/Daru::DataFrame/Daru::Vector] data
#
def init(data=[], options={}, _user_options={})
def 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 All @@ -53,6 +53,7 @@ def init(data=[], options={}, _user_options={})
f.series(type: series_type, name: series_name, data: data_new)
end
end
@chart.user_options = user_options
@chart
end

Expand Down
20 changes: 13 additions & 7 deletions lib/daru/view/adapters/highcharts/display.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class HighChart
# @return [String] The ID of the DIV element that the HighChart should
# be rendered in
attr_accessor :div_id
attr_accessor :user_options
# @example
#
# To display the html code of the chart, use `to_html`. To see the same
Expand Down Expand Up @@ -106,7 +107,11 @@ def to_html_iruby(placeholder=random_canvas_id)
# @return [String] css code of the chart
def high_chart_css(placeholder)
# contains the css provided by the user as a String array
css_data = options[:css].nil? ? '' : options.delete(:css)
css_data = if user_options && user_options[:css]
user_options[:css]
else
''
end
css_script = ''
if css_data != ''
css_script << "\n<style>"
Expand Down Expand Up @@ -151,16 +156,17 @@ def extract_dependencies
get_map_data_dependencies(dep_js)
# Dependencies provided in modules option (of highcharts mainly
# like tilemap) by the user
dep_js |= options.delete(:modules).map! { |js| "#{js}.js" } unless
options[:modules].nil?
dep_js |= user_options.delete(:modules).map! { |js| "#{js}.js" } if
user_options && user_options[:modules]
dep_js
end

# @param dep_js [Array] JS dependencies required for drawing a map(mapdata)
# @return [void] Appends the map data in dep_js
def get_map_data_dependencies(dep_js)
if !options[:chart_class].nil? && options[:chart_class].capitalize == 'Map' &&
options[:chart] && options[:chart][:map]
if user_options && user_options[:chart_class] &&
user_options[:chart_class].capitalize == 'Map' && options[:chart] &&
options[:chart][:map]
dep_js.push(options[:chart][:map].to_s)
dep_js.map! { |js| "mapdata/#{js}.js" }
end
Expand Down Expand Up @@ -258,8 +264,8 @@ def append_chart_type(export_type='png')
# @return [String] the class of the chart
def extract_chart_class
# Provided by user and can take two values ('stock' or 'map').
chart_class = options.delete(:chart_class).to_s.capitalize unless
options[:chart_class].nil?
chart_class = user_options[:chart_class].to_s.capitalize if
user_options && user_options[:chart_class]
chart_class = 'StockChart' if chart_class == 'Stock'
chart_class = 'Chart' if chart_class.nil?
unless %w[Chart StockChart Map].include?(chart_class)
Expand Down
43 changes: 22 additions & 21 deletions spec/adapters/highcharts/display_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@
chart: {
type: 'bar'
},
css: ['.highcharts-color-1 {fill: #90ed7d;stroke: #90ed7d;}',
'.highcharts-background {fill: #efefef;stroke: #a4edba;'\
'stroke-width: 2px;}'],
title: {
text: 'Bar chart'
},
Expand All @@ -76,16 +73,20 @@
reversed: true
}
}
@user_opts = {
css: ['.highcharts-color-1 {fill: #90ed7d;stroke: #90ed7d;}',
'.highcharts-background {fill: #efefef;stroke: #a4edba;'\
'stroke-width: 2px;}']
}
@data_vec1 = Daru::Vector.new([5, 3, 4])
@data_vec2 = Daru::Vector.new([2, 2, 3])
@data_vec3 = Daru::Vector.new([3,4,4])
@data_df = Daru::DataFrame.new({John: @data_vec1, Jane: @data_vec2, Joe: @data_vec3})
@hc = Daru::View::Plot.new(@data_df, @opts)
@hc = Daru::View::Plot.new(@data_df, @opts, @user_opts)
@placeholder = "placeholder"
end
let(:opts_map) do
{
chart_class: 'map',
chart: {
map: 'custom/europe',
borderWidth: 1
Expand All @@ -111,21 +112,15 @@
)
end
let(:map) do
Daru::View::Plot.new(df, opts_map)
Daru::View::Plot.new(df, opts_map, chart_class: 'map')
end

describe "#to_html" do
before(:each) do
@opts = {
chart_class: 'stock',
css: [
'.highcharts-background {fill: #efefef;stroke: #a4edba;'\
'stroke-width: 2px;}'
],
chart: {
type: 'arearange'
},
modules: ['highcharts-more'],
rangeSelector: {
selected: 1
},
Expand All @@ -134,6 +129,14 @@
text: 'AAPL Stock Price'
}
}
@user_opts = {
chart_class: 'stock',
modules: ['highcharts-more'],
css: [
'.highcharts-background {fill: #efefef;stroke: #a4edba;'\
'stroke-width: 2px;}'
]
}
@series_dt = [
{
name: 'AAPL Stock Price',
Expand Down Expand Up @@ -177,19 +180,17 @@
}
}
]
@chart = Daru::View::Plot.new
@chart.chart.options = @opts;
@chart.chart.series_data = @series_dt

@chart = Daru::View::Plot.new(@series_dt, @opts, @user_opts)
end

it "should plot HighMap when chart_class is set to map" do
@hc.options[:chart_class] = "Map";
@hc.user_options[:chart_class] = "Map";
expect(@hc.chart.to_html(
@placeholder)
).to match(/window\.chart_placeholder\s+=\s+new\s+Highcharts.Map/)
end
it "should plot Highstock when chart_class is set to stock" do
@hc.options[:chart_class] = "STock";
@hc.user_options[:chart_class] = "STock";
expect(@hc.chart.to_html(
@placeholder)
).to match(/window\.chart_placeholder\s+=\s+new\s+Highcharts.StockChart/)
Expand Down Expand Up @@ -407,18 +408,18 @@

describe "#extract_chart_class" do
it "should return Map class when chart_class is set to map" do
@hc.options[:chart_class] = "map";
@hc.user_options[:chart_class] = "map";
expect(@hc.chart.extract_chart_class).to eq 'Map'
end
it "should return StockChart class when chart_class is set to stock" do
@hc.options[:chart_class] = "SToCk";
@hc.user_options[:chart_class] = "SToCk";
expect(@hc.chart.extract_chart_class).to eq 'StockChart'
end
it "should return Chart class when chart_class is not set" do
expect(@hc.chart.extract_chart_class).to eq 'Chart'
end
it "should raise error when wrong input is given" do
@hc.options[:chart_class] = "Daru"
@hc.user_options[:chart_class] = "Daru"
expect{@hc.chart.extract_chart_class}.to raise_error(
'chart_class must be selected as either chart, stock or map'
)
Expand Down
Loading