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