lilygraph 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.4.0
@@ -220,6 +220,19 @@ An array of labels to use on the y axis. Make sure you have the right
220
220
  number of labels. The size of this array should = the size of the data
221
221
  array.
222
222
 
223
+ </td>
224
+ </tr>
225
+ <tr class="top-aligned-row context-row">
226
+ <td class="context-item-name">legend</td>
227
+ <td class="context-item-value">&nbsp;[RW]&nbsp;</td>
228
+ <td class="context-item-desc">
229
+ This allows you to make a legend for your graph. Just pass in a hash of
230
+ color =&gt; text pairs. Set to nil if you don&#8216;t want a legend. No
231
+ legend is the default.
232
+
233
+ <pre>
234
+ graph.legend = { '#ff0000' =&gt; 'Chris', '#00ff00' =&gt; 'Caitlin' }
235
+ </pre>
223
236
  </td>
224
237
  </tr>
225
238
  </table>
@@ -10,14 +10,16 @@
10
10
  <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
11
11
  </head>
12
12
  <body class="standalone-code">
13
- <pre><span class="ruby-comment cmt"># File lib/lilygraph.rb, line 77</span>
13
+ <pre><span class="ruby-comment cmt"># File lib/lilygraph.rb, line 84</span>
14
14
  <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">initialize</span>(<span class="ruby-identifier">options</span> = {})
15
15
  <span class="ruby-ivar">@options</span> = <span class="ruby-constant">DEFAULT_OPTIONS</span>.<span class="ruby-identifier">merge</span>(<span class="ruby-identifier">options</span>)
16
16
  <span class="ruby-ivar">@data</span> = []
17
17
  <span class="ruby-ivar">@labels</span> = []
18
18
 
19
+ <span class="ruby-ivar">@legend</span> = <span class="ruby-keyword kw">nil</span>
20
+
19
21
  <span class="ruby-ivar">@colors</span> = <span class="ruby-constant">Proc</span>.<span class="ruby-identifier">new</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">data_index</span>, <span class="ruby-identifier">number_index</span>, <span class="ruby-identifier">data_size</span>, <span class="ruby-identifier">number_size</span><span class="ruby-operator">|</span>
20
- <span class="ruby-constant">Color</span><span class="ruby-operator">::</span><span class="ruby-constant">HSL</span>.<span class="ruby-identifier">from_fraction</span>(<span class="ruby-constant">Float</span>(<span class="ruby-identifier">data_index</span>) <span class="ruby-operator">/</span> <span class="ruby-constant">Float</span>(<span class="ruby-identifier">data_size</span>), <span class="ruby-value">1.0</span>, <span class="ruby-value">0</span><span class="ruby-value">.4</span> <span class="ruby-operator">+</span> (<span class="ruby-constant">Float</span>(<span class="ruby-identifier">number_index</span>) <span class="ruby-operator">/</span> <span class="ruby-constant">Float</span>(<span class="ruby-identifier">number_size</span>) <span class="ruby-operator">*</span> <span class="ruby-value">0</span><span class="ruby-value">.4</span>)).<span class="ruby-identifier">to_rgb</span>.<span class="ruby-identifier">html</span>
22
+ <span class="ruby-constant">Color</span><span class="ruby-operator">::</span><span class="ruby-constant">HSL</span>.<span class="ruby-identifier">from_fraction</span>(<span class="ruby-constant">Float</span>(<span class="ruby-identifier">data_index</span>) <span class="ruby-operator">/</span> <span class="ruby-constant">Float</span>(<span class="ruby-identifier">data_size</span>), <span class="ruby-value">1.0</span>, <span class="ruby-value">0</span><span class="ruby-value">.4</span> <span class="ruby-operator">+</span> (<span class="ruby-constant">Float</span>(<span class="ruby-identifier">number_index</span>) <span class="ruby-operator">/</span> <span class="ruby-constant">Float</span>(<span class="ruby-identifier">number_size</span>) <span class="ruby-operator">*</span> <span class="ruby-value">0</span><span class="ruby-value">.4</span>)).<span class="ruby-identifier">html</span>
21
23
  <span class="ruby-keyword kw">end</span>
22
24
  <span class="ruby-keyword kw">end</span></pre>
23
25
  </body>
@@ -10,7 +10,7 @@
10
10
  <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
11
11
  </head>
12
12
  <body class="standalone-code">
