Class: SVG::Graph::Pie

Inherits:
SVG::Graph show all
Defined in:
lib/SVG/Graph/Pie.rb

Overview

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

  • 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

Constant Summary

Constants included from SVG::Graph

VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#datapoint_font_sizeObject

The font size of the data point labels



149
150
151
# File 'lib/SVG/Graph/Pie.rb', line 149

def datapoint_font_size
  @datapoint_font_size
end

#expand_gapObject

The amount of space between expanded wedges



147
148
149
# File 'lib/SVG/Graph/Pie.rb', line 147

def expand_gap
  @expand_gap
end

#expand_greatestObject

If true, expand the largest pie wedge



145
146
147
# File 'lib/SVG/Graph/Pie.rb', line 145

def expand_greatest
  @expand_greatest
end

#expandedObject

If true, “explode” the pie (put space between the wedges)



143
144
145
# File 'lib/SVG/Graph/Pie.rb', line 143

def expanded
  @expanded
end

#shadow_offsetObject

Sets the offset of the shadow from the pie chart



128
129
130
# File 'lib/SVG/Graph/Pie.rb', line 128

def shadow_offset
  @shadow_offset
end

#show_actual_valuesObject

If true, display the actual field values in the data labels



132
133
134
# File 'lib/SVG/Graph/Pie.rb', line 132

def show_actual_values
  @show_actual_values
end

#show_data_labelsObject

If true, display the data labels on the chart



130
131
132
# File 'lib/SVG/Graph/Pie.rb', line 130

def show_data_labels
  @show_data_labels
end

#show_key_actual_valuesObject

If true, display the actual value of the field in the key



139
140
141
# File 'lib/SVG/Graph/Pie.rb', line 139

def show_key_actual_values
  @show_key_actual_values
end

#show_key_data_labelsObject

If true, display the labels in the key



137
138
139
# File 'lib/SVG/Graph/Pie.rb', line 137

def show_key_data_labels
  @show_key_data_labels
end

#show_key_percentObject

If true, display the percentage value of the wedges in the key



141
142
143
# File 'lib/SVG/Graph/Pie.rb', line 141

def show_key_percent
  @show_key_percent
end

#show_percentObject

If true, display the percentage value of each pie wedge in the data labels



135
136
137
# File 'lib/SVG/Graph/Pie.rb', line 135

def show_percent
  @show_percent
end

#show_shadowObject

If true, displays a drop shadow for the chart



126
127
128
# File 'lib/SVG/Graph/Pie.rb', line 126

def show_shadow
  @show_shadow
end

Instance Method Details

#add_data(arg) ⇒ Object

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

nil values in the array will be replaced by 0

graph.add_data( { :data => [3,nil,nil,2] } ) is equivalent to graph.add_data( { :data => [3,0,0,2] } )


116
117
118
119
120
121
122
123
# File 'lib/SVG/Graph/Pie.rb', line 116

def add_data arg
  arg[:data].each_index {|idx|
    @data[idx] = 0 unless @data[idx]
    if !arg[:data][idx].nil?
      @data[idx] += arg[:data][idx]
    end
  }
end

#set_defaultsObject

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



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# 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