Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 30 additions & 9 deletions lib/prawn/graph/chart_components/bar_chart_renderer.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module Prawn
module Graph
module ChartComponents
# The Prawn::Graph::ChartComponents::BarChartRenderer is used to plot one or more bar charts
# in a sensible way on a a Prawn::Graph::ChartComponents::Canvas and its associated
# The Prawn::Graph::ChartComponents::BarChartRenderer is used to plot one or more bar charts
# in a sensible way on a a Prawn::Graph::ChartComponents::Canvas and its associated
# Prawn::Document.
#
class BarChartRenderer < SeriesRenderer
Expand Down Expand Up @@ -41,13 +41,14 @@ def mark_minimum_point(series_index, point, min_marked, x_position, y_position)

def render_the_chart
prawn.bounding_box [@graph_area.point[0] + 5, @graph_area.point[1] - 20], width: @plot_area_width, height: @plot_area_height do
prawn.save_graphics_state do

prawn.save_graphics_state do
num_points = @series[0].size
width_per_point = (@plot_area_width / num_points)
width = (((width_per_point * 0.9) / @series.size).round(2)).to_f
min_marked = false
max_marked = false
min_height = 2

num_points.times do |point|

Expand All @@ -60,10 +61,20 @@ def render_the_chart
starting = (prawn.bounds.left + (point * width_per_point))

x_position = ( (starting + (series_offset * width) ).to_f - (width / 2.0))
y_position = ((point_height_percentage(@series[series_index].values[point]) * @plot_area_height) - 5).to_f
proportional_height = (point_height_percentage(@series[series_index].values[point]) * @plot_area_height).to_f
y_position = [proportional_height, min_height].max

prawn.fill_and_stroke_line([ x_position ,0], [x_position ,y_position]) unless @series[series_index].values[point].zero?

prawn.fill_color = "000000"
prawn.text_box "#{@series[series_index].values[point]}",
at: [x_position - 6, y_position + 10],
height: 5,
overflow: :shrink_to_fit,
width: 12,
valign: :bottom,
align: :right

mark_average_line(series_index)
max_marked = mark_maximum_point(series_index, point, max_marked, x_position, y_position)
min_marked = mark_minimum_point(series_index, point, min_marked, x_position, y_position)
Expand All @@ -76,12 +87,22 @@ def render_the_chart
end
end

def max
@series.collect(&:max).max || 0
def render_axes
prawn.stroke_color = @canvas.theme.axes
prawn.fill_color = @canvas.theme.axes
prawn.stroke_horizontal_line(0, @plot_area_width, at: 0)
prawn.stroke_vertical_line(0, @plot_area_height, at: 0)
prawn.fill_and_stroke_ellipse [ 0,0], 1

add_x_axis_labels
end

def max
@series.map(&:max).compact.max || 0
end

def min
@series.collect(&:min).min || 0
@series.map(&:min).compact.min || 0
end

def avg
Expand All @@ -91,4 +112,4 @@ def avg
end
end
end
end
end
3 changes: 2 additions & 1 deletion lib/prawn/graph/chart_components/series_renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ def add_y_axis_label(value)
# the series.
#
def point_height_percentage(value)
((BigDecimal(value, 10)/BigDecimal(@canvas.series.collect(&:max).max, 10)) * BigDecimal(1)).round(2) rescue 0
result = ((BigDecimal(value, 10)/BigDecimal(@canvas.series.collect(&:max).max, 10)) * BigDecimal(1)).round(2) rescue 0
result.nan? ? 0 : result.to_f
end

def prawn
Expand Down