prawn-graph 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +22 -10
- data/Rakefile +1 -1
- data/examples/graph/advanced_bar_chart.rb +1 -1
- data/examples/graph/bar_chart.pdf +185 -0
- data/examples/graph/line_chart.pdf +219 -0
- data/examples/graph/themed_bar_chart.rb +18 -0
- data/examples/graph/themed_line_chart.rb +18 -0
- data/lib/prawn/graph.rb +3 -0
- data/lib/prawn/graph/bar.rb +1 -5
- data/lib/prawn/graph/base.rb +31 -7
- data/lib/prawn/graph/grid.rb +10 -8
- data/lib/prawn/graph/line.rb +2 -10
- data/lib/prawn/graph/themes.rb +116 -0
- data/lib/prawn/graph/themes/37signals.yml +14 -0
- data/lib/prawn/graph/themes/keynote.yml +14 -0
- data/lib/prawn/graph/themes/monochome.yml +8 -0
- data/lib/prawn/graph/themes/odeo.yml +14 -0
- metadata +27 -10
- data/lib/prawn/graph/theme.rb +0 -24
data/README.markdown
CHANGED
@@ -20,21 +20,15 @@ be in early alpha state at best. It's not recommended for use in
|
|
20
20
|
a production environment unless you're happy to put up with bugs and
|
21
21
|
are willing to help fix them!
|
22
22
|
|
23
|
-
## Branches
|
24
|
-
|
25
|
-
The master branch of this repository tracks edge Prawn and will
|
26
|
-
be updated to reflect any changes there. If you need to work on
|
27
|
-
a version which is compatible with the current stable release,
|
28
|
-
0.7.*, then please use the <tt>stable-compat</tt> branch instead.
|
29
|
-
|
30
23
|
## Installing & Using through RubyGems
|
31
24
|
|
32
25
|
Installing and running prawn-graph is very easy:
|
33
26
|
|
34
|
-
* <tt>gem install prawn
|
27
|
+
* <tt>gem install prawn-graph</tt>
|
35
28
|
|
36
|
-
|
37
|
-
|
29
|
+
Gem version 0.3 is compatible with the latest 0.8 stable release
|
30
|
+
of prawn. If you're still using 0.7, then install version 0.2 of
|
31
|
+
prawn-graph.
|
38
32
|
|
39
33
|
Using graphs in your PDF is as straightforward as:
|
40
34
|
|
@@ -50,3 +44,21 @@ Using graphs in your PDF is as straightforward as:
|
|
50
44
|
bar_graph data at => [10,10]
|
51
45
|
end
|
52
46
|
</pre>
|
47
|
+
|
48
|
+
## Using Themes
|
49
|
+
|
50
|
+
At the moment, work is underway on implementing 'Themes' within
|
51
|
+
Prawn::Graph, which are hugely inspired by (and are indeed based on
|
52
|
+
the actual themes from) Gruff.
|
53
|
+
|
54
|
+
To find the list of available themes, use:
|
55
|
+
|
56
|
+
Prawn::Chart::Themes.list
|
57
|
+
|
58
|
+
The default theme which will be used for rendering is:
|
59
|
+
|
60
|
+
Prawn::Chart::Theme.monochrome
|
61
|
+
|
62
|
+
This in-line with the design decision of Prawn::Graph to render in
|
63
|
+
monochrome by default, since most of our users are likely to be
|
64
|
+
printing these documents!
|
data/Rakefile
CHANGED
@@ -0,0 +1,185 @@
|
|
1
|
+
%PDF-1.3
|
2
|
+
%����
|
3
|
+
1 0 obj
|
4
|
+
<< /Creator (Prawn)
|
5
|
+
/Producer (Prawn)
|
6
|
+
>>
|
7
|
+
endobj
|
8
|
+
2 0 obj
|
9
|
+
<< /Count 1
|
10
|
+
/Type /Pages
|
11
|
+
/Kids [5 0 R]
|
12
|
+
>>
|
13
|
+
endobj
|
14
|
+
3 0 obj
|
15
|
+
<< /Pages 2 0 R
|
16
|
+
/Type /Catalog
|
17
|
+
>>
|
18
|
+
endobj
|
19
|
+
4 0 obj
|
20
|
+
<< /Length 1307
|
21
|
+
>>
|
22
|
+
stream
|
23
|
+
/DeviceRGB cs
|
24
|
+
0.000 0.000 0.000 scn
|
25
|
+
/DeviceRGB CS
|
26
|
+
0.000 0.000 0.000 SCN
|
27
|
+
q
|
28
|
+
0.000 0.000 0.000 scn
|
29
|
+
46.000 46.000 500.000 200.000 re
|
30
|
+
b
|
31
|
+
1.000 1.000 1.000 SCN
|
32
|
+
61.000 66.000 m
|
33
|
+
0.5 w
|
34
|
+
546.000 66.000 l
|
35
|
+
S
|
36
|
+
61.000 86.000 m
|
37
|
+
0.5 w
|
38
|
+
546.000 86.000 l
|
39
|
+
S
|
40
|
+
61.000 106.000 m
|
41
|
+
0.5 w
|
42
|
+
546.000 106.000 l
|
43
|
+
S
|
44
|
+
61.000 126.000 m
|
45
|
+
0.5 w
|
46
|
+
546.000 126.000 l
|
47
|
+
S
|
48
|
+
61.000 146.000 m
|
49
|
+
0.5 w
|
50
|
+
546.000 146.000 l
|
51
|
+
S
|
52
|
+
61.000 166.000 m
|
53
|
+
0.5 w
|
54
|
+
546.000 166.000 l
|
55
|
+
S
|
56
|
+
61.000 186.000 m
|
57
|
+
0.5 w
|
58
|
+
546.000 186.000 l
|
59
|
+
S
|
60
|
+
61.000 206.000 m
|
61
|
+
0.5 w
|
62
|
+
546.000 206.000 l
|
63
|
+
S
|
64
|
+
61.000 226.000 m
|
65
|
+
0.5 w
|
66
|
+
546.000 226.000 l
|
67
|
+
S
|
68
|
+
61.000 246.000 m
|
69
|
+
0.5 w
|
70
|
+
546.000 246.000 l
|
71
|
+
S
|
72
|
+
61.000 46.000 m
|
73
|
+
2 w
|
74
|
+
61.000 246.000 l
|
75
|
+
S
|
76
|
+
61.000 46.000 m
|
77
|
+
2 w
|
78
|
+
546.000 46.000 l
|
79
|
+
S
|
80
|
+
61.000 246.000 m
|
81
|
+
0.000 0.000 0.000 SCN
|
82
|
+
1 w
|
83
|
+
61.000 46.000 m
|
84
|
+
|
85
|
+
BT
|
86
|
+
47 247.0 Td
|
87
|
+
/F1.0 5 Tf
|
88
|
+
[<3435>] TJ
|
89
|
+
ET
|
90
|
+
|
91
|
+
|
92
|
+
BT
|
93
|
+
47 47.0 Td
|
94
|
+
/F1.0 5 Tf
|
95
|
+
[<30>] TJ
|
96
|
+
ET
|
97
|
+
|
98
|
+
|
99
|
+
BT
|
100
|
+
122 32.0 Td
|
101
|
+
/F1.0 5 Tf
|
102
|
+
[<43617473>] TJ
|
103
|
+
ET
|
104
|
+
|
105
|
+
|
106
|
+
BT
|
107
|
+
243 32.0 Td
|
108
|
+
/F1.0 5 Tf
|
109
|
+
[<446f6773>] TJ
|
110
|
+
ET
|
111
|
+
|
112
|
+
|
113
|
+
BT
|
114
|
+
364 32.0 Td
|
115
|
+
/F1.0 5 Tf
|
116
|
+
[<48616d7374657273>] TJ
|
117
|
+
ET
|
118
|
+
|
119
|
+
|
120
|
+
BT
|
121
|
+
485 32.0 Td
|
122
|
+
/F1.0 5 Tf
|
123
|
+
[<476f6c6466697368>] TJ
|
124
|
+
ET
|
125
|
+
|
126
|
+
60 w
|
127
|
+
88.000 47.000 m
|
128
|
+
0.408 0.525 0.706 SCN
|
129
|
+
88.000 135.000 l
|
130
|
+
S
|
131
|
+
209.000 47.000 m
|
132
|
+
0.992 0.847 0.306 SCN
|
133
|
+
209.000 207.000 l
|
134
|
+
S
|
135
|
+
330.000 47.000 m
|
136
|
+
0.447 0.682 0.431 SCN
|
137
|
+
330.000 247.000 l
|
138
|
+
S
|
139
|
+
451.000 47.000 m
|
140
|
+
0.820 0.412 0.369 SCN
|
141
|
+
451.000 69.000 l
|
142
|
+
S
|
143
|
+
1 w
|
144
|
+
0.000 0.000 0.000 SCN
|
145
|
+
0.000 0.000 0.000 scn
|
146
|
+
61.000 46.000 m
|
147
|
+
Q
|
148
|
+
|
149
|
+
endstream
|
150
|
+
endobj
|
151
|
+
5 0 obj
|
152
|
+
<< /Contents 4 0 R
|
153
|
+
/Type /Page
|
154
|
+
/Parent 2 0 R
|
155
|
+
/Resources << /Font << /F1.0 6 0 R
|
156
|
+
>>
|
157
|
+
/ProcSet [/PDF /Text /ImageB /ImageC /ImageI]
|
158
|
+
>>
|
159
|
+
/MediaBox [0 0 612.0 792.0]
|
160
|
+
>>
|
161
|
+
endobj
|
162
|
+
6 0 obj
|
163
|
+
<< /Encoding /WinAnsiEncoding
|
164
|
+
/Subtype /Type1
|
165
|
+
/Type /Font
|
166
|
+
/BaseFont /Helvetica
|
167
|
+
>>
|
168
|
+
endobj
|
169
|
+
xref
|
170
|
+
0 7
|
171
|
+
0000000000 65535 f
|
172
|
+
0000000015 00000 n
|
173
|
+
0000000071 00000 n
|
174
|
+
0000000128 00000 n
|
175
|
+
0000000177 00000 n
|
176
|
+
0000001536 00000 n
|
177
|
+
0000001714 00000 n
|
178
|
+
trailer
|
179
|
+
<< /Root 3 0 R
|
180
|
+
/Info 1 0 R
|
181
|
+
/Size 7
|
182
|
+
>>
|
183
|
+
startxref
|
184
|
+
1811
|
185
|
+
%%EOF
|
@@ -0,0 +1,219 @@
|
|
1
|
+
%PDF-1.3
|
2
|
+
%����
|
3
|
+
1 0 obj
|
4
|
+
<< /Creator (Prawn)
|
5
|
+
/Producer (Prawn)
|
6
|
+
>>
|
7
|
+
endobj
|
8
|
+
2 0 obj
|
9
|
+
<< /Count 1
|
10
|
+
/Type /Pages
|
11
|
+
/Kids [5 0 R]
|
12
|
+
>>
|
13
|
+
endobj
|
14
|
+
3 0 obj
|
15
|
+
<< /Pages 2 0 R
|
16
|
+
/Type /Catalog
|
17
|
+
>>
|
18
|
+
endobj
|
19
|
+
4 0 obj
|
20
|
+
<< /Length 2391
|
21
|
+
>>
|
22
|
+
stream
|
23
|
+
/DeviceRGB cs
|
24
|
+
0.000 0.000 0.000 scn
|
25
|
+
/DeviceRGB CS
|
26
|
+
0.000 0.000 0.000 SCN
|
27
|
+
q
|
28
|
+
1.000 0.278 0.643 scn
|
29
|
+
46.000 46.000 500.000 200.000 re
|
30
|
+
b
|
31
|
+
1.000 1.000 1.000 SCN
|
32
|
+
61.000 66.000 m
|
33
|
+
0.5 w
|
34
|
+
546.000 66.000 l
|
35
|
+
S
|
36
|
+
61.000 86.000 m
|
37
|
+
0.5 w
|
38
|
+
546.000 86.000 l
|
39
|
+
S
|
40
|
+
61.000 106.000 m
|
41
|
+
0.5 w
|
42
|
+
546.000 106.000 l
|
43
|
+
S
|
44
|
+
61.000 126.000 m
|
45
|
+
0.5 w
|
46
|
+
546.000 126.000 l
|
47
|
+
S
|
48
|
+
61.000 146.000 m
|
49
|
+
0.5 w
|
50
|
+
546.000 146.000 l
|
51
|
+
S
|
52
|
+
61.000 166.000 m
|
53
|
+
0.5 w
|
54
|
+
546.000 166.000 l
|
55
|
+
S
|
56
|
+
61.000 186.000 m
|
57
|
+
0.5 w
|
58
|
+
546.000 186.000 l
|
59
|
+
S
|
60
|
+
61.000 206.000 m
|
61
|
+
0.5 w
|
62
|
+
546.000 206.000 l
|
63
|
+
S
|
64
|
+
61.000 226.000 m
|
65
|
+
0.5 w
|
66
|
+
546.000 226.000 l
|
67
|
+
S
|
68
|
+
61.000 246.000 m
|
69
|
+
0.5 w
|
70
|
+
546.000 246.000 l
|
71
|
+
S
|
72
|
+
61.000 46.000 m
|
73
|
+
2 w
|
74
|
+
61.000 246.000 l
|
75
|
+
S
|
76
|
+
61.000 46.000 m
|
77
|
+
2 w
|
78
|
+
546.000 46.000 l
|
79
|
+
S
|
80
|
+
61.000 246.000 m
|
81
|
+
0.000 0.000 0.000 SCN
|
82
|
+
1 w
|
83
|
+
61.000 46.000 m
|
84
|
+
|
85
|
+
BT
|
86
|
+
47 247.0 Td
|
87
|
+
/F1.0 5 Tf
|
88
|
+
[<3435>] TJ
|
89
|
+
ET
|
90
|
+
|
91
|
+
|
92
|
+
BT
|
93
|
+
47 47.0 Td
|
94
|
+
/F1.0 5 Tf
|
95
|
+
[<30>] TJ
|
96
|
+
ET
|
97
|
+
|
98
|
+
|
99
|
+
BT
|
100
|
+
122 32.0 Td
|
101
|
+
/F1.0 5 Tf
|
102
|
+
[<4a> 20 <616e> 10 <756172> -30 <79>] TJ
|
103
|
+
ET
|
104
|
+
|
105
|
+
|
106
|
+
BT
|
107
|
+
243 32.0 Td
|
108
|
+
/F1.0 5 Tf
|
109
|
+
[<46> 30 <656272> -15 <756172> -30 <79>] TJ
|
110
|
+
ET
|
111
|
+
|
112
|
+
|
113
|
+
BT
|
114
|
+
364 32.0 Td
|
115
|
+
/F1.0 5 Tf
|
116
|
+
[<4d61726368>] TJ
|
117
|
+
ET
|
118
|
+
|
119
|
+
|
120
|
+
BT
|
121
|
+
485 32.0 Td
|
122
|
+
/F1.0 5 Tf
|
123
|
+
[<417072> -15 <696c>] TJ
|
124
|
+
ET
|
125
|
+
|
126
|
+
60 w
|
127
|
+
148.000 47.000 m
|
128
|
+
0.125 0.125 0.125 scn
|
129
|
+
149.000 135.000 m
|
130
|
+
149.000 135.552 148.552 136.000 148.000 136.000 c
|
131
|
+
147.448 136.000 147.000 135.552 147.000 135.000 c
|
132
|
+
147.000 134.448 147.448 134.000 148.000 134.000 c
|
133
|
+
148.552 134.000 149.000 134.448 149.000 135.000 c
|
134
|
+
148.000 135.000 m
|
135
|
+
f
|
136
|
+
269.000 47.000 m
|
137
|
+
0.125 0.125 0.125 scn
|
138
|
+
270.000 207.000 m
|
139
|
+
270.000 207.552 269.552 208.000 269.000 208.000 c
|
140
|
+
268.448 208.000 268.000 207.552 268.000 207.000 c
|
141
|
+
268.000 206.448 268.448 206.000 269.000 206.000 c
|
142
|
+
269.552 206.000 270.000 206.448 270.000 207.000 c
|
143
|
+
269.000 207.000 m
|
144
|
+
f
|
145
|
+
390.000 47.000 m
|
146
|
+
0.125 0.125 0.125 scn
|
147
|
+
391.000 247.000 m
|
148
|
+
391.000 247.552 390.552 248.000 390.000 248.000 c
|
149
|
+
389.448 248.000 389.000 247.552 389.000 247.000 c
|
150
|
+
389.000 246.448 389.448 246.000 390.000 246.000 c
|
151
|
+
390.552 246.000 391.000 246.448 391.000 247.000 c
|
152
|
+
390.000 247.000 m
|
153
|
+
f
|
154
|
+
511.000 47.000 m
|
155
|
+
0.125 0.125 0.125 scn
|
156
|
+
512.000 69.000 m
|
157
|
+
512.000 69.552 511.552 70.000 511.000 70.000 c
|
158
|
+
510.448 70.000 510.000 69.552 510.000 69.000 c
|
159
|
+
510.000 68.448 510.448 68.000 511.000 68.000 c
|
160
|
+
511.552 68.000 512.000 68.448 512.000 69.000 c
|
161
|
+
511.000 69.000 m
|
162
|
+
f
|
163
|
+
2 w
|
164
|
+
0.125 0.125 0.125 SCN
|
165
|
+
62.000 47.000 m
|
166
|
+
148.000 135.000 l
|
167
|
+
S
|
168
|
+
148.000 135.000 m
|
169
|
+
269.000 207.000 l
|
170
|
+
S
|
171
|
+
269.000 207.000 m
|
172
|
+
390.000 247.000 l
|
173
|
+
S
|
174
|
+
390.000 247.000 m
|
175
|
+
511.000 69.000 l
|
176
|
+
S
|
177
|
+
1 w
|
178
|
+
0.000 0.000 0.000 SCN
|
179
|
+
0.000 0.000 0.000 scn
|
180
|
+
61.000 46.000 m
|
181
|
+
Q
|
182
|
+
|
183
|
+
endstream
|
184
|
+
endobj
|
185
|
+
5 0 obj
|
186
|
+
<< /Contents 4 0 R
|
187
|
+
/Type /Page
|
188
|
+
/Parent 2 0 R
|
189
|
+
/Resources << /Font << /F1.0 6 0 R
|
190
|
+
>>
|
191
|
+
/ProcSet [/PDF /Text /ImageB /ImageC /ImageI]
|
192
|
+
>>
|
193
|
+
/MediaBox [0 0 612.0 792.0]
|
194
|
+
>>
|
195
|
+
endobj
|
196
|
+
6 0 obj
|
197
|
+
<< /Encoding /WinAnsiEncoding
|
198
|
+
/Subtype /Type1
|
199
|
+
/Type /Font
|
200
|
+
/BaseFont /Helvetica
|
201
|
+
>>
|
202
|
+
endobj
|
203
|
+
xref
|
204
|
+
0 7
|
205
|
+
0000000000 65535 f
|
206
|
+
0000000015 00000 n
|
207
|
+
0000000071 00000 n
|
208
|
+
0000000128 00000 n
|
209
|
+
0000000177 00000 n
|
210
|
+
0000002620 00000 n
|
211
|
+
0000002798 00000 n
|
212
|
+
trailer
|
213
|
+
<< /Root 3 0 R
|
214
|
+
/Info 1 0 R
|
215
|
+
/Size 7
|
216
|
+
>>
|
217
|
+
startxref
|
218
|
+
2895
|
219
|
+
%%EOF
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
require "#{File.dirname(__FILE__)}/../example_helper.rb"
|
4
|
+
|
5
|
+
# Simplest bar chart example
|
6
|
+
#
|
7
|
+
|
8
|
+
data = [
|
9
|
+
['Cats',20],
|
10
|
+
['Dogs',36],
|
11
|
+
['Hamsters',45],
|
12
|
+
['Goldfish', 5]
|
13
|
+
]
|
14
|
+
|
15
|
+
Prawn::Document.generate('bar_chart.pdf') do
|
16
|
+
bar_chart data, :at => [10, 10], :theme => Prawn::Chart::Themes.keynote
|
17
|
+
end
|
18
|
+
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
require "#{File.dirname(__FILE__)}/../example_helper.rb"
|
4
|
+
|
5
|
+
# Simplest line chart example
|
6
|
+
#
|
7
|
+
|
8
|
+
data = [
|
9
|
+
['January',20],
|
10
|
+
['February',36],
|
11
|
+
['March',45],
|
12
|
+
['April', 5]
|
13
|
+
]
|
14
|
+
|
15
|
+
Prawn::Document.generate('line_chart.pdf') do
|
16
|
+
line_chart data, :at => [10, 10], :theme => Prawn::Chart::Themes.odeo
|
17
|
+
end
|
18
|
+
|
data/lib/prawn/graph.rb
CHANGED
@@ -6,11 +6,14 @@ require File.dirname(__FILE__) + '/graph/base'
|
|
6
6
|
require File.dirname(__FILE__) + '/graph/grid'
|
7
7
|
require File.dirname(__FILE__) + '/graph/bar'
|
8
8
|
require File.dirname(__FILE__) + '/graph/line'
|
9
|
+
require File.dirname(__FILE__) + '/graph/themes'
|
9
10
|
|
10
11
|
module Prawn
|
11
12
|
|
12
13
|
class Document
|
13
14
|
|
15
|
+
Prawn::Chart::Themes.initialize_themes
|
16
|
+
|
14
17
|
# Draws a fairly simple bar chart. Data should be passed in as an array of
|
15
18
|
# arrays, much like how Tables are dealt with:
|
16
19
|
#
|
data/lib/prawn/graph/bar.rb
CHANGED
@@ -53,11 +53,7 @@ module Prawn
|
|
53
53
|
@values.each do |value|
|
54
54
|
@document.move_to [base_x + last_position, base_y]
|
55
55
|
bar_height = calculate_point_height_from value
|
56
|
-
|
57
|
-
@document.stroke_color [rand(255), rand(255), rand(255), 0]
|
58
|
-
else
|
59
|
-
@document.stroke_color 'AAAAAA'
|
60
|
-
end
|
56
|
+
@document.stroke_color @theme.next_colour
|
61
57
|
@document.stroke_line_to [base_x + last_position, base_y + bar_height]
|
62
58
|
last_position += point_spacing
|
63
59
|
end
|
data/lib/prawn/graph/base.rb
CHANGED
@@ -41,23 +41,28 @@ module Prawn
|
|
41
41
|
# :label_y, a label to be shown along the Y axis of he graph, rendered centered
|
42
42
|
# on the grid and rotated to be perpendicular to the axis.
|
43
43
|
#
|
44
|
+
# :theme, the theme to be used to draw this graph, defaults to monochrome.
|
45
|
+
#
|
44
46
|
def initialize(data, document, options = {})
|
45
47
|
if options[:at].nil? || options[:at].empty?
|
46
48
|
raise Prawn::Errors::NoGraphStartSet,
|
47
|
-
"you must specify options[:at] as the coordinates
|
49
|
+
"you must specify options[:at] as the coordinates where you" +
|
48
50
|
" wish this graph to be drawn from."
|
49
51
|
end
|
50
|
-
opts = { :width => 500, :height => 200, :spacing => 20 }.merge(options)
|
52
|
+
opts = { :theme => Prawn::Chart::Themes.monochrome, :width => 500, :height => 200, :spacing => 20 }.merge(options)
|
51
53
|
(@headings, @values, @highest_value) = process_the data
|
52
54
|
(grid_x_start, grid_y_start, grid_width, grid_height) = parse_sizing_from opts
|
53
55
|
@colour = (!opts[:use_color].nil? || !opts[:use_colour].nil?)
|
54
56
|
@document = document
|
55
|
-
@
|
57
|
+
@theme = opts[:theme]
|
58
|
+
off = 20
|
59
|
+
@grid = Prawn::Chart::Grid.new(grid_x_start, grid_y_start, grid_width, grid_height, opts[:spacing], document, @theme)
|
56
60
|
end
|
57
61
|
|
58
62
|
# Draws the graph on the document which we have a reference to.
|
59
63
|
#
|
60
64
|
def draw
|
65
|
+
draw_bounding_box
|
61
66
|
@grid.draw
|
62
67
|
label_axes
|
63
68
|
if @title
|
@@ -74,8 +79,15 @@ module Prawn
|
|
74
79
|
end
|
75
80
|
|
76
81
|
private
|
77
|
-
|
82
|
+
|
83
|
+
def draw_bounding_box
|
84
|
+
@document.fill_color @theme.background_colour
|
85
|
+
@document.fill_and_stroke_rectangle [(@point.first - 10), (@point.last + ( @total_height + 40 ))], @document.bounds.width, (@total_height + 40)
|
86
|
+
@document.fill_color '000000'
|
87
|
+
end
|
88
|
+
|
78
89
|
def label_axes
|
90
|
+
@document.fill_color @theme.font_colour
|
79
91
|
base_x = @grid.start_x + 1
|
80
92
|
base_y = @grid.start_y + 1
|
81
93
|
|
@@ -92,24 +104,31 @@ module Prawn
|
|
92
104
|
@document.draw_text heading, :at => [last_position, base_y - 15 ], :size => 5
|
93
105
|
last_position += point_spacing
|
94
106
|
end
|
107
|
+
@document.fill_color @theme.background_colour
|
95
108
|
end
|
96
109
|
|
97
110
|
def draw_title
|
111
|
+
@document.fill_color @theme.font_colour
|
98
112
|
x_coord = calculate_x_axis_center_point(@title, 10)
|
99
113
|
y_coord = @grid.start_y + @grid.height + 10
|
100
114
|
@document.draw_text @title, :at => [x_coord, y_coord] ,:size => 10
|
115
|
+
@document.fill_color @theme.background_colour
|
101
116
|
end
|
102
117
|
|
103
118
|
def draw_x_axis_label
|
119
|
+
@document.fill_color @theme.font_colour
|
104
120
|
x_coord = calculate_x_axis_center_point(@x_label, 8)
|
105
121
|
y_coord = @grid.start_y - 30
|
106
122
|
@document.draw_text @x_label, :at => [x_coord, y_coord] ,:size => 8
|
123
|
+
@document.fill_color @theme.background_colour
|
107
124
|
end
|
108
125
|
|
109
126
|
def draw_y_axis_label
|
127
|
+
@document.fill_color @theme.font_colour
|
110
128
|
y_coord = calculate_y_axis_center_point(@y_label, 8)
|
111
129
|
x_coord = @grid.start_x - 30
|
112
130
|
@document.draw_text @y_label, :at => [x_coord, y_coord] ,:size => 8, :rotate => 90
|
131
|
+
@document.fill_color @theme.background_colour
|
113
132
|
end
|
114
133
|
|
115
134
|
# All subclasses of Prawn::Chart::Base must implement thier own plot_values
|
@@ -140,6 +159,10 @@ module Prawn
|
|
140
159
|
move_y_up = 0
|
141
160
|
grid_width = o[:width]
|
142
161
|
grid_height = o[:height]
|
162
|
+
|
163
|
+
@total_width = o[:width]
|
164
|
+
@total_height = o[:height]
|
165
|
+
@point = o[:at].dup
|
143
166
|
|
144
167
|
# Make room for the title if we're choosing to Render it.
|
145
168
|
#
|
@@ -162,10 +185,11 @@ module Prawn
|
|
162
185
|
@y_label = o[:label_y]
|
163
186
|
x_offset += 15
|
164
187
|
end
|
165
|
-
|
188
|
+
|
189
|
+
|
166
190
|
# Return the values calculated here.
|
167
191
|
#
|
168
|
-
[ (o[:at][0] + x_offset), (o[:at][1] + move_y_up), (grid_width - x_offset), (grid_height - y_offset) ]
|
192
|
+
[ (o[:at][0] + x_offset), (o[:at][1] + move_y_up + 20), (grid_width - (x_offset - 20)), (grid_height - y_offset) ]
|
169
193
|
end
|
170
194
|
|
171
195
|
def process_the(data_array)
|
@@ -204,4 +228,4 @@ module Prawn
|
|
204
228
|
|
205
229
|
end
|
206
230
|
end
|
207
|
-
end
|
231
|
+
end
|
data/lib/prawn/graph/grid.rb
CHANGED
@@ -9,12 +9,13 @@ module Prawn
|
|
9
9
|
|
10
10
|
attr_accessor :width, :height, :point, :spacing, :document
|
11
11
|
|
12
|
-
def initialize(grid_x_start, grid_y_start, grid_width, grid_height, spacing, document)
|
12
|
+
def initialize(grid_x_start, grid_y_start, grid_width, grid_height, spacing, document, theme)
|
13
13
|
@point = [grid_x_start, grid_y_start]
|
14
14
|
@width = grid_width
|
15
15
|
@height = grid_height
|
16
16
|
@spacing = spacing
|
17
17
|
@document = document
|
18
|
+
@theme = theme
|
18
19
|
end
|
19
20
|
|
20
21
|
def start_x; @point.first; end
|
@@ -23,16 +24,17 @@ module Prawn
|
|
23
24
|
# Draws the Grid on the specified Prawn::Document
|
24
25
|
#
|
25
26
|
def draw
|
26
|
-
@document.stroke_color
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
27
|
+
@document.stroke_color @theme.marker_colour
|
28
|
+
if @theme.stroke_grid_markers?
|
29
|
+
(@height / @spacing).times do |x|
|
30
|
+
offset = @spacing * (x + 1)
|
31
|
+
@document.move_to [@point.first, (@point.last + offset)]
|
32
|
+
@document.line_width(0.5)
|
33
|
+
@document.stroke_line_to([(@point.first + @width), (@point.last + offset)])
|
34
|
+
end
|
32
35
|
end
|
33
36
|
@document.move_to @point
|
34
37
|
@document.line_width(2)
|
35
|
-
@document.stroke_color '000000'
|
36
38
|
@document.stroke_line_to([@point.first, @point.last + @height])
|
37
39
|
@document.move_to @point
|
38
40
|
@document.line_width(2)
|
data/lib/prawn/graph/line.rb
CHANGED
@@ -56,20 +56,12 @@ module Prawn
|
|
56
56
|
bar_height = calculate_point_height_from value
|
57
57
|
point = [base_x + last_position, base_y + bar_height]
|
58
58
|
p << point
|
59
|
-
|
60
|
-
@document.fill_color '00DD00'
|
61
|
-
else
|
62
|
-
@document.fill_color 'AAAAAA'
|
63
|
-
end
|
59
|
+
@document.fill_color @theme.colours.first
|
64
60
|
@document.fill_circle_at point, :radius => 1
|
65
61
|
last_position += point_spacing
|
66
62
|
end
|
67
63
|
@document.line_width 2
|
68
|
-
|
69
|
-
@document.stroke_color '00DD00'
|
70
|
-
else
|
71
|
-
@document.stroke_color 'AAAAAA'
|
72
|
-
end
|
64
|
+
@document.stroke_color @theme.colours.first
|
73
65
|
p.each_with_index do |point,i|
|
74
66
|
next if point == p.last
|
75
67
|
@document.move_to point
|
@@ -0,0 +1,116 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
|
3
|
+
module Prawn
|
4
|
+
module Chart
|
5
|
+
|
6
|
+
# Themes serves as a point of interaction between the user and the underlying
|
7
|
+
# collection of themes made available to Prawn::Graph.
|
8
|
+
#
|
9
|
+
class Themes
|
10
|
+
|
11
|
+
# Called once when Prawn::Graph is loaded, initializes the list of
|
12
|
+
# themes currently bundled. If you have your own custom theme you'd
|
13
|
+
# like to use instead, use _register_theme and give it the path to
|
14
|
+
# your theme file.
|
15
|
+
#
|
16
|
+
def self.initialize_themes
|
17
|
+
path = File.expand_path(File.dirname(__FILE__) + '/themes/')
|
18
|
+
Dir.open(path) do |dir|
|
19
|
+
dir.each do |file|
|
20
|
+
_register_theme(path + '/' + file) if file.include?('.yml')
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# Adds the theme defined by the yaml file specified to the mapping of
|
26
|
+
# registered themes stored in +@@_themes_list+. Converts the YAML object
|
27
|
+
# into a Theme object for use in the application.
|
28
|
+
#
|
29
|
+
def self._register_theme(theme_file_path)
|
30
|
+
theme = Theme.new(YAML.load(IO.read(File.expand_path(theme_file_path))))
|
31
|
+
if !defined? @@_themes_list
|
32
|
+
@@_themes_list = {}
|
33
|
+
end
|
34
|
+
@@_themes_list[theme.name.to_sym] = theme
|
35
|
+
end
|
36
|
+
|
37
|
+
# Returns an array of the themes currently registered.
|
38
|
+
#
|
39
|
+
def self.list
|
40
|
+
@@_themes_list.keys
|
41
|
+
end
|
42
|
+
|
43
|
+
protected
|
44
|
+
|
45
|
+
# Hook into method_missing to allow us to do things like:
|
46
|
+
# Prawn::Chart::Themes.theme_name
|
47
|
+
# To return the theme object being looked for.
|
48
|
+
#
|
49
|
+
def self.method_missing(method, *args)
|
50
|
+
if @@_themes_list.keys.include?(method)
|
51
|
+
return @@_themes_list[method]
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
|
56
|
+
class Theme
|
57
|
+
attr_accessor :name, :title, :colours, :font_colour, :background_colour, :marker_colour
|
58
|
+
|
59
|
+
# Creates a new theme from a theme hash. The hash comes from the
|
60
|
+
# library parsing YAML definitions of a theme.
|
61
|
+
#
|
62
|
+
def initialize(theme_hash)
|
63
|
+
@name = theme_hash['name']
|
64
|
+
@title = theme_hash['title']
|
65
|
+
|
66
|
+
if theme_hash.keys.include?('colours')
|
67
|
+
@colours = theme_hash['colours']
|
68
|
+
elsif theme_hash.keys.include?('colors')
|
69
|
+
@colours = theme_hash['colors']
|
70
|
+
end
|
71
|
+
|
72
|
+
if theme_hash.keys.include?('font_colour')
|
73
|
+
@font_colour = theme_hash['font_colour']
|
74
|
+
elsif theme_hash.keys.include?('font_color')
|
75
|
+
@font_colour = theme_hash['font_color']
|
76
|
+
end
|
77
|
+
|
78
|
+
if theme_hash.keys.include?('background_colour')
|
79
|
+
@background_colour = theme_hash['background_colour']
|
80
|
+
elsif theme_hash.keys.include?('background_color')
|
81
|
+
@background_colour = theme_hash['background_color']
|
82
|
+
end
|
83
|
+
|
84
|
+
if theme_hash.keys.include?('marker_colour')
|
85
|
+
@marker_colour = theme_hash['marker_colour']
|
86
|
+
elsif theme_hash.keys.include?('marker_color')
|
87
|
+
@marker_colour = theme_hash['marker_color']
|
88
|
+
end
|
89
|
+
|
90
|
+
@stroke_grid_markers = theme_hash['stroke_grid_markers'].to_i
|
91
|
+
end
|
92
|
+
|
93
|
+
# Returns the next colour in the array of colours associated
|
94
|
+
# with this theme. If it gets to the end, it starts again from
|
95
|
+
# the beginning.
|
96
|
+
#
|
97
|
+
def next_colour
|
98
|
+
unless @current_colour
|
99
|
+
@current_colour = 0
|
100
|
+
return @colours[0]
|
101
|
+
end
|
102
|
+
@current_colour += 1
|
103
|
+
@current_colour = 0 if @current_colour == @colours.nitems
|
104
|
+
@colours[@current_colour]
|
105
|
+
end
|
106
|
+
alias next_color next_colour
|
107
|
+
|
108
|
+
def stroke_grid_markers?
|
109
|
+
@stroke_grid_markers == 1
|
110
|
+
end
|
111
|
+
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
end
|
116
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: prawn-graph
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 4
|
9
|
+
version: 0.0.4
|
5
10
|
platform: ruby
|
6
11
|
authors:
|
7
12
|
- Ryan Stenhouse
|
@@ -9,19 +14,21 @@ autorequire:
|
|
9
14
|
bindir: bin
|
10
15
|
cert_chain: []
|
11
16
|
|
12
|
-
date: 2010-02-
|
17
|
+
date: 2010-02-27 00:00:00 +00:00
|
13
18
|
default_executable:
|
14
19
|
dependencies:
|
15
20
|
- !ruby/object:Gem::Dependency
|
16
21
|
name: prawn
|
17
|
-
|
18
|
-
|
19
|
-
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
20
24
|
requirements:
|
21
25
|
- - ">="
|
22
26
|
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 0
|
23
29
|
version: "0"
|
24
|
-
|
30
|
+
type: :runtime
|
31
|
+
version_requirements: *id001
|
25
32
|
description: " An extension to Prawn that provides the ability to draw basic graphs and charts natively in your PDFs.\n"
|
26
33
|
email: " ryan@ryanstenhouse.eu"
|
27
34
|
executables: []
|
@@ -33,15 +40,23 @@ extra_rdoc_files:
|
|
33
40
|
files:
|
34
41
|
- examples/example_helper.rb
|
35
42
|
- examples/graph/advanced_bar_chart.rb
|
43
|
+
- examples/graph/bar_chart.pdf
|
36
44
|
- examples/graph/bar_chart.rb
|
45
|
+
- examples/graph/line_chart.pdf
|
37
46
|
- examples/graph/line_chart.rb
|
47
|
+
- examples/graph/themed_bar_chart.rb
|
48
|
+
- examples/graph/themed_line_chart.rb
|
38
49
|
- lib/prawn/graph/bar.rb
|
39
50
|
- lib/prawn/graph/base.rb
|
40
51
|
- lib/prawn/graph/chart.rb
|
41
52
|
- lib/prawn/graph/errors.rb
|
42
53
|
- lib/prawn/graph/grid.rb
|
43
54
|
- lib/prawn/graph/line.rb
|
44
|
-
- lib/prawn/graph/
|
55
|
+
- lib/prawn/graph/themes/37signals.yml
|
56
|
+
- lib/prawn/graph/themes/keynote.yml
|
57
|
+
- lib/prawn/graph/themes/monochome.yml
|
58
|
+
- lib/prawn/graph/themes/odeo.yml
|
59
|
+
- lib/prawn/graph/themes.rb
|
45
60
|
- lib/prawn/graph.rb
|
46
61
|
- Rakefile
|
47
62
|
- README.markdown
|
@@ -62,18 +77,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
62
77
|
requirements:
|
63
78
|
- - ">="
|
64
79
|
- !ruby/object:Gem::Version
|
80
|
+
segments:
|
81
|
+
- 0
|
65
82
|
version: "0"
|
66
|
-
version:
|
67
83
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
84
|
requirements:
|
69
85
|
- - ">="
|
70
86
|
- !ruby/object:Gem::Version
|
87
|
+
segments:
|
88
|
+
- 0
|
71
89
|
version: "0"
|
72
|
-
version:
|
73
90
|
requirements: []
|
74
91
|
|
75
92
|
rubyforge_project: prawn
|
76
|
-
rubygems_version: 1.3.
|
93
|
+
rubygems_version: 1.3.6
|
77
94
|
signing_key:
|
78
95
|
specification_version: 3
|
79
96
|
summary: An extension to Prawn that provides the ability to draw basic graphs and charts natively in your PDFs.
|
data/lib/prawn/graph/theme.rb
DELETED
@@ -1,24 +0,0 @@
|
|
1
|
-
module Prawn
|
2
|
-
module Chart
|
3
|
-
|
4
|
-
class Theme
|
5
|
-
attr_accessor :red, :green, :blue, :orange, :yellow, :purple, :black, :white
|
6
|
-
end
|
7
|
-
|
8
|
-
class Themes
|
9
|
-
|
10
|
-
def self.register(theme)
|
11
|
-
if theme.is_a? Theme
|
12
|
-
unless defined? @@_themes
|
13
|
-
@@_themes = []
|
14
|
-
end
|
15
|
-
@@_themes << theme
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
end
|
22
|
-
|
23
|
-
end
|
24
|
-
|