1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
require File.dirname(__FILE__) + '/base'
require File.dirname(__FILE__) + '/stacked_mixin'
class Gruff::StackedArea < Gruff::Base
include StackedMixin
attr_accessor :last_series_goes_on_bottom
def draw
get_maximum_by_stack
super
return unless @has_data
@x_increment = @graph_width / (@column_count - 1).to_f
@d = @d.stroke 'transparent'
height = Array.new(@column_count, 0)
data_points = nil
iterator = last_series_goes_on_bottom ? :reverse_each : :each
@norm_data.send(iterator) do |data_row|
prev_data_points = data_points
data_points = Array.new
@d = @d.fill data_row[DATA_COLOR_INDEX]
data_row[DATA_VALUES_INDEX].each_with_index do |data_point, index|
# Use incremented x and scaled y
new_x = @graph_left + (@x_increment * index)
new_y = @graph_top + (@graph_height - data_point * @graph_height - height[index])
height[index] += (data_point * @graph_height)
data_points << new_x
data_points << new_y
draw_label(new_x, index)
end
if prev_data_points
poly_points = data_points.dup
(prev_data_points.length/2 - 1).downto(0) do |i|
poly_points << prev_data_points[2*i]
poly_points << prev_data_points[2*i+1]
end
poly_points << data_points[0]
poly_points << data_points[1]
else
poly_points = data_points.dup
poly_points << @graph_right
poly_points << @graph_bottom - 1
poly_points << @graph_left
poly_points << @graph_bottom - 1
poly_points << data_points[0]
poly_points << data_points[1]
end
@d = @d.polyline(*poly_points)
end
@d.draw(@base_image)
end
end
|