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


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

def datapoint_font_size
  @datapoint_font_size
end

#expand_gapObject

The amount of space between expanded wedges


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

def expand_gap
  @expand_gap
end

#expand_greatestObject

If true, expand the largest pie wedge


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

def expand_greatest
  @expand_greatest
end

#expandedObject

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


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

def expanded
  @expanded
end

#shadow_offsetObject

Sets the offset of the shadow from the pie chart


122
123
124
# File 'lib/SVG/Graph/Pie.rb', line 122

def shadow_offset
  @shadow_offset
end

#show_actual_valuesObject

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


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

def show_actual_values
  @show_actual_values
end

#show_data_labelsObject

If true, display the data labels on the chart


124
125
126
# File 'lib/SVG/Graph/Pie.rb', line 124

def show_data_labels
  @show_data_labels
end

#show_key_actual_valuesObject

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


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

def show_key_actual_values
  @show_key_actual_values
end

#show_key_data_labelsObject

If true, display the labels in the key


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

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


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

def show_key_percent
  @show_key_percent
end

#show_percentObject

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


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

def show_percent
  @show_percent
end

#show_shadowObject

If true, displays a drop shadow for the chart


120
121
122
# File 'lib/SVG/Graph/Pie.rb', line 120

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

112
113
114
115
116
117
# File 'lib/SVG/Graph/Pie.rb', line 112

def add_data arg
  arg[:data].each_index {|idx|
    @data[idx] = 0 unless @data[idx]
    @data[idx] += arg[:data][idx]
  }
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


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/SVG/Graph/Pie.rb', line 74

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