13
- <pre><span class="ruby-comment cmt"># File lib/lilygraph.rb, line 89</span>
13
+ <pre><span class="ruby-comment cmt"># File lib/lilygraph.rb, line 98</span>
14
14
  <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">update_options</span>(<span class="ruby-identifier">options</span> = {})
15
15
  <span class="ruby-ivar">@options</span> = <span class="ruby-ivar">@options</span>.<span class="ruby-identifier">merge</span>(<span class="ruby-identifier">options</span>)
16
16
  <span class="ruby-keyword kw">end</span></pre>
@@ -10,7 +10,7 @@
10
10
  <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
11
11
  </head>
12
12
  <body class="standalone-code">
13
- <pre><span class="ruby-comment cmt"># File lib/lilygraph.rb, line 101</span>
13
+ <pre><span class="ruby-comment cmt"># File lib/lilygraph.rb, line 110</span>
14
14
  <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">render</span>
15
15
  <span class="ruby-identifier">output</span> = <span class="ruby-value str">&quot;&quot;</span>
16
16
  <span class="ruby-identifier">xml</span> = <span class="ruby-constant">Builder</span><span class="ruby-operator">::</span><span class="ruby-constant">XmlMarkup</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">:target</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">output</span>, <span class="ruby-identifier">:indent</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-ivar">@options</span>[<span class="ruby-identifier">:indent</span>])
@@ -120,6 +120,19 @@
120
120
  <span class="ruby-keyword kw">end</span>
121
121
  <span class="ruby-keyword kw">end</span>
122
122
 
123
+ <span class="ruby-comment cmt"># Legend</span>
124
+ <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@legend</span>
125
+ <span class="ruby-identifier">legend_x</span> = <span class="ruby-ivar">@options</span>[<span class="ruby-identifier">:viewbox</span>][<span class="ruby-identifier">:width</span>] <span class="ruby-operator">-</span> (<span class="ruby-value">3</span> <span class="ruby-operator">*</span> <span class="ruby-ivar">@options</span>[<span class="ruby-identifier">:margin</span>][<span class="ruby-identifier">:right</span>])
126
+ <span class="ruby-identifier">legend_y</span> = <span class="ruby-ivar">@options</span>[<span class="ruby-identifier">:margin</span>][<span class="ruby-identifier">:top</span>] <span class="ruby-operator">/</span> <span class="ruby-value">2</span>
127
+ <span class="ruby-identifier">xml</span>.<span class="ruby-identifier">rect</span> <span class="ruby-identifier">:fill</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-value str">'#ffffff'</span>, <span class="ruby-identifier">:stroke</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-value str">'#000000'</span>, <span class="ruby-value str">'stroke-width'</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-value">2</span>, <span class="ruby-identifier">:x</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">legend_x</span>, <span class="ruby-identifier">:y</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">legend_y</span>, <span class="ruby-identifier">:width</span> =<span class="ruby-operator">&gt;</span> (<span class="ruby-value">2.5</span> <span class="ruby-operator">*</span> <span class="ruby-ivar">@options</span>[<span class="ruby-identifier">:margin</span>][<span class="ruby-identifier">:right</span>]), <span class="ruby-identifier">:height</span> =<span class="ruby-operator">&gt;</span> (<span class="ruby-ivar">@legend</span>.<span class="ruby-identifier">size</span> <span class="ruby-operator">*</span> <span class="ruby-value">15</span>) <span class="ruby-operator">+</span> <span class="ruby-value">16</span>
128
+
129
+ <span class="ruby-ivar">@legend</span>.<span class="ruby-identifier">sort</span>.<span class="ruby-identifier">each_with_index</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">data</span>, <span class="ruby-identifier">index</span><span class="ruby-operator">|</span>
130
+ <span class="ruby-identifier">color</span>, <span class="ruby-identifier">label</span> = <span class="ruby-identifier">data</span>
131
+ <span class="ruby-identifier">xml</span>.<span class="ruby-identifier">rect</span> <span class="ruby-identifier">:fill</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">color</span>, <span class="ruby-identifier">:stroke</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">color</span>, <span class="ruby-value str">'stroke-width'</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-value">0</span>, <span class="ruby-identifier">:x</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">legend_x</span> <span class="ruby-operator">+</span> <span class="ruby-value">10</span>, <span class="ruby-identifier">:y</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">legend_y</span> <span class="ruby-operator">+</span> <span class="ruby-value">10</span> <span class="ruby-operator">+</span> (<span class="ruby-identifier">index</span> <span class="ruby-operator">*</span> <span class="ruby-value">15</span>), <span class="ruby-identifier">:width</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-value">35</span>, <span class="ruby-identifier">:height</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-value">10</span>
132
+ <span class="ruby-identifier">xml</span>.<span class="ruby-identifier">text</span> <span class="ruby-identifier">label</span>, <span class="ruby-identifier">:x</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">legend_x</span> <span class="ruby-operator">+</span> <span class="ruby-value">70</span>, <span class="ruby-identifier">:y</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">legend_y</span> <span class="ruby-operator">+</span> <span class="ruby-value">18</span> <span class="ruby-operator">+</span> (<span class="ruby-identifier">index</span> <span class="ruby-operator">*</span> <span class="ruby-value">15</span>), <span class="ruby-value str">'text-anchor'</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-value str">'left'</span>
133
+ <span class="ruby-keyword kw">end</span>
134
+ <span class="ruby-keyword kw">end</span>
135
+
123
136
  <span class="ruby-comment cmt"># Yield in case they want to do some custom drawing and have a block ready</span>
