lazy_high_charts_tomgi 1.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,12 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage*
4
+ doc
5
+ rdoc
6
+ pkg
7
+ tmp
8
+ tags
9
+ rerun.txt
10
+ Gemfile.lock
11
+ .bundle
12
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format=progress
3
+
@@ -0,0 +1,43 @@
1
+ * Aug 31, 2012
2
+ 1. Correct highchart js location
3
+ [issue#74](https://github.com/michelson/lazy_high_charts/issues/74)
4
+ 2. support series data add javascript event
5
+ [issue#73](https://github.com/michelson/lazy_high_charts/issues/73)
6
+ 3. remove default html options.
7
+ [issue#72](https://github.com/michelson/lazy_high_charts/issues/72)
8
+
9
+ * Aug 26, 2012
10
+ 1. Troy Anderson (laptop) <troy@tlainvestments.com>
11
+ Added ability to supply an array of axes or other objects and still use .js_code within the array of hashes.
12
+ 2. David Biehl <lazylodr@gmail.com>
13
+ removing the window.onload function for AJAX requests
14
+
15
+ * Jun 27, 2012
16
+ update assets path for rails 3.x
17
+
18
+ * Jun 25, 2012
19
+ support inline javascript in click event on ruby runtime.
20
+ https://github.com/michelson/lazy_high_charts/issues/57
21
+
22
+ * Jul 14, 2011
23
+ add support rails 3.1 rc4 now
24
+
25
+ * Jul 13, 2011
26
+ HighStock support
27
+ remove some defaults
28
+
29
+ Jan 31, 2011
30
+ * backwards compatibility for rails 2.3.x in rendering callbacks options
31
+
32
+ Nov 30,2010
33
+ * dumped to gem 0.0.1
34
+
35
+ Sep 13,2010
36
+ * truely support rails 3.0(returning is deprecate function,use tap) deshi(xiaods@gmail.com)
37
+
38
+ Sep 14,2010
39
+ * update codebase to support rails3.0 and rspec2
40
+
41
+ Oct 8,2010
42
+ * update rake.it works now!
43
+
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "http://ruby.taobao.org"
2
+
3
+ # Specify your gem's dependencies in lazy_high_charts.gemspec
4
+ gemspec
5
+
6
+ group :test, :development do # This causes the plugins to NOT load
7
+ gem 'webrat','~> 0.7'
8
+ gem 'rspec', '~> 2.0'
9
+ gem 'rails'
10
+ gem 'ZenTest'
11
+ gem 'autotest'
12
+ end
13
+
14
+
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008-2010 Miguel Michelson Martinez
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,171 @@
1
+ # LazyHighCharts
2
+
3
+ Easily displaying Highcharts graphs with gem style.
4
+ [![Build Status](https://secure.travis-ci.org/michelson/lazy_high_charts.png)](http://travis-ci.org/michelson/lazy_high_charts)
5
+
6
+ ## Notice
7
+ latest version: 1.2.2
8
+ [ChangeLog](https://github.com/xiaods/lazy_high_charts/blob/master/CHANGELOG.md)
9
+
10
+ ### Installation instructions for Rails 3
11
+
12
+ ### Installing it by rubygems
13
+ To install it, you just need to add it to your Gemfile:
14
+ gem 'lazy_high_charts'
15
+
16
+ And then run this to install the javascript files:
17
+ rails g lazy_high_charts:install
18
+
19
+ ### Installing it as a plugin for rails 2.3.x
20
+
21
+ script/plugin install git://github.com/michelson/lazy_high_charts.git ##(for rails 2)
22
+
23
+ rails plugin install git://github.com/michelson/lazy_high_charts.git ##(for rails 3)
24
+
25
+ ### HighStocks
26
+ LazyHighCharts now compatible with beta HighStock, http://www.highcharts.com/stock/demo/
27
+
28
+ ## Usage
29
+
30
+ About javascript Assets notes:
31
+
32
+ ### For Rails 3.2.x
33
+ 1. add your highcart js to app/assets/javascripts/application.js
34
+ ````
35
+ //= require highcharts
36
+ ````
37
+
38
+ ### For Rails 2.x/3.0.x
39
+
40
+ 1. you need manually put jquery/highcharts js to public/javascript
41
+ 2. modify your layout html
42
+ Sample Code:
43
+ ````
44
+ <%= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" %>
45
+ <%= javascript_include_tag :highcharts %>
46
+ ````
47
+
48
+ 3. add gem name in your config/environment.rb
49
+ ````
50
+ config.gem "lazy_high_charts"
51
+ ````
52
+ 4. Done!
53
+
54
+ ### Demo Usage in Controller:
55
+ ````
56
+ @h = LazyHighCharts::HighChart.new('graph') do |f|
57
+ f.options[:chart][:defaultSeriesType] = "area"
58
+ f.series(:name=>'John', :data=>[3, 20, 3, 5, 4, 10, 12 ,3, 5,6,7,7,80,9,9])
59
+ f.series(:name=>'Jane', :data=> [1, 3, 4, 3, 3, 5, 4,-46,7,8,8,9,9,0,0,9] )
60
+ end
61
+ ````
62
+
63
+ Without overriding entire option , (only change a specific option index):
64
+
65
+ ````
66
+ @h = LazyHighCharts::HighChart.new('graph') do |f|
67
+ #.....
68
+ f.options[:chart][:defaultSeriesType] = "area"
69
+ f.options[:chart][:inverted] = true
70
+ f.options[:legend][:layout] = "horizontal"
71
+ f.options[:xAxis][:categories] = ["uno" ,"dos" , "tres" , "cuatro"]
72
+ #......
73
+ ````
74
+
75
+ Overriding entire option:
76
+
77
+ ````
78
+ @h = LazyHighCharts::HighChart.new('graph') do |f|
79
+ #.....
80
+ f.xAxis(:categories => @days.reverse! , :labels=>{:rotation=>-45 , :align => 'right'})
81
+ f.chart({:defaultSeriesType=>"spline" , :renderTo => "myRenderArea" , :inverted => true})
82
+ #.....
83
+ ````
84
+
85
+ If you want to use this syntax and still be able to build option step-by-step without overriding:
86
+
87
+ ````
88
+ @h = LazyHighCharts::HighChart.new('graph') do |f|
89
+ #.....
90
+ f.xAxis!(:categories => @days.reverse! , :labels=>{:rotation=>-45 , :align => 'right'})
91
+ f.chart!({:defaultSeriesType=>"spline" , :renderTo => "myRenderArea" , :inverted => true})
92
+ #.....
93
+ ````
94
+
95
+ Using the datetime axis type:
96
+
97
+ ````
98
+ @h = LazyHighCharts::HighChart.new('graph', style: '') do |f|
99
+ f.options[:chart][:defaultSeriesType] = "area"
100
+ f.options[:plotOptions] = {area: {pointInterval: 1.day, pointStart: 10.days.ago}}
101
+ f.series(:name=>'John', :data=>[3, 20, 3, 5, 4, 10, 12 ,3, 5,6,7,7,80,9,9])
102
+ f.series(:name=>'Jane', :data=> [1, 3, 4, 3, 3, 5, 4,-46,7,8,8,9,9,0,0,9])
103
+ f.xAxis(type: :datetime)
104
+ end
105
+ ````
106
+ A datetime axis [example](http://jsfiddle.net/gh/get/jquery/1.7.2/highslide-software/highcharts.com/tree/master/samples/highcharts/xaxis/type-datetime/)
107
+
108
+
109
+ Usage in layout:
110
+ ````
111
+ <%= javascript_include_tag :highcharts %>
112
+ ````
113
+
114
+ Usage in view:
115
+ ````
116
+ <%= high_chart("my_id", @h) %>
117
+ ````
118
+
119
+ You can pass in additional javascript into to the view with a block, this will be executed before the high chart is called
120
+
121
+ ````
122
+ <%= high_chart("my_id", @h) do |c| %>
123
+ alert('hello')
124
+ <%end %>
125
+ ````
126
+ To include javascript function calls or callbacks you can use the js_code method on your string`"function".js_code`:
127
+
128
+ ````
129
+ f.options[:plotOptions] = {
130
+ :column => { :events => { :click => %|function() { window.location = "http://www.highcharts.com" }|.js_code } }
131
+ }
132
+ ````
133
+
134
+
135
+ ## HighStock Support:
136
+
137
+ Just call HighChart Helper this way:
138
+ ````
139
+ <%= high_stock("my_id", @h) %>
140
+ ````
141
+
142
+ ## Option reference:
143
+
144
+ http://www.highcharts.com/ref/
145
+
146
+ ## HighCharts License:
147
+
148
+ http://www.highcharts.com/license
149
+
150
+
151
+ ## Contributing
152
+
153
+ We're open to any contribution. It has to be tested properly though.
154
+
155
+ * [Fork](http://help.github.com/forking/) the project
156
+ * Do your changes and commit them to your repository
157
+ * Test your changes. We won't accept any untested contributions (except if they're not testable).
158
+ * Create an [issue](https://github.com/michelson/lazy_high_charts/issues) with a link to your commits.
159
+
160
+ Contributer List:
161
+ * [Troy Anderson](https://github.com/troya2)
162
+ * [David Biehl](https://github.com/lazylodr)
163
+
164
+ Thanks for Troy & David
165
+ ## Maintainers
166
+ * Miguel Michelson Martinez [github/michelson](https://github.com/michelson)
167
+ * Deshi Xiao [github/xiaods](https://github.com/xiaods)
168
+
169
+ ## License
170
+ * Copyright (c) 2011,2012 Deshi Xiao,released under the MIT license
171
+ * Copyright (c) 2010 Miguel Michelson Martinez, released under the MIT license
@@ -0,0 +1,17 @@
1
+ # encoding: utf-8
2
+ require 'rubygems'
3
+ require 'rake'
4
+ require 'rspec/core/rake_task'
5
+ require 'bundler'
6
+
7
+ Bundler::GemHelper.install_tasks
8
+
9
+ desc 'Default: run unit specs.'
10
+ task :default => :spec
11
+
12
+ desc 'Test the lazy_high_charts plugin.'
13
+ RSpec::Core::RakeTask.new('spec') do |t|
14
+ t.pattern = FileList['spec/**/*_spec.rb']
15
+ end
16
+
17
+
@@ -0,0 +1 @@
1
+ Autotest.add_discovery { "rspec2" }
data/init.rb ADDED
@@ -0,0 +1,5 @@
1
+ # coding: utf-8
2
+ require 'lazy_high_charts'
3
+ require 'lazy_high_charts/layout_helper'
4
+ ActionView::Helpers::AssetTagHelper.register_javascript_expansion :high_charts => ["highcharts"]
5
+ ActionView::Base.send :include, LazyHighCharts::LayoutHelper
@@ -0,0 +1,31 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
3
+ require 'lazy_high_charts/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "lazy_high_charts_tomgi"
7
+ s.version = LazyHighCharts::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ['Miguel Michelson Martinez','Deshi Xiao']
10
+ s.email = ['miguelmichelson@gmail.com','xiaods@gmail.com']
11
+ s.homepage = "https://github.com/xiaods/lazy_high_charts"
12
+ s.summary = "lazy higcharts gem for rails"
13
+ s.description = "use highcharts js libary to visualization your data by rubygem/rails"
14
+
15
+ s.extra_rdoc_files = [ "README.md", "CHANGELOG.md" ]
16
+ s.rdoc_options = [ "--charset=UTF-8" ]
17
+
18
+ s.required_rubygems_version = "~> 1.3"
19
+
20
+ s.add_dependency "bundler", "~> 1.0"
21
+ s.add_dependency "hash-deep-merge"
22
+
23
+ s.description = <<-DESC
24
+ lazy_high_charts is a Rails 3.x gem for displaying Highcharts graphs.
25
+ DESC
26
+
27
+ s.files = `git ls-files`.split("\n")
28
+ s.executables = `git ls-files`.split("\n").select{|f| f =~ /^bin/}
29
+ s.require_path = 'lib'
30
+
31
+ end
@@ -0,0 +1,17 @@
1
+ module LazyHighCharts
2
+ class InstallGenerator < Rails::Generators::Base
3
+ desc "This generator install highcharts javascripts"
4
+
5
+ def install_highcharts(opts = nil)
6
+ say_status("installing", "Highcharts javascript(code.highcharts.com/stock version)", :green)
7
+ if ::Rails::VERSION::MAJOR == 3 && ::Rails::VERSION::MINOR >= 1
8
+ get "http://code.highcharts.com/stock/highstock.js", "vendor/assets/javascripts/highcharts.js"
9
+ else
10
+ get "http://code.highcharts.com/stock/highstock.js","public/javascripts/highcharts.js"
11
+ end
12
+ rescue OpenURI::HTTPError
13
+ say_status("warning", "could not find Highcharts javascript file", :yellow)
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,9 @@
1
+ require File.join(File.dirname(__FILE__), *%w[lazy_high_charts core_ext string])
2
+ require File.join(File.dirname(__FILE__), *%w[lazy_high_charts options_key_filter])
3
+ require File.join(File.dirname(__FILE__), *%w[lazy_high_charts high_chart])
4
+ require File.join(File.dirname(__FILE__), *%w[lazy_high_charts layout_helper])
5
+ require File.join(File.dirname(__FILE__), *%w[lazy_high_charts railtie]) if defined?(::Rails::Railtie)
6
+
7
+ module LazyHighCharts
8
+
9
+ end
@@ -0,0 +1,12 @@
1
+ class String
2
+
3
+ def js_code true_or_false = true
4
+ @_lazy_high_charts_js_code = true_or_false
5
+ self
6
+ end
7
+
8
+ def js_code?
9
+ @_lazy_high_charts_js_code || false
10
+ end
11
+
12
+ end
@@ -0,0 +1,84 @@
1
+ module LazyHighCharts
2
+ class HighChart
3
+ SERIES_OPTIONS = %w(data dataParser dataURL index legendIndex name stack type xAxis yAxis)
4
+
5
+ attr_accessor :data, :options, :placeholder, :html_options
6
+ alias :canvas :placeholder
7
+ alias :canvas= :placeholder=
8
+
9
+ def initialize(canvas = nil, html_opts = {})
10
+
11
+ @collection_filter = nil
12
+ self.tap do |high_chart|
13
+ high_chart.data ||= []
14
+ high_chart.options ||= {}
15
+ high_chart.defaults_options
16
+ high_chart.html_options ||={}
17
+ high_chart.canvas = canvas if canvas
18
+ yield high_chart if block_given?
19
+ end
20
+ end
21
+
22
+ # title: legend: xAxis: yAxis: tooltip: credits: :plotOptions
23
+ def defaults_options
24
+ self.title({ :text=>"example test title from highcharts gem"})
25
+ self.legend({ :layout=>"vertical", :style=>{} })
26
+ self.xAxis({})
27
+ self.yAxis({ :title=> {:text=> nil}, :labels=>{} })
28
+ self.tooltip({ :enabled=>true })
29
+ self.credits({ :enabled => false})
30
+ self.plotOptions({ :areaspline => { } })
31
+ self.chart({ :defaultSeriesType=>"line" , :renderTo => nil})
32
+ self.subtitle({})
33
+ end
34
+
35
+
36
+ # Pass other methods through to the javascript high_chart object.
37
+ #
38
+ # For instance: <tt>high_chart.grid(:color => "#699")</tt>
39
+ def method_missing(meth, opts = {})
40
+ if meth.to_s.end_with? '!'
41
+ deep_merge_options meth[0..-2].to_sym, opts
42
+ else
43
+ merge_options meth, opts
44
+ end
45
+ end
46
+
47
+ # Add a simple series to the graph:
48
+ #
49
+ # data = [[0,5], [1,5], [2,5]]
50
+ # @high_chart.series :name=>'Updated', :data=>data
51
+ # @high_chart.series :name=>'Updated', :data=>[5, 1, 6, 1, 5, 4, 9]
52
+ def series(opts = {})
53
+ @data ||= []
54
+ if not opts.empty?
55
+ @data << OptionsKeyFilter.filter(opts.merge(:name => opts[:name], :data => opts[:data]))
56
+ end
57
+ end
58
+
59
+ private
60
+
61
+ def series_options
62
+ @options.reject {|k,v| SERIES_OPTIONS.include?(k.to_s) == false}
63
+ end
64
+
65
+ def merge_options(name, opts)
66
+ @options.merge! name => opts
67
+ end
68
+
69
+ def deep_merge_options(name, opts)
70
+ @options.deep_merge! name => opts
71
+ end
72
+
73
+ def arguments_to_options(args)
74
+ if args.blank?
75
+ {:show => true}
76
+ elsif args.is_a? Array
77
+ args.first
78
+ else
79
+ args
80
+ end
81
+ end
82
+
83
+ end
84
+ end
@@ -0,0 +1,93 @@
1
+ # coding: utf-8
2
+
3
+ module LazyHighCharts
4
+ module LayoutHelper
5
+
6
+ def high_chart(placeholder, object , &block)
7
+ object.html_options.merge!({:id=>placeholder})
8
+ object.options[:chart][:renderTo] = placeholder
9
+ high_graph(placeholder,object , &block).concat(content_tag("div","", object.html_options))
10
+ end
11
+
12
+ def high_stock(placeholder, object , &block)
13
+ object.html_options.merge!({:id=>placeholder})
14
+ object.options[:chart][:renderTo] = placeholder
15
+ high_graph_stock(placeholder,object , &block).concat(content_tag("div","", object.html_options))
16
+ end
17
+
18
+ def high_graph(placeholder, object, &block)
19
+ build_html_output("Chart", placeholder, object, &block)
20
+ end
21
+
22
+ def high_graph_stock(placeholder, object, &block)
23
+ build_html_output("StockChart", placeholder, object, &block)
24
+ end
25
+
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.data)}]|
29
+
30
+ core_js =<<-EOJS
31
+ var options, chart;
32
+ options = { #{options_collection.join(',')} };
33
+ #{capture(&block) if block_given?}
34
+ chart = new Highcharts.#{type}(options);
35
+ EOJS
36
+
37
+ if defined?(request) && request.respond_to?(:xhr?) && request.xhr?
38
+ graph =<<-EOJS
39
+ <script type="text/javascript">
40
+ (function() {
41
+ #{core_js}
42
+ })()
43
+ </script>
44
+ EOJS
45
+ else
46
+ graph =<<-EOJS
47
+ <script type="text/javascript">
48
+ (function() {
49
+ var onload = window.onload;
50
+ window.onload = function(){
51
+ if (typeof onload == "function") onload();
52
+ #{core_js}
53
+ };
54
+ })()
55
+ </script>
56
+ EOJS
57
+ end
58
+
59
+ if defined?(raw)
60
+ return raw(graph)
61
+ else
62
+ return graph
63
+ end
64
+
65
+ end
66
+
67
+ private
68
+
69
+ def generate_json_from_hash hash
70
+ hash.each_pair.map do |key, value|
71
+ k = key.to_s.camelize.gsub!(/\b\w/) { $&.downcase }
72
+ %|"#{k}": #{generate_json_from_value value}|
73
+ end.flatten.join(',')
74
+ end
75
+
76
+ def generate_json_from_value value
77
+ if value.is_a? Hash
78
+ %|{ #{generate_json_from_hash value} }|
79
+ elsif value.is_a? Array
80
+ %|[ #{generate_json_from_array value} ]|
81
+ elsif value.respond_to?(:js_code) && value.js_code?
82
+ value
83
+ else
84
+ value.to_json
85
+ end
86
+ end
87
+
88
+ def generate_json_from_array array
89
+ array.map{|value| generate_json_from_value(value)}.join(",")
90
+ end
91
+
92
+ end
93
+ end
@@ -0,0 +1,39 @@
1
+ #
2
+ # A way to filter certain keys of data provided to the LazyHighCharts#series method and the LazyHighCharts#options
3
+ #
4
+ # Add methods or keys to the FILTER_MAP hash to have them applied to the options in a series
5
+ # In the FILTER_MAP, the hash keys are methods, and the values are arrays of the hash keys the filter should be
6
+ # applied to
7
+ #
8
+ # Be careful that it is OK to filter the hash keys you specify for every key of the options or series hash
9
+ #
10
+ module LazyHighCharts
11
+ module OptionsKeyFilter
12
+ def self.milliseconds value
13
+ value * 1000
14
+ end
15
+
16
+ def self.date_to_js_code date
17
+ "Date.UTC(#{date.year}, #{date.month - 1}, #{date.day})".js_code
18
+ end
19
+
20
+ def self.filter options
21
+ new_options = options.map do |k, v|
22
+ if v.is_a? ::Hash
23
+ v = filter v
24
+ else
25
+ FILTER_MAP.each_pair do |method, matchers|
26
+ v = method.call(v) if matchers.include?(k)
27
+ end
28
+ end
29
+ [k, v]
30
+ end
31
+ Hash[new_options]
32
+ end
33
+
34
+ FILTER_MAP = {
35
+ method(:milliseconds) => [:pointInterval],
36
+ method(:date_to_js_code) => [:pointStart]
37
+ }
38
+ end
39
+ end
@@ -0,0 +1,20 @@
1
+ require 'lazy_high_charts'
2
+ require 'lazy_high_charts/layout_helper.rb'
3
+
4
+ require 'rails'
5
+
6
+ module LazyHighCharts
7
+
8
+ class Railtie < ::Rails::Railtie
9
+ config.before_configuration do
10
+ config.action_view.javascript_expansions[:high_charts] = %w(highcharts)
11
+ end
12
+
13
+ initializer 'lazy_high_charts.initialize' do
14
+ ActiveSupport.on_load(:action_view) do
15
+ include LazyHighCharts::LayoutHelper
16
+ end
17
+ end
18
+ end
19
+
20
+ end
@@ -0,0 +1,3 @@
1
+ module LazyHighCharts
2
+ VERSION = "1.2.2"
3
+ end
@@ -0,0 +1,2 @@
1
+ # encoding: utf-8
2
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "init"))
@@ -0,0 +1,128 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ Record = Struct.new(:frequency, :amplitude)
4
+
5
+ describe "HighChart" do
6
+ before(:each) do
7
+ @collection = [Record.new(1,15), Record.new(2,30), Record.new(4,40)]
8
+ @data = [ [1,15], [2,30], [4,40]]
9
+
10
+ @placeholder = "placeholder"
11
+ @html_options = {:class => "stylin"}
12
+ @options = {:bars => {:show => true}}
13
+
14
+ @flot = LazyHighCharts::HighChart.new(@placeholder, @html_options) {|chart| chart.options = @options }
15
+ end
16
+
17
+ # this is almost all flotomatic stuff
18
+ describe "initialization" do
19
+ it "should take an optional 'placeholder' argument" do
20
+ LazyHighCharts::HighChart.new(@placeholder).placeholder.should == @placeholder
21
+ LazyHighCharts::HighChart.new.placeholder.should == nil
22
+ end
23
+
24
+ it "should take an optional html_options argument (defaulting to 300px height)" do
25
+ LazyHighCharts::HighChart.new(@html_options).placeholder.should == @html_options
26
+ end
27
+
28
+ it "should set options by default" do
29
+ LazyHighCharts::HighChart.new.options.should == {
30
+ :title=>{:text=>"example test title from highcharts gem"},
31
+ :legend=>{:layout=>"vertical", :style=>{}},
32
+ :xAxis=>{},
33
+ :yAxis=>{:title=>{:text=>nil}, :labels=>{}},
34
+ :tooltip=>{:enabled=>true},
35
+ :credits=>{:enabled=>false},
36
+ :plotOptions=>{:areaspline=>{}},
37
+ :chart=>{:defaultSeriesType=>"line", :renderTo=>nil},
38
+ :subtitle=>{}}
39
+ end
40
+
41
+ it "should set data empty by default" do
42
+ LazyHighCharts::HighChart.new.data.should == []
43
+ end
44
+
45
+ it "should take a block setting attributes" do
46
+ chart = LazyHighCharts::HighChart.new {|f| f.data = @data ; f.options = @options }
47
+ chart.data.should == @data
48
+ chart.options.should == @options
49
+ end
50
+
51
+ it "should take a block setting attributes" do
52
+ chart = LazyHighCharts::HighChart.new {|f| f.options[:legend][:layout] = "horizontal" }
53
+ chart.options[:legend][:layout].should == "horizontal"
54
+ end
55
+
56
+ it "should take a block setting attributes" do
57
+ chart = LazyHighCharts::HighChart.new {|f| f.options[:range_selector] = {}; f.options[:range_selector][:selected] = 1}
58
+ chart.options[:range_selector][:selected].should == 1
59
+ end
60
+
61
+ it "should change a block data without overriding options" do
62
+ chart = LazyHighCharts::HighChart.new('graph') do |f|
63
+ f.series(:name=>'John', :data=>[3, 20])
64
+ f.series(:name=>'Jane',:data=> [1, 3] )
65
+ # without overriding
66
+ f.options[:chart][:defaultSeriesType] = "area"
67
+ f.options[:chart][:inverted] = true
68
+ f.options[:legend][:layout] = "horizontal"
69
+ f.options[:xAxis][:categories] = ["uno" ,"dos" , "tres" , "cuatro"]
70
+ end
71
+ chart.data.should == [{:name=>"John", :data=>[3, 20]}, {:name=>"Jane", :data=>[1, 3]}]
72
+ chart.options[:legend][:layout].should == "horizontal"
73
+ chart.options[:xAxis][:categories].should == ["uno" ,"dos" , "tres" , "cuatro"]
74
+ chart.options[:chart][:defaultSeriesType].should == "area"
75
+ chart.options[:chart][:inverted].should == true
76
+ end
77
+
78
+ it "should change a block data with overriding entire options" do
79
+ chart = LazyHighCharts::HighChart.new('graph') do |f|
80
+ f.series(:name=>'John', :data=>[3, 20])
81
+ f.series(:name=>'Jane', :data=>[1, 3] )
82
+ f.title({ :text=>"example test title from controller"})
83
+ # without overriding
84
+ f.xAxis(:categories => ["uno" ,"dos" , "tres" , "cuatro"] , :labels=>{:rotation=>-45 , :align => 'right'})
85
+ f.chart({:defaultSeriesType=>"spline" , :renderTo => "myRenderArea" , :inverted => true})
86
+ end
87
+ chart.options[:xAxis][:categories].should == ["uno" ,"dos" , "tres" , "cuatro"]
88
+ chart.options[:xAxis][:labels][:rotation].should == -45
89
+ chart.options[:xAxis][:labels][:align].should == "right"
90
+ chart.options[:chart][:defaultSeriesType].should == "spline"
91
+ chart.options[:chart][:renderTo].should == "myRenderArea"
92
+ chart.options[:chart][:inverted].should == true
93
+ end
94
+
95
+ it "should have subtitles" do
96
+ chart = LazyHighCharts::HighChart.new('graph') do |f|
97
+ f.series(:name=>'John',:data=> [3, 20])
98
+ f.series(:name=>'Jane', :data=>[1, 3] )
99
+ f.title({ :text=>"example test title from controller"})
100
+ # without overriding
101
+ f.x_axis(:categories => ["uno" ,"dos" , "tres" , "cuatro"] , :labels=>{:rotation=>-45 , :align => 'right'})
102
+ f.chart({:defaultSeriesType=>"spline" , :renderTo => "myRenderArea" , :inverted => true})
103
+ f.subtitle({:text=>"Bar"})
104
+ end
105
+ chart.options[:subtitle][:text].should == "Bar"
106
+ end
107
+
108
+ it 'should override entire option by default when resetting it again' do
109
+ chart = LazyHighCharts::HighChart.new('graph') do |f|
110
+ f.xAxis(categories: [3, 5, 7])
111
+ f.xAxis(title: {text: 'x title'})
112
+ end
113
+ chart.options[:xAxis][:categories].should == nil
114
+ chart.options[:xAxis][:title][:text].should == 'x title'
115
+ end
116
+
117
+ it 'should allow to build options step by step without overriding previously set values' do
118
+ chart = LazyHighCharts::HighChart.new('graph') do |f|
119
+ f.xAxis!(categories: [3, 5, 7])
120
+ f.xAxis!(title: {text: 'x title'})
121
+ end
122
+ chart.options[:xAxis][:categories].should == [3, 5, 7]
123
+ chart.options[:xAxis][:title][:text].should == 'x title'
124
+ end
125
+
126
+ end
127
+
128
+ end
@@ -0,0 +1,134 @@
1
+ # coding: utf-8
2
+ require File.dirname(__FILE__) + '/spec_helper'
3
+
4
+ describe HighChartsHelper do
5
+ include LazyHighCharts::LayoutHelper
6
+
7
+ before(:each) do
8
+ @class = "stylin"
9
+ @placeholder = "placeholder"
10
+ @chart = LazyHighCharts::HighChart.new(@placeholder)
11
+ @data = "data"
12
+ @options = "options"
13
+ end
14
+
15
+ context "layout_helper" do
16
+ it "should return a div with an id of high_chart object" do
17
+ high_chart(@placeholder, @chart).should have_selector('div', :id => @placeholder)
18
+ end
19
+
20
+ it "should return a script" do
21
+ hc = LazyHighCharts::HighChart.new("placeholder")
22
+ high_chart(hc.placeholder, hc).should have_selector('script')
23
+ end
24
+ end
25
+
26
+ context "high_chart_graph" do
27
+ describe "ready function" do
28
+ it "should be a javascript script" do
29
+ high_chart(@placeholder, @chart).should have_selector('script', :type => 'text/javascript')
30
+ high_chart(@placeholder, @chart).should match(/}\)\(\)/)
31
+ end
32
+
33
+ it "should assign to the onload event" do
34
+ high_chart(@placeholder, @chart).should include('window.onload = function(){')
35
+ end
36
+ it "should call any existing onload function" do
37
+ high_chart(@placeholder, @chart).should match(/onload = window.onload;/)
38
+ high_chart(@placeholder, @chart).should match(/if \(typeof onload == "function"\)\s*onload\(\)/)
39
+ end
40
+ end
41
+ describe "initialize HighChart" do
42
+ it "should set variables `chart` `options`" do
43
+ high_chart(@placeholder, @chart).should include('var options, chart;')
44
+ end
45
+ it "should set Chart data" do
46
+ high_chart(@placeholder, @chart).should match(/chart\s+=\s+new\s+Highcharts.Chart/)
47
+ end
48
+
49
+ it "should set chart renderTo" do
50
+ high_chart(@placeholder, @chart).should match(/"renderTo": "placeholder"/)
51
+ end
52
+
53
+ it "should set Chart Stock" do
54
+ high_stock(@placeholder, @chart).should match(/chart\s+=\s+new\s+Highcharts.StockChart/)
55
+ end
56
+ end
57
+
58
+ end
59
+
60
+
61
+ it "should take a block setting attributes" do
62
+ chart = LazyHighCharts::HighChart.new {|f|
63
+ f.options[:rangeSelector] = {:selected=>1};
64
+ f.series(:type =>"spline",
65
+ :name =>"Historias",
66
+ :pointInterval => (1.day.to_i * 1000) ,
67
+ :pointStart => (Time.now.to_i * 1000),
68
+ :data => [0,1,2,3,5,6,0,7]
69
+ )
70
+ }
71
+ chart.options[:rangeSelector][:selected].should == 1
72
+ high_chart(@placeholder, chart).should match(/rangeSelector/)
73
+ high_chart(@placeholder, chart).should match(/xAxis/)
74
+ high_chart(@placeholder, chart).should match(/yAxis/)
75
+ high_chart(@placeholder, chart).should match(/series/)
76
+
77
+ end
78
+
79
+
80
+ it "should take a block setting attributes" do
81
+ chart = LazyHighCharts::HighChart.new {|f|
82
+ f.others(:foo =>"bar")
83
+ }
84
+ high_chart(@placeholder, chart).should match(/foo/)
85
+ end
86
+
87
+ it "should allow js code as attribute" do
88
+ chart = LazyHighCharts::HighChart.new {|f|
89
+ f.options[:foo] = "function () { alert('hello') }".js_code
90
+ }
91
+
92
+ high_chart(@placeholder, chart).should match(/"foo": function \(\) { alert\('hello'\) }/)
93
+ end
94
+
95
+ it "should convert keys to proper format" do
96
+ chart = LazyHighCharts::HighChart.new {|f|
97
+ f.options[:foo_bar] = { :bar_foo => "someattrib"}
98
+ }
99
+
100
+ high_chart(@placeholder, chart).should match(/fooBar/)
101
+ high_chart(@placeholder, chart).should match(/barFoo/)
102
+ end
103
+
104
+ # issue #62 .js_code setting ignored
105
+ # https://github.com/michelson/lazy_high_charts/issues/62
106
+ it "should allow js code in array && nest attributs" do
107
+ chart = LazyHighCharts::HighChart.new {|f|
108
+ f.yAxis([{
109
+ :labels => {
110
+ :formatter => %|function() {return this.value + ' W';}|.js_code
111
+ }
112
+ }])
113
+ }
114
+ high_chart(@placeholder,chart).should match(/"formatter": function\(\) {return this.value \+ ' W';}/)
115
+ end
116
+
117
+ it "should support js_code in Individual data label for each point" do
118
+ chart = LazyHighCharts::HighChart.new {|f|
119
+ f.series(
120
+ :data => [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, {
121
+ :dataLabels => {:enabled => true,
122
+ :align => 'left',
123
+ :x => 10,
124
+ :y => 4,
125
+ :style => {:fontWeight => 'bold'},
126
+ :formatter => "function() { return this.x; }".js_code
127
+ },
128
+ :y => 54.4}
129
+ ])
130
+ }
131
+ high_chart(@placeholder,chart).should match(/"formatter": function\(\) {\ return this.x;\ }/)
132
+ end
133
+
134
+ end
@@ -0,0 +1,28 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe LazyHighCharts::OptionsKeyFilter do
4
+ it "should filter :pointInterval from seconds to milliseconds" do
5
+ hash = LazyHighCharts::OptionsKeyFilter.filter(pointInterval: 1)
6
+ hash[:pointInterval].should == 1000
7
+ end
8
+
9
+ describe "filters :pointStart from a Date to a JavaScript compatible string" do
10
+ before(:each) do
11
+ hash = LazyHighCharts::OptionsKeyFilter.filter(pointStart: Date.new(2012, 9, 13))
12
+ @value = hash[:pointStart]
13
+ end
14
+
15
+ it "should be the correct string" do
16
+ @value.should == "Date.UTC(2012, 8, 13)"
17
+ end
18
+
19
+ it "should be js_code" do
20
+ @value.js_code.should be_true
21
+ end
22
+ end
23
+
24
+ it "should filter keys recursively" do
25
+ hash = LazyHighCharts::OptionsKeyFilter.filter({something: {another_thing: {pointInterval: 2}}})
26
+ hash[:something][:another_thing][:pointInterval].should == 2000
27
+ end
28
+ end
@@ -0,0 +1,46 @@
1
+ # coding: utf-8
2
+ require 'rubygems'
3
+ require 'bundler'
4
+ Bundler.setup
5
+
6
+ require 'active_support'
7
+ require 'action_pack'
8
+ require 'action_view'
9
+ require 'action_controller'
10
+ require 'rails'
11
+ #require 'action_mailer'
12
+ require "active_support/core_ext"
13
+
14
+
15
+ require File.expand_path(File.join(File.dirname(__FILE__), '../lib/lazy_high_charts'))
16
+ require File.expand_path(File.join(File.dirname(__FILE__), '../lib/lazy_high_charts/layout_helper'))
17
+ require File.expand_path(File.join(File.dirname(__FILE__), '../lib/lazy_high_charts/options_key_filter'))
18
+
19
+ require 'webrat'
20
+ require 'rspec'
21
+
22
+ # RSpec 1.x and 2.x compatibility
23
+ if defined?(RSpec)
24
+ RSPEC_NAMESPACE = RSPEC_CONFIGURER = RSpec
25
+ elsif defined?(Spec)
26
+ RSPEC_NAMESPACE = Spec
27
+ RSPEC_CONFIGURER = Spec::Runner
28
+ else
29
+ begin
30
+ require 'rspec'
31
+ RSPEC_NAMESPACE = RSPEC_CONFIGURER = Rspec
32
+ rescue LoadError
33
+ require 'spec'
34
+ RSPEC_NAMESPACE = Spec
35
+ RSPEC_CONFIGURER = Spec::Runner
36
+ end
37
+ end
38
+
39
+ RSPEC_CONFIGURER.configure do |config|
40
+ config.include ActionView::Helpers
41
+ config.include Webrat::Matchers
42
+ end
43
+
44
+ module HighChartsHelper
45
+ include ActionView::Helpers::TagHelper
46
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lazy_high_charts_tomgi
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.2.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Miguel Michelson Martinez
9
+ - Deshi Xiao
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2012-10-22 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: bundler
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: '1.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ~>
29
+ - !ruby/object:Gem::Version
30
+ version: '1.0'
31
+ - !ruby/object:Gem::Dependency
32
+ name: hash-deep-merge
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ type: :runtime
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ description: ! ' lazy_high_charts is a Rails 3.x gem for displaying Highcharts
48
+ graphs.
49
+
50
+ '
51
+ email:
52
+ - miguelmichelson@gmail.com
53
+ - xiaods@gmail.com
54
+ executables: []
55
+ extensions: []
56
+ extra_rdoc_files:
57
+ - README.md
58
+ - CHANGELOG.md
59
+ files:
60
+ - .gitignore
61
+ - .rspec
62
+ - CHANGELOG.md
63
+ - Gemfile
64
+ - MIT-LICENSE
65
+ - README.md
66
+ - Rakefile
67
+ - autotest/discover.rb
68
+ - init.rb
69
+ - lazy_high_charts.gemspec
70
+ - lib/generators/lazy_high_charts/install/install_generator.rb
71
+ - lib/lazy_high_charts.rb
72
+ - lib/lazy_high_charts/core_ext/string.rb
73
+ - lib/lazy_high_charts/high_chart.rb
74
+ - lib/lazy_high_charts/layout_helper.rb
75
+ - lib/lazy_high_charts/options_key_filter.rb
76
+ - lib/lazy_high_charts/railtie.rb
77
+ - lib/lazy_high_charts/version.rb
78
+ - rails/init.rb
79
+ - spec/high_chart_spec.rb
80
+ - spec/lazy_high_charts_spec.rb
81
+ - spec/options_key_filter_spec.rb
82
+ - spec/spec_helper.rb
83
+ homepage: https://github.com/xiaods/lazy_high_charts
84
+ licenses: []
85
+ post_install_message:
86
+ rdoc_options:
87
+ - --charset=UTF-8
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ! '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ segments:
97
+ - 0
98
+ hash: 2261933756179286063
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ~>
103
+ - !ruby/object:Gem::Version
104
+ version: '1.3'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 1.8.24
108
+ signing_key:
109
+ specification_version: 3
110
+ summary: lazy higcharts gem for rails
111
+ test_files: []