Permalink
Browse files

* Merge branch 'fork-queue'

* Issue #40 Added spacing factor to bar graphs
* Issue #41 Add rotation to spider chart
  • Loading branch information...
2 parents 7f0e146 + 0d45eaa commit fe8b7c76479ee22727ee0aaa4fc362c8e862cd00 @donv donv committed Nov 10, 2012
View
@@ -5,7 +5,12 @@ class Gruff::Bar < Gruff::Base
# Spacing factor applied between bars
attr_accessor :bar_spacing
-
+
+ def initialize(*args)
+ super
+ @spacing_factor = 0.9
+ end
+
def draw
# Labels will be centered over the left of the bar if
# there are more labels than columns. This is basically the same
@@ -18,13 +23,24 @@ def draw
draw_bars
end
+ # Can be used to adjust the spaces between the bars.
+ # Accepts values between 0.00 and 1.00 where 0.00 means no spacing at all
+ # and 1 means that each bars' width is nearly 0 (so each bar is a simple
+ # line with no x dimension).
+ #
+ # Default value is 0.9.
+ def spacing_factor=(space_percent)
+ raise ArgumentError, "spacing_factor must be between 0.00 and 1.00" unless (space_percent >= 0 and space_percent <= 1)
+ @spacing_factor = (1 - space_percent)
+ end
+
protected
def draw_bars
# Setup spacing.
#
# Columns sit side-by-side.
- @bar_spacing ||= 0.9 # space between the bars
+ @bar_spacing ||= @spacing_factor # space between the bars
@bar_width = @graph_width / (@column_count * @data.length).to_f
padding = (@bar_width * (1 - @bar_spacing)) / 2
View
@@ -566,13 +566,13 @@ def draw_axis_labels
unless @y_axis_label.nil?
# Y Axis, rotated vertically
- @d.rotation = 90.0
+ @d.rotation = -90.0
@d.gravity = CenterGravity
@d = @d.annotate_scaled( @base_image,
1.0, @raw_rows,
@left_margin + @marker_caps_height / 2.0, 0.0,
@y_axis_label, @scale)
- @d.rotation = -90.0
+ @d.rotation = 90.0
end
end
View
@@ -30,6 +30,11 @@ def draw_bars
length = Array.new(@column_count, @graph_left)
padding = (@bar_width * (1 - @bar_spacing)) / 2
+ # if we're a side stacked bar then we don't need to draw ourself at all
+ # because sometimes (due to different heights/min/max) you can actually
+ # see both graphs and it looks like crap
+ return if self.is_a?(Gruff::SideStackedBar)
+
@norm_data.each_with_index do |data_row, row_index|
@d = @d.fill data_row[DATA_COLOR_INDEX]
@@ -54,8 +54,14 @@ def draw_bars
length[point_index] += difference
height[point_index] += (data_point * @graph_width - 2)
- @d = @d.rectangle(left_x, left_y, right_x, right_y)
-
+ # if a data point is 0 it can result in weird really thing lines
+ # that shouldn't even be there being drawn on top of the existing
+ # bar - this is bad
+ if data_point != 0
+ @d = @d.rectangle(left_x, left_y, right_x, right_y)
+ # Calculate center based on bar_width and current row
+ end
+ # we still need to draw the labels
# Calculate center based on bar_width and current row
label_center = @graph_top + (@bar_width * point_index) + (@bar_width * @bar_spacing / 2.0)
draw_label(label_center, point_index)
View
@@ -10,6 +10,7 @@ class Gruff::Spider < Gruff::Base
attr_reader :hide_text
attr_accessor :hide_axes
attr_reader :transparent_background
+ attr_accessor :rotation
def transparent_background=(value)
@transparent_background = value
@@ -19,11 +20,12 @@ def transparent_background=(value)
def hide_text=(value)
@hide_title = @hide_text = value
end
-
+
def initialize(max_value, target_width = 800)
super(target_width)
@max_value = max_value
@hide_legend = true;
+ @rotation = 0
end
def draw
@@ -39,39 +41,37 @@ def draw
top_x = @graph_left + (@graph_width - diameter) / 2.0
center_x = @graph_left + (@graph_width / 2.0)
center_y = @graph_top + (@graph_height / 2.0) - 25 # Move graph up a bit
-
+
@unit_length = radius / @max_value
-
total_sum = sums_for_spider
prev_degrees = 0.0
additive_angle = (2 * Math::PI)/ @data.size
- current_angle = 0.0
+ current_angle = rotation * Math::PI / 180.0
# Draw axes
draw_axes(center_x, center_y, radius, additive_angle) unless hide_axes
-
+
# Draw polygon
draw_polygon(center_x, center_y, additive_angle)
-
-
+
@d.draw(@base_image)
end
private
-
+
def normalize_points(value)
value * @unit_length
end
-
+
def draw_label(center_x, center_y, angle, radius, amount)
r_offset = 50 # The distance out from the center of the pie to get point
x_offset = center_x # The label points need to be tweaked slightly
y_offset = center_y + 0 # This one doesn't though
x = x_offset + ((radius + r_offset) * Math.cos(angle))
y = y_offset + ((radius + r_offset) * Math.sin(angle))
-
+
# Draw label
@d.fill = @marker_color
@d.font = @font if @font
@@ -84,45 +84,46 @@ def draw_label(center_x, center_y, angle, radius, amount)
x, y,
amount, @scale)
end
-
+
def draw_axes(center_x, center_y, radius, additive_angle, line_color = nil)
return if hide_axes
-
- current_angle = 0.0
-
+
+ current_angle = rotation * Math::PI / 180.0
+
@data.each do |data_row|
@d.stroke(line_color || data_row[DATA_COLOR_INDEX])
@d.stroke_width 5.0
-
+
x_offset = radius * Math.cos(current_angle)
y_offset = radius * Math.sin(current_angle)
@d.line(center_x, center_y,
center_x + x_offset,
center_y + y_offset)
-
+
draw_label(center_x, center_y, current_angle, radius, data_row[DATA_LABEL_INDEX].to_s) unless hide_text
-
+
current_angle += additive_angle
end
end
-
+
def draw_polygon(center_x, center_y, additive_angle, color = nil)
points = []
- current_angle = 0.0
+ current_angle = rotation * Math::PI / 180.0
+
@data.each do |data_row|
points << center_x + normalize_points(data_row[DATA_VALUES_INDEX].first) * Math.cos(current_angle)
points << center_y + normalize_points(data_row[DATA_VALUES_INDEX].first) * Math.sin(current_angle)
current_angle += additive_angle
end
-
+
@d.stroke_width 1.0
@d.stroke(color || @marker_color)
@d.fill(color || @marker_color)
@d.fill_opacity 0.4
@d.polygon(*points)
end
-
+
def sums_for_spider
@data.inject(0.0) {|sum, data_row| sum += data_row[DATA_VALUES_INDEX].first}
end
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
@@ -232,6 +232,35 @@ def generate_with_y_axis_increment(increment)
g.write("test/output/bar_y_increment_#{increment}.png")
end
+ def test_custom_spacing
+ g = Gruff::Bar.new
+ g.spacing_factor = 0
+ g.title = "Zero spacing graff"
+ g.labels = {
+ 0 => '5/6',
+ 1 => '5/15',
+ 2 => '5/24',
+ 3 => '5/30',
+ }
+
+ g.data(:apples, [1, 5, 8, 4])
+ g.data(:peaches, [4, 1, 2, 10])
+ g.minimum_value = 0
+ g.maximum_value = 10
+ g.write("test/output/bar_zero_spacing.png")
+ end
+
+ def test_spacing_factor_does_not_accept_values_lt_0_and_gt_1
+ g = Gruff::Bar.new
+
+ assert_raise ArgumentError do
+ g.spacing_factor = 1.01
+ end
+
+ assert_raise ArgumentError do
+ g.spacing_factor = -0.01
+ end
+ end
def test_custom_theme
g = Gruff::Bar.new
View
@@ -201,6 +201,16 @@ def test_lots_of_data_with_large_names
g.write("test/output/spider_lots_of_data_normal_names.png")
end
+ def test_rotation
+ g = Gruff::Spider.new(20)
+ g.title = "Rotation"
+ @datasets.each do |data|
+ g.data(data[0], data[1])
+ end
+
+ g.rotation = 45 # degrees
+ g.write("test/output/spider_rotation.png")
+ end
protected

0 comments on commit fe8b7c7

Please sign in to comment.