google_visualr 0.0.1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. data/.gitignore +3 -0
  2. data/.rspec +2 -0
  3. data/.rvmrc +1 -0
  4. data/Gemfile +4 -0
  5. data/Gemfile.lock +25 -0
  6. data/MIT-LICENSE +15 -14
  7. data/README.rdoc +59 -48
  8. data/Rakefile +9 -30
  9. data/google_visualr.gemspec +24 -0
  10. data/lib/google_visualr.rb +39 -0
  11. data/lib/google_visualr/base_chart.rb +40 -0
  12. data/lib/google_visualr/data_table.rb +277 -0
  13. data/lib/google_visualr/formatters.rb +78 -0
  14. data/lib/google_visualr/image/spark_line.rb +13 -0
  15. data/lib/google_visualr/interactive/annotated_time_line.rb +11 -0
  16. data/lib/google_visualr/interactive/area_chart.rb +13 -0
  17. data/lib/google_visualr/interactive/bar_chart.rb +13 -0
  18. data/lib/google_visualr/interactive/candlestick_chart.rb +13 -0
  19. data/lib/google_visualr/interactive/column_chart.rb +13 -0
  20. data/lib/google_visualr/interactive/combo_chart.rb +13 -0
  21. data/lib/google_visualr/interactive/gauge.rb +11 -0
  22. data/lib/google_visualr/interactive/geo_chart.rb +11 -0
  23. data/lib/google_visualr/interactive/intensity_map.rb +11 -0
  24. data/lib/google_visualr/interactive/line_chart.rb +13 -0
  25. data/lib/google_visualr/interactive/map.rb +11 -0
  26. data/lib/google_visualr/interactive/motion_chart.rb +11 -0
  27. data/lib/google_visualr/interactive/org_chart.rb +11 -0
  28. data/lib/google_visualr/interactive/pie_chart.rb +13 -0
  29. data/lib/google_visualr/interactive/scatter_chart.rb +13 -0
  30. data/lib/google_visualr/interactive/table.rb +11 -0
  31. data/lib/google_visualr/interactive/treemap.rb +11 -0
  32. data/lib/google_visualr/packages.rb +30 -0
  33. data/lib/google_visualr/param_helpers.rb +50 -0
  34. data/lib/google_visualr/version.rb +3 -0
  35. data/spec/google_visualr/base_chart_spec.rb +55 -0
  36. data/spec/google_visualr/data_table_spec.rb +251 -0
  37. data/spec/google_visualr/formatters_spec.rb +104 -0
  38. data/spec/spec_helper.rb +8 -0
  39. data/test/google_visualr_spec.rb +7 -0
  40. data/test/helper.rb +18 -0
  41. metadata +73 -36
  42. data/VERSION +0 -1
  43. data/init.rb +0 -17
  44. data/install.rb +0 -1
  45. data/lib/annotated_time_line.rb +0 -50
  46. data/lib/area_chart.rb +0 -54
  47. data/lib/bar_chart.rb +0 -53
  48. data/lib/base_chart.rb +0 -285
  49. data/lib/column_chart.rb +0 -53
  50. data/lib/formatters.rb +0 -184
  51. data/lib/gauge.rb +0 -36
  52. data/lib/geo_map.rb +0 -32
  53. data/lib/image_spark_line.rb +0 -36
  54. data/lib/intensity_map.rb +0 -29
  55. data/lib/line_chart.rb +0 -54
  56. data/lib/map.rb +0 -31
  57. data/lib/motion_chart.rb +0 -36
  58. data/lib/org_chart.rb +0 -29
  59. data/lib/pie_chart.rb +0 -44
  60. data/lib/scatter_chart.rb +0 -52
  61. data/lib/table.rb +0 -39
  62. data/uninstall.rb +0 -1
