highcharts_on_rails 0.0.1.alpha

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.rdoc +3 -0
  3. data/Rakefile +40 -0
  4. data/app/assets/javascripts/highcharts_on_rails/application.js.coffee +15 -0
  5. data/app/assets/javascripts/highcharts_on_rails/charts.coffee +6 -0
  6. data/app/assets/stylesheets/highcharts_on_rails/application.css +13 -0
  7. data/app/controllers/highcharts_on_rails/application_controller.rb +4 -0
  8. data/app/helpers/highcharts_on_rails/application_helper.rb +20 -0
  9. data/app/views/layouts/highcharts_on_rails/application.html.erb +14 -0
  10. data/config/routes.rb +2 -0
  11. data/lib/highcharts_on_rails/bar_chart.rb +39 -0
  12. data/lib/highcharts_on_rails/base_chart.rb +118 -0
  13. data/lib/highcharts_on_rails/column_chart.rb +41 -0
  14. data/lib/highcharts_on_rails/engine.rb +5 -0
  15. data/lib/highcharts_on_rails/hollow_pie_chart.rb +46 -0
  16. data/lib/highcharts_on_rails/line_chart.rb +35 -0
  17. data/lib/highcharts_on_rails/pie_chart.rb +80 -0
  18. data/lib/highcharts_on_rails/stacked_bar_chart.rb +26 -0
  19. data/lib/highcharts_on_rails/version.rb +3 -0
  20. data/lib/highcharts_on_rails.rb +11 -0
  21. data/lib/tasks/highcharts_on_rails_tasks.rake +4 -0
  22. data/test/dummy/README.rdoc +261 -0
  23. data/test/dummy/Rakefile +7 -0
  24. data/test/dummy/app/assets/javascripts/application.js +15 -0
  25. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  26. data/test/dummy/app/controllers/application_controller.rb +3 -0
  27. data/test/dummy/app/helpers/application_helper.rb +2 -0
  28. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  29. data/test/dummy/config/application.rb +59 -0
  30. data/test/dummy/config/boot.rb +10 -0
  31. data/test/dummy/config/database.yml +25 -0
  32. data/test/dummy/config/environment.rb +5 -0
  33. data/test/dummy/config/environments/development.rb +37 -0
  34. data/test/dummy/config/environments/production.rb +67 -0
  35. data/test/dummy/config/environments/test.rb +37 -0
  36. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  37. data/test/dummy/config/initializers/inflections.rb +15 -0
  38. data/test/dummy/config/initializers/mime_types.rb +5 -0
  39. data/test/dummy/config/initializers/secret_token.rb +7 -0
  40. data/test/dummy/config/initializers/session_store.rb +8 -0
  41. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  42. data/test/dummy/config/locales/en.yml +5 -0
  43. data/test/dummy/config/routes.rb +4 -0
  44. data/test/dummy/config.ru +4 -0
  45. data/test/dummy/public/404.html +26 -0
  46. data/test/dummy/public/422.html +26 -0
  47. data/test/dummy/public/500.html +25 -0
  48. data/test/dummy/public/favicon.ico +0 -0
  49. data/test/dummy/script/rails +6 -0
  50. data/test/highcharts_on_rails_test.rb +7 -0
  51. data/test/integration/navigation_test.rb +10 -0
  52. data/test/test_helper.rb +15 -0
  53. metadata +160 -0
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2013 YOURNAME
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.
data/README.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ = HighchartsOnRails
2
+
3
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,40 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'HighchartsOnRails'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+ APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
24
+ load 'rails/tasks/engine.rake'
25
+
26
+
27
+
28
+ Bundler::GemHelper.install_tasks
29
+
30
+ require 'rake/testtask'
31
+
32
+ Rake::TestTask.new(:test) do |t|
33
+ t.libs << 'lib'
34
+ t.libs << 'test'
35
+ t.pattern = 'test/**/*_test.rb'
36
+ t.verbose = false
37
+ end
38
+
39
+
40
+ task :default => :test
@@ -0,0 +1,15 @@
1
+ # This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ # listed below.
3
+ #
4
+ # Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ # or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ #
7
+ # It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ # the compiled file.
9
+ #
10
+ # WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
11
+ # GO AFTER THE REQUIRES BELOW.
12
+ #
13
+ #= require jquery
14
+ #= require jquery_ujs
15
+ #= require_tree .
@@ -0,0 +1,6 @@
1
+ window.highChart = (config, callback) -> new Highcharts.Chart(config, callback)
2
+
3
+ window.radializeColors = (colors) ->
4
+ $.map(colors, (color) ->
5
+ return { radialGradient: { cx: 0.5, cy: 0.3, r: 0.7 }, stops: [[0, color], [1, Highcharts.Color(color).brighten(-0.3).get('rgb')]] }
6
+ )
@@ -0,0 +1,13 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the top of the
9
+ * compiled file, but it's generally better to create a new file per style scope.
10
+ *
11
+ *= require_self
12
+ *= require_tree .
13
+ */
@@ -0,0 +1,4 @@
1
+ module HighchartsOnRails
2
+ class ApplicationController < ActionController::Base
3
+ end
4
+ end
@@ -0,0 +1,20 @@
1
+ module HighchartsOnRails
2
+ module ApplicationHelper
3
+ def highchart(_chart)
4
+
5
+ if Rails.env.development?
6
+ puts "--------------------------------------"
7
+ puts "highChart(#{_chart.to_json})"
8
+ end
9
+
10
+ content_tag(:div, '', :id => _chart.container) + javascript_tag(<<JAVASCRIPT
11
+ try {
12
+ window.#{_chart.container}_chart = highChart(#{_chart.to_json});
13
+ } catch(err) {
14
+ console.log(err);
15
+ }
16
+ JAVASCRIPT
17
+ )
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>HighchartsOnRails</title>
5
+ <%= stylesheet_link_tag "highcharts_on_rails/application", :media => "all" %>
6
+ <%= javascript_include_tag "highcharts_on_rails/application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
data/config/routes.rb ADDED
@@ -0,0 +1,2 @@
1
+ HighchartsOnRails::Engine.routes.draw do
2
+ end
@@ -0,0 +1,39 @@
1
+ class HighchartsOnRails::BarChart < HighchartsOnRails::BaseChart
2
+ add_json({
3
+ chart: {
4
+ type: 'bar',
5
+ marginRight: 50
6
+ },
7
+ tooltip: {
8
+ formatter: "function () { return this.series.name + ': ' + this.y + ''; }"
9
+ },
10
+ yAxis: {
11
+ max: 100,
12
+ title: {
13
+ text: ''
14
+ }
15
+ },
16
+ plotOptions: {
17
+ bar: {
18
+ dataLabels: {
19
+ enabled: true,
20
+ formatter: "function () { return this.y + '%'; }",
21
+ style: {
22
+ fontWeight: 'bold',
23
+ fontSize: '10px',
24
+ color: "#373535"
25
+ }
26
+ },
27
+ enableMouseTracking: false
28
+ }
29
+ },
30
+ legend: false,
31
+ title: false
32
+ })
33
+
34
+
35
+ def add_series(label, data, options={})
36
+ @_series ||= []
37
+ @_series.unshift({name: label, data: data})
38
+ end
39
+ end
@@ -0,0 +1,118 @@
1
+ class HighchartsOnRails::BaseChart
2
+ attr_reader :container
3
+ class_attribute :json_hash, :functions_array
4
+
5
+ def initialize(&block)
6
+ block.call(self)
7
+ end
8
+
9
+ def self.add_json(_json_hash)
10
+ self.json_hash ||= {}
11
+ self.json_hash = json_hash.deep_merge(_json_hash)
12
+ end
13
+
14
+ def self.add_function(_function_string)
15
+ self.functions_array ||= []
16
+ self.functions_array += [_function_string]
17
+ end
18
+
19
+ # Defaults
20
+ add_json({exporting: {enabled: false}, credits: {enabled: false}})
21
+
22
+ def to_json
23
+ [dump_json, dump_function].compact.join(',')
24
+ end
25
+
26
+ def add_json(_json_hash)
27
+ @_json_customizations ||= []
28
+ @_json_customizations << _json_hash
29
+ end
30
+
31
+ def add_function(_function_string)
32
+ @_functions ||= []
33
+ @_functions << _function_string
34
+ end
35
+
36
+ def add_series(label, data, options={})
37
+ raise NotImplementedError, "The look of series differ between charts, therefore this needs to be implemented in subclass"
38
+ end
39
+
40
+
41
+ #==================================
42
+ #========== JSON Helpers ==========
43
+ #==================================
44
+
45
+ def set_container(name)
46
+ @container = name
47
+ add_json({chart: {renderTo: name}})
48
+ end
49
+
50
+ def colors(colors_array)
51
+ _colors_array = preprocess_gradients(colors_array)
52
+
53
+ add_json(colors: _colors_array)
54
+ end
55
+
56
+ def text_color(color)
57
+ add_json(title: {style: {color: color}})
58
+ end
59
+
60
+ def title(text)
61
+ add_json({title: {text: text}})
62
+ end
63
+
64
+ def subtitle(text)
65
+ add_json({subtitle: {text: text}})
66
+ end
67
+
68
+ def height(pixels)
69
+ add_json({chart: {height: pixels}})
70
+ end
71
+
72
+ def width(pixels)
73
+ add_json({chart: {width: pixels}})
74
+ end
75
+
76
+ def categories(categories)
77
+ add_json({xAxis: {categories: categories}})
78
+ end
79
+
80
+
81
+ private
82
+
83
+ def preprocess_gradients(colors_array)
84
+ colors_array.map do |color_definition|
85
+ if color_definition =~ />#/
86
+ color_from, color_to = color_definition.split('>')
87
+ gradient(color_from, color_to)
88
+ else
89
+ color_definition
90
+ end
91
+ end
92
+ end
93
+
94
+ def gradient(start, stop)
95
+ {
96
+ linearGradient: {x1: 0, y1: 0, x2: 1, y2: 0},
97
+ stops: [[0, start], [1, stop]]
98
+ }
99
+ end
100
+
101
+ def dump_json
102
+ final_json_hash.to_json.gsub(/"function[^"]+"/) { |m| m.gsub('"', '') }
103
+ end
104
+
105
+ def dump_function
106
+ "function (chart) { #{(Array.wrap(self.class.functions_array) + Array.wrap(@_functions)).compact.join("\n")} }"
107
+ end
108
+
109
+ def final_json_hash
110
+ add_json({series: @_series})
111
+
112
+ result = self.class.json_hash.deep_dup
113
+ @_json_customizations.each do |_json_hash|
114
+ result = result.deep_merge(_json_hash)
115
+ end
116
+ result
117
+ end
118
+ end
@@ -0,0 +1,41 @@
1
+ class HighchartsOnRails::ColumnChart < HighchartsOnRails::BaseChart
2
+ add_json({
3
+ chart: {
4
+ type: 'column',
5
+ marginRight: 50
6
+ },
7
+ tooltip: {
8
+ formatter: "function () { return this.series.name + ': ' + this.y + ''; }"
9
+ },
10
+ yAxis: {
11
+ max: 100,
12
+ title: {
13
+ text: ''
14
+ }
15
+ },
16
+ plotOptions: {
17
+ line: {
18
+ dataLabels: {
19
+ enabled: true,
20
+ formatter: "function () { return this.y + '%'; }",
21
+ style: {
22
+ fontWeight: 'bold',
23
+ fontSize: '10px',
24
+ color: "#373535"
25
+ }
26
+ },
27
+ enableMouseTracking: false
28
+ }
29
+ },
30
+ legend: {
31
+ enabled: false
32
+ },
33
+ title: false
34
+ })
35
+
36
+
37
+ def add_series(label, data, options={})
38
+ @_series ||= []
39
+ @_series += [{name: label, data: data}]
40
+ end
41
+ end
@@ -0,0 +1,5 @@
1
+ module HighchartsOnRails
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace HighchartsOnRails
4
+ end
5
+ end
@@ -0,0 +1,46 @@
1
+ class HighchartsOnRails::HollowPieChart < HighchartsOnRails::PieChart
2
+ add_json({
3
+ plotOptions: {
4
+ pie: {
5
+ size: "80%",
6
+ innerSize: "60%",
7
+ dataLabels: {
8
+ enabled: false
9
+ }
10
+ },
11
+
12
+ }
13
+ })
14
+
15
+ #============== Helpers ===============
16
+
17
+ def with_labels!
18
+ super
19
+ add_json({plotOptions: {pie: {size: "60%", innerSize: "40%"}}})
20
+ end
21
+
22
+ def centered_percentage_label(color)
23
+ centered_label("Math.round(chart.series[0].data[0].percentage) + '%'", color)
24
+ end
25
+
26
+ def centered_count_label(color)
27
+ centered_label("Math.round(chart.series[0].data[0].y)", color)
28
+ end
29
+
30
+ def centered_label(formula, color)
31
+ add_function <<END_OF_FORMULA
32
+ var x = chart.chartWidth / 2,
33
+ y = chart.chartHeight / 2;
34
+
35
+ // Render the text
36
+ chart.renderer.text(#{formula}, x, y + 5).css({
37
+ color: '#{color}',
38
+ fontSize: '18px',
39
+ fontWeight: 'bold'
40
+ }).attr({
41
+ align: 'center',
42
+ zIndex: 0
43
+ }).add();
44
+ END_OF_FORMULA
45
+ end
46
+ end
@@ -0,0 +1,35 @@
1
+ class HighchartsOnRails::LineChart < HighchartsOnRails::BaseChart
2
+ add_json({
3
+ chart: {
4
+ type: 'line'
5
+ },
6
+ tooltip: {
7
+ enabled: false,
8
+ formatter: "function() { return '<b>'+ this.series.name +'</b><br/>'+ this.x +': '+ this.y; }"
9
+ },
10
+ yAxis: {
11
+ min: 0,
12
+ title: {
13
+ text: ''
14
+ }
15
+ },
16
+ plotOptions: {
17
+ line: {
18
+ dataLabels: {
19
+ enabled: true
20
+ },
21
+ enableMouseTracking: false
22
+ }
23
+ },
24
+ })
25
+
26
+
27
+ def add_series(label, data, options={})
28
+ @_series ||= []
29
+ @_series += [{name: label, data: data}]
30
+ end
31
+
32
+ def color(color)
33
+ add_json({plotOptions: {series: {color: color}}})
34
+ end
35
+ end
@@ -0,0 +1,80 @@
1
+ class HighchartsOnRails::PieChart < HighchartsOnRails::BaseChart
2
+ add_json({
3
+ title: {
4
+ style: {
5
+ color: '#726E6C',
6
+ fontWeight: 'bold'
7
+ },
8
+ text: "",
9
+ floating: true
10
+ },
11
+ xAxis: {
12
+ categories: nil
13
+ },
14
+ chart: {
15
+ type: "pie",
16
+ borderWidth: false,
17
+ borderColor: false,
18
+ },
19
+ plotOptions: {
20
+ pie: {
21
+ size: "80%",
22
+ dataLabels: {
23
+ enabled: false
24
+ }
25
+ },
26
+ series: {
27
+ shadow: false
28
+ }
29
+ },
30
+ tooltip: {
31
+ formatter: "function () {return this.point.name + ':</b> <b>' + this.y + '</b>';}"
32
+ }
33
+ })
34
+
35
+
36
+ def add_series(label, data, options={})
37
+ labels = Array.wrap(label)
38
+ @_series ||= []
39
+ @_series.unshift({name: '', data: construct_series_data(data, labels)})
40
+ end
41
+
42
+ #We need this fancy hash for pie charts :/
43
+ def construct_series_data(data, labels)
44
+ colors = Array.new(data.size) { get_color }
45
+
46
+ if (lsize = labels.size) < data.size
47
+ labels = labels.fill(nil, lsize, data.size-lsize)
48
+ end
49
+
50
+ [data, labels, colors].transpose.map do |y, name, color|
51
+ {'color' => color, 'y' => y, 'name' => name}
52
+ end
53
+ end
54
+
55
+ #============== Helpers ===============
56
+
57
+ def with_labels!
58
+ add_json({plotOptions: {pie: {size: "90%", dataLabels: {enabled: true,
59
+ formatter: "function() { return '<b>'+ this.point.name +'</b>: '+ Math.round(this.percentage) +' %'; }",
60
+ distance: 5,
61
+ softConnector: false}}}})
62
+ end
63
+
64
+
65
+ def colors(_colors)
66
+ @colors = _colors
67
+ end
68
+
69
+ private
70
+
71
+ def get_colors
72
+ @colors ||= %w(#FF0000 #FFFFFF)
73
+ end
74
+
75
+ def get_color
76
+ color = get_colors.first
77
+ @colors.rotate!
78
+ color
79
+ end
80
+ end
@@ -0,0 +1,26 @@
1
+ class HighchartsOnRails::StackedBarChart < HighchartsOnRails::BarChart
2
+ add_json({
3
+ plotOptions: {
4
+ series: {
5
+ stacking: 'percent'
6
+ },
7
+ bar: {
8
+ dataLabels: {
9
+ formatter: "function() { return this.y > 0 ? (Math.round(this.percentage) +'%') : ''; }"
10
+ }
11
+ }
12
+ },
13
+ legend: {
14
+ borderColor: false,
15
+ backgroundColor: false,
16
+ verticalAlign: 'top',
17
+ reversed: true,
18
+ enabled: true
19
+ }
20
+ })
21
+
22
+ def title(text)
23
+ super
24
+ add_json({legend: {y: 20}, chart: {marginTop: 60}})
25
+ end
26
+ end
@@ -0,0 +1,3 @@
1
+ module HighchartsOnRails
2
+ VERSION = "0.0.1.alpha"
3
+ end
@@ -0,0 +1,11 @@
1
+ require "highcharts_on_rails/engine"
2
+ require "highcharts_on_rails/base_chart"
3
+ require "highcharts_on_rails/bar_chart"
4
+ require "highcharts_on_rails/column_chart"
5
+ require "highcharts_on_rails/pie_chart"
6
+ require "highcharts_on_rails/hollow_pie_chart"
7
+ require "highcharts_on_rails/line_chart"
8
+ require "highcharts_on_rails/stacked_bar_chart"
9
+
10
+ module HighchartsOnRails
11
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :highcharts_on_rails do
3
+ # # Task goes here
4
+ # end