annotate .svn/pristine/7d/7d9750d88bd333d9b7606eb3694629d8e8fa3a5e.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

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