ztk 2.2.0 → 2.3.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dd68d97935db83e53b34db76a8c2fc66b383dbf7
4
- data.tar.gz: 851497e4499a72195f60da2fae440d3b348f7250
3
+ metadata.gz: cabc2aa0f97fee6c28a6995a880ddbad7efaca29
4
+ data.tar.gz: fb80e691347422cb146335bb270de4b84677e284
5
5
  SHA512:
6
- metadata.gz: e6a0aee03b36ce204098a9c5e1224ba1ac02404855335c12b8b56d058844e15aa1ade9af1c7671c43c8ffd74a137dc7b5cefc8fc28b473cc190c2ea1ee3a9895
7
- data.tar.gz: 3bfd95f46161dfe5b3b2368932cb6fe0060ce166912a0e7a431ce3ac199ddc05ce55c2e7cc138c71ed44e3fb8d22556982ba551d5c82441054b9613a4b7940ac
6
+ metadata.gz: 3b8b4757fec35497d303641a3494cebef779357cce9a74869a28c79fae7de17283acc93841472de15b660b4c49eca7c84e2348677ca25781f4ca6da468ba23c2
7
+ data.tar.gz: 74afc96e8b46e75c7b7f39c8df2456e717ce7c6f83de33e77a0d85370a20bea5d3ae0a981316d2accba541ccfd785887bd828f1c669f49c71d5db194231d37e1
data/lib/ztk.rb CHANGED
@@ -22,6 +22,7 @@ module ZTK
22
22
  require 'ztk/benchmark'
23
23
  require 'ztk/command'
24
24
  require 'ztk/config'
25
+ require 'ztk/google_chart'
25
26
  require 'ztk/locator'
26
27
  require 'ztk/logger'
27
28
  require 'ztk/parallel'
