grosser-db_graph 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -3,9 +3,8 @@ Gem/Rails plugin to generate graphs from all your date fields, beautifully simpl
3
3
  Examples
4
4
  ========
5
5
  Weeks and months
6
- ![weeks](http://chart.apis.google.com/chart?chxl=0:|0||||||||||10||||||||||20||||||||||30||||||||||40||||||||||50|||1:|6|16|25|35|45|54|64|74|83|93&cht=lc&chs=445x400&chdl=Product+created_at|Product+updated_at&chd=s:JTKPKLKMUNPQHQSKRNLGPPLKUHNNPFJLLPNJINQex19u5xrrNFKLD,HLJMONLJLNHPQNOLNPMMINNKPMPGKLFJLRQLKOOtsxxvw137MHOLF&chco=333300,bbbb33&chxt=x,y)
7
- ![months](http://chart.apis.google.com/chart?chxl=0:|1|2|3|4|5|6|7|8|9|10|11|12|1:|67|98|130|161|193|224|256|287|319|350&cht=lc&chs=445x400&chdl=Product+created_at|Product+updated_at&chd=s:QOQROONPM92L,OMPPOOMPO77N&chco=884466,22dddd&chxt=x,y)
8
-
6
+ ![weeks](http://chart.apis.google.com/chart?chxl=0:|0||||||||||10||||||||||20||||||||||30||||||||||40||||||||||50|||1:|5|14|24|33|42|52|61|70|80|89&cht=lc&chs=445x400&chd=s:MOQLIKNMKMQNROTLIMLMNPPOKPJKKLNORNJKKOOo281ztr1yTROND,IMQJORQJKPMKNNOLONKKMKLMNNVMPKNLQONMKORkv3t7v609JGLNH&chco=dd0099,aa9944&chxt=x,y)
7
+ ![months](http://chart.apis.google.com/chart?chxl=0:|1|2|3|4|5|6|7|8|9|10|11|12|1:|68|100|133|165|198|230|263|295|328|360&cht=lc&chs=445x400&chdl=Product+created_at|Product+updated_at&chd=s:PMQNOOMON9yQ,OMONNNQPQ34L&chco=dd0099,aa9944&chxt=x,y)
9
8
 
10
9
  Install
11
10
  =======
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :minor: 1
3
- :patch: 4
3
+ :patch: 5
4
4
  :major: 0
data/lib/db_graph/line.rb CHANGED
@@ -33,7 +33,7 @@ module DBGraph
33
33
  size = @options[:size] || '600x500'
34
34
  GoogleChart::LineChart.new(size, nil, false) do |line|
35
35
  for name, hash in data
36
- line.data(name, self.class.filled_and_sorted_values(hash, x_values), random_color)
36
+ line.data(name, klass.filled_and_sorted_values(hash, x_values), klass.color_for(name))
37
37
  end
38
38
  line.axis :x, :labels => x_labels
39
39
  line.axis :y, :labels => y_labels
@@ -51,7 +51,7 @@ module DBGraph
51
51
 
52
52
  def y_labels
53
53
  values = []
54
- data.each{|name,hash|values += self.class.filled_and_sorted_values(hash, x_values)}
54
+ data.each{|name,hash|values += klass.filled_and_sorted_values(hash, x_values)}
55
55
  distribute_evently(values, NUM_Y_LABELS)
56
56
  end
57
57
 
@@ -63,6 +63,10 @@ module DBGraph
63
63
 
64
64
  private
65
65
 
66
+ def klass
67
+ self.class
68
+ end
69
+
66
70
  def x_values
67
71
  case @style
68
72
  when :minutes then 0..59
@@ -74,15 +78,20 @@ module DBGraph
74
78
  end.to_a
75
79
  end
76
80
 
77
- def random_color
78
- [1,2,3].map{ (('0'..'9').to_a + ('a'..'f').to_a)[rand(16)] * 2 } * ''
81
+ #determines a unique color based on a name
82
+ def self.color_for(name)
83
+ name = name.inspect + "some randomness for fun"
84
+ hash = (name.hash % 1_000_000_000).to_s #get a hash of a constant size
85
+ colors = [hash[0..1], hash[2..3], hash[4..5]].map{|c| c.to_f / 100.0 * 16} #use 3 parts of the hash to get numbers from 0 to 15.99
86
+ palette = ('0'..'9').to_a + ('a'..'f').to_a #hex values 0..f
87
+ colors.map{|c| palette[c.floor].to_s * 2} * '' #each color is duplicated and the joined
79
88
  end
80
89
 
81
90
  def interval_for(time)
82
91
  interval_word = {:minutes=>:hour,:hours=>:day,:days=>:month,:weeks=>:year,:months=>:year}[@style]
83
92
  raise "style #{@style} is not supported" unless interval_word
84
93
 
85
- start = self.class.at_beginning_of_interval(time, interval_word)
94
+ start = klass.at_beginning_of_interval(time, interval_word)
86
95
  ende = start + 1.send(interval_word) - 1.second
87
96
  [start, ende]
88
97
  end
@@ -205,14 +205,23 @@ describe DBGraph::Line do
205
205
  end
206
206
  end
207
207
 
208
- describe :random_color do
209
- before do
210
- @line = DBGraph::Line.new(:months)
208
+ describe :color_for do
209
+ it "returns the same color for the same names" do
210
+ c1 = DBGraph::Line.send(:color_for,'User found_at')
211
+ c2 = DBGraph::Line.send(:color_for,'User found_at')
212
+ c1.should == c2
213
+ end
214
+
215
+ it "returns different colors for different names" do
216
+ c1 = DBGraph::Line.send(:color_for,'User found_at')
217
+ c2 = DBGraph::Line.send(:color_for,'User created_at')
218
+ c1.should_not == c2
211
219
  end
212
220
 
213
- it "includes pairs of colors" do
214
- @line.should_receive(:rand).and_return 0,2,10
215
- @line.send(:random_color).should == '0022aa'
221
+ it "returns a color for empty/nil/[] names" do
222
+ [nil,[],'',' '].each do |value|
223
+ DBGraph::Line.send(:color_for,value).length.should == 6
224
+ end
216
225
  end
217
226
  end
218
227
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grosser-db_graph
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Grosser
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-05-29 00:00:00 -07:00
12
+ date: 2009-06-06 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency