# File lib/sparklines.rb, line 348 def pie diameter = @options[:diameter].to_f background_color = @options[:background_color] create_canvas(diameter, diameter, background_color) share_color = @options[:share_color] remain_color = @options[:remain_color] percent = @norm_data[0] # Adjust the radius so there's some edge left in the pie r = diameter/2.0 - 2 @draw.fill(remain_color) @draw.ellipse(r + 2, r + 2, r , r , 0, 360) @draw.fill(share_color) # Special exceptions if percent == 0 # For 0% return blank @draw.draw(@canvas) return @canvas elsif percent == 100 # For 100% just draw a full circle @draw.ellipse(r + 2, r + 2, r , r , 0, 360) @draw.draw(@canvas) return @canvas end # Okay, this part is as confusing as hell, so pay attention: # This line determines the horizontal portion of the point on the circle where the X-Axis # should end. It's caculated by taking the center of the on-image circle and adding that # to the radius multiplied by the formula for determinig the point on a unit circle that a # angle corresponds to. 3.6 * percent gives us that angle, but it's in degrees, so we need to # convert, hence the muliplication by Pi over 180 arc_end_x = r + 2 + (r * Math.cos((3.6 * percent)*(Math::PI/180))) # The same goes for here, except it's the vertical point instead of the horizontal one arc_end_y = r + 2 + (r * Math.sin((3.6 * percent)*(Math::PI/180))) # Because the SVG path format is seriously screwy, we need to set the large-arc-flag to 1 # if the angle of an arc is greater than 180 degrees. I have no idea why this is, but it is. percent > 50? large_arc_flag = 1: large_arc_flag = 0 # This is also confusing # M tells us to move to an absolute point on the image. We're moving to the center of the pie # h tells us to move to a relative point. We're moving to the right edge of the circle. # A tells us to start an absolute elliptical arc. The first two values are the radii of the ellipse # the third value is the x-axis-rotation (how to rotate the ellipse if we wanted to [could have some fun # with randomizing that maybe), the fourth value is our large-arc-flag, the fifth is the sweep-flag, # (again, confusing), the sixth and seventh values are the end point of the arc which we calculated previously # More info on the SVG path string format at: http://www.w3.org/TR/SVG/paths.html path = "M#{r + 2},#{r + 2} h#{r} A#{r},#{r} 0 #{large_arc_flag},1 #{arc_end_x},#{arc_end_y} z" @draw.path(path) @draw.draw(@canvas) @canvas end