lazy_high_charts 1.5.2 → 1.6.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 9b7f95d1ee5bcbdf67651b6b00d8e84931ac40c5
4
- data.tar.gz: 3854dc699812d374aea0bb7147732a1c45445de2
2
+ SHA256:
3
+ metadata.gz: 3eee1a5afbb4b3c63d64dfc871914509471aab415d129342eea21b54ccf09fee
4
+ data.tar.gz: 246cdc1ea7fba6367c8d19603ffb7f0b3df67f86a3be9082cd316af3475e74a6
5
5
  SHA512:
6
- metadata.gz: e5735e8b7ff7d0728724d700e975d93d9134aac7081c50d2f9d7d8a2c9306a00361b6c23fbaacdf016594c7d0c7f4c7813f3f9ba6eb84067291627a0f925cd7d
7
- data.tar.gz: bdb7421200c933c14b3af885ca63db543daa77fc8611211f3e0ab4a43712a2488ce192fe95fb451af362662220b7880089b11eeade22e55f18aee0831b06ac53
6
+ metadata.gz: 03d0468dd7d3223b36e28109222e958b68540a80114607a54daa0f8ce0bcdeeead869a23bec4a956a071e34e448d29c680c59bce239431576350c179555bd385
7
+ data.tar.gz: 031b37c48d4ce4085b18543e7c50ac278fb4e19dfbc13362b1934242700a9dab73504e50f1f8ce2831f891a60a4aea5a833b6936498ef76b10325b02e9dcaf52
@@ -5,5 +5,4 @@ bundler_args: --without production development
5
5
  script: bundle exec rake
6
6
 
7
7
  rvm:
