lilygraph 0.4.4 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/VERSION +1 -1
  2. data/lib/lilygraph.rb +50 -14
  3. data/lilygraph.gemspec +2 -2
  4. metadata +2 -2
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.4
1
+ 0.5.0
data/lib/lilygraph.rb CHANGED
@@ -19,7 +19,9 @@ class Lilygraph
19
19
  :width => '100%',
20
20
  :indent => 2,
21
21
  :padding => 14,
22
+ :legend => :right,
22
23
  :bar_text => true,
24
+ :type => :bar,
23
25
  :viewbox => {
24
26
  :width => 800,
25
27
  :height => 600
@@ -139,20 +141,20 @@ class Lilygraph
139
141
  line_x1 = @options[:margin][:left] + 1
140
142
  line_x2 = @options[:viewbox][:width] - @options[:margin][:right] - 1
141
143
 
142
- text_x = @options[:margin][:left] - 25
144
+ text_x = @options[:margin][:left] - 5
143
145
 
144
- xml.text 0, :x => text_x, :y => (@options[:viewbox][:height] - @options[:margin][:bottom] + 4), 'stroke-width' => 0.5
146
+ xml.text 0, :x => text_x, :y => (@options[:viewbox][:height] - @options[:margin][:bottom] + 4), 'stroke-width' => 0.5, 'text-anchor' => 'end'
145
147
 
146
- 1.upto((max / 10) - 1) do |line_number|
148
+ 1.upto((max / division) - 1) do |line_number|
147
149
  y = (@options[:margin][:top] + (line_number * dy)).round
148
150
  xml.line :x1 => line_x1, :y1 => y, :x2 => line_x2, :y2 => y, :stroke => '#666666'
149
- xml.text max - line_number * 10, :x => text_x, :y => y + 4, 'stroke-width' => 0.5
151
+ xml.text max - line_number * division, :x => text_x, :y => y + 4, 'stroke-width' => 0.5, 'text-anchor' => 'end'
150
152
 
151
153
  # Smaller Line
152
154
  xml.line(:x1 => line_x1, :y1 => y + (0.5 * dy), :x2 => line_x2, :y2 => y + (0.5 * dy), :stroke => '#999999') if max < 55
153
155
  end
154
156
 
155
- xml.text max, :x => text_x, :y => @options[:margin][:top] + 4, 'stroke-width' => 0.5
157
+ xml.text max, :x => text_x, :y => @options[:margin][:top] + 4, 'stroke-width' => 0.5, 'text-anchor' => 'end'
156
158
  # Smaller Line
157
159
  xml.line(:x1 => line_x1, :y1 => @options[:margin][:top] + (0.5 * dy), :x2 => line_x2, :y2 => @options[:margin][:top] + (0.5 * dy), :stroke => '#999999') if max < 55
158
160
  end
@@ -168,6 +170,8 @@ class Lilygraph
168
170
 
169
171
  # Bars
170
172
  xml.g 'font-size' => '10px', 'stroke-width' => 0.3 do |xml|
173
+ last_spot = []
174
+
171
175
  @data.each_with_index do |data, data_index|
172
176
  data = Array(data)
173
177
  width = dx - @options[:padding]
@@ -181,7 +185,7 @@ class Lilygraph
181
185
  @colors.call(data_index, number_index, @data.size, data.size)
182
186
  elsif @colors.class == Array
183
187
  first = @colors[data_index % (@colors.size)]
184
-
188
+
185
189
  if first.class == Array
186
190
  first[number_index % (first.size)]
187
191
  else
@@ -191,20 +195,40 @@ class Lilygraph
191
195
  @colors
192
196
  end
193
197
 
194
- height = ((dy / 10.0) * number).round
198
+ height = ((dy / division) * number).round
195
199
 
196
200
  bar_x = (x + ((dx - width) / 2.0) + (number_index * bar_width)).round
197
201
  bar_y = @options[:viewbox][:height] - @options[:margin][:bottom] - height
198
202
 
199
- xml.rect :fill => color, :stroke => color, 'stroke-width' => 0, :x => bar_x, :width => bar_width, :y => bar_y, :height => height - 1
203
+
204
+ case @options[:type]
205
+ when :bar then
206
+ xml.rect :fill => color, :stroke => color, 'stroke-width' => 0, :x => bar_x, :width => bar_width, :y => bar_y, :height => height - 1
207
+ when :line then
208
+ if last_spot[number_index]
209
+ xml.line(:x1 => last_spot[number_index][:x], :y1 => last_spot[number_index][:y], :x2 => bar_x, :y2 => bar_y,
210
+ :fill => color, :stroke => color, 'stroke-width' => 2.0)
211
+ end
212
+ xml.circle :cx => bar_x, :cy => bar_y, :fill => color, :stroke => color, :r => bar_width * 1.5
213
+ end
214
+
215
+ last_spot[number_index] = { :x => bar_x, :y => bar_y }
200
216
  end
217
+ end
218
+
219
+ @data.each_with_index do |data, data_index|
220
+ data = Array(data)
221
+ width = dx - @options[:padding]
222
+ bar_width = (width / Float(data.size)).round
223
+
224
+ x = (@options[:margin][:left] + (dx * data_index)).round
201
225
 
202
226
  # Text
203
227
  if @options[:bar_text]
204
228
  last_bar_height = false
205
229
  data.each_with_index do |number, number_index|
206
230
  if number > 0
207
- height = ((dy / 10.0) * number).round
231
+ height = ((dy / division) * number).round
208
232
 
209
233
  bar_x = (x + ((dx - width) / 2.0) + (number_index * bar_width)).round
210
234
  text_x = (bar_x + (bar_width / 2.0)).round
@@ -230,7 +254,11 @@ class Lilygraph
230
254
 
231
255
  # Legend
232
256
  if @legend
233
- legend_x = @options[:viewbox][:width] - (3 * @options[:margin][:right])
257
+ if @options[:legend] == :right
258
+ legend_x = @options[:viewbox][:width] - (3 * @options[:margin][:right])
259
+ else
260
+ legend_x = (@options[:margin][:left] * 1.5).round
261
+ end
234
262
  legend_y = (@options[:margin][:top] / 2) + @options[:margin][:top]
235
263
  xml.rect :fill => '#ffffff', :stroke => '#000000', 'stroke-width' => 2, :x => legend_x, :y => legend_y, :width => (2.5 * @options[:margin][:right]), :height => (@legend.size * 15) + 16
236
264
 
@@ -253,10 +281,18 @@ class Lilygraph
253
281
 
254
282
  private
255
283
 
284
+ def data_max
285
+ @data.map do |num|
286
+ num.respond_to?(:max) ? num.max : num
287
+ end.max || 0
288
+ end
289
+
290
+ def division
291
+ (10 ** Math.log10(data_max).floor) / 10
292
+ end
293
+
256
294
  def max
257
- ((((@data.map do |num|
258
- num.respond_to?(:max) ? num.max : num
259
- end.max || 0) + 1) / 10.0).ceil * 10).round
295
+ (((data_max + (division / 10)) / Float(division)).ceil * Float(division)).round
260
296
  end
261
297
 
262
298
  def graph_height
@@ -276,6 +312,6 @@ class Lilygraph
276
312
  end
277
313
 
278
314
  def dy
279
- (graph_height * 10.0) / Float(max)
315
+ (graph_height * division) / Float(max)
280
316
  end
281
317
  end
data/lilygraph.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{lilygraph}
8
- s.version = "0.4.4"
8
+ s.version = "0.5.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Christopher Giroir"]
12
- s.date = %q{2009-08-24}
12
+ s.date = %q{2009-08-26}
13
13
  s.description = %q{Lilygraph is a Ruby library for creating svg charts and graphs based on XmlBuilder.}
14
14
  s.email = %q{kelsin@valefor.com}
15
15
  s.extra_rdoc_files = [
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lilygraph
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.4
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christopher Giroir
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-08-24 00:00:00 -04:00
12
+ date: 2009-08-26 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency