html5jp_graphs 0.0.1

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 (40) hide show
  1. data/.gitigore +2 -0
  2. data/README.rdoc +82 -0
  3. data/Rakefile +22 -0
  4. data/html5jp_graphs.gemspec +17 -0
  5. data/lib/examples/sample_graph_circle_1.html +54 -0
  6. data/lib/examples/sample_graph_circle_2.html +61 -0
  7. data/lib/examples/sample_graph_line_1.html +44 -0
  8. data/lib/examples/sample_graph_line_2.html +53 -0
  9. data/lib/examples/sample_graph_radar_1.html +47 -0
  10. data/lib/examples/sample_graph_radar_2.html +52 -0
  11. data/lib/examples/sample_graph_vbar_1.html +51 -0
  12. data/lib/examples/sample_graph_vbar_2.html +52 -0
  13. data/lib/examples/sample_graph_vbar_3.html +52 -0
  14. data/lib/examples/sample_graph_vbar_4.html +52 -0
  15. data/lib/generators/html5jp_graphs/USAGE +8 -0
  16. data/lib/generators/html5jp_graphs/html5jp_graphs_generator.rb +12 -0
  17. data/lib/generators/html5jp_graphs/templates/excanvas/AUTHORS +10 -0
  18. data/lib/generators/html5jp_graphs/templates/excanvas/COPYING +202 -0
  19. data/lib/generators/html5jp_graphs/templates/excanvas/README +22 -0
  20. data/lib/generators/html5jp_graphs/templates/excanvas/examples/example1.html +93 -0
  21. data/lib/generators/html5jp_graphs/templates/excanvas/examples/example2.html +513 -0
  22. data/lib/generators/html5jp_graphs/templates/excanvas/examples/example3.html +284 -0
  23. data/lib/generators/html5jp_graphs/templates/excanvas/examples/ff.jpg +0 -0
  24. data/lib/generators/html5jp_graphs/templates/excanvas/excanvas-compressed.js +19 -0
  25. data/lib/generators/html5jp_graphs/templates/excanvas/excanvas.js +785 -0
  26. data/lib/generators/html5jp_graphs/templates/excanvas/testcases/arc.html +49 -0
  27. data/lib/generators/html5jp_graphs/templates/excanvas/testcases/linewidth.html +47 -0
  28. data/lib/generators/html5jp_graphs/templates/excanvas/testcases/overflow.html +37 -0
  29. data/lib/generators/html5jp_graphs/templates/excanvas/testcases/quadraticcurve.html +74 -0
  30. data/lib/generators/html5jp_graphs/templates/excanvas/testcases/resizing.html +65 -0
  31. data/lib/generators/html5jp_graphs/templates/graph/circle.js +407 -0
  32. data/lib/generators/html5jp_graphs/templates/graph/line.js +577 -0
  33. data/lib/generators/html5jp_graphs/templates/graph/radar.js +545 -0
  34. data/lib/generators/html5jp_graphs/templates/graph/vbar.js +1156 -0
  35. data/lib/html5jp_graphs.rb +1 -0
  36. data/lib/html5jp_graphs/version.rb +3 -0
  37. data/lib/html5jp_graphs_helper.rb +255 -0
  38. data/tasks/html5jp_graphs_tasks.rake +4 -0
  39. data/test/html5jp_graphs_test.rb +8 -0
  40. metadata +88 -0
