analytica 0.0.12 → 0.0.13

Sign up to get free protection for your applications and to get access to all the features.
data/lib/analytica.rb CHANGED
@@ -4,7 +4,7 @@ require File.join(File.dirname(__FILE__), 'computation')
4
4
  require File.join(File.dirname(__FILE__), 'visualization')
5
5
 
6
6
  module Analytica
7
- VERSION = '0.0.12'
7
+ VERSION = '0.0.13'
8
8
 
9
9
  include Strict
10
10
 
@@ -15,8 +15,16 @@ module Analytica
15
15
 
16
16
  register_supertype(:dataset_array, dataset_array_handler)
17
17
 
18
+ string_array_2d_handler = proc do |data, context|
19
+ enforce_primitive!(Array, data, context)
20
+ data.each {|item| enforce!(:string_array, item, context)}
21
+ end
22
+
23
+ register_supertype(:string_array_2d, string_array_2d_handler)
24
+
18
25
  class DataSet < Array
19
26
  include Analytica::Computation
27
+ include Analytica::Visualization::Common
20
28
  include Analytica::Visualization::DataSet
21
29
 
22
30
  def initialize(datapoints=[])
@@ -45,11 +53,15 @@ module Analytica
45
53
  end
46
54
 
47
55
  class DataSystem < Array
56
+ include Analytica::Visualization::Common
48
57
  include Analytica::Visualization::DataSystem
49
58
 
50
59
  def initialize(datasets=[])
51
60
  enforce!(:dataset_array, datasets)
52
61
 
62
+ @labels = []
63
+ @labels_set = false
64
+
53
65
  super datasets
54
66
  end
55
67
 
data/lib/computation.rb CHANGED
@@ -32,7 +32,7 @@ module Analytica
32
32
  sum.to_f / size
33
33
  end
34
34
 
35
- def linear_mean(params)
35
+ def linear_mean(params={})
36
36
  enforce_map!({
37
37
  :bias => [:last, :first],
38
38
  :samples => :natural_number}, params)
@@ -56,7 +56,7 @@ module Analytica
56
56
  numerator / denominator
57
57
  end
58
58
 
