Class SVG::Graph::Pie
In: lib/SVG/Graph/Pie.rb
Parent: Graph
Graph Pie Plot BarBase Schedule Line TimeSeries Bar BarHorizontal REXML Graph SVG dot/f_0.png

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

www.germane-software/repositories/public/SVG/test/single.rb

See also

Author

Sean E. Russell <serATgermaneHYPHENsoftwareDOTcom>

Copyright 2004 Sean E. Russell This software is available under the Ruby license

Methods

Constants

RADIANS = Math::PI/180

Attributes

datapoint_font_size  [RW]  The font size of the data point labels
expand_gap  [RW]  The amount of space between expanded wedges
expand_greatest  [RW]  If true, expand the largest pie wedge
expanded  [RW]  If true, "explode" the pie (put space between the wedges)
shadow_offset  [RW]  Sets the offset of the shadow from the pie chart
show_actual_values  [RW]  If true, display the actual field values in the data labels
show_data_labels  [RW]  If true, display the data labels on the chart
show_key_actual_values  [RW]  If true, display the actual value of the field in the key
show_key_data_labels  [RW]  If true, display the labels in the key
show_key_percent  [RW]  If true, display the percentage value of the wedges in the key
show_percent  [RW]  If true, display the percentage value of each pie wedge in the data labels
show_shadow  [RW]  If true, displays a drop shadow for the chart

Public Instance methods

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] } )

[Source]

# File lib/SVG/Graph/Pie.rb, line 111
      def add_data arg
        arg[:data].each_index {|idx|
          @data[idx] = 0 unless @data[idx]
          @data[idx] += arg[:data][idx]
        }
      end

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

[Source]

# File lib/SVG/Graph/Pie.rb, line 73
      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

Protected Instance methods

[Source]

# File lib/SVG/Graph/Pie.rb, line 147
      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

[Source]

# File lib/SVG/Graph/Pie.rb, line 187
      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)
          x_end -= 0.00001 if @data.length == 1
          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 if clear
          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

We don‘t need the graph

[Source]

# File lib/SVG/Graph/Pie.rb, line 160
      def draw_graph
      end

[Source]

# File lib/SVG/Graph/Pie.rb, line 308
      def get_css
        return ".dataPointLabel{\n        fill: #000000;\n        text-anchor:middle;\n        font-size: \#{datapoint_font_size}px;\n        font-family: \"Arial\", sans-serif;\n        font-weight: normal;\n}\n\n/* key - MUST match fill styles */\n.key1,.fill1{\n        fill: #ff0000;\n        fill-opacity: 0.7;\n        stroke: none;\n        stroke-width: 1px;     \n}\n.key2,.fill2{\n        fill: #0000ff;\n        fill-opacity: 0.7;\n        stroke: none;\n        stroke-width: 1px;     \n}\n.key3,.fill3{\n        fill-opacity: 0.7;\n        fill: #00ff00;\n        stroke: none;\n        stroke-width: 1px;     \n}\n.key4,.fill4{\n        fill-opacity: 0.7;\n        fill: #ffcc00;\n        stroke: none;\n        stroke-width: 1px;     \n}\n.key5,.fill5{\n        fill-opacity: 0.7;\n        fill: #00ccff;\n        stroke: none;\n        stroke-width: 1px;     \n}\n.key6,.fill6{\n        fill-opacity: 0.7;\n        fill: #ff00ff;\n        stroke: none;\n        stroke-width: 1px;     \n}\n.key7,.fill7{\n        fill-opacity: 0.7;\n        fill: #00ff99;\n        stroke: none;\n        stroke-width: 1px;     \n}\n.key8,.fill8{\n        fill-opacity: 0.7;\n        fill: #ffff00;\n        stroke: none;\n        stroke-width: 1px;     \n}\n.key9,.fill9{\n        fill-opacity: 0.7;\n        fill: #cc6666;\n        stroke: none;\n        stroke-width: 1px;     \n}\n.key10,.fill10{\n        fill-opacity: 0.7;\n        fill: #663399;\n        stroke: none;\n        stroke-width: 1px;     \n}\n.key11,.fill11{\n        fill-opacity: 0.7;\n        fill: #339900;\n        stroke: none;\n        stroke-width: 1px;     \n}\n.key12,.fill12{\n        fill-opacity: 0.7;\n        fill: #9966FF;\n        stroke: none;\n        stroke-width: 1px;     \n}\n"
      end

[Source]

# File lib/SVG/Graph/Pie.rb, line 167
      def get_x_labels
        [""]
      end

[Source]

# File lib/SVG/Graph/Pie.rb, line 163
      def get_y_labels
        [""]
      end

[Source]

# File lib/SVG/Graph/Pie.rb, line 171
      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

[Source]

# File lib/SVG/Graph/Pie.rb, line 302
      def round val, to
        up = 10**to.to_f
        (val * up).to_i / up
      end

[Validate]