@@ -0,0 +1 @@
1
+ require 'html5jp_graphs_helper'
@@ -0,0 +1,3 @@
1
+ module Html5jpGraphs
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,255 @@
1
+ # Html5jpGraphsHelper
2
+ module Html5jpGraphsHelper
3
+
4
+ # Draw a vertical bar chart using Html5jp vertical bar chart (http://www.html5.jp/library/graph_vbar.html).
5
+ #
6
+ # ex.
7
+ # <%= vertical_bar_chart [['accesses', 520, 340, 804, 602], ['clicks', 101, 76, 239, 321]] %>
8
+ #
9
+ # The first argument 'resource' can be one of the following forms.
10
+ # * ['accesses', 520, 340, 804, 602]
11
+ # * [['accesses', 520, 340, 804, 602], ['clicks', 101, 76, 239, 321]]
12
+ # * Any other object which has required methods as follows;
13
+ # * each - to supply record objects.
14
+ # * items - If :x option does not exist and your resource object has items method, it is called to get :x. You can change the method name by :items_method option.
15
+ # * scales - If :y option does not exist and your resource object has scales method, it is called to get :y. You can change the method name by :scales_method option
16
+ # * max_scale - Would be used as :yMax option if it exists.
17
+ # * The record object shoud have methods as follows.
18
+ # * label - Like 'accesses' in the example. You can use other method if you specify :label_method option.
19
+ # * values - Like [520, 340, 804, 602] in the example. You can use other method if you specify :values_method option.
20
+ # * color - If :barColors option does not exist and your record object has color method, it is used for :barColors. You can change the method name by :color_method option.
21
+ # Additionaly, the following options are available.
22
+ # * :canvas_id - element id of the canvas. Default is 'line_chart'. Note that all rader charts in one page sould have unique canvas_ids.
23
+ # * :width - The width of the canvas. Default is 400.
24
+ # * :height - The height of the canvas. Default is 300.
25
+ # * All options in http://www.html5.jp/library/graph_vbar.html. ex. 'backgroundColor' or :backgroundColor.
26
+ # And you can use html_options for the top level div.
27
+ def vertical_bar_chart(resource, options = {}, html_options = {})
28
+ options = {:width => 400, :height => 300, :canvas_id => 'vertical_bar_chart'}.merge(options.symbolize_keys)
29
+ output = canvas(options, html_options)
30
+ output << "\n"
31
+ output << vertical_bar_chart_js(resource, options)
32
+ end
33
+
34
+
35
+ # Draw a radar chart using Html5jp radar chart (http://www.html5.jp/library/graph_radar.html).
36
+ #
37
+ # ex.
38
+ # <%= radar_chart [['review1', 5, 4, 3], ['review2', 3, 5, 2]], :aCap => ['price', 'style', 'sound'] %>
39
+ #
40
+ # The first argument 'resource' can be one of the following forms.
41
+ # * ['my review', 1, 2, 3, 4]
42
+ # * [['my review', 1, 2, 3, 4], ['all reviews', 2, 3, 4, 3]]
43
+ # * Any other object which has required methods as follows;
44
+ # * each - to supply record objects.
45
+ # * items - If :aCap option does not exist and your resource object has items method, it is called to get :aCap. You can change the method name by :items_method option.
46
+ # * The record object shoud have methods as follows.
47
+ # * label - Like 'my review' in the example. You can use other method if you specify :label_method option.
48
+ # * values - Like [1, 2, 3, 4] in the example. You can use other method if you specify :values_method option.
49
+ # * color - If :faceColors option does not exist and your record object has color method, it is used for :faceColors. You can change the method name by :color_method option.
50
+ # Additionaly, the following options are available.
51
+ # * :canvas_id - element id of the canvas. Default is 'rader_chart'. Note that all rader charts in one page sould have unique canvas_ids.
52
+ # * :width - The width of the canvas. Default is 400.
53
+ # * :height - The height of the canvas. Default is 300.
54
+ # * All options in http://www.html5.jp/library/graph_radar.html. ex. 'aMax' or :aMax.
55
+ # And you can use html_options for the top level div.
56
+ def radar_chart(resource, options = {}, html_options = {})
57
+ options = {:width => 400, :height => 300, :canvas_id => 'reader_chart'}.merge(options.symbolize_keys)
58
+ output = canvas(options, html_options)
59
+ output << "\n"
60
+ output << rader_chart_js(resource, options)
61
+ end
62
+
63
+ # Draw a line chart using Html5jp radar chart (http://www.html5.jp/library/graph_line.html).
64
+ #
65
+ # ex.
66
+ # <%= line_chart [['accesses', 520, 340, 804, 602], ['clicks', 101, 76, 239, 321]] %>
67
+ #
68
+ # The first argument 'resource' can be one of the following forms.
69
+ # * ['accesses', 520, 340, 804, 602]
70
+ # * [['accesses', 520, 340, 804, 602], ['clicks', 101, 76, 239, 321]]
71
+ # * Any other object which has required methods as follows;
72
+ # * each - to supply record objects.
73
+ # * items - If :x option does not exist and your resource object has items method, it is called to get :x. You can change the method name by :items_method option.
74
+ # * scales - If :y option does not exist and your resource object has scales method, it is called to get :y. You can change the method name by :scales_method option
75
+ # * max_scale - Would be used as :yMax option if it exists.
76
+ # * min_scale - Would be used as :yMin option if it exists.
77
+ # * The record object shoud have methods as follows.
78
+ # * label - Like 'accesses' in the example. You can use other method if you specify :label_method option.
79
+ # * values - Like [520, 340, 804, 602] in the example. You can use other method if you specify :values_method option.
80
+ # Additionaly, the following options are available.
81
+ # * :canvas_id - element id of the canvas. Default is 'line_chart'. Note that all rader charts in one page sould have unique canvas_ids.
82
+ # * :width - The width of the canvas. Default is 400.
83
+ # * :height - The height of the canvas. Default is 300.
84
+ # * All options in http://www.html5.jp/library/graph_line.html. ex. 'yMax' or :yMax.
85
+ # And you can use html_options for the top level div.
86
+ def line_chart(resource, options = {}, html_options = {})
87
+ options = {:width => 400, :height => 300, :canvas_id => 'line_chart'}.merge(options.symbolize_keys)
88
+ output = canvas(options, html_options)
89
+ output << "\n"
90
+ output << line_chart_js(resource, options)
91
+ end
92
+
93
+ # Draw a pie chart using HTML5jp pie chart (http://www.html5.jp/library/graph_circle.html).
94
+ #
95
+ # ex.
96
+ # <%= pie_chart([["very good", 400], ["good", 300], ["bad", 100], ["very bad", 300]]) %>
97
+ #
98
+ # The first argument 'resource' can be one of the following forms.
99
+ # * [["very good", 400], ["good", 300], ["bad", 100], ["very bad", 300, "red"]]
100
+ # * Any other object which has required methods as follows;
101
+ # * each - to supply record objects.
102
+ # * The record object shoud have methods as follows.
103
+ # * label - Like 'very good' in the example. You can use other method if you specify :label_method option.
104
+ # * value - Like 400 in the example. You can use other method if you specify :value_method option.
105
+ # * color - Like "red" in the example. You can use other method if you specify :color_method option. This is optional.
106
+ # Additionaly, the following options are available.
107
+ # * :canvas_id - element id of the canvas. Default is 'pie_chart'. Note that all rader charts in one page sould have unique canvas_ids.
108
+ # * :width - The width of the canvas. Default is 400.
109
+ # * :height - The height of the canvas. Default is 300.
110
+ # * :sort - If true, sort the records in descending order. Default is false.
111
+ # * All options in http://www.html5.jp/library/graph_circle.html. ex. 'startAngle' or :startAngle.
112
+ # And you can use html_options for the top level div.
113
+ def pie_chart(resource, options = {}, html_options = {})
114
+ options = {:width => 400, :height => 300, :canvas_id => 'pie_chart', :sort => false}.merge(options.symbolize_keys)
115
+ output = canvas(options, html_options)
116
+ output << "\n"
117
+ output << pie_chart_js(resource, options)
118
+ end
119
+
120
+ private
121
+
122
+ def canvas(options, html_options)
123
+ content_tag(:div, content_tag(:canvas, '', :width => options[:width], :height => options[:height], :id => options[:canvas_id]), html_options)
124
+ end
125
+
126
+ def vertical_bar_chart_js(resource, options)
127
+ graph_options = options.dup.delete_if{|key, value| [:canvas_id, :width, :height, :items_method, :scales_method, :max_scale_method, :label_method, :values_method, :color_method].include?(key) || value.blank?}
128
+ options = {:items_method => :items, :scales_method => :scales, :max_scale_method => :max_scale, :label_method => :label, :values_method => :values, :color_method => :color}.merge(options)
129
+
130
+ if resource.kind_of?(Array) && resource.first.kind_of?(Array)
131
+ records = resource
132
+ elsif resource.kind_of?(Array) && resource.first.kind_of?(String)
133
+ records = [resource]
134
+ else
135
+ records = []
136
+ record_colors = []
137
+ for record in resource
138
+ records << [record.send(options[:label_method])].concat(record.send(options[:values_method]))
139
+ record_colors << record.send(options[:color_method]) if !graph_options[:barColors] && record.respond_to?(options[:color_method])
140
+ end
141
+ graph_options[:x] ||= resource.send(options[:items_method]) if resource.respond_to?(options[:items_method])
142
+ graph_options[:y] ||= resource.send(options[:scales_method]) if resource.respond_to?(options[:scales_method])
143
+ graph_options[:yMax] ||= resource.send(options[:max_scale_method]) if resource.respond_to?(options[:max_scale_method])
144
+ graph_options[:barColors] ||= record_colors unless record_colors.find{|c| c}.blank?
145
+ end
146
+
147
+ draw("vbar", options[:canvas_id], records, graph_options)
148
+ end
149
+
150
+ def rader_chart_js(resource, options)
151
+ graph_options = options.dup.delete_if{|key, value| [:canvas_id, :width, :height, :label_method, :values_method, :items_method, :color_method].include?(key) || value.blank?}
152
+ options = {:label_method => :label, :values_method => :values, :items_method => :items, :color_method => :color}.merge(options)
153
+
154
+ if resource.kind_of?(Array) && resource.first.kind_of?(Array)
155
+ records = resource
156
+ elsif resource.kind_of?(Array) && resource.first.kind_of?(String)
157
+ records = [resource]
158
+ else
159
+ records = []
160
+ record_colors = []
161
+ for record in resource
162
+ records << [record.send(options[:label_method])].concat(record.send(options[:values_method]))
163
+ record_colors << record.send(options[:color_method]) if !graph_options[:faceColors] && record.respond_to?(options[:color_method])
164
+ end
165
+ graph_options[:aCap] ||= resource.send(options[:items_method]) if resource.respond_to?(options[:items_method])
166
+ graph_options[:faceColors] ||= record_colors unless record_colors.find{|c| c}.blank?
167
+ end
168
+
169
+ draw("radar", options[:canvas_id], records, graph_options)
170
+ end
171
+
172
+ def line_chart_js(resource, options)
173
+ graph_options = options.dup.delete_if{|key, value| [:canvas_id, :width, :height, :items_method, :scales_method, :max_scale_method, :min_scale_method, :label_method, :values_method].include?(key) || value.blank?}
174
+ options = {:items_method => :items, :scales_method => :scales, :max_scale_method => :max_scale, :min_scale_method => :min_scale, :label_method => :label, :values_method => :values}.merge(options)
175
+
176
+ if resource.kind_of?(Array) && resource.first.kind_of?(Array)
177
+ records = resource
178
+ elsif resource.kind_of?(Array) && resource.first.kind_of?(String)
179
+ records = [resource]
180
+ else
181
+ records = []
182
+ for record in resource
183
+ records << [record.send(options[:label_method])].concat(record.send(options[:values_method]))
184
+ end
185
+ graph_options[:x] ||= resource.send(options[:items_method]) if resource.respond_to?(options[:items_method])
186
+ graph_options[:y] ||= resource.send(options[:scales_method]) if resource.respond_to?(options[:scales_method])
187
+ graph_options[:yMax] ||= resource.send(options[:max_scale_method]) if resource.respond_to?(options[:max_scale_method])
188
+ graph_options[:yMin] ||= resource.send(options[:min_scale_method]) if resource.respond_to?(options[:min_scale_method])
189
+ end
190
+
191
+ draw("line", options[:canvas_id], records, graph_options)
192
+ end
193
+
194
+ def pie_chart_js(resource, options)
195
+ graph_options = options.dup.delete_if{|key, value| [:canvas_id, :items, :width, :height, :label_method, :value_method, :color_method, :sort].include?(key) || value.blank?}
196
+ options = {:label_method => :label, :value_method => :value, :color_method => :color}.merge(options)
197
+
198
+ if resource.kind_of?(Array) && resource.first.kind_of?(Array)
199
+ records = resource
200
+ else
201
+ records = []
202
+ for record in resource
203
+ record_array = [record.send(options[:label_method]), record.send(options[:value_method])]
204
+ record_array << record.send(options[:color_method]) if record.respond_to?(options[:color_method])
205
+ records << record_array
206
+ end
207
+ end
208
+
209
+ records = records.sort {|r1, r2| r2[1] <=> r1[1]} if options[:sort]
210
+
211
+ draw("circle", options[:canvas_id], records, graph_options)
212
+ end
213
+
214
+
215
+ def draw(class_name, canvas_id, records, graph_options)
216
+ javascript_tag <<-EOS
217
+ Event.observe(window, 'load', function() {
218
+ var rc = new html5jp.graph.#{class_name}("#{canvas_id}");
219
+ if( ! rc ) { return; }
220
+ var records = #{to_js_value(records)};
221
+ var options = #{options_to_json(graph_options)};
222
+ rc.draw(records, options);
223
+ });
224
+ EOS
225
+ end
226
+
227
+ # Don't use to_json avoiding url encoding of local characters.
228
+ def options_to_json(options)
229
+ content = options.map{|key, value| "#{key}: #{to_js_value(value)}"}.join(",\n")
230
+ "{#{content}}"
231
+ end
232
+
233
+ def to_js_value(value)
234
+ if value.nil?
235
+ 'null'
236
+ elsif value.kind_of?(Array)
237
+ "[#{value.map{|v| array_element_to_js_value(v)}.join(', ')}]"
238
+ else
239
+ value.to_s
240
+ end
241
+ end
242
+
243
+ def array_element_to_js_value(value)
244
+ if value.nil?
245
+ 'null'
246
+ elsif value.kind_of?(Numeric) || value.to_s =~ /^-?[0-9][0-9]*[\.]?[0-9]*$/
247
+ value
248
+ elsif value.kind_of?(Array)
249
+ "[#{value.map{|v| array_element_to_js_value(v)}.join(', ')}]"
250
+ else
251
+ "\"#{value}\""
252
+ end
253
+ end
254
+
255
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :html5jp_graphs do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,8 @@
1
+ require 'test/unit'
2
+
3
+ class Html5jpGraphsTest < Test::Unit::TestCase
4
+ # Replace this with your real tests.
5
+ def test_this_plugin
6
+ flunk
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: html5jp_graphs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - nay3
9
+ - Koirhico Ohba
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2012-07-21 00:00:00.000000000 Z
14
+ dependencies: []
15
+ description: html5jp_graphs is a Rails Plugin for HTML5.jp graph libraries. It supports
16
+ vertical bar chart, radar chart, pie chart, and line chart.
17
+ email:
18
+ - y.ohba@everyleaf.com
19
+ executables: []
20
+ extensions: []
21
+ extra_rdoc_files: []
22
+ files:
23
+ - .gitigore
24
+ - README.rdoc
25
+ - Rakefile
26
+ - html5jp_graphs.gemspec
27
+ - lib/examples/sample_graph_circle_1.html
28
+ - lib/examples/sample_graph_circle_2.html
29
+ - lib/examples/sample_graph_line_1.html
30
+ - lib/examples/sample_graph_line_2.html
31
+ - lib/examples/sample_graph_radar_1.html
32
+ - lib/examples/sample_graph_radar_2.html
33
+ - lib/examples/sample_graph_vbar_1.html
34
+ - lib/examples/sample_graph_vbar_2.html
35
+ - lib/examples/sample_graph_vbar_3.html
36
+ - lib/examples/sample_graph_vbar_4.html
37
+ - lib/generators/html5jp_graphs/USAGE
38
+ - lib/generators/html5jp_graphs/html5jp_graphs_generator.rb
39
+ - lib/generators/html5jp_graphs/templates/excanvas/AUTHORS
40
+ - lib/generators/html5jp_graphs/templates/excanvas/COPYING
41
+ - lib/generators/html5jp_graphs/templates/excanvas/README
42
+ - lib/generators/html5jp_graphs/templates/excanvas/examples/example1.html
43
+ - lib/generators/html5jp_graphs/templates/excanvas/examples/example2.html
44
+ - lib/generators/html5jp_graphs/templates/excanvas/examples/example3.html
45
+ - lib/generators/html5jp_graphs/templates/excanvas/examples/ff.jpg
46
+ - lib/generators/html5jp_graphs/templates/excanvas/excanvas-compressed.js
47
+ - lib/generators/html5jp_graphs/templates/excanvas/excanvas.js
48
+ - lib/generators/html5jp_graphs/templates/excanvas/testcases/arc.html
49
+ - lib/generators/html5jp_graphs/templates/excanvas/testcases/linewidth.html
50
+ - lib/generators/html5jp_graphs/templates/excanvas/testcases/overflow.html
51
+ - lib/generators/html5jp_graphs/templates/excanvas/testcases/quadraticcurve.html
52
+ - lib/generators/html5jp_graphs/templates/excanvas/testcases/resizing.html
53
+ - lib/generators/html5jp_graphs/templates/graph/circle.js
54
+ - lib/generators/html5jp_graphs/templates/graph/line.js
55
+ - lib/generators/html5jp_graphs/templates/graph/radar.js
56
+ - lib/generators/html5jp_graphs/templates/graph/vbar.js
57
+ - lib/html5jp_graphs.rb
58
+ - lib/html5jp_graphs/version.rb
59
+ - lib/html5jp_graphs_helper.rb
60
+ - tasks/html5jp_graphs_tasks.rake
61
+ - test/html5jp_graphs_test.rb
62
+ homepage: http://github.com/nay/html5jp_graphs
63
+ licenses: []
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ! '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ! '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 1.8.17
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: Rails Plugin for HTML5.jp Graph Libraries.
86
+ test_files:
87
+ - test/html5jp_graphs_test.rb
88
+ has_rdoc: