Kelsin-lilygraph 0.4.2 → 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 +72 -24
  3. data/lilygraph.gemspec +2 -2
  4. metadata +2 -2
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.2
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
@@ -167,7 +169,9 @@ class Lilygraph
167
169
  end
168
170
 
169
171
  # Bars
170
- xml.g 'font-size' => '8px', 'stroke-width' => 0.3 do |xml|
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,26 +195,58 @@ 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]
228
+ last_bar_height = false
204
229
  data.each_with_index do |number, number_index|
205
- height = ((dy / 10.0) * number).round
206
-
207
- bar_x = (x + ((dx - width) / 2.0) + (number_index * bar_width)).round
208
- text_x = (bar_x + (bar_width / 2.0)).round
209
-
210
- bar_y = @options[:viewbox][:height] - @options[:margin][:bottom] - height
211
- text_y = bar_y - 3
212
-
213
- xml.text number, :x => text_x, :y => text_y, 'text-anchor' => 'middle'
230
+ if number > 0
231
+ height = ((dy / division) * number).round
232
+
233
+ bar_x = (x + ((dx - width) / 2.0) + (number_index * bar_width)).round
234
+ text_x = (bar_x + (bar_width / 2.0)).round
235
+
236
+ bar_y = @options[:viewbox][:height] - @options[:margin][:bottom] - height
237
+ text_y = bar_y - 3
238
+
239
+ if last_bar_height && (last_bar_height - height).abs < 14
240
+ text_y -= (14 - (height - last_bar_height))
241
+ last_bar_height = false
242
+ else
243
+ last_bar_height = height
244
+ end
245
+
246
+ xml.text number, :x => text_x, :y => text_y, 'text-anchor' => 'middle'
247
+ else
248
+ last_bar_height = false
249
+ end
214
250
  end
215
251
  end
216
252
  end
@@ -218,8 +254,12 @@ class Lilygraph
218
254
 
219
255
  # Legend
220
256
  if @legend
221
- legend_x = @options[:viewbox][:width] - (3 * @options[:margin][:right])
222
- legend_y = @options[:margin][:top] / 2
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
262
+ legend_y = (@options[:margin][:top] / 2) + @options[:margin][:top]
223
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
224
264
 
225
265
  @legend.sort.each_with_index do |data, index|
@@ -241,10 +281,18 @@ class Lilygraph
241
281
 
242
282
  private
243
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
+
244
294
  def max
245
- ((((@data.map do |num|
246
- num.respond_to?(:max) ? num.max : num
247
- end.max || 0) + 1) / 10.0).ceil * 10).round
295
+ (((data_max + (division / 10)) / Float(division)).ceil * Float(division)).round
248
296
  end
249
297
 
250
298
  def graph_height
@@ -264,6 +312,6 @@ class Lilygraph
264
312
  end
265
313
 
266
314
  def dy
267
- (graph_height * 10.0) / Float(max)
315
+ (graph_height * division) / Float(max)
268
316
  end
269
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.2"
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-21}
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: Kelsin-lilygraph
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
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-21 00:00:00 -07:00
12
+ date: 2009-08-26 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency