lilygraph 0.2.5 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION +1 -1
- data/doc/classes/Lilygraph.html +50 -1
- data/doc/classes/Lilygraph.src/M000001.html +5 -1
- data/doc/classes/Lilygraph.src/M000002.html +1 -1
- data/doc/classes/Lilygraph.src/M000003.html +16 -3
- data/doc/created.rid +1 -1
- data/doc/files/lib/lilygraph_rb.html +1 -1
- data/lib/lilygraph.rb +42 -3
- data/lilygraph.gemspec +1 -1
- metadata +1 -1
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.3.0
|
data/doc/classes/Lilygraph.html
CHANGED
@@ -80,7 +80,7 @@
|
|
80
80
|
|
81
81
|
<div id="description">
|
82
82
|
<p>
|
83
|
-
This is the main
|
83
|
+
This is the main class to use if you want to create a graph!
|
84
84
|
</p>
|
85
85
|
<pre>
|
86
86
|
graph = Lilygraph.new(:title => "My Awesome Graph")
|
@@ -142,6 +142,55 @@ Default options for the initializer
|
|
142
142
|
|
143
143
|
<div class="name-list">
|
144
144
|
<table>
|
145
|
+
<tr class="top-aligned-row context-row">
|
146
|
+
<td class="context-item-name">colors</td>
|
147
|
+
<td class="context-item-value"> [RW] </td>
|
148
|
+
<td class="context-item-desc">
|
149
|
+
This allows you to set colors for the bars.
|
150
|
+
|
151
|
+
<p>
|
152
|
+
If you just want a single color:
|
153
|
+
</p>
|
154
|
+
<pre>
|
155
|
+
graph.colors='#0000aa'
|
156
|
+
</pre>
|
157
|
+
<p>
|
158
|
+
If you want to make each bar (or bar group) different colors:
|
159
|
+
</p>
|
160
|
+
<pre>
|
161
|
+
graph.colors=['#aa0000','#00aa00','#0000aa']
|
162
|
+
</pre>
|
163
|
+
<p>
|
164
|
+
If you want every bar group to be the same, but each bar in the groups to
|
165
|
+
have a different color
|
166
|
+
</p>
|
167
|
+
<pre>
|
168
|
+
graph.colors=[['#aa0000','#00aa00','#0000aa']]
|
169
|
+
</pre>
|
170
|
+
<p>
|
171
|
+
If you want to set every bar group color:
|
172
|
+
</p>
|
173
|
+
<pre>
|
174
|
+
graph.colors=[['#aa0000','#00aa00','#0000aa'],['#bb0000','#00bb00','#0000bb']]
|
175
|
+
</pre>
|
176
|
+
<p>
|
177
|
+
Last but not least you can set the color value to any object that responds
|
178
|
+
to call (like a Proc). The proc takes four arguments. data_index: The index
|
179
|
+
of the current bar (or group) number_index: The index of the current bar
|
180
|
+
INSIDE of the current bar group (always 0 if you don‘t have grouped
|
181
|
+
bars) data_size: total number of bar or groups. number_size: total number
|
182
|
+
of bars in the current group (always 1 if you don‘t have bar groups)
|
183
|
+
</p>
|
184
|
+
<p>
|
185
|
+
The default proc looks like:
|
186
|
+
</p>
|
187
|
+
<pre>
|
188
|
+
graph.colors=Proc.new do |data_index, number_index, data_size, number_size|
|
189
|
+
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
|
190
|
+
end
|
191
|
+
</pre>
|
192
|
+
</td>
|
193
|
+
</tr>
|
145
194
|
<tr class="top-aligned-row context-row">
|
146
195
|
<td class="context-item-name">data</td>
|
147
196
|
<td class="context-item-value"> [RW] </td>
|
@@ -10,11 +10,15 @@
|
|
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
|
13
|
+
<pre><span class="ruby-comment cmt"># File lib/lilygraph.rb, line 77</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
|
+
|
19
|
+
<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>
|
21
|
+
<span class="ruby-keyword kw">end</span>
|
18
22
|
<span class="ruby-keyword kw">end</span></pre>
|
19
23
|
</body>
|
20
24
|
</html>
|
@@ -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
|
13
|
+
<pre><span class="ruby-comment cmt"># File lib/lilygraph.rb, line 89</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
|
13
|
+
<pre><span class="ruby-comment cmt"># File lib/lilygraph.rb, line 101</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">""</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">></span> <span class="ruby-identifier">output</span>, <span class="ruby-identifier">:indent</span> =<span class="ruby-operator">></span> <span class="ruby-ivar">@options</span>[<span class="ruby-identifier">:indent</span>])
|
@@ -81,13 +81,26 @@
|
|
81
81
|
|
82
82
|
<span class="ruby-comment cmt"># Rectangles</span>
|
83
83
|
<span class="ruby-identifier">data</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">number</span>, <span class="ruby-identifier">number_index</span><span class="ruby-operator">|</span>
|
84
|
-
<span class="ruby-identifier">color</span> = <span class="ruby-
|
84
|
+
<span class="ruby-identifier">color</span> = <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@colors</span>.<span class="ruby-identifier">respond_to?</span> <span class="ruby-identifier">:call</span>
|
85
|
+
<span class="ruby-ivar">@colors</span>.<span class="ruby-identifier">call</span>(<span class="ruby-identifier">data_index</span>, <span class="ruby-identifier">number_index</span>, <span class="ruby-ivar">@data</span>.<span class="ruby-identifier">size</span>, <span class="ruby-identifier">data</span>.<span class="ruby-identifier">size</span>)
|
86
|
+
<span class="ruby-keyword kw">elsif</span> <span class="ruby-ivar">@colors</span>.<span class="ruby-identifier">class</span> <span class="ruby-operator">==</span> <span class="ruby-constant">Array</span>
|
87
|
+
<span class="ruby-identifier">first</span> = <span class="ruby-ivar">@colors</span>[<span class="ruby-identifier">data_index</span> <span class="ruby-operator">%</span> (<span class="ruby-ivar">@colors</span>.<span class="ruby-identifier">size</span>)]
|
88
|
+
|
89
|
+
<span class="ruby-keyword kw">if</span> <span class="ruby-identifier">first</span>.<span class="ruby-identifier">class</span> <span class="ruby-operator">==</span> <span class="ruby-constant">Array</span>
|
90
|
+
<span class="ruby-identifier">first</span>[<span class="ruby-identifier">number_index</span> <span class="ruby-operator">%</span> (<span class="ruby-identifier">first</span>.<span class="ruby-identifier">size</span>)]
|
91
|
+
<span class="ruby-keyword kw">else</span>
|
92
|
+
<span class="ruby-identifier">first</span>
|
93
|
+
<span class="ruby-keyword kw">end</span>
|
94
|
+
<span class="ruby-keyword kw">else</span>
|
95
|
+
<span class="ruby-ivar">@colors</span>
|
96
|
+
<span class="ruby-keyword kw">end</span>
|
97
|
+
|
85
98
|
<span class="ruby-identifier">height</span> = ((<span class="ruby-identifier">dy</span> <span class="ruby-operator">/</span> <span class="ruby-value">10.0</span>) <span class="ruby-operator">*</span> <span class="ruby-identifier">number</span>).<span class="ruby-identifier">round</span>
|
86
99
|
|
87
100
|
<span class="ruby-identifier">bar_x</span> = (<span class="ruby-identifier">x</span> <span class="ruby-operator">+</span> ((<span class="ruby-identifier">dx</span> <span class="ruby-operator">-</span> <span class="ruby-identifier">width</span>) <span class="ruby-operator">/</span> <span class="ruby-value">2.0</span>) <span class="ruby-operator">+</span> (<span class="ruby-identifier">number_index</span> <span class="ruby-operator">*</span> <span class="ruby-identifier">bar_width</span>)).<span class="ruby-identifier">round</span>
|
88
101
|
<span class="ruby-identifier">bar_y</span> = <span class="ruby-ivar">@options</span>[<span class="ruby-identifier">:viewbox</span>][<span class="ruby-identifier">:height</span>] <span class="ruby-operator">-</span> <span class="ruby-ivar">@options</span>[<span class="ruby-identifier">:margin</span>][<span class="ruby-identifier">:bottom</span>] <span class="ruby-operator">-</span> <span class="ruby-identifier">height</span>
|
89
102
|
|
90
|
-
<span class="ruby-identifier">xml</span>.<span class="ruby-identifier">rect</span> <span class="ruby-identifier">:fill</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">color</span
|
103
|
+
<span class="ruby-identifier">xml</span>.<span class="ruby-identifier">rect</span> <span class="ruby-identifier">:fill</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">color</span>, <span class="ruby-identifier">:stroke</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">color</span>, <span class="ruby-value str">'stroke-width'</span> =<span class="ruby-operator">></span> <span class="ruby-value">0</span>, <span class="ruby-identifier">:x</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">bar_x</span>, <span class="ruby-identifier">:width</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">bar_width</span>, <span class="ruby-identifier">:y</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">bar_y</span>, <span class="ruby-identifier">:height</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">height</span> <span class="ruby-operator">-</span> <span class="ruby-value">1</span>
|
91
104
|
<span class="ruby-keyword kw">end</span>
|
92
105
|
|
93
106
|
<span class="ruby-comment cmt"># Text</span>
|
data/doc/created.rid
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
Thu, 20 Aug 2009 16:27:09 -0400
|
data/lib/lilygraph.rb
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
|
5
5
|
require 'color'
|
6
6
|
|
7
|
-
# This is the main
|
7
|
+
# This is the main class to use if you want to create a graph!
|
8
8
|
#
|
9
9
|
# graph = Lilygraph.new(:title => "My Awesome Graph")
|
10
10
|
# graph.data = [1,2,3]
|
@@ -39,6 +39,28 @@ class Lilygraph
|
|
39
39
|
# For a grouped bar graph:
|
40
40
|
# graph.data=[[1,10],[2,20],[3,30]]
|
41
41
|
attr_accessor :data
|
42
|
+
|
43
|
+
# This allows you to set colors for the bars.
|
44
|
+
#
|
45
|
+
# If you just want a single color:
|
46
|
+
# graph.colors='#0000aa'
|
47
|
+
# If you want to make each bar (or bar group) different colors:
|
48
|
+
# graph.colors=['#aa0000','#00aa00','#0000aa']
|
49
|
+
# If you want every bar group to be the same, but each bar in the groups to have a different color
|
50
|
+
# graph.colors=[['#aa0000','#00aa00','#0000aa']]
|
51
|
+
# If you want to set every bar group color:
|
52
|
+
# graph.colors=[['#aa0000','#00aa00','#0000aa'],['#bb0000','#00bb00','#0000bb']]
|
53
|
+
# Last but not least you can set the color value to any object that responds to call (like a Proc). The proc takes four arguments.
|
54
|
+
# data_index: The index of the current bar (or group)
|
55
|
+
# number_index: The index of the current bar INSIDE of the current bar group (always 0 if you don't have grouped bars)
|
56
|
+
# data_size: total number of bar or groups.
|
57
|
+
# number_size: total number of bars in the current group (always 1 if you don't have bar groups)
|
58
|
+
#
|
59
|
+
# The default proc looks like:
|
60
|
+
# graph.colors=Proc.new do |data_index, number_index, data_size, number_size|
|
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
|
+
# end
|
63
|
+
attr_accessor :colors
|
42
64
|
|
43
65
|
# Returns a new graph creator with some default options specified via a hash:
|
44
66
|
# height:: String to use as height parameter on the svg tag. Default is <tt>'100%'</tt>.
|
@@ -56,6 +78,10 @@ class Lilygraph
|
|
56
78
|
@options = DEFAULT_OPTIONS.merge(options)
|
57
79
|
@data = []
|
58
80
|
@labels = []
|
81
|
+
|
82
|
+
@colors = Proc.new do |data_index, number_index, data_size, number_size|
|
83
|
+
Color::HSL.from_fraction(Float(data_index) / Float(data_size), 1.0, 0.4 + (Float(number_index) / Float(number_size) * 0.4)).html
|
84
|
+
end
|
59
85
|
end
|
60
86
|
|
61
87
|
# Updates the graph options with items from the passed in hash. Please refer
|
@@ -142,13 +168,26 @@ class Lilygraph
|
|
142
168
|
|
143
169
|
# Rectangles
|
144
170
|
data.each_with_index do |number, number_index|
|
145
|
-
color =
|
171
|
+
color = if @colors.respond_to? :call
|
172
|
+
@colors.call(data_index, number_index, @data.size, data.size)
|
173
|
+
elsif @colors.class == Array
|
174
|
+
first = @colors[data_index % (@colors.size)]
|
175
|
+
|
176
|
+
if first.class == Array
|
177
|
+
first[number_index % (first.size)]
|
178
|
+
else
|
179
|
+
first
|
180
|
+
end
|
181
|
+
else
|
182
|
+
@colors
|
183
|
+
end
|
184
|
+
|
146
185
|
height = ((dy / 10.0) * number).round
|
147
186
|
|
148
187
|
bar_x = (x + ((dx - width) / 2.0) + (number_index * bar_width)).round
|
149
188
|
bar_y = @options[:viewbox][:height] - @options[:margin][:bottom] - height
|
150
189
|
|
151
|
-
xml.rect :fill => color
|
190
|
+
xml.rect :fill => color, :stroke => color, 'stroke-width' => 0, :x => bar_x, :width => bar_width, :y => bar_y, :height => height - 1
|
152
191
|
end
|
153
192
|
|
154
193
|
# Text
|
data/lilygraph.gemspec
CHANGED