Creating an SVG pie chart using the svg-graph gem

require 'SVG/Graph/Pie'

title = "Pie"
#data1 = []
#(rand(10)+5).times{
#  data1 << rand(20)
#  data1 << rand(20)
#}
data1 = [3, 11.2, 2.2, 5.4, 18.5, 7.6, 3, 2.2,]
#data2 = []
#(rand(10)+5).times{
#  data2 << rand(20)
#  data2 << rand(20)
#}
data2 = [4, 18, 3, 7, 8, 13, 19, 1,]

#data1 = [3]
#data2 = [2]

field = %w{jan feb mar apr may jun long_key_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aug}

graph = SVG::Graph::Pie.new( {
  :width => 640,
  :height => 300,
  :graph_title => title,
  :show_graph_title => true,
  :key => true,
  :key_position => :right, #:bottom, # or :right
  :fields => field,
  :show_data_labels => false,
  :show_actual_values => true,
  :show_shadow => true,
  :expanded => false,
  :expand_greatest => true
})
graph.add_data(
  :data => data1,
  :title => "Dataset 1"
  )
graph.add_data(
  :data => data2,
  :title => "Dataset 2"
  )
#puts graph.burn
File.open(File.expand_path("pie.svg",__dir__), "w") {|fout| 
  fout.print( graph.burn )
}

## Ouput

Pie jan [7] feb [29.2] mar [5.2] apr [12.4] may [26.5] jun [20.6] long_key_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa [22] aug [3.2]

Resources