124
137
  <span class="ruby-keyword kw">yield</span>(<span class="ruby-identifier">xml</span>, <span class="ruby-ivar">@options</span>) <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">block_given?</span>
125
138
 
data/doc/created.rid CHANGED
@@ -1 +1 @@
1
- Thu, 20 Aug 2009 16:27:09 -0400
1
+ Thu, 20 Aug 2009 17:21:08 -0400
@@ -56,7 +56,7 @@
56
56
  </tr>
57
57
  <tr class="top-aligned-row">
58
58
  <td><strong>Last Update:</strong></td>
59
- <td>Thu Aug 20 16:27:03 -0400 2009</td>
59
+ <td>Thu Aug 20 17:21:05 -0400 2009</td>
60
60
  </tr>
61
61
  </table>
62
62
  </div>
data/lib/lilygraph.rb CHANGED
@@ -61,7 +61,14 @@ class Lilygraph
61
61
  # Color::HSL.from_fraction(Float(data_index) / Float(data_size), 1.0, 0.4 + (Float(number_index) / Float(number_size) * 0.4)).to_rgb.html
62
62
  # end
63
63
  attr_accessor :colors
64
-
64
+
65
+ # This allows you to make a legend for your graph. Just pass in a hash of
66
+ # color => text pairs. Set to nil if you don't want a legend. No legend is the
67
+ # default.
68
+ #
69
+ # graph.legend = { '#ff0000' => 'Chris', '#00ff00' => 'Caitlin' }
70
+ attr_accessor :legend
71
+
65
72
  # Returns a new graph creator with some default options specified via a hash:
66
73
  # height:: String to use as height parameter on the svg tag. Default is <tt>'100%'</tt>.
67
74
  # width:: String to use as width parameter on the svg tag. Default is <tt>'100%'</tt>.
@@ -79,6 +86,8 @@ class Lilygraph
79
86
  @data = []
80
87
  @labels = []
81
88
 
89
+ @legend = nil
90
+
82
91
  @colors = Proc.new do |data_index, number_index, data_size, number_size|
83
92
  Color::HSL.from_fraction(Float(data_index) / Float(data_size), 1.0, 0.4 + (Float(number_index) / Float(number_size) * 0.4)).html
84
93
  end
@@ -207,6 +216,19 @@ class Lilygraph
207
216
  end
208
217
  end
209
218
 
219
+ # Legend
220
+ if @legend
221
+ legend_x = @options[:viewbox][:width] - (3 * @options[:margin][:right])
222
+ legend_y = @options[:margin][:top] / 2
223
+ 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
+
225
+ @legend.sort.each_with_index do |data, index|
226
+ color, label = data
227
+ xml.rect :fill => color, :stroke => color, 'stroke-width' => 0, :x => legend_x + 10, :y => legend_y + 10 + (index * 15), :width => 35, :height => 10
228
+ xml.text label, :x => legend_x + 70, :y => legend_y + 18 + (index * 15), 'text-anchor' => 'left'
229
+ end
230
+ end
231
+
210
232
  # Yield in case they want to do some custom drawing and have a block ready
211
233
  yield(xml, @options) if block_given?
212
234
 
data/lilygraph.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{lilygraph}
8
- s.version = "0.3.0"
8
+ s.version = "0.4.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"]
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.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christopher Giroir