1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
|
require 'SVG/Graph/Graph'
module SVG
module Graph
# === Create presentation quality SVG pie graphs easily
#
# == Synopsis
#
# require 'SVG/Graph/Pie'
#
# fields = %w(Jan Feb Mar)
# data_sales_02 = [12, 45, 21]
#
# graph = SVG::Graph::Pie.new({
# :height => 500,
# :width => 300,
# :fields => fields,
# })
#
# graph.add_data({
# :data => data_sales_02,
# :title => 'Sales 2002',
# })
#
# print "Content-type: image/svg+xml\r\n\r\n"
# print graph.burn();
#
# == Description
#
# This object aims to allow you to easily create high quality
# SVG pie graphs. You can either use the default style sheet
# or supply your own. Either way there are many options which can
# be configured to give you control over how the graph is
# generated - with or without a key, display percent on pie chart,
# title, subtitle etc.
#
# = Examples
#
# http://www.germane-software/repositories/public/SVG/test/single.rb
#
# == See also
#
# * SVG::Graph::Graph
# * SVG::Graph::BarHorizontal
# * SVG::Graph::Bar
# * SVG::Graph::Line
# * SVG::Graph::Plot
# * SVG::Graph::TimeSeries
#
# == Author
#
# Sean E. Russell <serATgermaneHYPHENsoftwareDOTcom>
#
# Copyright 2004 Sean E. Russell
# This software is available under the Ruby license[LICENSE.txt]
#
class Pie < Graph
# Defaults are those set by Graph::initialize, and
# [show_shadow] true
# [shadow_offset] 10
# [show_data_labels] false
# [show_actual_values] false
# [show_percent] true
# [show_key_data_labels] true
# [show_key_actual_values] true
# [show_key_percent] false
# [expanded] false
# [expand_greatest] false
# [expand_gap] 10
# [show_x_labels] false
# [show_y_labels] false
# [datapoint_font_size] 12
def set_defaults
init_with(
:show_shadow => true,
:shadow_offset => 10,
:show_data_labels => false,
:show_actual_values => false,
:show_percent => true,
:show_key_data_labels => true,
:show_key_actual_values => true,
:show_key_percent => false,
:expanded => false,
:expand_greatest => false,
:expand_gap => 10,
:show_x_labels => false,
:show_y_labels => false,
:datapoint_font_size => 12
)
@data = []
end
# Adds a data set to the graph.
#
# graph.add_data( { :data => [1,2,3,4] } )
#
# Note that the :title is not necessary. If multiple
# data sets are added to the graph, the pie chart will
# display the +sums+ of the data. EG:
#
# graph.add_data( { :data => [1,2,3,4] } )
# graph.add_data( { :data => [2,3,5,9] } )
#
# is the same as:
#
# graph.add_data( { :data => [3,5,8,13] } )
def add_data arg
arg[:data].each_index {|idx|
@data[idx] = 0 unless @data[idx]
@data[idx] += arg[:data][idx]
}
end
# If true, displays a drop shadow for the chart
attr_accessor :show_shadow
# Sets the offset of the shadow from the pie chart
attr_accessor :shadow_offset
# If true, display the data labels on the chart
attr_accessor :show_data_labels
# If true, display the actual field values in the data labels
attr_accessor :show_actual_values
# If true, display the percentage value of each pie wedge in the data
# labels
attr_accessor :show_percent
# If true, display the labels in the key
attr_accessor :show_key_data_labels
# If true, display the actual value of the field in the key
attr_accessor :show_key_actual_values
# If true, display the percentage value of the wedges in the key
attr_accessor :show_key_percent
# If true, "explode" the pie (put space between the wedges)
attr_accessor :expanded
# If true, expand the largest pie wedge
attr_accessor :expand_greatest
# The amount of space between expanded wedges
attr_accessor :expand_gap
# The font size of the data point labels
attr_accessor :datapoint_font_size
protected
def add_defs defs
gradient = defs.add_element( "filter", {
"id"=>"dropshadow",
"width" => "1.2",
"height" => "1.2",
} )
gradient.add_element( "feGaussianBlur", {
"stdDeviation" => "4",
"result" => "blur"
})
end
# We don't need the graph
def draw_graph
end
def get_y_labels
[""]
end
def get_x_labels
[""]
end
def keys
total = 0
max_value = 0
@data.each {|x| total += x }
percent_scale = 100.0 / total
count = -1
a = @config[:fields].collect{ |x|
count += 1
v = @data[count]
perc = show_key_percent ? " "+(v * percent_scale).round.to_s+"%" : ""
x + " [" + v.to_s + "]" + perc
}
end
RADIANS = Math::PI/180
def draw_data
@graph = @root.add_element( "g" )
background = @graph.add_element("g")
midground = @graph.add_element("g")
diameter = @graph_height > @graph_width ? @graph_width : @graph_height
diameter -= expand_gap if expanded or expand_greatest
diameter -= datapoint_font_size if show_data_labels
diameter -= 10 if show_shadow
radius = diameter / 2.0
xoff = (width - diameter) / 2
yoff = (height - @border_bottom - diameter)
yoff -= 10 if show_shadow
@graph.attributes['transform'] = "translate( #{xoff} #{yoff} )"
wedge_text_pad = 5
wedge_text_pad = 20 if show_percent and show_data_labels
total = 0
max_value = 0
@data.each {|x|
max_value = max_value < x ? x : max_value
total += x
}
percent_scale = 100.0 / total
prev_percent = 0
rad_mult = 3.6 * RADIANS
@config[:fields].each_index { |count|
value = @data[count]
percent = percent_scale * value
radians = prev_percent * rad_mult
x_start = radius+(Math.sin(radians) * radius)
y_start = radius-(Math.cos(radians) * radius)
radians = (prev_percent+percent) * rad_mult
x_end = radius+(Math.sin(radians) * radius)
y_end = radius-(Math.cos(radians) * radius)
path = "M#{radius},#{radius} L#{x_start},#{y_start} "+
"A#{radius},#{radius} "+
"0, #{percent >= 50 ? '1' : '0'},1, "+
"#{x_end} #{y_end} Z"
wedge = @foreground.add_element( "path", {
"d" => path,
"class" => "fill#{count+1}"
})
translate = nil
tx = 0
ty = 0
half_percent = prev_percent + percent / 2
radians = half_percent * rad_mult
if show_shadow
shadow = background.add_element( "path", {
"d" => path,
"filter" => "url(#dropshadow)",
"style" => "fill: #ccc; stroke: none;"
})
clear = midground.add_element( "path", {
"d" => path,
"style" => "fill: #fff; stroke: none;"
})
end
if expanded or (expand_greatest && value == max_value)
tx = (Math.sin(radians) * expand_gap)
ty = -(Math.cos(radians) * expand_gap)
translate = "translate( #{tx} #{ty} )"
wedge.attributes["transform"] = translate
clear.attributes["transform"] = translate
end
if show_shadow
shadow.attributes["transform"] =
"translate( #{tx+shadow_offset} #{ty+shadow_offset} )"
end
if show_data_labels and value != 0
label = ""
label += @config[:fields][count] if show_key_data_labels
label += " ["+value.to_s+"]" if show_actual_values
label += " "+percent.round.to_s+"%" if show_percent
msr = Math.sin(radians)
mcr = Math.cos(radians)
tx = radius + (msr * radius)
ty = radius -(mcr * radius)
if expanded or (expand_greatest && value == max_value)
tx += (msr * expand_gap)
ty -= (mcr * expand_gap)
end
@foreground.add_element( "text", {
"x" => tx.to_s,
"y" => ty.to_s,
"class" => "dataPointLabel",
"style" => "stroke: #fff; stroke-width: 2;"
}).text = label.to_s
@foreground.add_element( "text", {
"x" => tx.to_s,
"y" => ty.to_s,
"class" => "dataPointLabel",
}).text = label.to_s
end
prev_percent += percent
}
end
def round val, to
up = 10**to.to_f
(val * up).to_i / up
end
def get_css
return <<EOL
.dataPointLabel{
fill: #000000;
text-anchor:middle;
font-size: #{datapoint_font_size}px;
font-family: "Arial", sans-serif;
font-weight: normal;
}
/* key - MUST match fill styles */
.key1,.fill1{
fill: #ff0000;
fill-opacity: 0.7;
stroke: none;
stroke-width: 1px;
}
.key2,.fill2{
fill: #0000ff;
fill-opacity: 0.7;
stroke: none;
stroke-width: 1px;
}
.key3,.fill3{
fill-opacity: 0.7;
fill: #00ff00;
stroke: none;
stroke-width: 1px;
}
.key4,.fill4{
fill-opacity: 0.7;
fill: #ffcc00;
stroke: none;
stroke-width: 1px;
}
.key5,.fill5{
fill-opacity: 0.7;
fill: #00ccff;
stroke: none;
stroke-width: 1px;
}
.key6,.fill6{
fill-opacity: 0.7;
fill: #ff00ff;
stroke: none;
stroke-width: 1px;
}
.key7,.fill7{
fill-opacity: 0.7;
fill: #00ff99;
stroke: none;
stroke-width: 1px;
}
.key8,.fill8{
fill-opacity: 0.7;
fill: #ffff00;
stroke: none;
stroke-width: 1px;
}
.key9,.fill9{
fill-opacity: 0.7;
fill: #cc6666;
stroke: none;
stroke-width: 1px;
}
.key10,.fill10{
fill-opacity: 0.7;
fill: #663399;
stroke: none;
stroke-width: 1px;
}
.key11,.fill11{
fill-opacity: 0.7;
fill: #339900;
stroke: none;
stroke-width: 1px;
}
.key12,.fill12{
fill-opacity: 0.7;
fill: #9966FF;
stroke: none;
stroke-width: 1px;
}
EOL
end
end
end
end
|