mountain-goat 0.0.12 → 0.0.13

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/flotilla.rb +136 -135
  2. data/lib/mountain-goat/version.rb +1 -1
  3. metadata +3 -3
data/lib/flotilla.rb CHANGED
@@ -4,61 +4,44 @@ rescue LoadError
4
4
  p "Flotilla will not work without the 'json' gem"
5
5
  end
6
6
 
7
- module ActionView
7
+ module Flotilla
8
8
  module Helpers
9
- module ScriptaculousHelper
10
9
 
11
- # Insert a flot chart into the page. <tt>placeholder</tt> should be the
12
- # name of the div that will hold the chart, <tt>collections</tt> is a hash
13
- # of legends (as strings) and datasets with options as hashes, <tt>options</tt>
14
- # contains graph-wide options.
15
- #
16
- # Example usage:
17
- #
18
- # chart("graph_div", {
19
- # "January" => { :collection => @january, :x => :day, :y => :sales, :options => { :lines => {:show =>true}} },
20
- # "February" => { :collection => @february, :x => :day, :y => :sales, :options => { :points => {:show =>true} } },
21
- # :grid => { :backgroundColor => "#fffaff" })
22
- #
23
- # Options:
24
- # :js_includes - includes flot library inline
25
- # :js_tags - wraps resulting javascript in javascript tags if true. Defaults to true.
26
- # :placeholder_tag - appends a placeholder div for graph
27
- # :placeholder_size - specifys the size of the placeholder div
28
- def chart(placeholder, series, options = {}, html_options = {})
29
- html_options.reverse_merge!({ :js_includes => true, :js_tags => true, :placeholder_tag => true, :placeholder_size => "800x300", :pie_hover => false, :pie_hover_absolute => false })
30
- width, height = html_options[:placeholder_size].split("x") if html_options[:placeholder_size].respond_to?(:split)
31
- additional_js = get_additional_js(placeholder, html_options)
32
-
33
- data, x_is_date, y_is_date = series_to_json(series)
34
- if x_is_date
35
- options[:xaxis] ||= {}
36
- options[:xaxis].merge!({ :mode => 'time' })
37
- end
38
- if y_is_date
39
- options[:yaxis] ||= {}
40
- options[:yaxis].merge!({ :mode => 'time' })
41
- end
10
+ # Insert a flot chart into the page. <tt>placeholder</tt> should be the
11
+ # name of the div that will hold the chart, <tt>collections</tt> is a hash
12
+ # of legends (as strings) and datasets with options as hashes, <tt>options</tt>
13
+ # contains graph-wide options.
14
+ #
15
+ # Example usage:
16
+ #
17
+ # chart("graph_div", {
18
+ # "January" => { :collection => @january, :x => :day, :y => :sales, :options => { :lines => {:show =>true}} },
19
+ # "February" => { :collection => @february, :x => :day, :y => :sales, :options => { :points => {:show =>true} } },
20
+ # :grid => { :backgroundColor => "#fffaff" })
21
+ #
22
+ # Options:
23
+ # :js_includes - includes flot library inline
24
+ # :js_tags - wraps resulting javascript in javascript tags if true. Defaults to true.
25
+ # :placeholder_tag - appends a placeholder div for graph
26
+ # :placeholder_size - specifys the size of the placeholder div
27
+ def chart(placeholder, series, options = {}, html_options = {})
28
+ html_options.reverse_merge!({ :js_includes => true, :js_tags => true, :placeholder_tag => true, :placeholder_size => "800x300", :pie_hover => false, :pie_hover_absolute => false })
29
+ width, height = html_options[:placeholder_size].split("x") if html_options[:placeholder_size].respond_to?(:split)
30
+ additional_js = get_additional_js(placeholder, html_options)
31
+
32
+ data, x_is_date, y_is_date = series_to_json(series)
33
+ if x_is_date
34
+ options[:xaxis] ||= {}
35
+ options[:xaxis].merge!({ :mode => 'time' })
36
+ end
37
+ if y_is_date
38
+ options[:yaxis] ||= {}
39
+ options[:yaxis].merge!({ :mode => 'time' })
40
+ end
42
41
 
43
- if html_options[:js_includes]
44
- chart_js = <<-EOF
45
- <script type="text/javascript">
46
- (function () {
47
- if (typeof(jQuery) == 'undefined') {
48
- alert("Please include jQuery to view flot");
49
- } else {
50
- if (typeof(jQuery.plot) == 'undefined') {
51
- $('##{placeholder}').html("<span class='flot-error'>Please include jQuery.plot to view flot</span>");
52
- } else {
53
- var plot = jQuery.plot($('##{placeholder}'), #{data}, #{options.to_json});
54
- #{additional_js}
55
- }
56
- }
57
- })();
58
- </script>
59
- EOF
60
- else
61
- chart_js = <<-EOF
42
+ if html_options[:js_includes]
43
+ chart_js = <<-EOF
44
+ <script type="text/javascript">
62
45
  (function () {
63
46
  if (typeof(jQuery) == 'undefined') {
64
47
  alert("Please include jQuery to view flot");
@@ -71,99 +54,117 @@ module ActionView
71
54
  }
72
55
  }
73
56
  })();
