annotate .svn/pristine/5d/5dc63759de7dfd35f5bca9ebf67da227968a751f.svn-base @ 1628:9c5f8e24dadc live tip

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