Module: PrawnCharts::PrawnChartRenderer

Defined in:
lib/prawn_charts/prawn_chart_renderer.rb

Instance Method Summary collapse

Instance Method Details

#draw_dots(graph_points, dot_radius) ⇒ Object



29
30
31
32
33
# File 'lib/prawn_charts/prawn_chart_renderer.rb', line 29

def draw_dots(graph_points, dot_radius)
  graph_points.each do |point|
    fill_circle(point.coordinate, dot_radius)
  end
end

#draw_dots_with_colorObject



96
97
98
99
100
# File 'lib/prawn_charts/prawn_chart_renderer.rb', line 96

def draw_dots_with_color
  set_color_and_execute(:fill_color, :dots) do
    draw_dots(@collector.graph_points, @assistant.dot_radius)
  end
end

#draw_graph(input, colors = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/prawn_charts/prawn_chart_renderer.rb', line 4

def draw_graph(input, colors = {})
  @assistant = RendererAssistant.new(input)
  @collector = @assistant.collector
  @colors = colors
  #stroke_axis
  translate(*input[:graph][:starting_point]) do
    draw_rectangle_with_color
    fill_rectangle_with_color
    draw_line_with_color
    draw_dots_with_color
    draw_x_labels_with_color
    draw_y_labels_with_color
    draw_horizontal_lines_with_color
    draw_title_with_color
    draw_x_title_with_color
    draw_y_title_with_color
  end
end

#draw_horizontal_lines(data) ⇒ Object



55
56
57
58
59
# File 'lib/prawn_charts/prawn_chart_renderer.rb', line 55

def draw_horizontal_lines(data)
  data.each do |start_point, end_point|
    stroke_line(start_point, end_point)
  end
end

#draw_horizontal_lines_with_colorObject



114
115
116
117
118
# File 'lib/prawn_charts/prawn_chart_renderer.rb', line 114

def draw_horizontal_lines_with_color
  set_color_and_execute(:stroke_color, :horizontal_lines) do
    draw_horizontal_lines(@collector.horizontal_lines)
  end
end

#draw_line(graph_points) ⇒ Object



23
24
25
26
27
# File 'lib/prawn_charts/prawn_chart_renderer.rb', line 23

def draw_line(graph_points)
  graph_points.each_index do |i|
    stroke_line(graph_points[i].coordinate, graph_points[i + 1].coordinate) unless graph_points[i + 1].nil?
  end
end

#draw_line_with_colorObject



90
91
92
93
94
# File 'lib/prawn_charts/prawn_chart_renderer.rb', line 90

def draw_line_with_color
  set_color_and_execute(:stroke_color, :line) do
    draw_line(@collector.graph_points)
  end
end

#draw_rectangle_with_colorObject



78
79
80
81
82
# File 'lib/prawn_charts/prawn_chart_renderer.rb', line 78

def draw_rectangle_with_color
  set_color_and_execute(:stroke_color, :rectangle_border) do
    stroke_rectangle([0, @collector.height], @collector.width, @collector.height)
  end
end

#draw_title(translation, title, options) ⇒ Object



61
62
63
64
65
# File 'lib/prawn_charts/prawn_chart_renderer.rb', line 61

def draw_title(translation, title, options)
  translate(*translation) do
    text_box(title, options)
  end
end

#draw_title_with_colorObject



120
121
122
123
124
# File 'lib/prawn_charts/prawn_chart_renderer.rb', line 120

def draw_title_with_color
  set_color_and_execute(:fill_color, :graph_title) do
    draw_title(@assistant.graph_title_translate, @assistant.graph_title, @assistant.graph_title_options)
  end
end

#draw_x_labels(data_points, offset, options) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/prawn_charts/prawn_chart_renderer.rb', line 35

def draw_x_labels(data_points, offset, options)
  label_width = options[:width]
  centering_adjustment = label_width / 2
  translate(0, offset) do
    data_points.each do |data_point|
     text_box(data_point.x_label, {at: [(data_point.x_pdf - centering_adjustment), 0]}.merge(options))
    end
  end
end

#draw_x_labels_with_colorObject



102
103
104
105
106
# File 'lib/prawn_charts/prawn_chart_renderer.rb', line 102

def draw_x_labels_with_color
  set_color_and_execute(:fill_color, :x_labels) do
    draw_x_labels(@collector.data_points, @assistant.x_labels_offset, @assistant.x_labels_options)
  end
end

#draw_x_title_with_colorObject



126
127
128
129
130
# File 'lib/prawn_charts/prawn_chart_renderer.rb', line 126

def draw_x_title_with_color
  set_color_and_execute(:fill_color, :x_title) do
    draw_title(@assistant.x_title_translate, @assistant.x_title, @assistant.x_title_options)
  end
end

#draw_y_labels(label_data, offset, options) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/prawn_charts/prawn_chart_renderer.rb', line 45

def draw_y_labels(label_data, offset, options)
  label_height = options[:height]
  centering_adjustment = label_height / 2
  translate(offset, 0) do
    label_data.each do |data|
      text_box(data[:label], {at: ([0, data[:y] + centering_adjustment]) }.merge(options))
    end
  end
end

#draw_y_labels_with_colorObject



108
109
110
111
112
# File 'lib/prawn_charts/prawn_chart_renderer.rb', line 108

def draw_y_labels_with_color
  set_color_and_execute(:fill_color, :y_labels) do
    draw_y_labels(@collector.y_labels, @assistant.y_labels_offset, @assistant.y_labels_options)
  end
end

#draw_y_title_with_colorObject



132
133
134
135
136
# File 'lib/prawn_charts/prawn_chart_renderer.rb', line 132

def draw_y_title_with_color
  set_color_and_execute(:fill_color, :y_title) do
    draw_title(@assistant.y_title_translate, @assistant.y_title, @assistant.y_title_options)
  end
end

#fill_rectangle_with_colorObject



84
85
86
87
88
# File 'lib/prawn_charts/prawn_chart_renderer.rb', line 84

def fill_rectangle_with_color
  set_color_and_execute(:fill_color, :rectangle_fill) do
    fill_rectangle([0, @collector.height], @collector.width, @collector.height)
  end
end

#set_color_and_execute(method, component, &block) ⇒ Object



67
68
69
70
71
# File 'lib/prawn_charts/prawn_chart_renderer.rb', line 67

def set_color_and_execute(method, component, &block)
  self.send(method, @colors[component]) if @colors[component]
  yield if @assistant.components_to_draw.include?(component)
  set_color_black(method)
end

#set_color_black(method) ⇒ Object



73
74
75
76
# File 'lib/prawn_charts/prawn_chart_renderer.rb', line 73

def set_color_black(method)
  black = "000000"
  self.send(method, black)
end