garru-g_viz 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.
data/CHANGELOG ADDED
@@ -0,0 +1 @@
1
+ v0.0.1 making this a gem
data/Manifest ADDED
@@ -0,0 +1,10 @@
1
+ CHANGELOG
2
+ lib/g_viz/g_viz.rb
3
+ lib/g_viz/templates/visualization.erb
4
+ Manifest
5
+ Rakefile
6
+ README
7
+ spec/g_viz/fixtures/annotated_timeline.rb
8
+ spec/g_viz/g_viz_spec.rb
9
+ spec/g_viz/templates/rendered_annotated_timeline.html
10
+ spec/test_helper.rb
data/README ADDED
File without changes
data/Rakefile ADDED
@@ -0,0 +1,19 @@
1
+ require 'echoe'
2
+ require 'rake'
3
+ require 'spec/rake/spectask'
4
+
5
+ Echoe.new('g_viz') do |p|
6
+ p.project = 'g_viz'
7
+ p.author = 'Gary Tsang'
8
+ p.summary = ' Ruby Scribe Client. Package and Wrapper for generated ruby interfaces'
9
+ p.url = 'http://github.com/garru/GViz'
10
+ p.development_dependencies = []
11
+ end
12
+
13
+
14
+ desc "Run all tests"
15
+ Spec::Rake::SpecTask.new do |t|
16
+ t.libs = [File.join(File.dirname(__FILE__), 'lib', 'g_viz')]
17
+ t.fail_on_error = false
18
+ t.spec_files = FileList['spec/*.rb']
19
+ end
data/g_viz.gemspec ADDED
@@ -0,0 +1,31 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{g_viz}
5
+ s.version = "0.0.1"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Gary Tsang"]
9
+ s.date = %q{2009-05-04}
10
+ s.description = %q{Ruby Scribe Client. Package and Wrapper for generated ruby interfaces}
11
+ s.email = %q{}
12
+ s.extra_rdoc_files = ["CHANGELOG", "lib/g_viz/g_viz.rb", "lib/g_viz/templates/visualization.erb", "README"]
13
+ s.files = ["CHANGELOG", "lib/g_viz/g_viz.rb", "lib/g_viz/templates/visualization.erb", "Manifest", "Rakefile", "README", "spec/g_viz/fixtures/annotated_timeline.rb", "spec/g_viz/g_viz_spec.rb", "spec/g_viz/templates/rendered_annotated_timeline.html", "spec/test_helper.rb", "g_viz.gemspec"]
14
+ s.has_rdoc = true
15
+ s.homepage = %q{http://github.com/garru/GViz}
16
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "G_viz", "--main", "README"]
17
+ s.require_paths = ["lib"]
18
+ s.rubyforge_project = %q{g_viz}
19
+ s.rubygems_version = %q{1.3.1}
20
+ s.summary = %q{Ruby Scribe Client. Package and Wrapper for generated ruby interfaces}
21
+
22
+ if s.respond_to? :specification_version then
23
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
24
+ s.specification_version = 2
25
+
26
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
27
+ else
28
+ end
29
+ else
30
+ end
31
+ end
@@ -0,0 +1,148 @@
1
+ require 'set'
2
+ require 'date'
3
+ require 'erb'
4
+ require 'json'
5
+ class GViz
6
+ @@viz_package_names = Set.new
7
+ @@config = {}
8
+ @@graphs = []
9
+ @@template_name = File.join(File.dirname(__FILE__), 'templates', 'visualization.erb')
10
+ class << self
11
+
12
+ # <script type='text/javascript' src='http://www.google.com/jsapi<%= @api_key %>'></script>
13
+ # <script type='text/javascript'>
14
+ # google.load('visualization', '1', {'packages':[<%= @@viz_package_names.map{|p|p.downcase}.join(',') %>]});
15
+ # google.setOnLoadCallback(drawChart);
16
+ # function drawChart() {
17
+ # <% @graphs.each do |graph| %>
18
+ # var <%= graph.data_name%> = new google.visualization.DataTable();
19
+ #
20
+ # <% graph.columns.each do |column| %>
21
+ # <%= graph.data_name%>.addColumn('<%= column[0] %>', '<%= column[1] %>')
22
+ # <% end %>
23
+ #
24
+ # <%= graph.data_name%>.addRows(<%= graph.size %>)
25
+ #
26
+ # <% graph.rows.each_with_index do |row, i| %>
27
+ # <% row.each_with_index do |value, j| %>
28
+ # <%= graph.data_name%>.setValue(<%= i %>, <%= j %>, value)
29
+ # <% end %>
30
+ # <% end %>
31
+ # <% end %>
32
+ # var chart_<%= graph.data_name%> = new google.visualization.<%=graph.chart_type%>(document.getElementById('<%=graph.chart_id%>'));
33
+ # chart.draw(data, <%=graph.options%>);
34
+ # }
35
+ # </script>
36
+
37
+ def output
38
+ b = binding
39
+ rhtml = ERB.new(IO.read(@@template_name), 0, "-")
40
+ @output = rhtml.result(b)
41
+ return @output
42
+ end
43
+
44
+ def add_graph(type, data, mapping, options= {})
45
+ @@viz_package_names.add(type)
46
+ new_graph = new(type, data, mapping, @@graphs.size, options)
47
+ @@graphs << new_graph
48
+ return new_graph.chart_id
49
+ end
50
+
51
+ def google_data_type(value)
52
+ if value.is_a?(Numeric)
53
+ data_type = 'numeric'
54
+ elsif value.is_a?(Date)
55
+ data_type = 'date'
56
+ elsif value.is_a?(DateTime) || value.is_a?(Time)
57
+ data_type = 'datetime'
58
+ elsif value.respond_to?(:to_s)
59
+ if !value.match(/[^0-9.]/)
60
+ data_type = 'numeric'
61
+ else
62
+ if (a = Date._parse(value)).size >= 3
63
+ if a.size >= 5
64
+ data_type = 'datetime'
65
+ else
66
+ data_type = 'date'
67
+ end
68
+ else
69
+ data_type = 'string'
70
+ end
71
+ end
72
+ end
73
+ data_type
74
+ end
75
+
76
+ def ruby_to_js(type, value)
77
+ return nil if value.nil?
78
+ if type == 'string'
79
+ value = "'#{value}'"
80
+ elsif type == 'date'
81
+ temp_d = Date.parse(value.to_s)
82
+ value = "new Date(#{temp_d.year}, #{temp_d.month - 1}, #{temp_d.day})"
83
+ elsif type == 'datetime'
84
+ temp_d DateTime.parse(value.to_s)
85
+ value = "new Date(#{temp_d.year}, #{temp_d.month - 1}, #{temp_d.day}, #{temp_d.hour}, #{temp_d.min}, #{temp_d.sec})"
86
+ end
87
+ return value
88
+ end
89
+ end
90
+
91
+ attr_reader :type, :options
92
+
93
+ def initialize(type, data, map, id = 0, options = {})
94
+ @type = type
95
+ @map = map
96
+ @data = data
97
+ @data_type = {}
98
+ @options = options
99
+ @id = id
100
+ import_data_types
101
+ end
102
+
103
+ def import_data_types
104
+ not_all_data_types_found = true
105
+ find_data_types = @map.dup
106
+
107
+ @data.each do |x|
108
+ break if find_data_types.size == 0
109
+ find_data_types.each do |k, v|
110
+ if x.key?(k)
111
+ @data_type[k] = self.class.google_data_type(x[k])
112
+ find_data_types.delete([k,v])
113
+ end
114
+ end
115
+ end
116
+
117
+ # if there is no data that responds to a mapped value, remove that mapping
118
+ @map -= find_data_types
119
+
120
+ end
121
+
122
+
123
+ def columns
124
+ data = @map.map do |k, v|
125
+ [@data_type[k], v]
126
+ end
127
+ end
128
+
129
+ def size
130
+ @data.size
131
+ end
132
+
133
+ def rows
134
+ @data.map do |value|
135
+ @map.map do |k, v|
136
+ self.class.ruby_to_js(@data_type[k], value[k])
137
+ end
138
+ end
139
+ end
140
+
141
+ def chart_id
142
+ return @type.downcase + @id.to_s
143
+ end
144
+
145
+ def data_name
146
+ "data_#{@id}"
147
+ end
148
+ end
@@ -0,0 +1,24 @@
1
+ <script type='text/javascript' src='http://www.google.com/jsapi<%= @api_key %>'></script>
2
+ <script type='text/javascript'>
3
+ google.load('visualization', '1', {'packages':[<%= @@viz_package_names.map{|p|"'" + p.downcase + "'"}.join(',') -%>]});
4
+ google.setOnLoadCallback(drawChart);
5
+ function drawChart() {
6
+ <%- @@graphs.each do |graph| -%>
7
+ var <%= graph.data_name%> = new google.visualization.DataTable();
8
+ <%- graph.columns.each do |column| -%>
9
+ <%= graph.data_name%>.addColumn('<%= column[0] %>', '<%= column[1] %>')
10
+ <%- end -%>
11
+ <%= graph.data_name%>.addRows(<%= graph.size %>)
12
+ <%- graph.rows.each_with_index do |row, i|
13
+ row.each_with_index do |value, j|
14
+ next if value.nil?
15
+ -%>
16
+ <%= graph.data_name%>.setValue(<%= i %>, <%= j %>, <%= value %>)
17
+ <%-
18
+ end
19
+ end -%>
20
+ var chart_<%= graph.data_name %> = new google.visualization.<%= graph.type %>(document.getElementById('<%= graph.chart_id %>'));
21
+ chart.draw(<%= graph.data_name%>, <%=graph.options.to_json%>);
22
+ <%- end -%>
23
+ }
24
+ </script>
@@ -0,0 +1,44 @@
1
+ #http://code.google.com/apis/visualization/documentation/gallery/annotatedtimeline.html
2
+ # data.addColumn('date', 'Date');
3
+ # data.addColumn('number', 'Sold Pencils');
4
+ # data.addColumn('string', 'title1');
5
+ # data.addColumn('string', 'text1');
6
+ # data.addColumn('number', 'Sold Pens');
7
+ # data.addColumn('string', 'title2');
8
+ # data.addColumn('string', 'text2');
9
+ # data.addRows(6);
10
+ # data.setValue(0, 0, new Date(2008, 1 ,1));
11
+ # data.setValue(0, 1, 30000);
12
+ # data.setValue(0, 4, 40645);
13
+ # data.setValue(1, 0, new Date(2008, 1 ,2));
14
+ # data.setValue(1, 1, 14045);
15
+ # data.setValue(1, 4, 20374);
16
+ # data.setValue(2, 0, new Date(2008, 1 ,3));
17
+ # data.setValue(2, 1, 55022);
18
+ # data.setValue(2, 4, 50766);
19
+ # data.setValue(3, 0, new Date(2008, 1 ,4));
20
+ # data.setValue(3, 1, 75284);
21
+ # data.setValue(3, 4, 14334);
22
+ # data.setValue(3, 5, 'Out of Stock');
23
+ # data.setValue(3, 6, 'Ran out of stock on pens at 4pm');
24
+ # data.setValue(4, 0, new Date(2008, 1 ,5));
25
+ # data.setValue(4, 1, 41476);
26
+ # data.setValue(4, 2, 'Bought Pens');
27
+ # data.setValue(4, 3, 'Bought 200k pens');
28
+ # data.setValue(4, 4, 66467);
29
+ # data.setValue(5, 0, new Date(2008, 1 ,6));
30
+ # data.setValue(5, 1, 33322);
31
+ # data.setValue(5, 4, 39463);
32
+ mapping = [[:date, 'Date'],[:sold_pencils, 'Solid Pencils'], [:title1, 'title1'], [:text1, 'text1'],
33
+ [:sold_pens, 'Sold Pens'], [:title2, 'title2'], [:text2, 'text2']]
34
+
35
+ data = [
36
+ {:date => "2008-01-01", :sold_pencils => 30000, :sold_pens => 40645},
37
+ {:date => Date.parse("2008-01-02"), :sold_pencils => 14045, :sold_pens => 20374},
38
+ {:date => Date.parse("2008-01-03"), :sold_pencils => 55022, :sold_pens => 50766},
39
+ {:date => Date.parse("2008-01-04"), :sold_pencils => 75284, :sold_pens => 14334, :title2 => 'Out of Stock',:text2 => 'Ran out of stock on pens at 4pm'},
40
+ {:date => Date.parse("2008-01-05"), :sold_pencils => 41476, :sold_pens => 66467, :title1 => 'Bought Pens',:text2 => 'Bought 200k pens'},
41
+ {:date => Date.parse("2008-01-06"), :sold_pencils => 33322, :sold_pens => 39463, :title1 => 'Bought Pens',:text2 => 'Bought 200k pens'}
42
+ ]
43
+
44
+ @test_data = [data, mapping]
@@ -0,0 +1,88 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'test_helper')
2
+
3
+ describe GViz do
4
+ describe 'api' do
5
+ describe 'examples' do
6
+
7
+ describe 'annotated timeline' do
8
+ @mapping = [[:date, 'Date'],[:sold_pencils, 'Solid Pencils'], [:title1, 'title1'], [:text1, 'text1'],
9
+ [:sold_pens, 'Sold Pens'], [:title2, 'title2'], [:text2, 'text2']]
10
+
11
+ @data = [
12
+ {:date => "2008-02-01", :sold_pencils => 30000, :sold_pens => 40645},
13
+ {:date => Date.parse("2008-02-02"), :sold_pencils => 14045, :sold_pens => 20374},
14
+ {:date => Date.parse("2008-02-03"), :sold_pencils => 55022, :sold_pens => 50766},
15
+ {:date => Date.parse("2008-02-04"), :sold_pencils => 75284, :sold_pens => 14334, :title2 => 'Out of Stock',:text2 => 'Ran out of stock on pens at 4pm'},
16
+ {:date => Date.parse("2008-02-05"), :sold_pencils => 41476, :sold_pens => 66467, :title1 => 'Bought Pens',:text2 => 'Bought 200k pens'},
17
+ {:date => Date.parse("2008-02-06"), :sold_pencils => 33322, :sold_pens => 39463, :title1 => 'Bought Pens',:text2 => 'Bought 200k pens'}
18
+ ]
19
+ GViz.add_graph('AnnotatedTimeline', @data, @mapping)
20
+ output = GViz.output
21
+ puts output
22
+ output.should == TestHelper.template('rendered_annotated_timeline')
23
+ end
24
+ end
25
+
26
+ describe 'graph' do
27
+ @games = [{:name => "Gary", :games_won => "2", :birthday => "2008-02-01"},
28
+ {:name => "John", :games_won => "4", :birthday => Date.parse("2009-01-01 11:11")},
29
+ {:name => "A", :games_won => "2", :birthday => "Jun 14 1982"}]
30
+ GViz.add_graph('MotionGraph', @games, [[:name, "Name"], [:games_won, "# Games"]])
31
+ end
32
+ end
33
+
34
+ describe 'implementation details - class methods' do
35
+ describe 'google_data_type' do
36
+ it "should classify ruby data types to google visualization data types" do
37
+ GViz.google_data_type(Time.now).should == 'datetime'
38
+ GViz.google_data_type(Date.new).should == 'date'
39
+ GViz.google_data_type("June 23 1982").should == 'date'
40
+ GViz.google_data_type("June 23 1982 12:23").should == 'datetime'
41
+ GViz.google_data_type("12312412").should == 'numeric'
42
+ GViz.google_data_type("12312412.1234").should == 'numeric'
43
+ GViz.google_data_type(1234123).should == 'numeric'
44
+ GViz.google_data_type(1234123.1234).should == 'numeric'
45
+ GViz.google_data_type("a123124123").should == 'string'
46
+ # should this really happen? documenting it because i need to think if this is ok.
47
+ GViz.google_data_type("a12312412").should == 'date'
48
+ end
49
+ end
50
+
51
+ describe 'ruby_to_js' do
52
+ it "should return the js string representation of that data " do
53
+ GViz.ruby_to_js('numeric', 12341234).should == 12341234
54
+ GViz.ruby_to_js('string', 12341234).should == "'12341234'"
55
+ GViz.ruby_to_js('date', "June 23 1982").should == "new Date(1982, 5, 23)"
56
+ GViz.ruby_to_js('date', Date.parse("June 23 1982")).should == "new Date(1982, 5, 23)"
57
+ end
58
+ end
59
+ end
60
+
61
+ describe 'implmentation details - instances methods' do
62
+ before(:all) do
63
+ @games = [{:name => "Gary", :games_won => "2", :birthday => "2008-02-01"},
64
+ {:name => "John", :games_won => "4", :birthday => Date.parse("2009-01-01 11:11")},
65
+ {:name => "A", :games_won => "2", :birthday => "Jun 14 1982"}]
66
+
67
+ @rows = [["'Gary'", "2", "new Date(2008, 1, 1)"],
68
+ ["'John'", "4", "new Date(2009, 0, 1)"],
69
+ ["'A'", "2", "new Date(1982, 5, 14)"]]
70
+
71
+ @gviz = GViz.send(:new, 'bar', @games, [[:name, "Name"], [:games_won, "Games Won"], [:birthday, 'Birthday']])
72
+ end
73
+
74
+ describe 'columns' do
75
+ it 'should return google data types and labels' do
76
+ @gviz.columns.should == [['string', 'Name'], ['numeric', 'Games Won'], ['date', 'Birthday']]
77
+ end
78
+
79
+ end
80
+
81
+ describe 'rows' do
82
+ it 'should return data rows' do
83
+ @gviz.rows.should == @rows
84
+ end
85
+ end
86
+
87
+ end
88
+ end
@@ -0,0 +1,41 @@
1
+ <script type='text/javascript' src='http://www.google.com/jsapi'></script>
2
+ <script type='text/javascript'>
3
+ google.load('visualization', '1', {'packages':['annotatedtimeline']});
4
+ google.setOnLoadCallback(drawChart);
5
+ function drawChart() {
6
+ var data = new google.visualization.DataTable();
7
+ data.addColumn('date', 'Date');
8
+ data.addColumn('number', 'Sold Pencils');
9
+ data.addColumn('string', 'title1');
10
+ data.addColumn('string', 'text1');
11
+ data.addColumn('number', 'Sold Pens');
12
+ data.addColumn('string', 'title2');
13
+ data.addColumn('string', 'text2');
14
+ data.addRows(6);
15
+ data.setValue(0, 0, new Date(2008, 1 ,1));
16
+ data.setValue(0, 1, 30000);
17
+ data.setValue(0, 4, 40645);
18
+ data.setValue(1, 0, new Date(2008, 1 ,2));
19
+ data.setValue(1, 1, 14045);
20
+ data.setValue(1, 4, 20374);
21
+ data.setValue(2, 0, new Date(2008, 1 ,3));
22
+ data.setValue(2, 1, 55022);
23
+ data.setValue(2, 4, 50766);
24
+ data.setValue(3, 0, new Date(2008, 1 ,4));
25
+ data.setValue(3, 1, 75284);
26
+ data.setValue(3, 4, 14334);
27
+ data.setValue(3, 5, 'Out of Stock');
28
+ data.setValue(3, 6, 'Ran out of stock on pens at 4pm');
29
+ data.setValue(4, 0, new Date(2008, 1 ,5));
30
+ data.setValue(4, 1, 41476);
31
+ data.setValue(4, 2, 'Bought Pens');
32
+ data.setValue(4, 3, 'Bought 200k pens');
33
+ data.setValue(4, 4, 66467);
34
+ data.setValue(5, 0, new Date(2008, 1 ,6));
35
+ data.setValue(5, 1, 33322);
36
+ data.setValue(5, 4, 39463);
37
+
38
+ var chart = new google.visualization.AnnotatedTimeLine(document.getElementById('chart_div'));
39
+ chart.draw(data, {displayAnnotations: true});
40
+ }
41
+ </script>
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'g_viz', 'g_viz')
3
+
4
+ class TestHelper
5
+ def self.template(name)
6
+ f = File.open(File.join(File.dirname(__FILE__), 'g_viz', 'templates', "#{name}.html"))
7
+ d = f.readlines(nil)
8
+ return d[0].to_s
9
+ end
10
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: garru-g_viz
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Gary Tsang
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-04 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Ruby Scribe Client. Package and Wrapper for generated ruby interfaces
17
+ email: ""
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - CHANGELOG
24
+ - lib/g_viz/g_viz.rb
25
+ - lib/g_viz/templates/visualization.erb
26
+ - README
27
+ files:
28
+ - CHANGELOG
29
+ - lib/g_viz/g_viz.rb
30
+ - lib/g_viz/templates/visualization.erb
31
+ - Manifest
32
+ - Rakefile
33
+ - README
34
+ - spec/g_viz/fixtures/annotated_timeline.rb
35
+ - spec/g_viz/g_viz_spec.rb
36
+ - spec/g_viz/templates/rendered_annotated_timeline.html
37
+ - spec/test_helper.rb
38
+ - g_viz.gemspec
39
+ has_rdoc: true
40
+ homepage: http://github.com/garru/GViz
41
+ post_install_message:
42
+ rdoc_options:
43
+ - --line-numbers
44
+ - --inline-source
45
+ - --title
46
+ - G_viz
47
+ - --main
48
+ - README
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "1.2"
62
+ version:
63
+ requirements: []
64
+
65
+ rubyforge_project: g_viz
66
+ rubygems_version: 1.2.0
67
+ signing_key:
68
+ specification_version: 2
69
+ summary: Ruby Scribe Client. Package and Wrapper for generated ruby interfaces
70
+ test_files: []
71
+