8
- - 1.9.3
9
- - 2.0.0
8
+ - 2.2.5
@@ -1,10 +1,24 @@
1
+ # VERSION 1.5.5
2
+ * Mon Oct 19, 2015
3
+ 1. Support for HighChart global settings.
4
+ [#149](https://github.com/michelson/lazy_high_charts/issues/149)
5
+ 2. Minor code refactorings.
6
+
7
+ # VERSION 1.5.4
8
+ * Fri May 21
9
+ 1. Include ActionView::Helpers::TagHelper to the layout helper to avoid method conflict in object.
10
+
11
+ # VERSION 1.5.3
12
+ * Fri Apr 18
13
+ 1. update HighChart js lib to latest.
14
+
1
15
  # VERSION 1.5.2
2
16
  * Sun Mar 11, 2014
3
17
  1. Merge some requirement tweaks to use gem in NON-Rails contexts
4
18
 
5
19
  # VERSION 1.5.1
6
20
  * Sun Dec 22, 2013
7
- 1. Share highcharts:update rake task for gem and rails host app
21
+ 1. Share highcharts:update rake task for gem and rails host app
8
22
 
9
23
  # VERSION 1.5.0
10
24
  * Sun Jul 21, 2013
@@ -1 +1 @@
1
- 1.5.2
1
+ 1.6.1
data/Gemfile CHANGED
@@ -5,7 +5,7 @@ gemspec
5
5
 
6
6
  group :test, :development do # This causes the plugins to NOT load
7
7
  gem 'webrat', '~> 0.7'
8
- gem 'rspec', '~> 2.0'
8
+ gem 'rspec', '~> 3.4'
9
9
  gem 'rails', '>= 3.2'
10
10
  gem 'guard-rspec'
11
11
  gem 'rb-fsevent', '~> 0.9.1'
data/README.md CHANGED
@@ -1,7 +1,9 @@
1
1
  # LazyHighCharts
2
2
 
3
+ [![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/michelson/lazy_high_charts?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
4
+
3
5
  This gem provides a simple and extremely flexible way to use HighCharts from ruby code.
4
- Tested on Ruby on Rails, Sinatra and Nanoc, but it should work with others too.
6
+ Tested on Ruby on Rails, Sinatra and Nanoc, but it should work with others too. Highcharts is not free for commercial use, so make sure you have a valid license to use Highcharts.
5
7
 
6
8
 
7
9
  [![Build Status](https://travis-ci.org/michelson/lazy_high_charts.png?branch=master)](http://travis-ci.org/michelson/lazy_high_charts)
@@ -40,43 +42,104 @@ then run
40
42
  bundle install
41
43
  ```
42
44
 
45
+ and make sure to get the latest version (unless you'd like an older version for some reason)
46
+ ```bash
47
+ rake highcharts:update
48
+ ```
49
+
50
+ and add this in the application.js
51
+
52
+ ```js
53
+ //= require jquery
54
+ //= require jquery_ujs
55
+ //= require_tree .
56
+ //= require turbolinks
57
+
58
+ //= require highcharts/highcharts
59
+ //= require highcharts/highcharts-more
60
+ //= require highcharts/highstock
61
+ ```
62
+
43
63
  to install it.
44
64
 
45
65
  ## Usage:
46
66
 
47
67
  ### Controller code:
48
68
  ```ruby
49
- @chart2 = LazyHighCharts::HighChart.new('graph') do |f|
50
- f.title(:text => "Population vs GDP For 5 Big Countries [2009]")
51
- f.xAxis(:categories => ["United States", "Japan", "China", "Germany", "France"])
52
- f.series(:name => "GDP in Billions", :yAxis => 0, :data => [14119, 5068, 4985, 3339, 2656])
53
- f.series(:name => "Population in Millions", :yAxis => 1, :data => [310, 127, 1340, 81, 65])
69
+ @chart = LazyHighCharts::HighChart.new('graph') do |f|
70
+ f.title(text: "Population vs GDP For 5 Big Countries [2009]")
71
+ f.xAxis(categories: ["United States", "Japan", "China", "Germany", "France"])
72
+ f.series(name: "GDP in Billions", yAxis: 0, data: [14119, 5068, 4985, 3339, 2656])
73
+ f.series(name: "Population in Millions", yAxis: 1, data: [310, 127, 1340, 81, 65])
54
74
 
55
75
  f.yAxis [
56
- {:title => {:text => "GDP in Billions", :margin => 70} },
57
- {:title => {:text => "Population in Millions"}, :opposite => true},
76
+ {title: {text: "GDP in Billions", margin: 70} },
77
+ {title: {text: "Population in Millions"}, opposite: true},
58
78
  ]
59
79
 
60
- f.legend(:align => 'right', :verticalAlign => 'top', :y => 75, :x => -50, :layout => 'vertical',)
61
- f.chart({:defaultSeriesType=>"column"})
80
+ f.legend(align: 'right', verticalAlign: 'top', y: 75, x: -50, layout: 'vertical')
81
+ f.chart({defaultSeriesType: "column"})
82
+ end
83
+
84
+ @chart_globals = LazyHighCharts::HighChartGlobals.new do |f|
85
+ f.global(useUTC: false)
86
+ f.chart(
87
+ backgroundColor: {
88
+ linearGradient: [0, 0, 500, 500],
89
+ stops: [
90
+ [0, "rgb(255, 255, 255)"],
91
+ [1, "rgb(240, 240, 255)"]
92
+ ]
93
+ },
94
+ borderWidth: 2,
95
+ plotBackgroundColor: "rgba(255, 255, 255, .9)",
96
+ plotShadow: true,
97
+ plotBorderWidth: 1
98
+ )
99
+ f.lang(thousandsSep: ",")
100
+ f.colors(["#90ed7d", "#f7a35c", "#8085e9", "#f15c80", "#e4d354"])
62
101
  end
63
102
  ```
64
103
 
65
104
  ### View Helpers:
66
105
  ```ruby
67
- <%= high_chart("my_id4", @chart4) %>
106
+ <%= high_chart_globals(@chart_globals) %>
107
+ <%= high_chart("some_id", @chart) %>
68
108
  ```
69
109
 
70
- ###Demo projects:
110
+ `high_chart_globals` is optional. Use it to set the global options of all charts that are currently displayed on the page. More info [here](http://api.highcharts.com/highcharts#global).
111
+
112
+ ### No Data
113
+
114
+ When your series has no data and you want to display a message using Highcharts' noData feature ([Highcarts doc](http://api.highcharts.com/highcharts/noData)) you have to include the `no-data-to-display.js` file like so:
115
+
116
+ ````
117
+ //= require highcharts/highcharts
118
+ //= require highcharts/highcharts-more
119
+ //= require highcharts/modules/no-data-to-display
120
+ ````
121
+
122
+ You can then set the actual message that is displayed like so:
123
+
124
+ ````
125
+ @chart = LazyHighCharts::HighChart.new('graph') do |f|
126
+ f.options[:lang] = { noData: "My beautiful noData message" }
127
+ f.title(text: nil)
128
+ f.series([])
129
+ end
130
+ ````
131
+
132
+
133
+ ### Demo projects:
71
134
 
72
- [Nanoc App](spec/dummy_nanoc/README.md)
135
+ [Nanoc App](spec/dummy_nanoc)
73
136
 
74
- [Rails App](spec/dummy_rails/README.md)
137
+ [Rails App](spec/dummy_rails)
75
138
 
76
- [Sinatra App](spec/dummy_sinatra/README.md)
139
+ [Sinatra App](spec/dummy_sinatra)
77
140
 
78
141
 
79
- ### Update to latest js library. Aditional command line
142
+ ### Update to latest js library. Additional command line
80
143
 
81
144
  To update to the current highcharts.js directly from http://code.highcharts.com/", you can always run
82
145
 
@@ -97,7 +160,7 @@ We're open to any contribution. It has to be tested properly though.
97
160
  * Test your changes. We won't accept any untested contributions (except if they're not testable).
98
161
  * Create an [issue](https://github.com/michelson/lazy_high_charts/issues) with a link to your commits.
99
162
 
100
- Thanks for all [contributers](https://github.com/michelson/lazy_high_charts/contributors)
163
+ Thanks for all [contributors](https://github.com/michelson/lazy_high_charts/contributors)
101
164
 
102
165
  ## Maintainers
103
166
  * Deshi Xiao [github/xiaods](https://github.com/xiaods)
@@ -22,7 +22,7 @@ Gem::Specification.new do |s|
22
22
 
23
23
  s.required_rubygems_version = ">= 1.3"
24
24
 
25
- s.add_dependency "bundler", ">= 1.0"
25
+ s.add_development_dependency "bundler", ">= 1.0"
26
26
  s.add_dependency "hash-deep-merge"
27
27
 
28
28
  s.description = <<-DESC
@@ -1,11 +1,11 @@
1
1
  require 'action_view'
2
- include ActionView::Helpers::TagHelper
3
2
  require 'json'
4
3
 
5
4
  require File.join(File.dirname(__FILE__), *%w[lazy_high_charts core_ext string])
6
5
  require File.join(File.dirname(__FILE__), *%w[lazy_high_charts options_key_filter])
7
6
  require File.join(File.dirname(__FILE__), *%w[lazy_high_charts layout_helper])
8
7
  require File.join(File.dirname(__FILE__), *%w[lazy_high_charts high_chart])
8
+ require File.join(File.dirname(__FILE__), *%w[lazy_high_charts high_chart_globals])
9
9
  if defined?(::Rails::Railtie)
10
10
  require File.join(File.dirname(__FILE__), *%w[lazy_high_charts railtie])
11
11
  require File.join(File.dirname(__FILE__), *%w[lazy_high_charts engine]) if ::Rails.version.to_s >= '3.1'
@@ -34,7 +34,6 @@ module LazyHighCharts
34
34
  self.subtitle({})
35
35
  end
36
36
 
37
-
38
37
  # Pass other methods through to the javascript high_chart object.
39
38
  #
40
39
  # For instance: <tt>high_chart.grid(:color => "#699")</tt>
@@ -66,11 +65,13 @@ module LazyHighCharts
66
65
  #
67
66
  # @return [Hash] options JSON options hash
68
67
  def full_options
69
- options_collection = [generate_json_from_hash(OptionsKeyFilter.filter(self.options))]
70
- options_collection << %|"series": [#{generate_json_from_array(self.data)}]|
71
- <<-EOJS
72
- { #{options_collection.join(', ')} }
73
- EOJS
68
+ options_collection_as_string self
69
+ end
70
+
71
+ def to_json
72
+ data = self.options.clone
73
+ data[:series] = self.series_data.clone
74
+ data
74
75
  end
75
76
 
76
77
  private
@@ -82,7 +83,7 @@ module LazyHighCharts
82
83
  end
83
84
 
84
85
  def series_options
85
- @options.reject { |k, v| SERIES_OPTIONS.include?(k.to_s) == false }
86
+ @options.reject { |k, _| SERIES_OPTIONS.include?(k.to_s) == false }
86
87
  end
87
88
 
88
89
  def merge_options(name, opts)
@@ -0,0 +1,29 @@
1
+ module LazyHighCharts
2
+ class HighChartGlobals
3
+ include LayoutHelper
4
+
5
+ attr_accessor :options
6
+
7
+ def initialize
8
+ self.tap do |chart_globals|
9
+ chart_globals.options ||= {}
10
+ yield chart_globals if block_given?
11
+ end
12
+ end
13
+
14
+ # Pass other methods through to the high_chart_globals object.
15
+ #
16
+ # For instance: <tt>high_chart_globals.global(:useUTC => false)</tt>
17
+ def method_missing(meth, opts = {})
18
+ if meth.to_s == 'to_ary'
19
+ super
20
+ end
21
+ merge_options meth, opts
22
+ end
23
+
24
+ def merge_options(name, opts)
25
+ @options.merge! name => opts
26
+ end
27
+
28
+ end
29
+ end
@@ -2,6 +2,7 @@
2
2
 
3
3
  module LazyHighCharts
4
4
  module LayoutHelper
5
+ include ActionView::Helpers::TagHelper
5
6
 
6
7
  def high_chart(placeholder, object, &block)
7
8
  object.html_options.merge!({:id => placeholder})
@@ -15,6 +16,10 @@ module LazyHighCharts
15
16
  high_graph_stock(placeholder, object, &block).concat(content_tag("div", "", object.html_options))
16
17
  end
17
18
 
19
+ def high_chart_globals(object)
20
+ build_globals_html_output(object)
21
+ end
22
+
18
23
  def high_graph(placeholder, object, &block)
19
24
  build_html_output("Chart", placeholder, object, &block)
20
25
  end
@@ -23,59 +28,27 @@ module LazyHighCharts
23
28
  build_html_output("StockChart", placeholder, object, &block)
24
29
  end
25
30
 
26
- def build_html_output(type, placeholder, object, &block)
27
- options_collection = [generate_json_from_hash(OptionsKeyFilter.filter(object.options))]
28
- options_collection << %|"series": [#{generate_json_from_array(object.series_data)}]|
31
+ private
29
32
 
33
+ def build_html_output(type, placeholder, object, &block)
30
34
  core_js =<<-EOJS
31
- var options = { #{options_collection.join(',')} };
35
+ var options = #{options_collection_as_string(object)};
32
36
  #{capture(&block) if block_given?}
33
37
  window.chart_#{placeholder.underscore} = new Highcharts.#{type}(options);
34
38
  EOJS
35
39
 
36
- if defined?(request) && request.respond_to?(:xhr?) && request.xhr?
37
- graph =<<-EOJS
38
- <script type="text/javascript">
39
- (function() {
40
- #{core_js}
41
- })()
42
- </script>
43
- EOJS
44
- elsif defined?(Turbolinks) && request.headers["X-XHR-Referer"]
45
- graph =<<-EOJS
46
- <script type="text/javascript">
47
- (function() {
48
- var f = function(){
49
- document.removeEventListener('page:load', f, true);
50
- #{core_js}
51
- };
52
- document.addEventListener('page:load', f, true);
53
- })()
54
- </script>
55
- EOJS
56
- else
57
- graph =<<-EOJS
58
- <script type="text/javascript">
59
- (function() {
60
- var onload = window.onload;
61
- window.onload = function(){
62
- if (typeof onload == "function") onload();
63
- #{core_js}
64
- };
65
- })()
66
- </script>
67
- EOJS
68
- end
40
+ encapsulate_js core_js
41
+ end
69
42
 
70
- if defined?(raw)
71
- return raw(graph)
72
- else
73
- return graph
74
- end
43
+ def build_globals_html_output(object)
44
+ options_collection = [generate_json_from_hash(OptionsKeyFilter.filter(object.options))]
75
45
 
76
- end
46
+ core_js =<<-EOJS
47
+ Highcharts.setOptions({ #{options_collection.join(',')} });
48
+ EOJS
77
49
 
78
- private
50
+ encapsulate_js core_js
51
+ end
79
52
 
80
53
  def generate_json_from_hash hash
81
54
  hash.each_pair.map do |key, value|
@@ -100,6 +73,77 @@ module LazyHighCharts
100
73
  array.map { |value| generate_json_from_value(value) }.join(",")
101
74
  end
102
75
 
76
+ def request_is_xhr?
77
+ defined?(request) && request.respond_to?(:xhr?) && request.xhr?
78
+ end
79
+
80
+ def request_is_referrer?
81
+ defined?(request) && request.respond_to?(:headers) && request.headers["X-XHR-Referer"]
82
+ end
83
+
84
+ def request_turbolinks_5_tureferrer?
85
+ defined?(request) && request.respond_to?(:headers) && request.headers["Turbolinks-Referrer"]
86
+ end
87
+
88
+ def options_collection_as_string object
89
+ options_collection = [generate_json_from_hash(OptionsKeyFilter.filter(object.options))]
90
+ options_collection << %|"series": [#{generate_json_from_array(object.series_data)}]|
91
+ "{ #{options_collection.join(',')} }"
92
+ end
93
+
94
+ def encapsulate_js(core_js)
95
+ if request_is_xhr?
96
+ js_output = "#{js_start} #{core_js} #{js_end}"
97
+ # Turbolinks.version < 5
98
+ elsif defined?(Turbolinks) && request_is_referrer?
99
+ js_output =<<-EOJS
100
+ #{js_start}
101
+ var f = function(){
102
+ document.removeEventListener('page:load', f, true);
103
+ #{core_js}
104
+ };
105
+ document.addEventListener('page:load', f, true);
106
+ #{js_end}
107
+ EOJS
108
+ # Turbolinks >= 5
109
+ elsif defined?(Turbolinks) && request_turbolinks_5_tureferrer?
110
+ js_output =<<-EOJS
111
+ #{js_start}
112
+ document.addEventListener("turbolinks:load", function() {
113
+ #{core_js}
114
+ });
115
+ #{js_end}
116
+ EOJS
117
+ else
118
+ js_output =<<-EOJS
119
+ #{js_start}
120
+ window.addEventListener('load', function() {
121
+ #{core_js}
122
+ });
123
+ #{js_end}
124
+ EOJS
125
+ end
126
+
127
+ if defined?(raw)
128
+ return raw(js_output)
129
+ else
130
+ return js_output
131
+ end
132
+ end
133
+
134
+ def js_start
135
+ <<-EOJS
136
+ <script type="text/javascript">
137
+ (function() {
138
+ EOJS
139
+ end
140
+
141
+ def js_end
142
+ <<-EOJS
143
+ })()
144
+ </script>
145
+ EOJS
146
+ end
103
147
  end
104
148
  end
105
149