just_high_charts 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +14 -0
- data/.rspec +3 -0
- data/CHANGELOG.md +3 -0
- data/GEM_VERSION +1 -0
- data/Gemfile +11 -0
- data/Guardfile +9 -0
- data/README.md +36 -0
- data/Rakefile +13 -0
- data/just_high_charts.gemspec +34 -0
- data/lib/just_high_charts.rb +13 -0
- data/lib/just_high_charts/core_ext/string.rb +12 -0
- data/lib/just_high_charts/high_chart.rb +106 -0
- data/lib/just_high_charts/layout_helper.rb +105 -0
- data/lib/just_high_charts/options_key_filter.rb +39 -0
- data/spec/high_chart_spec.rb +159 -0
- data/spec/just_high_charts_spec.rb +141 -0
- data/spec/options_key_filter_spec.rb +28 -0
- data/spec/spec_helper.rb +22 -0
- metadata +131 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/CHANGELOG.md
ADDED
data/GEM_VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
data/Gemfile
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
source "https://rubygems.org"
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in just_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 'guard-rspec'
|
10
|
+
gem 'rb-fsevent', '~> 0.9.1'
|
11
|
+
end
|
data/Guardfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# just_high_charts
|
2
|
+
|
3
|
+
Easily include HighCharts in your project with this gem, without Rails.
|
4
|
+
|
5
|
+
Extracted from the
|
6
|
+
[lazy_high_charts Rails gem](https://github.com/michelson/lazy_high_charts)
|
7
|
+
by Deshi Xiao and Miguel Michelson Martinez.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
### Installation with rubygems
|
12
|
+
|
13
|
+
To install it, you just need to add it to your Gemfile:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'just_high_charts'
|
17
|
+
```
|
18
|
+
|
19
|
+
then run
|
20
|
+
|
21
|
+
```bash
|
22
|
+
bundle install
|
23
|
+
```
|
24
|
+
|
25
|
+
to install it.
|
26
|
+
|
27
|
+
## just_high_charts User Guide
|
28
|
+
|
29
|
+
TODO
|
30
|
+
|
31
|
+
|
32
|
+
## Maintainers
|
33
|
+
* [Steven Clontz](https://github.com/StevenClontz)
|
34
|
+
|
35
|
+
## License
|
36
|
+
Released under the MIT license.
|
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rspec/core/rake_task'
|
4
|
+
require 'bundler'
|
5
|
+
Bundler::GemHelper.install_tasks
|
6
|
+
|
7
|
+
desc 'Default: run unit specs.'
|
8
|
+
task :default => :spec
|
9
|
+
|
10
|
+
desc 'Test the just_high_charts plugin.'
|
11
|
+
RSpec::Core::RakeTask.new('spec') do |t|
|
12
|
+
t.pattern = FileList['spec/**/*_spec.rb']
|
13
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
|
3
|
+
version = File.read(File.expand_path("../GEM_VERSION", __FILE__)).strip
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "just_high_charts"
|
7
|
+
s.version = version
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ['Steven Clontz']
|
10
|
+
s.email = ['steven.clontz@gmail.com']
|
11
|
+
s.homepage = "https://github.com/StevenClontz/just_high_charts"
|
12
|
+
s.summary = <<-EOF
|
13
|
+
extraction of HighCharts object from LazyHighCharts
|
14
|
+
without Rails requirement
|
15
|
+
EOF
|
16
|
+
s.description = <<-EOF
|
17
|
+
just_high_charts simply provides a HighCharts object for use with
|
18
|
+
Highcharts.js
|
19
|
+
EOF
|
20
|
+
|
21
|
+
s.extra_rdoc_files = ["README.md", "CHANGELOG.md"]
|
22
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
23
|
+
|
24
|
+
s.required_rubygems_version = ">= 1.3"
|
25
|
+
|
26
|
+
s.add_dependency "bundler", ">= 1.0"
|
27
|
+
s.add_dependency "hash-deep-merge"
|
28
|
+
s.add_dependency "activesupport"
|
29
|
+
s.add_dependency "actionview"
|
30
|
+
|
31
|
+
s.files = `git ls-files`.split("\n")
|
32
|
+
s.executables = `git ls-files`.split("\n").select { |f| f =~ /^bin/ }
|
33
|
+
s.require_path = 'lib'
|
34
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'active_support/core_ext'
|
2
|
+
require 'action_view'
|
3
|
+
|
4
|
+
require File.join(File.dirname(__FILE__), *%w[just_high_charts core_ext string])
|
5
|
+
require File.join(File.dirname(__FILE__), *%w[just_high_charts options_key_filter])
|
6
|
+
require File.join(File.dirname(__FILE__), *%w[just_high_charts layout_helper])
|
7
|
+
require File.join(File.dirname(__FILE__), *%w[just_high_charts high_chart])
|
8
|
+
|
9
|
+
module JustHighCharts
|
10
|
+
def self.root
|
11
|
+
File.expand_path '../..', __FILE__
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,106 @@
|
|
1
|
+
module JustHighCharts
|
2
|
+
class HighChart
|
3
|
+
include LayoutHelper
|
4
|
+
|
5
|
+
SERIES_OPTIONS = %w(data index legendIndex name stack type xAxis yAxis)
|
6
|
+
|
7
|
+
attr_accessor :series_data, :options, :placeholder, :html_options
|
8
|
+
alias :canvas :placeholder
|
9
|
+
alias :canvas= :placeholder=
|
10
|
+
|
11
|
+
def initialize(canvas = nil, html_opts = {})
|
12
|
+
|
13
|
+
self.tap do |high_chart|
|
14
|
+
high_chart.series_data ||= []
|
15
|
+
high_chart.options ||= {}
|
16
|
+
high_chart.defaults_options
|
17
|
+
high_chart.html_options ||= html_opts
|
18
|
+
high_chart.canvas = (canvas ? canvas : random_canvas_id)
|
19
|
+
yield high_chart if block_given?
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# title: legend: xAxis: yAxis: tooltip: credits: :plotOptions
|
24
|
+
def defaults_options
|
25
|
+
self.title({:text => nil})
|
26
|
+
self.legend({:layout => "vertical", :style => {}})
|
27
|
+
self.xAxis({})
|
28
|
+
self.yAxis({:title => {:text => nil}, :labels => {}})
|
29
|
+
self.tooltip({:enabled => true})
|
30
|
+
self.credits({:enabled => false})
|
31
|
+
self.plotOptions({:areaspline => {}})
|
32
|
+
self.chart({:defaultSeriesType => "line", :renderTo => nil})
|
33
|
+
self.subtitle({})
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
# Pass other methods through to the javascript high_chart object.
|
38
|
+
#
|
39
|
+
# For instance: <tt>high_chart.grid(:color => "#699")</tt>
|
40
|
+
def method_missing(meth, opts = {})
|
41
|
+
if meth.to_s == 'to_ary'
|
42
|
+
super
|
43
|
+
end
|
44
|
+
|
45
|
+
if meth.to_s.end_with? '!'
|
46
|
+
deep_merge_options meth[0..-2].to_sym, opts
|
47
|
+
else
|
48
|
+
merge_options meth, opts
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
# Add a simple series to the graph:
|
53
|
+
#
|
54
|
+
# data = [[0,5], [1,5], [2,5]]
|
55
|
+
# @high_chart.series :name=>'Updated', :data=>data
|
56
|
+
# @high_chart.series :name=>'Updated', :data=>[5, 1, 6, 1, 5, 4, 9]
|
57
|
+
def series(opts = {})
|
58
|
+
if not opts.empty?
|
59
|
+
@series_data << OptionsKeyFilter.filter(opts.merge(:name => opts[:name], :data => opts[:data]))
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
# Pre-processes and returns full set of options relevant to the chart. Identical to what happens in the high_charts
|
64
|
+
# view helper.
|
65
|
+
#
|
66
|
+
# @return [Hash] options JSON options hash
|
67
|
+
def full_options
|
68
|
+
options_collection = [generate_json_from_hash(OptionsKeyFilter.filter(self.options))]
|
69
|
+
options_collection << %|"series": [#{generate_json_from_array(self.data)}]|
|
70
|
+
<<-EOJS
|
71
|
+
{ #{options_collection.join(', ')} }
|
72
|
+
EOJS
|
73
|
+
end
|
74
|
+
|
75
|
+
private
|
76
|
+
|
77
|
+
def random_canvas_id
|
78
|
+
canvas_id_length = 11
|
79
|
+
# Don't use SecureRandom.urlsafe_base64; it gives invalid characters.
|
80
|
+
('a'..'z').to_a.shuffle.take(canvas_id_length).join
|
81
|
+
end
|
82
|
+
|
83
|
+
def series_options
|
84
|
+
@options.reject { |k, v| SERIES_OPTIONS.include?(k.to_s) == false }
|
85
|
+
end
|
86
|
+
|
87
|
+
def merge_options(name, opts)
|
88
|
+
@options.merge! name => opts
|
89
|
+
end
|
90
|
+
|
91
|
+
def deep_merge_options(name, opts)
|
92
|
+
@options.deep_merge! name => opts
|
93
|
+
end
|
94
|
+
|
95
|
+
def arguments_to_options(args)
|
96
|
+
if args.blank?
|
97
|
+
{:show => true}
|
98
|
+
elsif args.is_a? Array
|
99
|
+
args.first
|
100
|
+
else
|
101
|
+
args
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
106
|
+
end
|
@@ -0,0 +1,105 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
module JustHighCharts
|
4
|
+
module LayoutHelper
|
5
|
+
include ActionView::Helpers::TagHelper
|
6
|
+
|
7
|
+
def high_chart(placeholder, object, &block)
|
8
|
+
object.html_options.merge!({:id => placeholder})
|
9
|
+
object.options[:chart][:renderTo] = placeholder
|
10
|
+
high_graph(placeholder, object, &block).concat(content_tag("div", "", object.html_options))
|
11
|
+
end
|
12
|
+
|
13
|
+
def high_stock(placeholder, object, &block)
|
14
|
+
object.html_options.merge!({:id => placeholder})
|
15
|
+
object.options[:chart][:renderTo] = placeholder
|
16
|
+
high_graph_stock(placeholder, object, &block).concat(content_tag("div", "", object.html_options))
|
17
|
+
end
|
18
|
+
|
19
|
+
def high_graph(placeholder, object, &block)
|
20
|
+
build_html_output("Chart", placeholder, object, &block)
|
21
|
+
end
|
22
|
+
|
23
|
+
def high_graph_stock(placeholder, object, &block)
|
24
|
+
build_html_output("StockChart", placeholder, object, &block)
|
25
|
+
end
|
26
|
+
|
27
|
+
def build_html_output(type, placeholder, object, &block)
|
28
|
+
options_collection = [generate_json_from_hash(OptionsKeyFilter.filter(object.options))]
|
29
|
+
options_collection << %|"series": [#{generate_json_from_array(object.series_data)}]|
|
30
|
+
|
31
|
+
core_js =<<-EOJS
|
32
|
+
var options = { #{options_collection.join(',')} };
|
33
|
+
#{capture(&block) if block_given?}
|
34
|
+
window.chart_#{placeholder.underscore} = 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
|
+
elsif defined?(Turbolinks) && request.headers["X-XHR-Referer"]
|
46
|
+
graph =<<-EOJS
|
47
|
+
<script type="text/javascript">
|
48
|
+
(function() {
|
49
|
+
var f = function(){
|
50
|
+
document.removeEventListener('page:load', f, true);
|
51
|
+
#{core_js}
|
52
|
+
};
|
53
|
+
document.addEventListener('page:load', f, true);
|
54
|
+
})()
|
55
|
+
</script>
|
56
|
+
EOJS
|
57
|
+
else
|
58
|
+
graph =<<-EOJS
|
59
|
+
<script type="text/javascript">
|
60
|
+
(function() {
|
61
|
+
var onload = window.onload;
|
62
|
+
window.onload = function(){
|
63
|
+
if (typeof onload == "function") onload();
|
64
|
+
#{core_js}
|
65
|
+
};
|
66
|
+
})()
|
67
|
+
</script>
|
68
|
+
EOJS
|
69
|
+
end
|
70
|
+
|
71
|
+
if defined?(raw)
|
72
|
+
return raw(graph)
|
73
|
+
else
|
74
|
+
return graph
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
private
|
80
|
+
|
81
|
+
def generate_json_from_hash hash
|
82
|
+
hash.each_pair.map do |key, value|
|
83
|
+
k = key.to_s.camelize.gsub!(/\b\w/) { $&.downcase }
|
84
|
+
%|"#{k}": #{generate_json_from_value value}|
|
85
|
+
end.flatten.join(',')
|
86
|
+
end
|
87
|
+
|
88
|
+
def generate_json_from_value value
|
89
|
+
if value.is_a? Hash
|
90
|
+
%|{ #{generate_json_from_hash value} }|
|
91
|
+
elsif value.is_a? Array
|
92
|
+
%|[ #{generate_json_from_array value} ]|
|
93
|
+
elsif value.respond_to?(:js_code) && value.js_code?
|
94
|
+
value
|
95
|
+
else
|
96
|
+
value.to_json
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
def generate_json_from_array array
|
101
|
+
array.map { |value| generate_json_from_value(value) }.join(",")
|
102
|
+
end
|
103
|
+
|
104
|
+
end
|
105
|
+
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 JustHighCharts
|
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,159 @@
|
|
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 = JustHighCharts::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
|
+
JustHighCharts::HighChart.new(@placeholder).placeholder.should == @placeholder
|
21
|
+
JustHighCharts::HighChart.new.placeholder.should_not == @placeholder
|
22
|
+
end
|
23
|
+
|
24
|
+
it "shouldn't generate a nil placeholder" do
|
25
|
+
JustHighCharts::HighChart.new.placeholder.should_not be_nil
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should generate different placeholders for different charts" do
|
29
|
+
a_different_placeholder = JustHighCharts::HighChart.new.placeholder
|
30
|
+
JustHighCharts::HighChart.new.placeholder.should_not == a_different_placeholder
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should take an optional html_options argument (defaulting to 300px height)" do
|
34
|
+
JustHighCharts::HighChart.new(@placeholder, @html_options).html_options.should == @html_options
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should set options by default" do
|
38
|
+
JustHighCharts::HighChart.new.options.should == {
|
39
|
+
:title => {:text => nil},
|
40
|
+
:legend => {:layout => "vertical", :style => {}},
|
41
|
+
:xAxis => {},
|
42
|
+
:yAxis => {:title => {:text => nil}, :labels => {}},
|
43
|
+
:tooltip => {:enabled => true},
|
44
|
+
:credits => {:enabled => false},
|
45
|
+
:plotOptions => {:areaspline => {}},
|
46
|
+
:chart => {:defaultSeriesType => "line", :renderTo => nil},
|
47
|
+
:subtitle => {}}
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should set data empty by default" do
|
51
|
+
JustHighCharts::HighChart.new.series_data.should == []
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should take a block setting attributes" do
|
55
|
+
chart = JustHighCharts::HighChart.new { |f| f.series_data = @data; f.options = @options }
|
56
|
+
chart.series_data.should == @data
|
57
|
+
chart.options.should == @options
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should take a block setting attributes" do
|
61
|
+
chart = JustHighCharts::HighChart.new { |f| f.options[:legend][:layout] = "horizontal" }
|
62
|
+
chart.options[:legend][:layout].should == "horizontal"
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should take a block setting attributes" do
|
66
|
+
chart = JustHighCharts::HighChart.new { |f| f.options[:range_selector] = {}; f.options[:range_selector][:selected] = 1 }
|
67
|
+
chart.options[:range_selector][:selected].should == 1
|
68
|
+
end
|
69
|
+
|
70
|
+
it "should change a block data without overriding options" do
|
71
|
+
chart = JustHighCharts::HighChart.new('graph') do |f|
|
72
|
+
f.series(:name => 'John', :data => [3, 20])
|
73
|
+
f.series(:name => 'Jane', :data => [1, 3])
|
74
|
+
# without overriding
|
75
|
+
f.options[:chart][:defaultSeriesType] = "area"
|
76
|
+
f.options[:chart][:inverted] = true
|
77
|
+
f.options[:legend][:layout] = "horizontal"
|
78
|
+
f.options[:xAxis][:categories] = ["uno", "dos", "tres", "cuatro"]
|
79
|
+
end
|
80
|
+
chart.series_data.should == [{:name => "John", :data => [3, 20]}, {:name => "Jane", :data => [1, 3]}]
|
81
|
+
chart.options[:legend][:layout].should == "horizontal"
|
82
|
+
chart.options[:xAxis][:categories].should == ["uno", "dos", "tres", "cuatro"]
|
83
|
+
chart.options[:chart][:defaultSeriesType].should == "area"
|
84
|
+
chart.options[:chart][:inverted].should == true
|
85
|
+
end
|
86
|
+
|
87
|
+
it "should change a block data with overriding entire options" do
|
88
|
+
chart = JustHighCharts::HighChart.new('graph') do |f|
|
89
|
+
f.series(:name => 'John', :data => [3, 20])
|
90
|
+
f.series(:name => 'Jane', :data => [1, 3])
|
91
|
+
f.title({:text => nil})
|
92
|
+
# without overriding
|
93
|
+
f.xAxis(:categories => ["uno", "dos", "tres", "cuatro"], :labels => {:rotation => -45, :align => 'right'})
|
94
|
+
f.chart({:defaultSeriesType => "spline", :renderTo => "myRenderArea", :inverted => true})
|
95
|
+
end
|
96
|
+
chart.options[:xAxis][:categories].should == ["uno", "dos", "tres", "cuatro"]
|
97
|
+
chart.options[:xAxis][:labels][:rotation].should == -45
|
98
|
+
chart.options[:xAxis][:labels][:align].should == "right"
|
99
|
+
chart.options[:chart][:defaultSeriesType].should == "spline"
|
100
|
+
chart.options[:chart][:renderTo].should == "myRenderArea"
|
101
|
+
chart.options[:chart][:inverted].should == true
|
102
|
+
end
|
103
|
+
|
104
|
+
it "should have subtitles" do
|
105
|
+
chart = JustHighCharts::HighChart.new('graph') do |f|
|
106
|
+
f.series(:name => 'John', :data => [3, 20])
|
107
|
+
f.series(:name => 'Jane', :data => [1, 3])
|
108
|
+
f.title({:text => nil})
|
109
|
+
# without overriding
|
110
|
+
f.x_axis(:categories => ["uno", "dos", "tres", "cuatro"], :labels => {:rotation => -45, :align => 'right'})
|
111
|
+
f.chart({:defaultSeriesType => "spline", :renderTo => "myRenderArea", :inverted => true})
|
112
|
+
f.subtitle({:text => "Bar"})
|
113
|
+
end
|
114
|
+
chart.options[:subtitle][:text].should == "Bar"
|
115
|
+
end
|
116
|
+
|
117
|
+
it 'should override entire option by default when resetting it again' do
|
118
|
+
chart = JustHighCharts::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 == nil
|
123
|
+
chart.options[:xAxis][:title][:text].should == 'x title'
|
124
|
+
end
|
125
|
+
|
126
|
+
it 'should allow to build options step by step without overriding previously set values' do
|
127
|
+
chart = JustHighCharts::HighChart.new('graph') do |f|
|
128
|
+
f.xAxis!(categories: [3, 5, 7])
|
129
|
+
f.xAxis!(title: {text: 'x title'})
|
130
|
+
end
|
131
|
+
chart.options[:xAxis][:categories].should == [3, 5, 7]
|
132
|
+
chart.options[:xAxis][:title][:text].should == 'x title'
|
133
|
+
end
|
134
|
+
|
135
|
+
it 'should merge options and data into a full options hash' do
|
136
|
+
chart = JustHighCharts::HighChart.new('graph') do |f|
|
137
|
+
f.series(name: 'John', data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4])
|
138
|
+
f.series(name: 'Jane', data: [140.02, 41.63, 66.72, 113.21, 107.98, 105.71, 28.59, 114.23, 5.56, 93.71, 137.35, 93.16])
|
139
|
+
f.title({text: 'Example Data'})
|
140
|
+
f.xAxis(categories: %w(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec), labels: {rotation: -45, align: 'right'})
|
141
|
+
f.options[:tooltip][:formatter] = "function(){ return '<b>'+ this.point.name +'</b>: '+ Highcharts.numberFormat(this.percentage, 2) +' %'; }"
|
142
|
+
end
|
143
|
+
|
144
|
+
json = chart.full_options
|
145
|
+
json.should match /\"series\"/
|
146
|
+
json.should match /\"title\"/
|
147
|
+
json.should match /\"tooltip\": { \"enabled\": true,\"formatter\"/
|
148
|
+
end
|
149
|
+
|
150
|
+
end
|
151
|
+
|
152
|
+
describe '#to_ary' do
|
153
|
+
subject { JustHighCharts::HighChart.new }
|
154
|
+
|
155
|
+
it 'raises NoMethodError' do
|
156
|
+
expect { subject.to_ary }.to raise_error(NoMethodError)
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|
@@ -0,0 +1,141 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
3
|
+
|
4
|
+
describe HighChartsHelper do
|
5
|
+
include JustHighCharts::LayoutHelper
|
6
|
+
|
7
|
+
before(:each) do
|
8
|
+
@class = "stylin"
|
9
|
+
@placeholder = "placeholder"
|
10
|
+
@chart = JustHighCharts::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 = JustHighCharts::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 match(/var\s+options\s+=/)
|
44
|
+
high_chart(@placeholder, @chart).should match(/window.chart_placeholder\s=/)
|
45
|
+
end
|
46
|
+
it "should set Chart data" do
|
47
|
+
high_chart(@placeholder, @chart).should match(/window\.chart_placeholder\s=\snew\sHighcharts.Chart/)
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should set chart renderTo" do
|
51
|
+
high_chart(@placeholder, @chart).should match(/"renderTo": "placeholder"/)
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should set Chart Stock" do
|
55
|
+
high_stock(@placeholder, @chart).should match(/window\.chart_placeholder\s+=\s+new\s+Highcharts.StockChart/)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe "HighChart Variable" do
|
60
|
+
it "should underscore chart_ variable" do
|
61
|
+
high_chart("place-holder", @chart).should match(/window.chart_place_holder\s=/)
|
62
|
+
high_chart("PlaceHolder", @chart).should match(/window.chart_place_holder\s=/)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
|
68
|
+
it "should take a block setting attributes" do
|
69
|
+
chart = JustHighCharts::HighChart.new { |f|
|
70
|
+
f.options[:rangeSelector] = {:selected => 1};
|
71
|
+
f.series(:type => "spline",
|
72
|
+
:name => "Historias",
|
73
|
+
:pointInterval => (1.day.to_i * 1000),
|
74
|
+
:pointStart => (Time.now.to_i * 1000),
|
75
|
+
:data => [0, 1, 2, 3, 5, 6, 0, 7]
|
76
|
+
)
|
77
|
+
}
|
78
|
+
chart.options[:rangeSelector][:selected].should == 1
|
79
|
+
high_chart(@placeholder, chart).should match(/rangeSelector/)
|
80
|
+
high_chart(@placeholder, chart).should match(/xAxis/)
|
81
|
+
high_chart(@placeholder, chart).should match(/yAxis/)
|
82
|
+
high_chart(@placeholder, chart).should match(/series/)
|
83
|
+
|
84
|
+
end
|
85
|
+
|
86
|
+
|
87
|
+
it "should take a block setting attributes" do
|
88
|
+
chart = JustHighCharts::HighChart.new { |f|
|
89
|
+
f.others(:foo => "bar")
|
90
|
+
}
|
91
|
+
high_chart(@placeholder, chart).should match(/foo/)
|
92
|
+
end
|
93
|
+
|
94
|
+
it "should allow js code as attribute" do
|
95
|
+
chart = JustHighCharts::HighChart.new { |f|
|
96
|
+
f.options[:foo] = "function () { alert('hello') }".js_code
|
97
|
+
}
|
98
|
+
|
99
|
+
high_chart(@placeholder, chart).should match(/"foo": function \(\) { alert\('hello'\) }/)
|
100
|
+
end
|
101
|
+
|
102
|
+
it "should convert keys to proper format" do
|
103
|
+
chart = JustHighCharts::HighChart.new { |f|
|
104
|
+
f.options[:foo_bar] = {:bar_foo => "someattrib"}
|
105
|
+
}
|
106
|
+
|
107
|
+
high_chart(@placeholder, chart).should match(/fooBar/)
|
108
|
+
high_chart(@placeholder, chart).should match(/barFoo/)
|
109
|
+
end
|
110
|
+
|
111
|
+
# issue #62 .js_code setting ignored
|
112
|
+
# https://github.com/michelson/lazy_high_charts/issues/62
|
113
|
+
it "should allow js code in array && nest attributs" do
|
114
|
+
chart = JustHighCharts::HighChart.new { |f|
|
115
|
+
f.yAxis([{
|
116
|
+
:labels => {
|
117
|
+
:formatter => %|function() {return this.value + ' W';}|.js_code
|
118
|
+
}
|
119
|
+
}])
|
120
|
+
}
|
121
|
+
high_chart(@placeholder, chart).should match(/"formatter": function\(\) {return this.value \+ ' W';}/)
|
122
|
+
end
|
123
|
+
|
124
|
+
it "should support js_code in Individual data label for each point" do
|
125
|
+
chart = JustHighCharts::HighChart.new { |f|
|
126
|
+
f.series(
|
127
|
+
:data => [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, {
|
128
|
+
:dataLabels => {:enabled => true,
|
129
|
+
:align => 'left',
|
130
|
+
:x => 10,
|
131
|
+
:y => 4,
|
132
|
+
:style => {:fontWeight => 'bold'},
|
133
|
+
:formatter => "function() { return this.x; }".js_code
|
134
|
+
},
|
135
|
+
:y => 54.4}
|
136
|
+
])
|
137
|
+
}
|
138
|
+
high_chart(@placeholder, chart).should match(/"formatter": function\(\) {\ return this.x;\ }/)
|
139
|
+
end
|
140
|
+
|
141
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
describe JustHighCharts::OptionsKeyFilter do
|
4
|
+
it "should filter :pointInterval from seconds to milliseconds" do
|
5
|
+
hash = JustHighCharts::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 = JustHighCharts::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 = JustHighCharts::OptionsKeyFilter.filter({something: {another_thing: {pointInterval: 2}}})
|
26
|
+
hash[:something][:another_thing][:pointInterval].should == 2000
|
27
|
+
end
|
28
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'rubygems'
|
3
|
+
require 'bundler/setup'
|
4
|
+
|
5
|
+
|
6
|
+
require File.expand_path(File.join(File.dirname(__FILE__),
|
7
|
+
'../lib/just_high_charts'))
|
8
|
+
require File.expand_path(File.join(File.dirname(__FILE__),
|
9
|
+
'../lib/just_high_charts/layout_helper'))
|
10
|
+
require File.expand_path(File.join(File.dirname(__FILE__),
|
11
|
+
'../lib/just_high_charts/options_key_filter'))
|
12
|
+
|
13
|
+
require 'webrat'
|
14
|
+
require 'rspec'
|
15
|
+
|
16
|
+
RSpec.configure do |config|
|
17
|
+
config.include Webrat::Matchers
|
18
|
+
end
|
19
|
+
|
20
|
+
module HighChartsHelper
|
21
|
+
include ActionView::Helpers::TagHelper
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,131 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: just_high_charts
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Steven Clontz
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-03-10 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: hash-deep-merge
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: activesupport
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: actionview
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :runtime
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: ! " just_high_charts simply provides a HighCharts object for use with\n
|
79
|
+
\ Highcharts.js\n"
|
80
|
+
email:
|
81
|
+
- steven.clontz@gmail.com
|
82
|
+
executables: []
|
83
|
+
extensions: []
|
84
|
+
extra_rdoc_files:
|
85
|
+
- README.md
|
86
|
+
- CHANGELOG.md
|
87
|
+
files:
|
88
|
+
- .gitignore
|
89
|
+
- .rspec
|
90
|
+
- CHANGELOG.md
|
91
|
+
- GEM_VERSION
|
92
|
+
- Gemfile
|
93
|
+
- Guardfile
|
94
|
+
- README.md
|
95
|
+
- Rakefile
|
96
|
+
- just_high_charts.gemspec
|
97
|
+
- lib/just_high_charts.rb
|
98
|
+
- lib/just_high_charts/core_ext/string.rb
|
99
|
+
- lib/just_high_charts/high_chart.rb
|
100
|
+
- lib/just_high_charts/layout_helper.rb
|
101
|
+
- lib/just_high_charts/options_key_filter.rb
|
102
|
+
- spec/high_chart_spec.rb
|
103
|
+
- spec/just_high_charts_spec.rb
|
104
|
+
- spec/options_key_filter_spec.rb
|
105
|
+
- spec/spec_helper.rb
|
106
|
+
homepage: https://github.com/StevenClontz/just_high_charts
|
107
|
+
licenses: []
|
108
|
+
post_install_message:
|
109
|
+
rdoc_options:
|
110
|
+
- --charset=UTF-8
|
111
|
+
require_paths:
|
112
|
+
- lib
|
113
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
114
|
+
none: false
|
115
|
+
requirements:
|
116
|
+
- - ! '>='
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
120
|
+
none: false
|
121
|
+
requirements:
|
122
|
+
- - ! '>='
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '1.3'
|
125
|
+
requirements: []
|
126
|
+
rubyforge_project:
|
127
|
+
rubygems_version: 1.8.23
|
128
|
+
signing_key:
|
129
|
+
specification_version: 3
|
130
|
+
summary: extraction of HighCharts object from LazyHighCharts without Rails requirement
|
131
|
+
test_files: []
|