@@ -0,0 +1,26 @@
1
+ module ZTK
2
+
3
+ # GoogleChart Class
4
+ #
5
+ # @author Zachary Patten <zachary AT jovelabs DOT com>
6
+ module GoogleChart
7
+
8
+ # GoogleChart Error Class
9
+ #
10
+ # @author Zachary Patten <zachary AT jovelabs DOT com>
11
+ class GoogleChartError < Error; end
12
+
13
+ require 'ztk/google_chart/base'
14
+
15
+ require 'ztk/google_chart/annotation'
16
+ require 'ztk/google_chart/bar'
17
+ require 'ztk/google_chart/candlestick'
18
+ require 'ztk/google_chart/column'
19
+ require 'ztk/google_chart/combo'
20
+ require 'ztk/google_chart/line'
21
+ require 'ztk/google_chart/pie'
22
+ require 'ztk/google_chart/sankey'
23
+
24
+ end
25
+
26
+ end
@@ -0,0 +1,14 @@
1
+ module ZTK
2
+ module GoogleChart
3
+
4
+ # GoogleChart Annotation Chart
5
+ class Annotation < ZTK::GoogleChart::Base
6
+
7
+ def initialize(configuration={})
8
+ super({ :type => 'AnnotationChart' }.merge(configuration))
9
+ end
10
+
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module ZTK
2
+ module GoogleChart
3
+
4
+ # GoogleChart Bar Chart
5
+ class Bar < ZTK::GoogleChart::Base
6
+
7
+ def initialize(configuration={})
8
+ super({ :type => 'BarChart' }.merge(configuration))
9
+ end
10
+
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,77 @@
1
+ class String
2
+ def __fix_date
3
+ self.gsub(/"new/, 'new').gsub(/\)"/, ')')
4
+ end
5
+ end
6
+
7
+ module ZTK
8
+ module GoogleChart
9
+
10
+ # GoogleChart Base Class
11
+ class Base < ZTK::Base
12
+ require 'ztk/google_chart/base/array_to_data_table'
13
+ require 'ztk/google_chart/base/data_table'
14
+ require 'ztk/google_chart/base/dates'
15
+ require 'ztk/google_chart/base/options'
16
+ require 'ztk/google_chart/base/ticks'
17
+
18
+ include ZTK::GoogleChart::Base::ArrayToDataTable
19
+ include ZTK::GoogleChart::Base::DataTable
20
+ include ZTK::GoogleChart::Base::Dates
21
+ include ZTK::GoogleChart::Base::Options
22
+ include ZTK::GoogleChart::Base::Ticks
23
+
24
+ # @param [Hash] configuration Configuration options hash.
25
+ def initialize(configuration={})
26
+ super({ :id => generate_id }, configuration)
27
+
28
+ @id = config.id.to_s.underscore.gsub(/ /, '')
29
+ @chart_name_tag = "chart_#{@id}"
30
+ @chart_data_tag = "#{@chart_name_tag}_data"
31
+ @chart_options_tag = "#{@chart_name_tag}_options"
32
+ @chart_draw_tag = "#{@chart_name_tag}_draw"
33
+ @chart_div_tag = "#{@chart_name_tag}_div"
34
+ @chart_type_tag = config.type
35
+ end
36
+
37
+ def render(content=nil)
38
+ case @chart_method
39
+ when :data_table
40
+ data_table_render
41
+ when :array_to_data_table
42
+ array_to_data_table_render
43
+ else
44
+ raise "You must supply chart data via DataTable or ArrayToDataTable!"
45
+ end
46
+ end
47
+
48
+ def body(&block)
49
+ <<-EOCHART
50
+ <script type="text/javascript" src="https://www.google.com/jsapi"></script>
51
+ <script type="text/javascript">
52
+ var #{@chart_data_tag};
53
+ var #{@chart_name_tag};
54
+
55
+ google.load('visualization', '1', {'packages':['corechart']});
56
+ google.load('visualization', '1.1', {'packages':['annotationchart']});
57
+ google.load('visualization', '1.1', {'packages':['sankey']});
58
+ google.setOnLoadCallback(#{@chart_draw_tag});
59
+
60
+ #{block.call.chomp}
61
+ </script>
62
+ <div id="#{@chart_div_tag}"></div>
63
+ EOCHART
64
+ end
65
+
66
+ def generate_id
67
+ generated_id = Array.new
68
+
69
+ generated_id << SecureRandom.hex(16)
70
+
71
+ generated_id.join('_')
72
+ end
73
+
74
+ end
75
+
76
+ end
77
+ end
@@ -0,0 +1,42 @@
1
+ module ZTK
2
+ module GoogleChart
3
+ class Base
4
+
5
+ module ArrayToDataTable
6
+
7
+ def array_to_data_table(data)
8
+ @chart_method = :array_to_data_table
9
+ @chart_data = data
10
+
11
+ config.ui.logger.info { "array_to_data_table(#{data.inspect})" }
12
+
13
+ @chart_data
14
+ end
15
+
16
+ def array_to_data_table_function
17
+ <<-EOCHART
18
+ function #{@chart_draw_tag}() {
19
+
20
+ #{@chart_data_tag} = new google.visualization.arrayToDataTable(
21
+ #{@chart_data.to_json}
22
+ );
23
+
24
+ var #{@chart_options_tag} = #{JSON.pretty_generate(@chart_options)};
25
+
26
+ #{@chart_name_tag} = new google.visualization.#{@chart_type_tag}(document.getElementById('#{@chart_div_tag}'));
27
+ #{@chart_name_tag}.draw(#{@chart_data_tag}, #{@chart_options_tag});
28
+ }
29
+ EOCHART
30
+ end
31
+
32
+ def array_to_data_table_render
33
+ body do
34
+ array_to_data_table_function
35
+ end
36
+ end
37
+
38
+ end
39
+
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,53 @@
1
+ module ZTK
2
+ module GoogleChart
3
+ class Base
4
+
5
+ module DataTable
6
+
7
+ def data_table(function, *args)
8
+ @chart_method = :data_table
9
+ @chart_data_table ||= Array.new
10
+
11
+ @chart_data_table << [function, *args]
12
+ end
13
+
14
+ def data_table_function(&block)
15
+ <<-EOCHART
16
+ function #{@chart_draw_tag}() {
17
+
18
+ #{@chart_data_tag} = new google.visualization.DataTable();
19
+ #{block.call.chomp}
20
+
21
+ var #{@chart_options_tag} = #{JSON.pretty_generate(@chart_options).__fix_date};
22
+
23
+ #{@chart_name_tag} = new google.visualization.#{@chart_type_tag}(document.getElementById('#{@chart_div_tag}'));
24
+ #{@chart_name_tag}.draw(#{@chart_data_tag}, #{@chart_options_tag});
25
+ }
26
+ EOCHART
27
+ end
28
+
29
+ def data_table_render
30
+ data_table_blob = Array.new
31
+ @chart_data_table.each do |function, *args|
32
+ args = args.collect do |arg|
33
+ if arg.is_a?(Array)
34
+ JSON.pretty_generate(arg).__fix_date
35
+ else
36
+ arg.to_json
37
+ end
38
+ end.join(', ')
39
+ data_table_blob << %( #{@chart_data_tag}.#{function}(#{args});)
40
+ end
41
+
42
+ body do
43
+ data_table_function do
44
+ data_table_blob.join("\n")
45
+ end
46
+ end
47
+ end
48
+
49
+ end
50
+
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,76 @@
1
+ module ZTK
2
+ module GoogleChart
3
+ class Base
4
+
5
+ module Dates
6
+
7
+ DATE_HELPERS = {
8
+ :second => -1,
9
+ :minute => -2,
10
+ :hour => -3,
11
+ :day => -4,
12
+ :month => -5,
13
+ :year => -6
14
+ }
15
+
16
+ def date_scale(scale, *args)
17
+ case scale
18
+ when :year then
19
+ send(:date_month, *args)
20
+ when :month, :week then
21
+ send(:date_day, *args)
22
+ when :day then
23
+ send(:date_hour, *args)
24
+ when :hour then
25
+ send(:date_minute, *args)
26
+ when :minute then
27
+ send(:date_second, *args)
28
+ end
29
+ end
30
+
31
+ def date_seed(start_time, end_time, unit, default)
32
+ start_time = start_time.dup
33
+ timeline = Hash.new
34
+
35
+ scale = case unit
36
+ when :year then
37
+ :month
38
+ when :month, :week then
39
+ :day
40
+ when :day then
41
+ :hour
42
+ when :hour then
43
+ :minute
44
+ when :minute then
45
+ :second
46
+ end
47
+
48
+ loop do
49
+ timeline.merge!(date_scale(unit, start_time) => default.dup)
50
+ start_time += 1.send(scale)
51
+ break if (start_time > end_time)
52
+ end
53
+
54
+ timeline
55
+ end
56
+
57
+ def date_wrapper(value)
58
+ "new Date(#{value})"
59
+ end
60
+
61
+ def date_format(*args)
62
+ %w( %Y %%d %-d %-H %-M %-S )[*args].join(',')
63
+ end
64
+
65
+ DATE_HELPERS.each do |unit, offset|
66
+ method_name = "date_#{unit}".downcase.to_sym
67
+ define_method(method_name) do |date=Time.now.utc|
68
+ date_wrapper(date.strftime(date_format(0..offset)) % [ (date.month - 1) ])
69
+ end
70
+ end
71
+
72
+ end
73
+
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,40 @@
1
+ module ZTK
2
+ module GoogleChart
3
+ class Base
4
+
5
+ module Options
6
+
7
+ def options=(value)
8
+ set_options(value)
9
+ end
10
+
11
+ def options(value=nil)
12
+ set_options(value)
13
+ end
14
+
15
+ private
16
+
17
+ def default_width
18
+ 900
19
+ end
20
+
21
+ def set_options(value=nil)
22
+ if @chart_options.nil?
23
+ @chart_options = Hash.new
24
+ @chart_options.merge!(:width => default_width.to_i, :height => default_width.div(2).to_i)
25
+ end
26
+
27
+ if !value.nil? && !value.empty?
28
+ @chart_options.merge!(value)
29
+ end
30
+
31
+ config.ui.logger.info { "options(#{value.inspect}) -> #{@chart_options.inspect}" }
32
+
33
+ @chart_options
34
+ end
35
+
36
+ end
37
+
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,24 @@
1
+ module ZTK
2
+ module GoogleChart
3
+ class Base
4
+
5
+ module Ticks
6
+
7
+ def tick_seed(start_time, end_time, unit)
8
+ start_time = start_time.dup
9
+ ticks = Array.new
10
+
11
+ loop do
12
+ ticks << date_scale(unit, start_time)
13
+ start_time += 1.send(unit)
14
+ break if (start_time > end_time)
15
+ end
16
+
17
+ ticks
18
+ end
19
+
20
+ end
21
+
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,14 @@
1
+ module ZTK
2
+ module GoogleChart
3
+
4
+ # GoogleChart Candlestick Chart
5
+ class Candlestick < ZTK::GoogleChart::Base
6
+
7
+ def initialize(configuration={})
8
+ super({ :type => 'CandlestickChart' }.merge(configuration))
9
+ end
10
+
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module ZTK
2
+ module GoogleChart
3
+
4
+ # GoogleChart Column Chart
5
+ class Column < ZTK::GoogleChart::Base
6
+
7
+ def initialize(configuration={})
8
+ super({ :type => 'ColumnChart' }.merge(configuration))
9
+ end
10
+
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module ZTK
2
+ module GoogleChart
3
+
4
+ # GoogleChart Combo Chart
5
+ class Combo < ZTK::GoogleChart::Base
6
+
7
+ def initialize(configuration={})
8
+ super({ :type => 'ComboChart' }.merge(configuration))
9
+ end
10
+
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module ZTK
2
+ module GoogleChart
3
+
4
+ # GoogleChart Line Chart
5
+ class Line < ZTK::GoogleChart::Base
6
+
7
+ def initialize(configuration={})
8
+ super({ :type => 'LineChart' }.merge(configuration))
9
+ end
10
+
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module ZTK
2
+ module GoogleChart
3
+
4
+ # GoogleChart Pie Chart
5
+ class Pie < ZTK::GoogleChart::Base
6
+
7
+ def initialize(configuration={})
8
+ super({ :type => 'PieChart' }.merge(configuration))
9
+ end
10
+
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module ZTK
2
+ module GoogleChart
3
+
4
+ # GoogleChart Sankey Chart
5
+ class Sankey < ZTK::GoogleChart::Base
6
+
7
+ def initialize(configuration={})
8
+ super({ :type => 'Sankey' }.merge(configuration))
9
+ end
10
+
11
+ end
12
+
13
+ end
14
+ end
data/lib/ztk/version.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  module ZTK
2
2
 
3
3
  # ZTK Version String
4
- VERSION = "2.2.0"
4
+ VERSION = "2.3.0"
5
5
 
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ztk
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.0
4
+ version: 2.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zachary Patten
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-08 00:00:00.000000000 Z
11
+ date: 2014-07-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -297,6 +297,21 @@ files:
297
297
  - lib/ztk/dsl/core/relations.rb
298
298
  - lib/ztk/dsl/core/relations/belongs_to.rb
299
299
  - lib/ztk/dsl/core/relations/has_many.rb
300
+ - lib/ztk/google_chart.rb
301
+ - lib/ztk/google_chart/annotation.rb
302
+ - lib/ztk/google_chart/bar.rb
303
+ - lib/ztk/google_chart/base.rb
304
+ - lib/ztk/google_chart/base/array_to_data_table.rb
305
+ - lib/ztk/google_chart/base/data_table.rb
306
+ - lib/ztk/google_chart/base/dates.rb
307
+ - lib/ztk/google_chart/base/options.rb
308
+ - lib/ztk/google_chart/base/ticks.rb
309
+ - lib/ztk/google_chart/candlestick.rb
310
+ - lib/ztk/google_chart/column.rb
311
+ - lib/ztk/google_chart/combo.rb
312
+ - lib/ztk/google_chart/line.rb
313
+ - lib/ztk/google_chart/pie.rb
314
+ - lib/ztk/google_chart/sankey.rb
300
315
  - lib/ztk/locator.rb
301
316
  - lib/ztk/logger.rb
302
317
  - lib/ztk/parallel.rb