59
- def exponential_mean(params)
59
+ def exponential_mean(params={})
60
60
  enforce_map!({
61
61
  :bias => [:last, :first],
62
62
  :alpha => :numeric,
@@ -89,7 +89,7 @@ module Analytica
89
89
  end
90
90
 
91
91
 
92
- def average_filter(params)
92
+ def average_filter(params={})
93
93
  enforce_map!({
94
94
  :decay => [:simple, :linear, :exponential],
95
95
  :offset => :natural_number, # offset from latest data point
@@ -130,7 +130,7 @@ module Analytica
130
130
 
131
131
  alias_method(:avg, :average_filter)
132
132
 
133
- def moving_average(params)
133
+ def moving_average(params={})
134
134
  enforce_map!({
135
135
  :decay => [:simple, :linear, :exponential],
136
136
  :samples => :natural_number}, params)
@@ -150,7 +150,7 @@ module Analytica
150
150
  d.reverse
151
151
  end
152
152
 
153
- def simple_moving_average(params)
153
+ def simple_moving_average(params={})
154
154
  enforce_map!({
155
155
  :samples => :natural_number}, params)
156
156
  moving_average(:decay => :simple, :samples => params[:samples])
@@ -158,7 +158,7 @@ module Analytica
158
158
 
159
159
  alias_method :sma, :simple_moving_average
160
160
 
161
- def linear_moving_average(params)
161
+ def linear_moving_average(params={})
162
162
  enforce_map!({
163
163
  :samples => :natural_number}, params)
164
164
  moving_average(:decay => :linear, :samples => params[:samples])
@@ -166,7 +166,7 @@ module Analytica
166
166
 
167
167
  alias_method :lma, :linear_moving_average
168
168
 
169
- def exponential_moving_average(params)
169
+ def exponential_moving_average(params={})
170
170
  enforce_map!({
171
171
  :samples => :integer,
172
172
  :alpha => :float}, params)
data/lib/visualization.rb CHANGED
@@ -7,6 +7,37 @@ module Analytica
7
7
  {:width => 25, :spacing => 10, :group_spacing => 12}
8
8
  end
9
9
 
10
+ module Common
11
+ def set_title(params)
12
+ enforce_map!({
13
+ :title => :string,
14
+ :title_size => :natural_number,
15
+ :title_color => :hex_color}, params)
16
+
17
+ @title = params
18
+ @title_set = true
19
+ end
20
+
21
+ def common_options
22
+ options = {}
23
+
24
+ if @labels_set
25
+ label = {
26
+ :axis_with_labels => ['x', 'y'],
27
+ :axis_labels => [@labels, ["#{0}", "#{(datamax*0.25).to_i}", "#{(datamax*0.5).to_i}", "#{(datamax*0.75).to_i}", "#{datamax.to_i}"]]
28
+ }
29
+
30
+ options.merge!(label)
31
+ end
32
+
33
+ if @title_set
34
+ options.merge!(@title)
35
+ end
36
+
37
+ options
38
+ end
39
+ end
40
+
10
41
  module DataSet
11
42
  include Strict
12
43
 
@@ -21,7 +52,7 @@ module Analytica
21
52
  (max > 0) ? (1.25*max) : 1
22
53
  end
23
54
 
24
- def to_linegraph(params)
55
+ def to_linegraph(params={})
25
56
  enforce_map!({
26
57
  :width => :natural_number,
27
58
  :height => :natural_number,
@@ -44,14 +75,6 @@ module Analytica
44
75
 
45
76
  options.merge!(base)
46
77
 
47
- title = {
48
- :title => params[:title],
49
- :title_color => params[:title_color],
50
- :title_size => params[:title_size]
51
- }
52
-
53
- #options.merge!(title)
54
-
55
78
  color = {
56
79
  :line_colors => params[:color],
57
80
  :background => params[:background_color],
@@ -60,19 +83,12 @@ module Analytica
60
83
 
61
84
  options.merge!(color)
62
85
 
63
- label = {
64
- :legend => ['x', 'y'], # TODO number of datasets
65
- :axis_with_labels => ['x','r'], #TODO number of datasets
66
- :axis_labels => [[],[]] # TODO
67
- }
68
-
69
-
70
- #options.merge!(label)
86
+ options.merge!(common_options)
71
87
 
72
88
  Gchart.line(options)
73
89
  end
74
90
 
75
- def to_sparkline(params)
91
+ def to_sparkline(params={})
76
92
  enforce_map!({
77
93
  :width => :natural_number,
78
94
  :height => :natural_number,
@@ -95,14 +111,6 @@ module Analytica
95
111
 
96
112
  options.merge!(base)
97
113
 
98
- title = {
99
- :title => params[:title],
100
- :title_color => params[:title_color],
101
- :title_size => params[:title_size]
102
- }
103
-
104
- #options.merge!(title)
105
-
106
114
  color = {
107
115
  :line_colors => params[:color],
108
116
  :background => params[:background_color],
@@ -111,19 +119,13 @@ module Analytica
111
119
 
112
120
  options.merge!(color)
113
121
 
114
- label = {
115
- :legend => ['x', 'y'], # TODO number of datasets
116
- :axis_with_labels => ['x','r'], #TODO number of datasets
117
- :axis_labels => [[],[]] # TODO
118
- }
119
-
120
- #options.merge!(label)
122
+ options.merge!(common_options)
121
123
 
122
124
  Gchart.sparkline(options)
123
125
  end
124
126
 
125
127
 
126
- def to_bargraph(params)
128
+ def to_bargraph(params={})
127
129
  enforce_map!({
128
130
  :width => :natural_number,
129
131
  :height => :natural_number,
@@ -145,14 +147,6 @@ module Analytica
145
147
 
146
148
  options.merge!(base)
147
149
 
148
- title = {
149
- :title => params[:title],
150
- :title_color => params[:title_color],
151
- :title_size => params[:title_size]
152
- }
153
-
154
- #options.merge!(title)
155
-
156
150
  color = {
157
151
  :bar_colors => params[:color],
158
152
  :background => params[:background_color],
@@ -161,25 +155,26 @@ module Analytica
161
155
 
162
156
  options.merge!(color)
163
157
 
164
- label = {
165
- :legend => ['x', 'y'], # TODO number of datasets
166
- :axis_with_labels => ['x','r'], #TODO number of datasets
167
- :axis_labels => [[],[]] # TODO
168
- }
169
-
170
- #options.merge!(label)
171
-
158
+ options.merge!(common_options)
172
159
 
173
160
  return Gchart.bar(options)
174
161
  end
175
162
  end
176
163
 
177
164
  module DataSystem
165
+
166
+ def set_labels(labels)
167
+ enforce!(:string_array_2d, labels)
168
+
169
+ @labels = labels
170
+ @labels_set = true
171
+ end
172
+
178
173
  def datamax
179
174
  (self.map{|dset| dset.datamax}).max
180
175
  end
181
176
 
182
- def to_linegraph(params)
177
+ def to_linegraph(params={})
183
178
  enforce_map!({
184
179
  :width => :natural_number,
185
180
  :height => :natural_number,
@@ -202,14 +197,6 @@ module Analytica
202
197
 
203
198
  options.merge!(base)
204
199
 
205
- title = {
206
- :title => params[:title],
207
- :title_color => params[:title_color],
208
- :title_size => params[:title_size]
209
- }
210
-
211
- #options.merge!(title)
212
-
213
200
  color = {
214
201
  :line_colors => params[:colors],
215
202
  :background => params[:background_color],
@@ -218,19 +205,12 @@ module Analytica
218
205
 
219
206
  options.merge!(color)
220
207
 
221
- if @labels_set
222
- label = {
223
- :axis_with_labels => ['x', 'y' ,'r'],
224
- :axis_labels => @labels
225
- }
226
-
227
- options.merge!(label)
228
- end
208
+ options.merge!(common_options)
229
209
 
230
210
  Gchart.line(options)
231
211
  end
232
212
 
233
- def to_bargraph(params)
213
+ def to_bargraph(params={})
234
214
  enforce_map!({
235
215
  :width => :natural_number,
236
216
  :height => :natural_number,
@@ -253,14 +233,6 @@ module Analytica
253
233
 
254
234
  options.merge!(base)
255
235
 
256
- title = {
257
- :title => params[:title],
258
- :title_color => params[:title_color],
259
- :title_size => params[:title_size]
260
- }
261
-
262
- #options.merge!(title)
263
-
264
236
  color = {
265
237
  :bar_colors => params[:colors],
266
238
  :background => params[:background_color],
@@ -269,14 +241,7 @@ module Analytica
269
241
 
270
242
  options.merge!(color)
271
243
 
272
- label = {
273
- :legend => ['x', 'y'], # TODO number of datasets
274
- :axis_with_labels => ['x','r'], #TODO number of datasets
275
- :axis_labels => [[],[]] # TODO
276
- }
277
-
278
- #options.merge!(label)
279
-
244
+ options.merge!(common_options)
280
245
 
281
246
  return Gchart.bar(options)
282
247
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: analytica
3
3
  version: !ruby/object:Gem::Version
4
- hash: 7
4
+ hash: 5
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 12
10
- version: 0.0.12
9
+ - 13
10
+ version: 0.0.13
11
11
  platform: ruby
12
12
  authors:
13
13
  - Raeez Lorgat
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-08-14 00:00:00 +00:00
18
+ date: 2010-08-14 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency