highcharts-js-rails 0.0.3 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +21 -2
- data/README.md +39 -67
- data/highcharts-js-rails.gemspec +1 -2
- data/{LICENSE → lib/LICENSE} +0 -0
- data/lib/highcharts-js-rails.rb +26 -3
- data/lib/highcharts.rb +74 -0
- data/lib/highcharts/axis.rb +2 -47
- data/lib/highcharts/axis/events.rb +13 -0
- data/lib/highcharts/axis/plot_bands.rb +14 -0
- data/lib/highcharts/axis/plot_lines.rb +14 -0
- data/lib/highcharts/axis/stack_labels.rb +13 -0
- data/lib/highcharts/axis/x.rb +59 -0
- data/lib/highcharts/axis/y.rb +20 -0
- data/lib/highcharts/base.rb +43 -12
- data/lib/highcharts/chart.rb +4 -75
- data/lib/highcharts/color.rb +15 -0
- data/lib/highcharts/credits.rb +10 -0
- data/lib/highcharts/engine.rb +4 -0
- data/lib/highcharts/labels.rb +11 -0
- data/lib/highcharts/legend.rb +6 -10
- data/lib/highcharts/plot_options.rb +14 -18
- data/lib/highcharts/plot_options/plot_type.rb +18 -0
- data/lib/highcharts/plot_options/plot_type/events.rb +14 -0
- data/lib/highcharts/plot_options/plot_type/marker.rb +17 -0
- data/lib/highcharts/plot_options/plot_type/marker/states.rb +19 -0
- data/lib/highcharts/plot_options/plot_type/states.rb +16 -0
- data/lib/highcharts/plot_options/plot_type/states/hover.rb +18 -0
- data/lib/highcharts/point.rb +13 -0
- data/lib/highcharts/point/events.rb +12 -0
- data/lib/highcharts/series.rb +4 -14
- data/lib/highcharts/title.rb +10 -0
- data/lib/highcharts/tooltip.rb +11 -0
- data/lib/highcharts/version.rb +1 -1
- data/spec/highcharts/base_spec.rb +1 -2
- metadata +35 -26
- data/lib/highcharts/rails.rb +0 -6
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,22 @@
|
|
1
|
-
## v0.0
|
1
|
+
## v0.1.0 (2012-03-17) ##
|
2
2
|
|
3
|
-
*
|
3
|
+
* Near-complete rewrite of the entire gem. Now accepts all major Highcharts options, providing convenient help throughout the way.
|
4
|
+
* Bump dependency on railties to 3.1.
|
5
|
+
* Read the updated README to learn how to change your implementation to support the 0.1.x tree.
|
6
|
+
|
7
|
+
## v0.0.3 (2012-02-19) ##
|
8
|
+
|
9
|
+
* We should not be hiding credit to Highcharts.com on the charts.
|
10
|
+
* Fix core lib/ file name to load correctly.
|
11
|
+
* Fix Rails.env sourcing.
|
12
|
+
|
13
|
+
## v0.0.2 (2012-02-19) ##
|
14
|
+
|
15
|
+
* Whitespace now conforms to Rails conventions.
|
16
|
+
* require action_view in Highcharts::Base.
|
17
|
+
* Add line breaks after closing bracket (]) when not running in production environment.
|
18
|
+
* Privatize methods that are not being used outside of the class.
|
19
|
+
|
20
|
+
## v0.0.1 (2012-02-19) ##
|
21
|
+
|
22
|
+
* First release.
|
data/README.md
CHANGED
@@ -3,83 +3,55 @@ Highcharts JS Rails
|
|
3
3
|
|
4
4
|
Easily configure a Highcharts JS chart for use in a Rails application.
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
*
|
13
|
-
|
14
|
-
|
6
|
+
All major options in Highcharts are available to be used with Highcharts JS Rails. However, the exporting and navigation options are not yet built in. You will need to do those manually.
|
7
|
+
|
8
|
+
The Highcharts class's methods match up to the corresponding Highcharts options. Take a look at http://www.highcharts.com/ref/ for the full reference.
|
9
|
+
|
10
|
+
Many have a default option assigned to them. Those that have one are listed below:
|
11
|
+
|
12
|
+
* chart: renderTo (String)
|
13
|
+
* credits: enabled (Boolean)
|
14
|
+
* legend: enabled (Boolean)
|
15
|
+
* plotOptions.marker: enabled (Boolean)
|
16
|
+
* subtitle: text (String)
|
17
|
+
* title: text (String)
|
18
|
+
* xAxis: categories (Array)
|
19
|
+
* xAxis.events: setExtremes (function)
|
20
|
+
* xAxis.labels: enabled (Boolean)
|
21
|
+
* xAxis.title: text (String)
|
22
|
+
* yAxis: title (String)
|
23
|
+
* yAxis.events: setExtremes (function)
|
24
|
+
* yAxis.stackLabels: enabled (Boolean)
|
25
|
+
* yAxis.labels: enabled (Boolean)
|
26
|
+
* yAxis.title: text (String)
|
27
|
+
|
28
|
+
xAxis Notes
|
15
29
|
-------
|
16
30
|
|
17
|
-
|
18
|
-
|
19
|
-
`chart = Highcharts::Chart.new`
|
20
|
-
|
21
|
-
The Highcharts::Chart class can receive a hash of the following options:
|
22
|
-
|
23
|
-
* container: string (required)
|
24
|
-
* title: string (required)
|
25
|
-
* subtitle: string
|
26
|
-
* xAxes: hash or array of hashes (required)
|
27
|
-
* yAxes: hash or array of hashes (required)
|
28
|
-
* legend: hash
|
29
|
-
* series: hash or array of hashes (required)
|
30
|
-
* tooltip: string
|
31
|
-
|
32
|
-
Both the `xAxes` and `yAxes` options can be either a single hash (meaning there is only one axis) or an array of hashes (for multiple-axis charts). Each hash can include the following options:
|
33
|
-
|
34
|
-
* title: string
|
35
|
-
* categories: array
|
36
|
-
* tickInterval: integer
|
37
|
-
* min: integer
|
38
|
-
* max: integer
|
39
|
-
* labels: hash
|
40
|
-
* opposite: boolean
|
41
|
-
|
42
|
-
The `categories` option's array of values will be automatically formatted based on its class.
|
43
|
-
The `tickInterval` option is only necessary if you want to override automatic tickInterval creation. If the option is not passed, the tickInterval will be calculated based on the amount of data passed to the attached `series`.
|
44
|
-
The `labels` option can receive a set of options that includes `prepend` and `append`.
|
31
|
+
* The `categories` option's array of values will be automatically formatted based on its class unless you pass :skip_category_formatting => true.
|
32
|
+
* The `tickInterval` option is only necessary if you want to override automatic tickInterval creation. If the option is not passed or :auto is passed, the tickInterval will be calculated based on the number of categories passed.
|
45
33
|
|
46
|
-
|
47
|
-
|
48
|
-
* layout: string (possible values can be horizontal or vertical - default is horizontal)
|
49
|
-
* align: string
|
50
|
-
* verticalAlign: string
|
51
|
-
* x: integer
|
52
|
-
* y: integer
|
53
|
-
* borderWidth: integer
|
54
|
-
|
55
|
-
The `series` option for the Highcharts::Chart class can receive a hash of the following options:
|
56
|
-
|
57
|
-
* name: string
|
58
|
-
* type: string (possible values can be area, areaspline, bar, column, line, pie, scatter or spline - default is line)
|
59
|
-
* xAxis: integer (the index of the xAxis this series should be attached to)
|
60
|
-
* yAxis: integer (the index of the yAxis this series should be attached to)
|
61
|
-
* data: array
|
62
|
-
|
63
|
-
The `data` option's array of values will be automatically formatted as a float, if necessary.
|
34
|
+
For the xAxis, yAxis, and series options, you must pass an array of hashes. Each of those hashes will be converted into the appropriate set of options.
|
64
35
|
|
65
36
|
Example
|
66
37
|
--------
|
67
38
|
|
68
39
|
Ruby:
|
69
40
|
|
70
|
-
```
|
71
|
-
chart = Highcharts
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
)
|
80
|
-
<%= chart %>
|
41
|
+
```ruby
|
42
|
+
chart = Highcharts.new
|
43
|
+
chart.chart({:renderTo => 'graph'})
|
44
|
+
chart.title('Highcharts Example')
|
45
|
+
chart.xAxis([{:categories => ['October 12', 'October 13', 'October 14']}])
|
46
|
+
chart.yAxis([{:title => 'Impressions', :min => 0}])
|
47
|
+
chart.series([{:name => 'Impressions', :yAxis => 0, :type => 'line', :data => [100000, 122000, 127000]}])
|
48
|
+
chart.legend({:layout => 'vertical', :align => 'right', :verticalAlign => 'top', :x => -10, :y => 100, :borderWidth => 0})
|
49
|
+
chart.tooltip({:formatter => "function(){ return '<b>' + this.series.name + '</b><br/>' + this.x + ': ' + this.y; }"})
|
81
50
|
```
|
82
51
|
|
83
52
|
HTML/ERB:
|
84
53
|
|
85
|
-
|
54
|
+
```html+erb
|
55
|
+
<%= chart %>
|
56
|
+
<div id="graph" style="width: 400px; height: 200px;"></div>
|
57
|
+
```
|
data/highcharts-js-rails.gemspec
CHANGED
@@ -18,8 +18,7 @@ Gem::Specification.new do |s|
|
|
18
18
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
19
|
s.require_paths = ['lib']
|
20
20
|
|
21
|
-
s.add_dependency '
|
22
|
-
s.add_dependency 'railties', '~> 3.0'
|
21
|
+
s.add_dependency 'railties', '~> 3.1'
|
23
22
|
s.add_development_dependency 'bundler'
|
24
23
|
s.add_development_dependency 'rake'
|
25
24
|
s.add_development_dependency 'rspec'
|
data/{LICENSE → lib/LICENSE}
RENAMED
File without changes
|
data/lib/highcharts-js-rails.rb
CHANGED
@@ -1,3 +1,26 @@
|
|
1
|
-
require '
|
2
|
-
|
3
|
-
|
1
|
+
require 'highcharts'
|
2
|
+
require 'highcharts/base'
|
3
|
+
require 'highcharts/axis'
|
4
|
+
require 'highcharts/axis/events'
|
5
|
+
require 'highcharts/axis/plot_bands'
|
6
|
+
require 'highcharts/axis/plot_lines'
|
7
|
+
require 'highcharts/axis/stack_labels'
|
8
|
+
require 'highcharts/axis/x'
|
9
|
+
require 'highcharts/axis/y'
|
10
|
+
require 'highcharts/chart'
|
11
|
+
require 'highcharts/color'
|
12
|
+
require 'highcharts/credits'
|
13
|
+
require 'highcharts/labels'
|
14
|
+
require 'highcharts/legend'
|
15
|
+
require 'highcharts/plot_options'
|
16
|
+
require 'highcharts/plot_options/plot_type'
|
17
|
+
require 'highcharts/plot_options/plot_type/events'
|
18
|
+
require 'highcharts/plot_options/plot_type/marker'
|
19
|
+
require 'highcharts/plot_options/plot_type/marker/states'
|
20
|
+
require 'highcharts/plot_options/plot_type/states'
|
21
|
+
require 'highcharts/plot_options/plot_type/states/hover'
|
22
|
+
require 'highcharts/point'
|
23
|
+
require 'highcharts/point/events'
|
24
|
+
require 'highcharts/title'
|
25
|
+
require 'highcharts/tooltip'
|
26
|
+
require 'highcharts/engine'
|
data/lib/highcharts.rb
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'action_view'
|
2
|
+
|
3
|
+
class Highcharts < ActionView::Base
|
4
|
+
include ActionView::Helpers
|
5
|
+
|
6
|
+
attr_reader :options, :base_options, :default_options, :custom_options
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
@options = {}
|
10
|
+
|
11
|
+
@base_options = %w(labels lang loading)
|
12
|
+
@default_options = %w(chart colors credits legend point series symbols title tooltip)
|
13
|
+
@custom_options = {
|
14
|
+
'plotOptions' => 'PlotOptions',
|
15
|
+
'subtitle' => 'Title',
|
16
|
+
'xAxis' => 'Axis::X',
|
17
|
+
'yAxis' => 'Axis::Y'
|
18
|
+
}
|
19
|
+
end
|
20
|
+
|
21
|
+
def inspect
|
22
|
+
"#<#{self.class}:0x#{object_id} #{options.inspect}>"
|
23
|
+
end
|
24
|
+
|
25
|
+
def method_missing(method, *args)
|
26
|
+
# We only want to allow the base + custom options to be constantized and instantiated
|
27
|
+
if (base_options + default_options + custom_options.keys).include?(method.to_s)
|
28
|
+
# If there are arguments passed to this method, we want to set the option.
|
29
|
+
if args.length > 0
|
30
|
+
# For xAxis, yAxis, and series, we need to take the array that is passed as the option
|
31
|
+
# and for each value, instantiate a new class.
|
32
|
+
@options[method] = if %w(xAxis yAxis series).include?(method.to_s)
|
33
|
+
args.first.collect {|v| determine_method_class(method).constantize.new(v)}
|
34
|
+
# For all others, just instantiate the class with the original arguments.
|
35
|
+
else
|
36
|
+
determine_method_class(method).constantize.new(*args)
|
37
|
+
end
|
38
|
+
# Otherwise, just return the option's value.
|
39
|
+
else
|
40
|
+
options[method]
|
41
|
+
end
|
42
|
+
else
|
43
|
+
raise NoMethodError
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# Called by default when you output the final chart. Spits out jQuery-encapsulated Highcharts JavaScript.
|
48
|
+
# <% chart = Highcharts.new -%>
|
49
|
+
# <%= chart %>
|
50
|
+
def to_s
|
51
|
+
javascript_tag "$(function(){" +
|
52
|
+
"new Highcharts.Chart({" +
|
53
|
+
to_json +
|
54
|
+
"})" +
|
55
|
+
"});"
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
# Only to be used in the #to_s method of a Highcharts instance. Because the standard #to_json method
|
60
|
+
# will not work the correct way (introspecting on the class and failing to simply call #to_json on each
|
61
|
+
# subsequent class), we have to do it ourselves.
|
62
|
+
def to_json
|
63
|
+
options.collect {|k, v| "\"#{k}\":#{v.is_a?(Array) ? "[{#{v.collect(&:to_json).join('},{')}}]" : "{#{v.to_json}}"}"}.join(',')
|
64
|
+
end
|
65
|
+
|
66
|
+
# Figure out which class a specific method should map too. Ones within the base_options array will be Highcharts::Base.
|
67
|
+
# Ones within the default_options array will be, if the method is title, Highcharts::Title.
|
68
|
+
# When the method is within the custom_options array, it will be the corresponding value. For plotOptions, it will be Highcharts::PlotOptions.
|
69
|
+
def determine_method_class(method)
|
70
|
+
method = method.to_s
|
71
|
+
"Highcharts::#{base_options.include?(method) ? 'Base' : default_options.include?(method) ? method.capitalize : custom_options[method]}"
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
data/lib/highcharts/axis.rb
CHANGED
@@ -1,49 +1,4 @@
|
|
1
|
-
|
2
|
-
class Axis
|
3
|
-
|
4
|
-
attr_accessor :title, :categories, :tickInterval, :min, :max, :labels, :opposite
|
5
|
-
|
6
|
-
def to_s
|
7
|
-
"{" +
|
8
|
-
[render_title, render_categories, render_tickInterval, render_options(:objects => 'min max opposite'), render_labels].flatten.compact.join(',') +
|
9
|
-
"}"
|
10
|
-
end
|
11
|
-
|
12
|
-
private
|
13
|
-
def render_title
|
14
|
-
title.present? ? "title: {text: '#{title}'}" : nil
|
15
|
-
end
|
16
|
-
|
17
|
-
def render_categories
|
18
|
-
return nil unless categories.present?
|
19
|
-
"categories: [#{categories.collect {|c| "'#{format_category(c)}'"}.join(', ')}]" # need to encapsulate each category in quotes and format it before joining them with a comma
|
20
|
-
end
|
21
|
-
|
22
|
-
def render_tickInterval
|
23
|
-
return tickInterval if tickInterval.present?
|
24
|
-
return nil unless categories.present?
|
25
|
-
"tickInterval: #{(Math.sqrt(categories.length) < 5 ? 1 : Math.sqrt(categories.length)).floor}"
|
26
|
-
end
|
27
|
-
|
28
|
-
def format_category(category)
|
29
|
-
case category
|
30
|
-
when Date
|
31
|
-
category.strftime("%b. %d")
|
32
|
-
when Time
|
33
|
-
category.strftime("%H:%M")
|
34
|
-
when DateTime
|
35
|
-
category.strftime("%b. %d, %H:%M")
|
36
|
-
else
|
37
|
-
category.to_s
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
def render_labels
|
42
|
-
return nil unless labels.present?
|
43
|
-
"labels: {" +
|
44
|
-
"formatter: function(){ return '#{labels[:prepend]}' + this.value + '#{labels[:append]}'; }" +
|
45
|
-
"}"
|
46
|
-
end
|
47
|
-
|
1
|
+
class Highcharts
|
2
|
+
class Axis
|
48
3
|
end
|
49
4
|
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
class Highcharts
|
2
|
+
class Axis
|
3
|
+
class X < Base
|
4
|
+
|
5
|
+
attr_reader :skip_category_formatting
|
6
|
+
|
7
|
+
def initialize(opts = {})
|
8
|
+
@default = :categories
|
9
|
+
@suboptions = {
|
10
|
+
:events => 'Axis::Events',
|
11
|
+
:labels => 'Labels',
|
12
|
+
:plotBands => 'Axis::PlotBands',
|
13
|
+
:plotLines => 'Axis::PlotLines',
|
14
|
+
:title => 'Title'
|
15
|
+
}
|
16
|
+
# Check to see if we want to skip automatic formatting of categories.
|
17
|
+
@skip_category_formatting = true if opts.delete(:skip_category_formatting)
|
18
|
+
super
|
19
|
+
end
|
20
|
+
|
21
|
+
def to_json
|
22
|
+
@options[:categories] = options[:categories].collect {|c| format_category(c)} if options[:categories].present? && !skip_category_formatting
|
23
|
+
automate_tick_interval if options[:tickInterval] == :auto || options[:tickInterval].blank?
|
24
|
+
super
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
# The tickInterval option for an Axis can be automatically calculated by highcharts-js-rails.
|
29
|
+
# Either pass :auto to the :tickInterval option, or do not include the option.
|
30
|
+
# This will only be calculated if the :categories option is passed to the Axis.
|
31
|
+
def automate_tick_interval
|
32
|
+
@options[:tickInterval] = (Math.sqrt(options[:categories].length) < 5 ? 1 : Math.sqrt(options[:categories].length)).floor if options[:categories].present?
|
33
|
+
end
|
34
|
+
|
35
|
+
# Used in the #to_json method to cleanly format the categories for this axis. Depending on the class
|
36
|
+
# of the category, we will return a differently formatted string. Currently supports:
|
37
|
+
#
|
38
|
+
# Date
|
39
|
+
# Time
|
40
|
+
# DateTime
|
41
|
+
#
|
42
|
+
# If the class of the category is none of the above, #to_s is returned.
|
43
|
+
# You can prevent this by passing :skip_category_formatting => true to the Axis options.
|
44
|
+
def format_category(category)
|
45
|
+
case category
|
46
|
+
when Date
|
47
|
+
category.strftime("%b. %d")
|
48
|
+
when Time || DateTime
|
49
|
+
# category.strftime("%H:%M")
|
50
|
+
# when DateTime
|
51
|
+
category.strftime("%b. %d, %H:%M")
|
52
|
+
else
|
53
|
+
category.to_s
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
class Highcharts
|
2
|
+
class Axis
|
3
|
+
class Y < Base
|
4
|
+
|
5
|
+
def initialize(opts = {})
|
6
|
+
@default = :title
|
7
|
+
@suboptions = {
|
8
|
+
:events => 'Axis::Events',
|
9
|
+
:labels => 'Labels',
|
10
|
+
:plotBands => 'Axis::PlotBands',
|
11
|
+
:plotLines => 'Axis::PlotLines',
|
12
|
+
:stackLabels => 'Axis::StackLabels',
|
13
|
+
:title => 'Title'
|
14
|
+
}
|
15
|
+
super
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/highcharts/base.rb
CHANGED
@@ -1,20 +1,51 @@
|
|
1
|
-
require '
|
1
|
+
require 'rails'
|
2
2
|
|
3
|
-
|
4
|
-
class Base
|
3
|
+
class Highcharts
|
4
|
+
class Base
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
attr_reader :options, :default, :skip_quotation, :suboptions
|
7
|
+
|
8
|
+
def initialize(opts = {})
|
9
|
+
@options = opts
|
10
|
+
@suboptions ||= {}
|
9
11
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
12
|
+
# We want to make sure that options in the end is actually a Hash of options, or an Array of hashes of options.
|
13
|
+
# When default is defined in a subclass and the passed options are not a Hash or Array, we want to replace options
|
14
|
+
# with a Hash where the only key/value pair is the default key and the options value.
|
15
|
+
if !options.is_a?(Hash) || (options.is_a?(Array) && !options.first.is_a?(Hash))
|
16
|
+
if default.present?
|
17
|
+
@options = {default => options}
|
18
|
+
else
|
19
|
+
raise ArgumentError, "You must pass a Hash to #{self.class}. You passed #{options.inspect}"
|
15
20
|
end
|
16
21
|
end
|
17
|
-
|
22
|
+
# If there is an option that is available as a suboption to the current class,
|
23
|
+
# let's set the option to an instance of that subclass.
|
24
|
+
options.each do |k, v|
|
25
|
+
@options[k] = "Highcharts::#{suboptions[k]}".constantize.new(v) if suboptions.keys.include?(k)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def inspect
|
30
|
+
"#<#{self.class}:0x#{object_id} #{options.inspect}>"
|
31
|
+
end
|
32
|
+
|
33
|
+
# This method is used in the parent class, Highcharts, in order to render the options in a JavaScript-friendly (JSON) format.
|
34
|
+
def to_json
|
35
|
+
json = options.collect do |k, v|
|
36
|
+
"\"#{k}\":" +
|
37
|
+
(suboptions.keys.include?(k) && !v.is_a?(Array) ? '{' : '') +
|
38
|
+
(v.is_a?(Array) && suboptions.keys.include?(k) ? "[{#{v.collect(&:to_json).join('},{')}}]" : check_quotation(k, v.to_json)) +
|
39
|
+
(suboptions.keys.include?(k) && !v.is_a?(Array) ? '}' : '')
|
40
|
+
end
|
41
|
+
json.join(',')
|
42
|
+
end
|
43
|
+
|
44
|
+
# This method is used in #to_json to remove the first and last characters from an option's value.
|
45
|
+
# In the end this removes the encapsulating quotation marks (") from a specific option, provided
|
46
|
+
# it is included in the current class's skip_quotation array.
|
47
|
+
def check_quotation(key, json)
|
48
|
+
skip_quotation.present? && Array.wrap(skip_quotation).include?(key) ? json[1..-2] : json
|
18
49
|
end
|
19
50
|
|
20
51
|
end
|
data/lib/highcharts/chart.rb
CHANGED
@@ -1,81 +1,10 @@
|
|
1
|
-
|
1
|
+
class Highcharts
|
2
2
|
class Chart < Base
|
3
|
-
include ActionView::Helpers
|
4
3
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
check_required_fields
|
9
|
-
|
10
|
-
javascript_tag do
|
11
|
-
safe_concat("$(function(){" +
|
12
|
-
"new Highcharts.Chart({" +
|
13
|
-
render_all +
|
14
|
-
"})" +
|
15
|
-
"});")
|
16
|
-
end
|
4
|
+
def initialize(opts = {})
|
5
|
+
@default = :renderTo
|
6
|
+
super
|
17
7
|
end
|
18
8
|
|
19
|
-
private
|
20
|
-
def render_all
|
21
|
-
content = [render_container, render_titles, render_plot_options, render_axes, render_series, render_legend, render_tooltip].flatten.compact.join(",")
|
22
|
-
if ::Rails.env.production?
|
23
|
-
content
|
24
|
-
else
|
25
|
-
content.gsub(/(,|{|\[)/, "\\1\n").gsub(/(}|\])/, "\n\\1")
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
def check_required_fields
|
30
|
-
%w(container title series).each do |required_argument|
|
31
|
-
raise ArgumentError, ":#{required_argument} must be passed" if send(required_argument).blank? || (send(required_argument).is_a?(Array) && send(required_argument).length == 0)
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
def render_container
|
36
|
-
"chart: {renderTo: '#{container}'}"
|
37
|
-
end
|
38
|
-
|
39
|
-
def render_titles
|
40
|
-
%w(title subtitle).collect do |t|
|
41
|
-
"#{t}: {text: '#{send(t)}'}" if send(t).present?
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
def render_plot_options
|
46
|
-
return nil unless plot_options.present?
|
47
|
-
"plotOptions: {" +
|
48
|
-
Array.wrap(plot_options).collect {|po| PlotOptions.new(po)}.join(',') +
|
49
|
-
"}"
|
50
|
-
end
|
51
|
-
|
52
|
-
def render_axes
|
53
|
-
result = %w(x y).collect do |axis|
|
54
|
-
return nil unless send("#{axis}Axes").present?
|
55
|
-
"#{axis}Axis: [" +
|
56
|
-
Array.wrap(send("#{axis}Axes")).collect {|a| Axis.new(a)}.join(',') +
|
57
|
-
"]"
|
58
|
-
end
|
59
|
-
result.compact.join(",")
|
60
|
-
end
|
61
|
-
|
62
|
-
def render_legend
|
63
|
-
return nil unless legend.present?
|
64
|
-
Legend.new(legend)
|
65
|
-
end
|
66
|
-
|
67
|
-
def render_series
|
68
|
-
"series: [" +
|
69
|
-
Array.wrap(series).collect {|s| Series.new(s)}.join(',') +
|
70
|
-
"]"
|
71
|
-
end
|
72
|
-
|
73
|
-
def render_tooltip
|
74
|
-
return nil unless tooltip.present?
|
75
|
-
"tooltip: {" +
|
76
|
-
"formatter: function(){ return #{tooltip}; }" +
|
77
|
-
"}"
|
78
|
-
end
|
79
|
-
|
80
9
|
end
|
81
10
|
end
|
data/lib/highcharts/legend.rb
CHANGED
@@ -1,15 +1,11 @@
|
|
1
|
-
|
1
|
+
class Highcharts
|
2
2
|
class Legend < Base
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
return nil if rendered_options.length == 0
|
9
|
-
"legend: {" +
|
10
|
-
rendered_options.join(',') +
|
11
|
-
"}"
|
4
|
+
def initialize(opts = {})
|
5
|
+
@default = :enabled
|
6
|
+
@skip_quotation = :labelFormatter
|
7
|
+
super
|
12
8
|
end
|
13
9
|
|
14
10
|
end
|
15
|
-
end
|
11
|
+
end
|
@@ -1,24 +1,20 @@
|
|
1
|
-
|
1
|
+
class Highcharts
|
2
2
|
class PlotOptions < Base
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
4
|
+
def initialize(opts = {})
|
5
|
+
@suboptions = {
|
6
|
+
:area => 'PlotOptions::PlotType',
|
7
|
+
:areaspline => 'PlotOptions::PlotType',
|
8
|
+
:bar => 'PlotOptions::PlotType',
|
9
|
+
:column => 'PlotOptions::PlotType',
|
10
|
+
:line => 'PlotOptions::PlotType',
|
11
|
+
:pie => 'PlotOptions::PlotType',
|
12
|
+
:series => 'PlotOptions::PlotType',
|
13
|
+
:scatter => 'PlotOptions::PlotType',
|
14
|
+
:spline => 'PlotOptions::PlotType'
|
15
|
+
}
|
16
|
+
super
|
10
17
|
end
|
11
18
|
|
12
|
-
private
|
13
|
-
def render_data_labels
|
14
|
-
"dataLabels: {" +
|
15
|
-
"enabled: #{data_labels == false ? 'false' : 'true'}" +
|
16
|
-
"}"
|
17
|
-
end
|
18
|
-
|
19
|
-
def render_legend
|
20
|
-
"showInLegend: #{legend ? 'true' : 'false'}"
|
21
|
-
end
|
22
|
-
|
23
19
|
end
|
24
20
|
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
class Highcharts
|
2
|
+
class PlotOptions
|
3
|
+
class PlotType < Base
|
4
|
+
|
5
|
+
def initialize(opts = {})
|
6
|
+
@suboptions = {
|
7
|
+
:dataLabels => 'Labels',
|
8
|
+
:events => 'PlotOptions::PlotType::Events',
|
9
|
+
:marker => 'PlotOptions::PlotType::Marker',
|
10
|
+
:point => 'Point',
|
11
|
+
:states => 'PlotOptions::PlotType::States'
|
12
|
+
}
|
13
|
+
super
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class Highcharts
|
2
|
+
class PlotOptions
|
3
|
+
class PlotType
|
4
|
+
class Marker < Base
|
5
|
+
|
6
|
+
def initialize(opts = {})
|
7
|
+
@default = :enabled
|
8
|
+
@suboptions = {
|
9
|
+
:states => 'PlotOptions::PlotType::Marker::States'
|
10
|
+
}
|
11
|
+
super
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/lib/highcharts/series.rb
CHANGED
@@ -1,20 +1,10 @@
|
|
1
|
-
|
1
|
+
class Highcharts
|
2
2
|
class Series < Base
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
rendered_options = render_options(:strings => 'name type', :objects => 'xAxis yAxis')
|
8
|
-
return nil if rendered_options.length == 0
|
9
|
-
"{" +
|
10
|
-
[rendered_options, render_data].flatten.compact.join(',') +
|
11
|
-
"}"
|
4
|
+
def to_json
|
5
|
+
@options[:data] = options[:data].collect(&:to_f)
|
6
|
+
super
|
12
7
|
end
|
13
8
|
|
14
|
-
private
|
15
|
-
def render_data
|
16
|
-
"data: #{data.first.is_a?(Array) ? data : data.collect(&:to_f)}"
|
17
|
-
end
|
18
|
-
|
19
9
|
end
|
20
10
|
end
|
data/lib/highcharts/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: highcharts-js-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,33 +9,22 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-03-17 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
|
-
- !ruby/object:Gem::Dependency
|
15
|
-
name: actionpack
|
16
|
-
requirement: &70118123745480 !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
|
-
requirements:
|
19
|
-
- - ~>
|
20
|
-
- !ruby/object:Gem::Version
|
21
|
-
version: '3.0'
|
22
|
-
type: :runtime
|
23
|
-
prerelease: false
|
24
|
-
version_requirements: *70118123745480
|
25
14
|
- !ruby/object:Gem::Dependency
|
26
15
|
name: railties
|
27
|
-
requirement: &
|
16
|
+
requirement: &70364668687900 !ruby/object:Gem::Requirement
|
28
17
|
none: false
|
29
18
|
requirements:
|
30
19
|
- - ~>
|
31
20
|
- !ruby/object:Gem::Version
|
32
|
-
version: '3.
|
21
|
+
version: '3.1'
|
33
22
|
type: :runtime
|
34
23
|
prerelease: false
|
35
|
-
version_requirements: *
|
24
|
+
version_requirements: *70364668687900
|
36
25
|
- !ruby/object:Gem::Dependency
|
37
26
|
name: bundler
|
38
|
-
requirement: &
|
27
|
+
requirement: &70364668687480 !ruby/object:Gem::Requirement
|
39
28
|
none: false
|
40
29
|
requirements:
|
41
30
|
- - ! '>='
|
@@ -43,10 +32,10 @@ dependencies:
|
|
43
32
|
version: '0'
|
44
33
|
type: :development
|
45
34
|
prerelease: false
|
46
|
-
version_requirements: *
|
35
|
+
version_requirements: *70364668687480
|
47
36
|
- !ruby/object:Gem::Dependency
|
48
37
|
name: rake
|
49
|
-
requirement: &
|
38
|
+
requirement: &70364668687020 !ruby/object:Gem::Requirement
|
50
39
|
none: false
|
51
40
|
requirements:
|
52
41
|
- - ! '>='
|
@@ -54,10 +43,10 @@ dependencies:
|
|
54
43
|
version: '0'
|
55
44
|
type: :development
|
56
45
|
prerelease: false
|
57
|
-
version_requirements: *
|
46
|
+
version_requirements: *70364668687020
|
58
47
|
- !ruby/object:Gem::Dependency
|
59
48
|
name: rspec
|
60
|
-
requirement: &
|
49
|
+
requirement: &70364668686600 !ruby/object:Gem::Requirement
|
61
50
|
none: false
|
62
51
|
requirements:
|
63
52
|
- - ! '>='
|
@@ -65,10 +54,10 @@ dependencies:
|
|
65
54
|
version: '0'
|
66
55
|
type: :development
|
67
56
|
prerelease: false
|
68
|
-
version_requirements: *
|
57
|
+
version_requirements: *70364668686600
|
69
58
|
- !ruby/object:Gem::Dependency
|
70
59
|
name: simplecov
|
71
|
-
requirement: &
|
60
|
+
requirement: &70364668686180 !ruby/object:Gem::Requirement
|
72
61
|
none: false
|
73
62
|
requirements:
|
74
63
|
- - ! '>='
|
@@ -76,7 +65,7 @@ dependencies:
|
|
76
65
|
version: '0'
|
77
66
|
type: :development
|
78
67
|
prerelease: false
|
79
|
-
version_requirements: *
|
68
|
+
version_requirements: *70364668686180
|
80
69
|
description: Easily configure a Highcharts JS chart for use in a Rails application
|
81
70
|
email:
|
82
71
|
- agrobbin@gmail.com
|
@@ -88,18 +77,38 @@ files:
|
|
88
77
|
- .rspec
|
89
78
|
- CHANGELOG.md
|
90
79
|
- Gemfile
|
91
|
-
- LICENSE
|
92
80
|
- README.md
|
93
81
|
- Rakefile
|
94
82
|
- highcharts-js-rails.gemspec
|
83
|
+
- lib/LICENSE
|
95
84
|
- lib/highcharts-js-rails.rb
|
85
|
+
- lib/highcharts.rb
|
96
86
|
- lib/highcharts/axis.rb
|
87
|
+
- lib/highcharts/axis/events.rb
|
88
|
+
- lib/highcharts/axis/plot_bands.rb
|
89
|
+
- lib/highcharts/axis/plot_lines.rb
|
90
|
+
- lib/highcharts/axis/stack_labels.rb
|
91
|
+
- lib/highcharts/axis/x.rb
|
92
|
+
- lib/highcharts/axis/y.rb
|
97
93
|
- lib/highcharts/base.rb
|
98
94
|
- lib/highcharts/chart.rb
|
95
|
+
- lib/highcharts/color.rb
|
96
|
+
- lib/highcharts/credits.rb
|
97
|
+
- lib/highcharts/engine.rb
|
98
|
+
- lib/highcharts/labels.rb
|
99
99
|
- lib/highcharts/legend.rb
|
100
100
|
- lib/highcharts/plot_options.rb
|
101
|
-
- lib/highcharts/
|
101
|
+
- lib/highcharts/plot_options/plot_type.rb
|
102
|
+
- lib/highcharts/plot_options/plot_type/events.rb
|
103
|
+
- lib/highcharts/plot_options/plot_type/marker.rb
|
104
|
+
- lib/highcharts/plot_options/plot_type/marker/states.rb
|
105
|
+
- lib/highcharts/plot_options/plot_type/states.rb
|
106
|
+
- lib/highcharts/plot_options/plot_type/states/hover.rb
|
107
|
+
- lib/highcharts/point.rb
|
108
|
+
- lib/highcharts/point/events.rb
|
102
109
|
- lib/highcharts/series.rb
|
110
|
+
- lib/highcharts/title.rb
|
111
|
+
- lib/highcharts/tooltip.rb
|
103
112
|
- lib/highcharts/version.rb
|
104
113
|
- spec/highcharts/base_spec.rb
|
105
114
|
- spec/spec_helper.rb
|