@@ -0,0 +1,78 @@
1
+ module GoogleVisualr
2
+
3
+ # http://code.google.com/apis/chart/interactive/docs/reference.html#formatters
4
+ class Formatter
5
+ include GoogleVisualr::ParamHelpers
6
+
7
+ def initialize(options={})
8
+ @options = options
9
+ end
10
+
11
+
12
+ def columns(*columns)
13
+ @columns = columns.flatten
14
+ end
15
+
16
+ def options(*options)
17
+ @options = stringify_keys!(options.pop)
18
+ end
19
+
20
+ def to_js(&block)
21
+ js = "\nvar formatter = new google.visualization.#{self.class.to_s.split('::').last}("
22
+ js << js_parameters(@options)
23
+ js << ");"
24
+
25
+ yield js if block_given?
26
+
27
+ @columns.each do |column|
28
+ js << "\nformatter.format(data_table, #{column});"
29
+ end
30
+
31
+ js
32
+ end
33
+
34
+ end
35
+
36
+ class ArrowFormat < Formatter
37
+ end
38
+
39
+ class BarFormat < Formatter
40
+ end
41
+
42
+ class ColorFormat < Formatter
43
+
44
+ attr_accessor :ranges
45
+ attr_accessor :gradient_ranges
46
+
47
+ def initialize
48
+ @ranges = Array.new
49
+ @gradient_ranges = Array.new
50
+ end
51
+
52
+ def add_range(from, to, color, bgcolor)
53
+ @ranges << { :from => from, :to => to, :color => color, :bgcolor => bgcolor }
54
+ end
55
+
56
+ def add_gradient_range(from, to, color, fromBgColor, toBgColor)
57
+ @gradient_ranges << { :from => from, :to => to, :color => color, :fromBgColor => fromBgColor, :toBgColor => toBgColor }
58
+ end
59
+
60
+ def to_js
61
+ super do |js|
62
+ @ranges.each do |r|
63
+ js << "\nformatter.addRange(#{typecast(r[:from])}, #{typecast(r[:to])}, '#{r[:color]}', '#{r[:bgcolor]}');"
64
+ end
65
+ @gradient_ranges.each do |r|
66
+ js << "\nformatter.addGradientRange(#{typecast(r[:from])}, #{typecast(r[:to])}, '#{r[:color]}', '#{r[:fromBgColor]}', '#{r[:toBgColor]}');"
67
+ end
68
+ end
69
+ end
70
+ end
71
+
72
+ class DateFormat < Formatter
73
+ end
74
+
75
+ class NumberFormat < Formatter
76
+ end
77
+
78
+ end
@@ -0,0 +1,13 @@
1
+ module GoogleVisualr
2
+ module Image
3
+
4
+ # http://code.google.com/apis/chart/interactive/docs/gallery/imagesparkline.html
5
+ class SparkLine < BaseChart
6
+ include GoogleVisualr::Packages::ImageChart
7
+
8
+ # For Configuration Options, please refer to:
9
+ # http://code.google.com/apis/chart/interactive/docs/gallery/imagesparkline.html#Configuration_Options
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ module GoogleVisualr
2
+ module Interactive
3
+
4
+ # http://code.google.com/apis/chart/interactive/docs/gallery/annotatedtimeline.html
5
+ class AnnotatedTimeLine < BaseChart
6
+ # For Configuration Options, please refer to:
7
+ # http://code.google.com/apis/chart/interactive/docs/gallery/annotatedtimeline.html#Configuration_Options
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,13 @@
1
+ module GoogleVisualr
2
+ module Interactive
3
+
4
+ # http://code.google.com/apis/chart/interactive/docs/gallery/areachart.html
5
+ class AreaChart < BaseChart
6
+ include GoogleVisualr::Packages::CoreChart
7
+
8
+ # For Configuration Options, please refer to:
9
+ # http://code.google.com/apis/chart/interactive/docs/gallery/areachart.html#Configuration_Options
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module GoogleVisualr
2
+ module Interactive
3
+
4
+ # http://code.google.com/apis/chart/interactive/docs/gallery/barchart.html
5
+ class BarChart < BaseChart
6
+ include GoogleVisualr::Packages::CoreChart
7
+
8
+ # For Configuration Options, please refer to:
9
+ # http://code.google.com/apis/chart/interactive/docs/gallery/barchart.html#Configuration_Options
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module GoogleVisualr
2
+ module Interactive
3
+
4
+ # http://code.google.com/apis/chart/interactive/docs/gallery/candlestickchart.html
5
+ class CandlestickChart < BaseChart
6
+ include GoogleVisualr::Packages::CoreChart
7
+
8
+ # For Configuration Options, please refer to:
9
+ # http://code.google.com/apis/chart/interactive/docs/gallery/candlestickchart.html#Configuration_Options
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module GoogleVisualr
2
+ module Interactive
3
+
4
+ # http://code.google.com/apis/chart/interactive/docs/gallery/columnchart.html
5
+ class ColumnChart < BaseChart
6
+ include GoogleVisualr::Packages::CoreChart
7
+
8
+ # For Configuration Options, please refer to:
9
+ # http://code.google.com/apis/chart/interactive/docs/gallery/columnchart.html#Configuration_Options
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module GoogleVisualr
2
+ module Interactive
3
+
4
+ # http://code.google.com/apis/chart/interactive/docs/gallery/combochart.html
5
+ class ComboChart < BaseChart
6
+ include GoogleVisualr::Packages::CoreChart
7
+
8
+ # For Configuration Options, please refer to:
9
+ # http://code.google.com/apis/chart/interactive/docs/gallery/combochart.html#Configuration_Options
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ module GoogleVisualr
2
+ module Interactive
3
+
4
+ # http://code.google.com/apis/chart/interactive/docs/gallery/gauge.html
5
+ class Gauge < BaseChart
6
+ # For Configuration Options, please refer to:
7
+ # http://code.google.com/apis/chart/interactive/docs/gallery/gauge.html#Configuration_Options
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module GoogleVisualr
2
+ module Interactive
3
+
4
+ # http://code.google.com/apis/chart/interactive/docs/gallery/geochart.html
5
+ class GeoChart < BaseChart
6
+ # For Configuration Options, please refer to:
7
+ # http://code.google.com/apis/chart/interactive/docs/gallery/geochart.html#Configuration_Options
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module GoogleVisualr
2
+ module Interactive
3
+
4
+ # http://code.google.com/apis/chart/interactive/docs/gallery/intensitymap.html
5
+ class IntensityMap < BaseChart
6
+ # For Configuration Options, please refer to:
7
+ # http://code.google.com/apis/chart/interactive/docs/gallery/intensitymap.html#Configuration_Options
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,13 @@
1
+ module GoogleVisualr
2
+ module Interactive
3
+
4
+ # http://code.google.com/apis/chart/interactive/docs/gallery/linechart.html
5
+ class LineChart < BaseChart
6
+ include GoogleVisualr::Packages::CoreChart
7
+
8
+ # For Configuration Options, please refer to:
9
+ # http://code.google.com/apis/chart/interactive/docs/gallery/linechart.html#Configuration_Options
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ module GoogleVisualr
2
+ module Interactive
3
+
4
+ # http://code.google.com/apis/chart/interactive/docs/gallery/map.html
5
+ class Map < BaseChart
6
+ # For Configuration Options, please refer to:
7
+ # http://code.google.com/apis/chart/interactive/docs/gallery/map.html#Configuration_Options
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module GoogleVisualr
2
+ module Interactive
3
+
4
+ # http://code.google.com/apis/chart/interactive/docs/gallery/motionchart.html
5
+ class MotionChart < BaseChart
6
+ # For Configuration Options, please refer to:
7
+ # http://code.google.com/apis/chart/interactive/docs/gallery/motionchart.html#Configuration_Options
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module GoogleVisualr
2
+ module Interactive
3
+
4
+ # http://code.google.com/apis/chart/interactive/docs/gallery/orgchart.html
5
+ class OrgChart < BaseChart
6
+ # For Configuration Options, please refer to:
7
+ # http://code.google.com/apis/chart/interactive/docs/gallery/orgchart.html#Configuration_Options
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,13 @@
1
+ module GoogleVisualr
2
+ module Interactive
3
+
4
+ # http://code.google.com/apis/chart/interactive/docs/gallery/piechart.html
5
+ class PieChart < BaseChart
6
+ include GoogleVisualr::Packages::CoreChart
7
+
8
+ # For Configuration Options, please refer to:
9
+ # http://code.google.com/apis/chart/interactive/docs/gallery/piechart.html#Configuration_Options
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module GoogleVisualr
2
+ module Interactive
3
+
4
+ # http://code.google.com/apis/chart/interactive/docs/gallery/scatterchart.html
5
+ class ScatterChart < BaseChart
6
+ include GoogleVisualr::Packages::CoreChart
7
+
8
+ # For Configuration Options, please refer to:
9
+ # http://code.google.com/apis/chart/interactive/docs/gallery/scatterchart.html#Configuration_Options
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ module GoogleVisualr
2
+ module Interactive
3
+
4
+ # http://code.google.com/apis/chart/interactive/docs/gallery/table.html
5
+ class Table < BaseChart
6
+ # For Configuration Options, please refer to:
7
+ # http://code.google.com/apis/chart/interactive/docs/gallery/table.html#Configuration_Options
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module GoogleVisualr
2
+ module Interactive
3
+
4
+ # http://code.google.com/apis/chart/interactive/docs/gallery/treemap.html
5
+ class Treemap < BaseChart
6
+ # For Configuration Options, please refer to:
7
+ # http://code.google.com/apis/chart/interactive/docs/gallery/treemap.html#Configuration_Options
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,30 @@
1
+ module GoogleVisualr
2
+
3
+ module Packages
4
+
5
+ def package_name
6
+ self.class.to_s.split("::").last.downcase
7
+ end
8
+
9
+ def class_name
10
+ self.class.to_s.split('::').last
11
+ end
12
+
13
+ module CoreChart
14
+ def package_name
15
+ "corechart"
16
+ end
17
+ end
18
+
19
+ module ImageChart
20
+ def package_name
21
+ "image#{self.class.to_s.split("::").last.downcase}"
22
+ end
23
+ def class_name
24
+ "Image#{self.class.to_s.split('::').last}"
25
+ end
26
+ end
27
+
28
+ end
29
+
30
+ end
@@ -0,0 +1,50 @@
1
+ module GoogleVisualr
2
+
3
+ module ParamHelpers
4
+
5
+ def stringify_keys!(options)
6
+ options.keys.each do |key|
7
+ options[key.to_s] = options.delete(key)
8
+ end
9
+ options
10
+ end
11
+
12
+ def js_parameters(options)
13
+ return "" if options.nil?
14
+
15
+ attributes = options.collect { |(key, value)| "#{key}: #{typecast(value)}" }
16
+ "{" + attributes.join(", ") + "}"
17
+ end
18
+
19
+ # If the column type is 'string' , the value should be a string.
20
+ # If the column type is 'number' , the value should be a number.
21
+ # If the column type is 'boolean' , the value should be a boolean.
22
+ # If the column type is 'date' , the value should be a Date object.
23
+ # If the column type is 'datetime' , the value should be a DateTime or Time object.
24
+ # If the column type is 'timeofday' , the value should be an array of three or four numbers: [hour, minute, second, optional milliseconds].
25
+ # Returns 'null' when value is nil.
26
+ # Recursive typecasting when value is a hash.
27
+ def typecast(value)
28
+ case
29
+ when value.is_a?(String)
30
+ return "'#{value.gsub(/[']/, '\\\\\'')}'"
31
+ when value.is_a?(Integer) || value.is_a?(Float)
32
+ return value
33
+ when value.is_a?(TrueClass) || value.is_a?(FalseClass)
34
+ return "#{value}"
35
+ when value.is_a?(Date)
36
+ return "new Date(#{value.year}, #{value.month-1}, #{value.day})"
37
+ when value.is_a?(DateTime) || value.is_a?(Time)
38
+ return "new Date(#{value.year}, #{value.month-1}, #{value.day}, #{value.hour}, #{value.min}, #{value.sec})"
39
+ when value.nil?
40
+ return "null"
41
+ when value.is_a?(Hash)
42
+ return js_parameters(value)
43
+ else
44
+ return value
45
+ end
46
+ end
47
+
48
+ end
49
+
50
+ end
@@ -0,0 +1,3 @@
1
+ module GoogleVisualr
2
+ VERSION = "2.0.0"
3
+ end
@@ -0,0 +1,55 @@
1
+ require 'spec_helper'
2
+
3
+ describe GoogleVisualr::BaseChart do
4
+
5
+ def data_table
6
+
7
+ @cols = [
8
+ { :type => "string", :label => "Year" },
9
+ { :type => "number", :label => "Sales" },
10
+ { :type => "number", :label => "Expenses" }
11
+ ]
12
+ @rows = [
13
+ { :c => [ {:v => "2004"}, {:v => 1000}, {:v => 400} ] },
14
+ { :c => [ {:v => "2005"}, {:v => 1200}, {:v => 450} ] },
15
+ { :c => [ {:v => "2006"}, {:v => 1500}, {:v => 600} ] },
16
+ { :c => [ {:v => "2007"}, {:v => 800 }, {:v => 500} ] }
17
+ ]
18
+ GoogleVisualr::DataTable.new(:cols => @cols, :rows => @rows)
19
+
20
+ end
21
+
22
+ before do
23
+ @dt = data_table
24
+ @chart = GoogleVisualr::BaseChart.new( @dt, { :legend => "Test Chart", :width => 800, :is3D => true } )
25
+ end
26
+
27
+ describe "#initialize" do
28
+ it "works" do
29
+ @chart.data_table.should == @dt
30
+ @chart.options.should == { "legend" => "Test Chart", "width" => 800, "is3D" => true }
31
+ end
32
+ end
33
+
34
+ describe "#options=" do
35
+ it "works" do
36
+ @chart.options = { :legend => "Awesome Chart", :width => 1000, :is3D => false }
37
+ @chart.options.should == { "legend" => "Awesome Chart", "width" => 1000, "is3D" => false }
38
+ end
39
+ end
40
+
41
+ describe "#to_js" do
42
+ it "generates JS" do
43
+ js = @chart.to_js("#body")
44
+ js.should_not be_nil
45
+ js.should match /DataTable/i
46
+ js.should match /addColumn/i
47
+ js.should match /addRow/i
48
+ js.should match /legend/i
49
+ js.should match /Test Chart/i
50
+ js.should match /width/i
51
+ js.should match /800/i
52
+ end
53
+ end
54
+
55
+ end