74
- EOF
75
- end
76
-
77
- html_options[:js_tags] ? javascript_tag(chart_js) : chart_js
78
- output = html_options[:placeholder_tag] ? content_tag(:div, nil, :id => placeholder, :style => "width:#{width}px;height:#{height}px;", :class => "chart") + chart_js : chart_js
79
- output.html_safe
80
- end
57
+ </script>
58
+ EOF
59
+ else
60
+ chart_js = <<-EOF
61
+ (function () {
62
+ if (typeof(jQuery) == 'undefined') {
63
+ alert("Please include jQuery to view flot");
64
+ } else {
65
+ if (typeof(jQuery.plot) == 'undefined') {
66
+ $('##{placeholder}').html("<span class='flot-error'>Please include jQuery.plot to view flot</span>");
67
+ } else {
68
+ var plot = jQuery.plot($('##{placeholder}'), #{data}, #{options.to_json});
69
+ #{additional_js}
70
+ }
71
+ }
72
+ })();
73
+ EOF
74
+ end
75
+
76
+ html_options[:js_tags] ? javascript_tag(chart_js) : chart_js
77
+ output = html_options[:placeholder_tag] ? content_tag(:div, nil, :id => placeholder, :style => "width:#{width}px;height:#{height}px;", :class => "chart") + chart_js : chart_js
78
+ output.html_safe
79
+ end
81
80
 
82
- private
83
- def series_to_json(series)
84
- data_sets = []
85
- x_is_date, y_is_date = false, false
86
- series.each do |name, values|
87
- set, data = {}, []
88
- set[:label] = name
89
- first = values[:collection].first
90
- logger.warn "Collection is: #{values[:collection].inspect}"
91
- if first #&& !values[:collection].is_a?(Array)
92
- if first.is_a?(Hash)
93
- x_is_date = first[values[:x]].acts_like?(:date) || first[values[:x]].acts_like?(:time)
94
- y_is_date = first[values[:y]].acts_like?(:date) || first[values[:y]].acts_like?(:time)
95
- else
96
- x_is_date = first.send(values[:x]).acts_like?(:date) || first.send(values[:x]).acts_like?(:time)
97
- y_is_date = first.send(values[:y]).acts_like?(:date) || first.send(values[:y]).acts_like?(:time)
98
- end
81
+ private
82
+ def series_to_json(series)
83
+ data_sets = []
84
+ x_is_date, y_is_date = false, false
85
+ series.each do |name, values|
86
+ set, data = {}, []
87
+ set[:label] = name
88
+ first = values[:collection].first
89
+ logger.warn "Collection is: #{values[:collection].inspect}"
90
+ if first #&& !values[:collection].is_a?(Array)
91
+ if first.is_a?(Hash)
92
+ x_is_date = first[values[:x]].acts_like?(:date) || first[values[:x]].acts_like?(:time)
93
+ y_is_date = first[values[:y]].acts_like?(:date) || first[values[:y]].acts_like?(:time)
94
+ else
95
+ x_is_date = first.send(values[:x]).acts_like?(:date) || first.send(values[:x]).acts_like?(:time)
96
+ y_is_date = first.send(values[:y]).acts_like?(:date) || first.send(values[:y]).acts_like?(:time)
99
97
  end
100
- values[:collection].each do |object|
101
- if values[:collection].is_a?(Hash) || object.is_a?(Hash)
102
- x_value, y_value = object[values[:x]], object[values[:y]]
103
- #logger.warn "A: Object is: #{object}, x,y: #{[x_value, y_value]} from #{[values[:x], values[:y]]}"
104
- else
105
- x_value, y_value = object.send(values[:x]), object.send(values[:y])
106
- #logger.warn "B: Object is: #{object}, x,y: #{[x_value, y_value]}"
107
- end
108
- x = x_is_date ? x_value.to_time.to_i * 1000 : x_value.to_f
109
- y = y_is_date ? y_value.to_time.to_i * 1000 : y_value.to_f
110
- #logger.warn "Tally x,y: #{[x, y]}"
111
- data << [x,y]
98
+ end
99
+ values[:collection].each do |object|
100
+ if values[:collection].is_a?(Hash) || object.is_a?(Hash)
101
+ x_value, y_value = object[values[:x]], object[values[:y]]
102
+ #logger.warn "A: Object is: #{object}, x,y: #{[x_value, y_value]} from #{[values[:x], values[:y]]}"
103
+ else
104
+ x_value, y_value = object.send(values[:x]), object.send(values[:y])
105
+ #logger.warn "B: Object is: #{object}, x,y: #{[x_value, y_value]}"
112
106
  end
113
- set[:data] = data
114
- values[:options].each {|option, parameters| set[option] = parameters } if values[:options]
115
- data_sets << set
107
+ x = x_is_date ? x_value.to_time.to_i * 1000 : x_value.to_f
108
+ y = y_is_date ? y_value.to_time.to_i * 1000 : y_value.to_f
109
+ #logger.warn "Tally x,y: #{[x, y]}"
110
+ data << [x,y]
116
111
  end
117
- return data_sets.to_json, x_is_date, y_is_date
112
+ set[:data] = data
113
+ values[:options].each {|option, parameters| set[option] = parameters } if values[:options]
114
+ data_sets << set
118
115
  end
116
+ return data_sets.to_json, x_is_date, y_is_date
117
+ end
119
118
 
120
- def get_additional_js(placeholder, options)
121
- res = ""
122
- if options[:pie_hover]
123
- res << <<-EOF
124
- function showTooltip(x, y, contents) {
125
- $('<div id="tooltip">' + contents + '</div>').css( {
126
- position: 'absolute',
127
- //display: 'none',
128
- top: y + 5,
129
- left: x + 5,
130
- border: '1px solid #fdd',
131
- padding: '2px',
132
- 'background-color': '#fee',
133
- opacity: 0.80
134
- }).appendTo("body");//.fadeIn(200);
135
- }
136
-
137
- var previousPoint = null;
138
- $('##{placeholder}').bind('mouseout', function() {
139
- plot.unhighlight();
140
- $("#tooltip").remove();
141
- $(this).data('previous-post', -1);
142
- });
143
-
144
- $('##{placeholder}').bind('plothover', function(event, pos, item) {
145
- if (item) {
146
- if ($(this).data('previous-post') != item.seriesIndex) {
147
- plot.unhighlight();
148
- plot.highlight(item.series, item.datapoint);
149
- $(this).data('previous-post', item.seriesIndex);
150
- }
151
- $("#tooltip").remove();
152
- #{ options[:line_hover_absolute] ? "y = 'on ' + (new Date(item.datapoint[0])).toDateString() + ': ' + item.datapoint[1] + ' #{options[:item_title] || ''}'" : !options[:pie_hover_absolute] ? "y = item.datapoint[0].toFixed(0) + '%';" : "y = item.datapoint[1][0][1] + ' #{options[:item_title] || ''}'" }
153
- showTooltip(pos.pageX, pos.pageY, item.series.label + " " + y);
154
- } else {
155
- //console.log('unhighlight (3)');
119
+ def get_additional_js(placeholder, options)
120
+ res = ""
121
+ if options[:pie_hover]
122
+ res << <<-EOF
123
+ function showTooltip(x, y, contents) {
124
+ $('<div id="tooltip">' + contents + '</div>').css( {
125
+ position: 'absolute',
126
+ //display: 'none',
127
+ top: y + 5,
128
+ left: x + 5,
129
+ border: '1px solid #fdd',
130
+ padding: '2px',
131
+ 'background-color': '#fee',
132
+ opacity: 0.80
133
+ }).appendTo("body");//.fadeIn(200);
134
+ }
135
+
136
+ var previousPoint = null;
137
+ $('##{placeholder}').bind('mouseout', function() {
138
+ plot.unhighlight();
139
+ $("#tooltip").remove();
140
+ $(this).data('previous-post', -1);
141
+ });
142
+
143
+ $('##{placeholder}').bind('plothover', function(event, pos, item) {
144
+ if (item) {
145
+ if ($(this).data('previous-post') != item.seriesIndex) {
156
146
  plot.unhighlight();
157
- $("#tooltip").remove();
158
- previousPost = $(this).data('previous-post', -1);
147
+ plot.highlight(item.series, item.datapoint);
148
+ $(this).data('previous-post', item.seriesIndex);
159
149
  }
160
- });
161
- EOF
162
- end
163
-
164
- res
150
+ $("#tooltip").remove();
151
+ #{ options[:line_hover_absolute] ? "y = 'on ' + (new Date(item.datapoint[0])).toDateString() + ': ' + item.datapoint[1] + ' #{options[:item_title] || ''}'" : !options[:pie_hover_absolute] ? "y = item.datapoint[0].toFixed(0) + '%';" : "y = item.datapoint[1][0][1] + ' #{options[:item_title] || ''}'" }
152
+ showTooltip(pos.pageX, pos.pageY, item.series.label + " " + y);
153
+ } else {
154
+ //console.log('unhighlight (3)');
155
+ plot.unhighlight();
156
+ $("#tooltip").remove();
157
+ previousPost = $(this).data('previous-post', -1);
158
+ }
159
+ });
160
+ EOF
165
161
  end
162
+
163
+ res
166
164
  end
167
165
  end
168
166
  end
169
167
 
168
+ class ActionView::Base
169
+ include Flotilla::Helpers
170
+ end
@@ -1,3 +1,3 @@
1
1
  class MountainGoat
2
- VERSION = "0.0.12"
2
+ VERSION = "0.0.13"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mountain-goat
3
3
  version: !ruby/object:Gem::Version
4
- hash: 7
4
+ hash: 5
5
5
  prerelease:
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
  - Geoffrey Hayes