Class: SVG::Graph::Pie

Inherits:
  • Object
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

http://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[LICENSE.txt]

Constant Summary

RADIANS =
Math::PI/180

Constants included from

VERSION

Instance Attribute Summary

Instance Method Summary

Instance Attribute Details

- (Object) datapoint_font_size

The font size of the data point labels



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

def datapoint_font_size
  @datapoint_font_size
end

- (Object) expand_gap

The amount of space between expanded wedges



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

def expand_gap
  @expand_gap
end

- (Object) expand_greatest

If true, expand the largest pie wedge



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

def expand_greatest
  @expand_greatest
end

- (Object) expanded

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



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

def expanded
  @expanded
end

- (Object) shadow_offset

Sets the offset of the shadow from the pie chart



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

def shadow_offset
  @shadow_offset
end

- (Object) show_actual_values

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



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

def show_actual_values
  @show_actual_values
end

- (Object) show_data_labels

If true, display the data labels on the chart



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

def show_data_labels
  @show_data_labels
end

- (Object) show_key_actual_values

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



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

def show_key_actual_values
  @show_key_actual_values
end

- (Object) show_key_data_labels

If true, display the labels in the key



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

def show_key_data_labels
  @show_key_data_labels
end

- (Object) show_key_percent

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



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

def show_key_percent
  @show_key_percent
end

- (Object) show_percent

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



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

def show_percent
  @show_percent
end

- (Object) show_shadow

If true, displays a drop shadow for the chart



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

def show_shadow
  @show_shadow
end

Instance Method Details

- (Object) add_data(arg)

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


111
112
113
114
115
116
# 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

- (Object) set_defaults

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