lib/gruff/pie.rb in gruff-0.2.8 vs lib/gruff/pie.rb in gruff-0.2.9
- old
+ new
@@ -1,16 +1,26 @@
-
require File.dirname(__FILE__) + '/base'
+##
+# Here's how to make a Pie graph:
+#
+# g = Gruff::Pie.new
+# g.title = "Visual Pie Graph Test"
+# g.data 'Fries', 20
+# g.data 'Hamburgers', 50
+# g.write("test/output/pie_keynote.png")
+#
+# To control where the pie chart starts creating slices, use #zero_degree.
+
class Gruff::Pie < Gruff::Base
TEXT_OFFSET_PERCENTAGE = 0.15
# Can be used to make the pie start cutting slices at the top (-90.0)
# or at another angle. Default is 0.0, which starts at 3 o'clock.
attr_accessor :zero_degree
-
+
def initialize_ivars
super
@zero_degree = 0.0
end
@@ -28,11 +38,12 @@
center_y = @graph_top + (@graph_height / 2.0) - 10 # Move graph up a bit
total_sum = sums_for_pie()
prev_degrees = @zero_degree
# Use full data since we can easily calculate percentages
- @data.sort{ |a, b| a[DATA_VALUES_INDEX][0] <=> b[DATA_VALUES_INDEX][0] }.each do |data_row|
+ data = (@sort ? @data.sort{ |a, b| a[DATA_VALUES_INDEX][0] <=> b[DATA_VALUES_INDEX][0] } : @data)
+ data.each do |data_row|
if data_row[DATA_VALUES_INDEX][0] > 0
@d = @d.stroke data_row[DATA_COLOR_INDEX]
@d = @d.fill 'transparent'
@d.stroke_width(radius) # stroke width should be equal to radius. we'll draw centered on (radius / 2)
@@ -45,17 +56,20 @@
radius / 2.0, radius / 2.0,
prev_degrees, prev_degrees + current_degrees + 0.5) # <= +0.5 'fudge factor' gets rid of the ugly gaps
half_angle = prev_degrees + ((prev_degrees + current_degrees) - prev_degrees) / 2
- # End the string with %% to escape the single %.
- # RMagick must use sprintf with the string and % has special significance.
- label_string = ((data_row[DATA_VALUES_INDEX][0] / total_sum) * 100.0).round.to_s + '%%'
- @d = draw_label(center_x,center_y,
- half_angle, radius + (radius * TEXT_OFFSET_PERCENTAGE),
- label_string)
-
+ unless @hide_line_markers then
+ # End the string with %% to escape the single %.
+ # RMagick must use sprintf with the string and % has special significance.
+ label_string = ((data_row[DATA_VALUES_INDEX][0] / total_sum) *
+ 100.0).round.to_s + '%%'
+ @d = draw_label(center_x,center_y, half_angle,
+ radius + (radius * TEXT_OFFSET_PERCENTAGE),
+ label_string)
+ end
+
prev_degrees += current_degrees
end
end
# TODO debug a circle where the text is drawn...
@@ -97,12 +111,12 @@
total_sum
end
end
-
class Float
# Used for degree => radian conversions
def deg2rad
self * (Math::PI/180.0)
end
end
+