annotate lib/SVG/Graph/.svn/text-base/Pie.rb.svn-base @ 711:60773bbfe050 feature_36_testing

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