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