Chris@909: require 'SVG/Graph/Graph' Chris@909: Chris@909: module SVG Chris@909: module Graph Chris@909: # === Create presentation quality SVG pie graphs easily Chris@909: # Chris@909: # == Synopsis Chris@909: # Chris@909: # require 'SVG/Graph/Pie' Chris@909: # Chris@909: # fields = %w(Jan Feb Mar) Chris@909: # data_sales_02 = [12, 45, 21] Chris@909: # Chris@909: # graph = SVG::Graph::Pie.new({ Chris@909: # :height => 500, Chris@909: # :width => 300, Chris@909: # :fields => fields, Chris@909: # }) Chris@909: # Chris@909: # graph.add_data({ Chris@909: # :data => data_sales_02, Chris@909: # :title => 'Sales 2002', Chris@909: # }) Chris@909: # Chris@909: # print "Content-type: image/svg+xml\r\n\r\n" Chris@909: # print graph.burn(); Chris@909: # Chris@909: # == Description Chris@909: # Chris@909: # This object aims to allow you to easily create high quality Chris@909: # SVG pie graphs. You can either use the default style sheet Chris@909: # or supply your own. Either way there are many options which can Chris@909: # be configured to give you control over how the graph is Chris@909: # generated - with or without a key, display percent on pie chart, Chris@909: # title, subtitle etc. Chris@909: # Chris@909: # = Examples Chris@909: # Chris@909: # http://www.germane-software/repositories/public/SVG/test/single.rb Chris@909: # Chris@909: # == See also Chris@909: # Chris@909: # * SVG::Graph::Graph Chris@909: # * SVG::Graph::BarHorizontal Chris@909: # * SVG::Graph::Bar Chris@909: # * SVG::Graph::Line Chris@909: # * SVG::Graph::Plot Chris@909: # * SVG::Graph::TimeSeries Chris@909: # Chris@909: # == Author Chris@909: # Chris@909: # Sean E. Russell Chris@909: # Chris@909: # Copyright 2004 Sean E. Russell Chris@909: # This software is available under the Ruby license[LICENSE.txt] Chris@909: # Chris@909: class Pie < Graph Chris@909: # Defaults are those set by Graph::initialize, and Chris@909: # [show_shadow] true Chris@909: # [shadow_offset] 10 Chris@909: # [show_data_labels] false Chris@909: # [show_actual_values] false Chris@909: # [show_percent] true Chris@909: # [show_key_data_labels] true Chris@909: # [show_key_actual_values] true Chris@909: # [show_key_percent] false Chris@909: # [expanded] false Chris@909: # [expand_greatest] false Chris@909: # [expand_gap] 10 Chris@909: # [show_x_labels] false Chris@909: # [show_y_labels] false Chris@909: # [datapoint_font_size] 12 Chris@909: def set_defaults Chris@909: init_with( Chris@909: :show_shadow => true, Chris@909: :shadow_offset => 10, Chris@909: Chris@909: :show_data_labels => false, Chris@909: :show_actual_values => false, Chris@909: :show_percent => true, Chris@909: Chris@909: :show_key_data_labels => true, Chris@909: :show_key_actual_values => true, Chris@909: :show_key_percent => false, Chris@909: Chris@909: :expanded => false, Chris@909: :expand_greatest => false, Chris@909: :expand_gap => 10, Chris@909: Chris@909: :show_x_labels => false, Chris@909: :show_y_labels => false, Chris@909: :datapoint_font_size => 12 Chris@909: ) Chris@909: @data = [] Chris@909: end Chris@909: Chris@909: # Adds a data set to the graph. Chris@909: # Chris@909: # graph.add_data( { :data => [1,2,3,4] } ) Chris@909: # Chris@909: # Note that the :title is not necessary. If multiple Chris@909: # data sets are added to the graph, the pie chart will Chris@909: # display the +sums+ of the data. EG: Chris@909: # Chris@909: # graph.add_data( { :data => [1,2,3,4] } ) Chris@909: # graph.add_data( { :data => [2,3,5,9] } ) Chris@909: # Chris@909: # is the same as: Chris@909: # Chris@909: # graph.add_data( { :data => [3,5,8,13] } ) Chris@909: def add_data arg Chris@909: arg[:data].each_index {|idx| Chris@909: @data[idx] = 0 unless @data[idx] Chris@909: @data[idx] += arg[:data][idx] Chris@909: } Chris@909: end Chris@909: Chris@909: # If true, displays a drop shadow for the chart Chris@909: attr_accessor :show_shadow Chris@909: # Sets the offset of the shadow from the pie chart Chris@909: attr_accessor :shadow_offset Chris@909: # If true, display the data labels on the chart Chris@909: attr_accessor :show_data_labels Chris@909: # If true, display the actual field values in the data labels Chris@909: attr_accessor :show_actual_values Chris@909: # If true, display the percentage value of each pie wedge in the data Chris@909: # labels Chris@909: attr_accessor :show_percent Chris@909: # If true, display the labels in the key Chris@909: attr_accessor :show_key_data_labels Chris@909: # If true, display the actual value of the field in the key Chris@909: attr_accessor :show_key_actual_values Chris@909: # If true, display the percentage value of the wedges in the key Chris@909: attr_accessor :show_key_percent Chris@909: # If true, "explode" the pie (put space between the wedges) Chris@909: attr_accessor :expanded Chris@909: # If true, expand the largest pie wedge Chris@909: attr_accessor :expand_greatest Chris@909: # The amount of space between expanded wedges Chris@909: attr_accessor :expand_gap Chris@909: # The font size of the data point labels Chris@909: attr_accessor :datapoint_font_size Chris@909: Chris@909: Chris@909: protected Chris@909: Chris@909: def add_defs defs Chris@909: gradient = defs.add_element( "filter", { Chris@909: "id"=>"dropshadow", Chris@909: "width" => "1.2", Chris@909: "height" => "1.2", Chris@909: } ) Chris@909: gradient.add_element( "feGaussianBlur", { Chris@909: "stdDeviation" => "4", Chris@909: "result" => "blur" Chris@909: }) Chris@909: end Chris@909: Chris@909: # We don't need the graph Chris@909: def draw_graph Chris@909: end Chris@909: Chris@909: def get_y_labels Chris@909: [""] Chris@909: end Chris@909: Chris@909: def get_x_labels Chris@909: [""] Chris@909: end Chris@909: Chris@909: def keys Chris@909: total = 0 Chris@909: max_value = 0 Chris@909: @data.each {|x| total += x } Chris@909: percent_scale = 100.0 / total Chris@909: count = -1 Chris@909: a = @config[:fields].collect{ |x| Chris@909: count += 1 Chris@909: v = @data[count] Chris@909: perc = show_key_percent ? " "+(v * percent_scale).round.to_s+"%" : "" Chris@909: x + " [" + v.to_s + "]" + perc Chris@909: } Chris@909: end Chris@909: Chris@909: RADIANS = Math::PI/180 Chris@909: Chris@909: def draw_data Chris@909: @graph = @root.add_element( "g" ) Chris@909: background = @graph.add_element("g") Chris@909: midground = @graph.add_element("g") Chris@909: Chris@909: diameter = @graph_height > @graph_width ? @graph_width : @graph_height Chris@909: diameter -= expand_gap if expanded or expand_greatest Chris@909: diameter -= datapoint_font_size if show_data_labels Chris@909: diameter -= 10 if show_shadow Chris@909: radius = diameter / 2.0 Chris@909: Chris@909: xoff = (width - diameter) / 2 Chris@909: yoff = (height - @border_bottom - diameter) Chris@909: yoff -= 10 if show_shadow Chris@909: @graph.attributes['transform'] = "translate( #{xoff} #{yoff} )" Chris@909: Chris@909: wedge_text_pad = 5 Chris@909: wedge_text_pad = 20 if show_percent and show_data_labels Chris@909: Chris@909: total = 0 Chris@909: max_value = 0 Chris@909: @data.each {|x| Chris@909: max_value = max_value < x ? x : max_value Chris@909: total += x Chris@909: } Chris@909: percent_scale = 100.0 / total Chris@909: Chris@909: prev_percent = 0 Chris@909: rad_mult = 3.6 * RADIANS Chris@909: @config[:fields].each_index { |count| Chris@909: value = @data[count] Chris@909: percent = percent_scale * value Chris@909: Chris@909: radians = prev_percent * rad_mult Chris@909: x_start = radius+(Math.sin(radians) * radius) Chris@909: y_start = radius-(Math.cos(radians) * radius) Chris@909: radians = (prev_percent+percent) * rad_mult Chris@909: x_end = radius+(Math.sin(radians) * radius) Chris@909: x_end -= 0.00001 if @data.length == 1 Chris@909: y_end = radius-(Math.cos(radians) * radius) Chris@909: path = "M#{radius},#{radius} L#{x_start},#{y_start} "+ Chris@909: "A#{radius},#{radius} "+ Chris@909: "0, #{percent >= 50 ? '1' : '0'},1, "+ Chris@909: "#{x_end} #{y_end} Z" Chris@909: Chris@909: Chris@909: wedge = @foreground.add_element( "path", { Chris@909: "d" => path, Chris@909: "class" => "fill#{count+1}" Chris@909: }) Chris@909: Chris@909: translate = nil Chris@909: tx = 0 Chris@909: ty = 0 Chris@909: half_percent = prev_percent + percent / 2 Chris@909: radians = half_percent * rad_mult Chris@909: Chris@909: if show_shadow Chris@909: shadow = background.add_element( "path", { Chris@909: "d" => path, Chris@909: "filter" => "url(#dropshadow)", Chris@909: "style" => "fill: #ccc; stroke: none;" Chris@909: }) Chris@909: clear = midground.add_element( "path", { Chris@909: "d" => path, Chris@909: "style" => "fill: #fff; stroke: none;" Chris@909: }) Chris@909: end Chris@909: Chris@909: if expanded or (expand_greatest && value == max_value) Chris@909: tx = (Math.sin(radians) * expand_gap) Chris@909: ty = -(Math.cos(radians) * expand_gap) Chris@909: translate = "translate( #{tx} #{ty} )" Chris@909: wedge.attributes["transform"] = translate Chris@909: clear.attributes["transform"] = translate if clear Chris@909: end Chris@909: Chris@909: if show_shadow Chris@909: shadow.attributes["transform"] = Chris@909: "translate( #{tx+shadow_offset} #{ty+shadow_offset} )" Chris@909: end Chris@909: Chris@909: if show_data_labels and value != 0 Chris@909: label = "" Chris@909: label += @config[:fields][count] if show_key_data_labels Chris@909: label += " ["+value.to_s+"]" if show_actual_values Chris@909: label += " "+percent.round.to_s+"%" if show_percent Chris@909: Chris@909: msr = Math.sin(radians) Chris@909: mcr = Math.cos(radians) Chris@909: tx = radius + (msr * radius) Chris@909: ty = radius -(mcr * radius) Chris@909: Chris@909: if expanded or (expand_greatest && value == max_value) Chris@909: tx += (msr * expand_gap) Chris@909: ty -= (mcr * expand_gap) Chris@909: end Chris@909: @foreground.add_element( "text", { Chris@909: "x" => tx.to_s, Chris@909: "y" => ty.to_s, Chris@909: "class" => "dataPointLabel", Chris@909: "style" => "stroke: #fff; stroke-width: 2;" Chris@909: }).text = label.to_s Chris@909: @foreground.add_element( "text", { Chris@909: "x" => tx.to_s, Chris@909: "y" => ty.to_s, Chris@909: "class" => "dataPointLabel", Chris@909: }).text = label.to_s Chris@909: end Chris@909: Chris@909: prev_percent += percent Chris@909: } Chris@909: end Chris@909: Chris@909: Chris@909: def round val, to Chris@909: up = 10**to.to_f Chris@909: (val * up).to_i / up Chris@909: end Chris@909: Chris@909: Chris@909: def get_css Chris@909: return <