Skip to content

Commit 20acde5

Browse files
authored
Merge pull request #89 from Prakriti-nith/highstock
Added Highstock feature
2 parents 5b5d7bc + 0bb016e commit 20acde5

File tree

46 files changed

+18017
-23807
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+18017
-23807
lines changed

lib/daru/view/adapters/highcharts/display.rb

+28-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
module LazyHighCharts
55
def self.init_script(
6-
dependent_js=['highstock.js', 'modules/exporting.js',
6+
dependent_js=['highstock.js', 'highcharts-more.js', 'modules/exporting.js',
77
'highcharts-3d.js', 'modules/data.js']
88
)
99
# Highstock is based on Highcharts, meaning it has all the core
@@ -33,13 +33,23 @@ class HighChart
3333
#
3434
def to_html(placeholder=random_canvas_id)
3535
chart_hash_must_be_present
36-
high_chart(placeholder, self)
36+
# Helps to denote either of the three classes.
37+
chart_class = extract_chart_class
38+
# When user wants to plot a HighMap
39+
if chart_class == 'Map'
40+
high_map(placeholder, self)
41+
# When user wants to plot a HighStock
42+
elsif chart_class == 'StockChart'
43+
high_stock(placeholder, self)
44+
# When user wants to plot a HighChart
45+
elsif chart_class == 'Chart'
46+
high_chart(placeholder, self)
47+
end
3748
end
3849

3950
def show_in_iruby(placeholder=random_canvas_id)
4051
# TODO : placeholder pass, in plot#div
41-
chart_hash_must_be_present
42-
IRuby.html high_chart_iruby(placeholder, self)
52+
IRuby.html to_html_iruby(placeholder)
4353
end
4454

4555
# This method is not needed if `to_html` generates the same code. Here
@@ -48,7 +58,20 @@ def show_in_iruby(placeholder=random_canvas_id)
4858
def to_html_iruby(placeholder=random_canvas_id)
4959
# TODO : placeholder pass, in plot#div
5060
chart_hash_must_be_present
51-
high_chart_iruby(placeholder, self)
61+
high_chart_iruby(extract_chart_class, placeholder, self)
62+
end
63+
64+
# @return [String] the class of the chart
65+
def extract_chart_class
66+
# Provided by user and can take two values ('stock' or 'map').
67+
chart_class = options.delete(:chart_class).to_s.capitalize unless
68+
options[:chart_class].nil?
69+
chart_class = 'StockChart' if chart_class == 'Stock'
70+
chart_class = 'Chart' if chart_class.nil?
71+
unless %w[Chart StockChart Map].include?(chart_class)
72+
raise 'chart_class must be selected as either chart, stock or map'
73+
end
74+
chart_class
5275
end
5376

5477
def chart_hash_must_be_present

lib/daru/view/adapters/highcharts/iruby_notebook.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def self.generate_init_code(dependent_js)
99

1010
# Enable to show plots on IRuby notebook
1111
def self.init_iruby(
12-
dependent_js=['highcharts.js', 'modules/exporting.js',
12+
dependent_js=['highstock.js', 'highcharts-more.js', 'modules/exporting.js',
1313
'highcharts-3d.js', 'modules/data.js']
1414
)
1515
# TODO: include highstock.js for highstock and modules/*.js files for

lib/daru/view/adapters/highcharts/layout_helper_iruby.rb

+10-12
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
module LazyHighCharts
22
module LayoutHelper
3-
def high_chart_iruby(placeholder, object, &block)
3+
def high_chart_iruby(chart_class, placeholder, object, &block)
44
object.html_options[:id] = placeholder
55
object.options[:chart][:renderTo] = placeholder
6-
high_graph_iruby(placeholder, object, &block).concat(content_tag('div', '', object.html_options))
7-
end
8-
9-
def high_graph_iruby(placeholder, object, &block)
10-
build_html_output_iruby('Chart', placeholder, object, &block)
6+
build_html_output_iruby(
7+
chart_class, placeholder, object, &block
8+
).concat(content_tag('div', '', object.html_options))
119
end
1210

1311
private
@@ -29,18 +27,18 @@ def encapsulate_js_iruby(core_js)
2927
"#{js_start_iruby} #{core_js} #{js_end_iruby}"
3028
# Turbolinks.version < 5
3129
elsif defined?(Turbolinks) && request_is_referrer?
32-
to_s(eventlistener_page_load)
30+
eventlistener_page_load(core_js)
3331
elsif defined?(Turbolinks) && request_turbolinks_5_tureferrer?
34-
to_s(eventlistener_turbolinks_load)
32+
eventlistener_turbolinks_load(core_js)
3533
else
36-
to_s(call_core_js)
34+
call_core_js(core_js)
3735
end
3836

3937
defined?(raw) ? raw(js_output) : js_output
4038
end
4139
# rubocop:enable Metrics/PerceivedComplexity
4240

43-
def eventlistener_page_load
41+
def eventlistener_page_load(core_js)
4442
<<-EOJS
4543
#{js_start_iruby}
4644
var f = function(){
@@ -52,7 +50,7 @@ def eventlistener_page_load
5250
EOJS
5351
end
5452

55-
def eventlistener_turbolinks_load
53+
def eventlistener_turbolinks_load(core_js)
5654
<<-EOJS
5755
#{js_start_iruby}
5856
document.addEventListener("turbolinks:load", function() {
@@ -62,7 +60,7 @@ def eventlistener_turbolinks_load
6260
EOJS
6361
end
6462

65-
def call_core_js
63+
def call_core_js(core_js)
6664
<<-EOJS
6765
#{js_start_iruby}
6866
#{core_js}

lib/daru/view/templates/highcharts/init.inline.js.erb

-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,3 @@ var event = document.createEvent("HTMLEvents");
1313
event.initEvent("load_highcharts", false, false);
1414
window.dispatchEvent(event);
1515
console.log("Finish loading highchartsjs");
16-
+222
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
require 'spec_helper.rb'
2+
3+
describe LazyHighCharts do
4+
before { Daru::View.plotting_library = :highcharts }
5+
describe "#init_script" do
6+
it "generates valid initial script" do
7+
js = LazyHighCharts.init_script
8+
expect(js).to match(/BEGIN highstock.js/i)
9+
expect(js).to match(/Highstock JS/i)
10+
expect(js).to match(/END highstock.js/i)
11+
expect(js).to match(/BEGIN highcharts-more.js/i)
12+
expect(js).to match(/END highcharts-more.js/i)
13+
expect(js).to match(/BEGIN modules\/exporting.js/i)
14+
expect(js).to match(/END modules\/exporting.js/i)
15+
expect(js).to match(/BEGIN highcharts-3d.js/i)
16+
expect(js).to match(/END highcharts-3d.js/i)
17+
expect(js).to match(/BEGIN modules\/data.js/i)
18+
expect(js).to match(/END modules\/data.js/i)
19+
expect(js).to match(/<script type='text\/javascript'>/i)
20+
expect(js).to match(
21+
/var event = document.createEvent\(\"HTMLEvents\"\)/i)
22+
expect(js).to match(
23+
/event.initEvent\(\"load_highcharts\", false, false\)/i)
24+
expect(js).to match(/window.dispatchEvent\(event\)/i)
25+
expect(js).to match(
26+
/console.log\(\"Finish loading highchartsjs\"\)/i)
27+
end
28+
end
29+
end
30+
31+
describe LazyHighCharts::HighChart do
32+
before { Daru::View.plotting_library = :highcharts }
33+
before(:each) do
34+
@opts = {
35+
chart: {
36+
type: 'bar'
37+
},
38+
title: {
39+
text: 'Bar chart'
40+
},
41+
yAxis: {
42+
min: 0,
43+
title: {
44+
text: 'Total consumption'
45+
}
46+
},
47+
legend: {
48+
reversed: true
49+
}
50+
}
51+
@data_vec1 = Daru::Vector.new([5, 3, 4])
52+
@data_vec2 = Daru::Vector.new([2, 2, 3])
53+
@data_vec3 = Daru::Vector.new([3,4,4])
54+
@data_df = Daru::DataFrame.new({John: @data_vec1, Jane: @data_vec2, Joe: @data_vec3})
55+
@hc = Daru::View::Plot.new(@data_df, @opts)
56+
@placeholder = "placeholder"
57+
end
58+
59+
describe "#to_html" do
60+
before(:each) do
61+
@opts = {
62+
chart_class: 'stock',
63+
chart: {
64+
type: 'arearange'
65+
},
66+
rangeSelector: {
67+
selected: 1
68+
},
69+
70+
title: {
71+
text: 'AAPL Stock Price'
72+
}
73+
}
74+
@series_dt = [
75+
{
76+
name: 'AAPL Stock Price',
77+
data: [
78+
[1147651200000,67.79],
79+
[1147737600000,64.98],
80+
[1147824000000,65.26],
81+
82+
[1149120000000,62.17],
83+
[1149206400000,61.66],
84+
[1149465600000,60.00],
85+
[1149552000000,59.72],
86+
87+
[1157932800000,72.50],
88+
[1158019200000,72.63],
89+
[1158105600000,74.20],
90+
[1158192000000,74.17],
91+
[1158278400000,74.10],
92+
[1158537600000,73.89],
93+
94+
[1170288000000,84.74],
95+
[1170374400000,84.75],
96+
97+
[1174953600000,95.46],
98+
[1175040000000,93.24],
99+
[1175126400000,93.75],
100+
[1175212800000,92.91],
101+
102+
[1180051200000,113.62],
103+
[1180396800000,114.35],
104+
[1180483200000,118.77],
105+
[1180569600000,121.19],
106+
],
107+
marker: {
108+
enabled: true,
109+
radius: 3
110+
},
111+
shadow: true,
112+
tooltip: {
113+
valueDecimals: 2
114+
}
115+
}
116+
]
117+
@chart = Daru::View::Plot.new
118+
@chart.chart.options = @opts;
119+
@chart.chart.series_data = @series_dt
120+
end
121+
it "should plot Highstock when chart_class is set to stock" do
122+
@hc.options[:chart_class] = "STock";
123+
expect(@hc.chart.to_html(
124+
@placeholder)
125+
).to match(/window\.chart_placeholder\s+=\s+new\s+Highcharts.StockChart/)
126+
end
127+
it "should plot HighChart otherwise" do
128+
expect(@hc.chart.to_html(
129+
@placeholder)
130+
).to match(/window\.chart_placeholder\s+=\s+new\s+Highcharts.Chart/)
131+
end
132+
it "should return a div with an id of high_chart object" do
133+
expect(@chart.chart.to_html(@placeholder)).to match(/<div id="placeholder">/)
134+
expect(@hc.chart.to_html(@placeholder)).to match(/<div id="placeholder">/)
135+
end
136+
it "should return a script" do
137+
expect(@chart.chart.to_html(@placeholder)).to match(/script/)
138+
expect(@hc.chart.to_html(@placeholder)).to match(/script/)
139+
end
140+
it "should set variables `chart` `options`" do
141+
expect(@chart.chart.to_html(@placeholder)).to match(/var\s+options\s+=/)
142+
expect(@chart.chart.to_html(@placeholder)).to match(/window.chart_placeholder/)
143+
expect(@hc.chart.to_html(@placeholder)).to match(/var\s+options\s+=/)
144+
expect(@hc.chart.to_html(@placeholder)).to match(/window.chart_placeholder/)
145+
end
146+
it "should take a block setting attributes" do
147+
expect(@chart.chart.options[:rangeSelector][:selected]).to eq(1)
148+
expect(@chart.chart.to_html(@placeholder)).to match(/rangeSelector/)
149+
expect(@chart.chart.to_html(
150+
@placeholder)
151+
).to match(/series\": \[\{ \"name\": \"AAPL Stock Price\"/)
152+
expect(@chart.chart.to_html(
153+
@placeholder)
154+
).to match(/\"data\": \[ \[ 1147651200000,67.79 \]/)
155+
expect(@chart.chart.to_html(
156+
@placeholder)
157+
).to match(/\"title\": \{ \"text\": \"AAPL Stock Price\" \}/)
158+
expect(@chart.chart.to_html(
159+
@placeholder)
160+
).to match(/\"chart\": \{ \"type\": \"arearange\"/)
161+
expect(@chart.chart.to_html(
162+
@placeholder)
163+
).to match(/\"marker\": \{ \"enabled\": true/)
164+
expect(@chart.chart.to_html(@placeholder)).to match(/\"shadow\": true/)
165+
expect(@chart.chart.to_html(
166+
@placeholder)
167+
).to match(/\"tooltip\": \{ \"valueDecimals\": 2/)
168+
expect(@hc.chart.to_html(
169+
@placeholder)
170+
).to match(/\"chart\": \{ \"type\": \"bar\"/)
171+
expect(@hc.chart.to_html(
172+
@placeholder)
173+
).to match(/\"data\": \[ \[ 5,2,3 \],\[ 3,2,4 \],\[ 4,3,4 \] \]/)
174+
expect(@hc.chart.to_html(
175+
@placeholder)
176+
).to match(/\"title\": \{ \"text\": \"Bar chart\" \}/)
177+
expect(@hc.chart.to_html(
178+
@placeholder)
179+
).to match(/\"legend\": \{ \"reversed\": true \}/)
180+
expect(@hc.chart.to_html(
181+
@placeholder)
182+
).to match(/\"yAxis\": \{ \"min\": 0/)
183+
end
184+
end
185+
186+
describe "#to_html_iruby" do
187+
it "should plot Highstock when chart_class is set to stock" do
188+
@hc.options[:chart_class] = "SToCk";
189+
expect(@hc.chart.to_html_iruby(
190+
@placeholder)
191+
).to match(/window\.chart_placeholder\s+=\s+new\s+Highcharts.StockChart/)
192+
end
193+
it "should plot HighChart otherwise" do
194+
expect(@hc.chart.to_html_iruby(
195+
@placeholder)
196+
).to match(/window\.chart_placeholder\s+=\s+new\s+Highcharts.Chart/)
197+
end
198+
end
199+
200+
describe "#extract_chart_class" do
201+
it "should return StockChart class when chart_class is set to stock" do
202+
@hc.options[:chart_class] = "SToCk";
203+
expect(@hc.chart.extract_chart_class).to eq 'StockChart'
204+
end
205+
it "should return Chart class when chart_class is not set" do
206+
expect(@hc.chart.extract_chart_class).to eq 'Chart'
207+
end
208+
it "should raise error when wrong input is given" do
209+
@hc.options[:chart_class] = "Daru"
210+
expect{@hc.chart.extract_chart_class}.to raise_error(
211+
'chart_class must be selected as either chart, stock or map'
212+
)
213+
end
214+
end
215+
216+
describe "#chart_hash_must_be_present" do
217+
it "should check the presence of chart hash in options" do
218+
@hc.chart.chart_hash_must_be_present
219+
expect(@hc.options[:chart]).to eq :type=>"bar"
220+
end
221+
end
222+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require 'spec_helper.rb'
2+
3+
describe LazyHighCharts do
4+
before { Daru::View.plotting_library = :highcharts }
5+
describe "#init_script" do
6+
it "generates valid initial script" do
7+
js = LazyHighCharts.init_script
8+
expect(js).to match(/BEGIN highstock.js/i)
9+
expect(js).to match(/Highstock JS/i)
10+
expect(js).to match(/END highstock.js/i)
11+
expect(js).to match(/BEGIN highcharts-more.js/i)
12+
expect(js).to match(/END highcharts-more.js/i)
13+
expect(js).to match(/BEGIN modules\/exporting.js/i)
14+
expect(js).to match(/END modules\/exporting.js/i)
15+
expect(js).to match(/BEGIN highcharts-3d.js/i)
16+
expect(js).to match(/END highcharts-3d.js/i)
17+
expect(js).to match(/BEGIN modules\/data.js/i)
18+
expect(js).to match(/END modules\/data.js/i)
19+
expect(js).to match(
20+
/var event = document.createEvent\(\"HTMLEvents\"\)/i)
21+
expect(js).to match(
22+
/event.initEvent\(\"load_highcharts\", false, false\)/i)
23+
expect(js).to match(/window.dispatchEvent\(event\)/i)
24+
expect(js).to match(
25+
/console.log\(\"Finish loading highchartsjs\"\)/i)
26+
end
27+
end
28+
end

0 commit comments

Comments
 (0)