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