prawn_charts 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (24) hide show
  1. data/lib/data_collectors/container/container_data_collector.rb +57 -55
  2. data/lib/data_collectors/container/graph_title_data_collector.rb +12 -10
  3. data/lib/data_collectors/graph/horizontal_lines_data_collector.rb +16 -14
  4. data/lib/data_collectors/graph/linear_y_pdf_data_collector.rb +17 -15
  5. data/lib/data_collectors/graph/log_y_pdf_data_collector.rb +16 -14
  6. data/lib/data_collectors/graph/pdf_data_collector.rb +20 -18
  7. data/lib/data_collectors/graph/x_labels_data_collector.rb +22 -20
  8. data/lib/data_collectors/graph/x_pdf_data_collector.rb +12 -10
  9. data/lib/data_collectors/graph/y_labels_data_collector.rb +27 -25
  10. data/lib/data_collectors/graph/y_pdf_data_collector.rb +17 -15
  11. data/lib/examples/log_example.rb +124 -122
  12. data/lib/examples/simple_linear_example.rb +56 -54
  13. data/lib/examples/simple_log_example.rb +56 -54
  14. data/lib/prawn_charts/version.rb +1 -1
  15. data/lib/renderers/prawn_chart_renderer.rb +21 -19
  16. data/spec/data_collectors/container/container_data_collector_spec.rb +43 -41
  17. data/spec/data_collectors/graph/horizontal_lines_data_collector_spec.rb +13 -11
  18. data/spec/data_collectors/graph/linear_y_pdf_data_collector_spec.rb +13 -11
  19. data/spec/data_collectors/graph/log_y_pdf_data_collector_spec.rb +19 -17
  20. data/spec/data_collectors/graph/pdf_data_collector_spec.rb +38 -36
  21. data/spec/data_collectors/graph/x_labels_data_collector_spec.rb +14 -12
  22. data/spec/data_collectors/graph/x_pdf_data_collector_spec.rb +15 -13
  23. data/spec/data_collectors/graph/y_labels_data_collector_spec.rb +15 -13
  24. metadata +1 -1
@@ -1,57 +1,59 @@
1
- class ContainerDataCollector
2
- attr_reader :inputs
3
- def initialize(inputs)
4
- @inputs = inputs
5
- end
6
-
7
- def height
8
- bottom_gap +
9
- inputs[:graph_height] +
10
- top_gap
11
- end
12
-
13
- def width
14
- left_gap + right_gap + inputs[:graph_width]
15
- end
16
-
17
- def graph_top_left
18
- [left_gap, (height - top_gap)]
19
- end
20
-
21
- def graph_title_top_left
22
- [left_gap, (bottom_gap + inputs[:graph_height] + inputs[:graph_title_height])]
23
- end
24
-
25
- def y_title_top_left
26
- [inputs[:container_left_padding], (bottom_gap + inputs[:graph_height])]
27
- end
28
-
29
- def x_title_top_left
30
- [left_gap, (inputs[:container_bottom_padding] + inputs[:x_title_height])]
31
- end
32
-
33
- private
34
-
35
- # gap is area between container edge and graph
36
- def left_gap
37
- inputs[:container_left_padding] +
38
- inputs[:y_label_width] +
39
- inputs[:y_label_offset] +
40
- inputs[:y_title_width]
41
- end
42
-
43
- def right_gap
44
- inputs[:container_right_padding]
45
- end
46
-
47
- def top_gap
48
- inputs[:container_top_padding] +
49
- inputs[:graph_title_height]
50
- end
51
-
52
- def bottom_gap
53
- inputs[:container_bottom_padding] +
54
- inputs[:x_label_height] +
55
- inputs[:x_title_height]
1
+ module PrawnCharts
2
+ class ContainerDataCollector
3
+ attr_reader :inputs
4
+ def initialize(inputs)
5
+ @inputs = inputs
6
+ end
7
+
8
+ def height
9
+ bottom_gap +
10
+ inputs[:graph_height] +
11
+ top_gap
12
+ end
13
+
14
+ def width
15
+ left_gap + right_gap + inputs[:graph_width]
16
+ end
17
+
18
+ def graph_top_left
19
+ [left_gap, (height - top_gap)]
20
+ end
21
+
22
+ def graph_title_top_left
23
+ [left_gap, (bottom_gap + inputs[:graph_height] + inputs[:graph_title_height])]
24
+ end
25
+
26
+ def y_title_top_left
27
+ [inputs[:container_left_padding], (bottom_gap + inputs[:graph_height])]
28
+ end
29
+
30
+ def x_title_top_left
31
+ [left_gap, (inputs[:container_bottom_padding] + inputs[:x_title_height])]
32
+ end
33
+
34
+ private
35
+
36
+ # gap is area between container edge and graph
37
+ def left_gap
38
+ inputs[:container_left_padding] +
39
+ inputs[:y_label_width] +
40
+ inputs[:y_label_offset] +
41
+ inputs[:y_title_width]
42
+ end
43
+
44
+ def right_gap
45
+ inputs[:container_right_padding]
46
+ end
47
+
48
+ def top_gap
49
+ inputs[:container_top_padding] +
50
+ inputs[:graph_title_height]
51
+ end
52
+
53
+ def bottom_gap
54
+ inputs[:container_bottom_padding] +
55
+ inputs[:x_label_height] +
56
+ inputs[:x_title_height]
57
+ end
56
58
  end
57
59
  end
@@ -1,13 +1,15 @@
1
- class GraphTitleDataCollector
2
- attr_reader :graph_title, :position, :graph_title_height, :graph_title_width
3
- def initialize(graph_title, position, graph_title_height, graph_title_width)
4
- @graph_title = graph_title
5
- @position = position
6
- @graph_title_height = graph_title_height
7
- @graph_title_width = graph_title_width
8
- end
1
+ module PrawnCharts
2
+ class GraphTitleDataCollector
3
+ attr_reader :graph_title, :position, :graph_title_height, :graph_title_width
4
+ def initialize(graph_title, position, graph_title_height, graph_title_width)
5
+ @graph_title = graph_title
6
+ @position = position
7
+ @graph_title_height = graph_title_height
8
+ @graph_title_width = graph_title_width
9
+ end
9
10
 
10
- def collect
11
- { title: graph_title, at: position, height: graph_title_height, width: graph_title_width }
11
+ def collect
12
+ { title: graph_title, at: position, height: graph_title_height, width: graph_title_width }
13
+ end
12
14
  end
13
15
  end
@@ -1,20 +1,22 @@
1
- class HorizontalLinesDataCollector
2
- attr_reader :graph_height_pdf, :graph_width_pdf, :y_labels
3
- def initialize(graph_height_pdf, graph_width_pdf, y_labels)
4
- @graph_height_pdf = graph_height_pdf
5
- @graph_width_pdf = graph_width_pdf
6
- @y_labels = y_labels
7
- end
1
+ module PrawnCharts
2
+ class HorizontalLinesDataCollector
3
+ attr_reader :graph_height_pdf, :graph_width_pdf, :y_labels
4
+ def initialize(graph_height_pdf, graph_width_pdf, y_labels)
5
+ @graph_height_pdf = graph_height_pdf
6
+ @graph_width_pdf = graph_width_pdf
7
+ @y_labels = y_labels
8
+ end
8
9
 
9
- def collect
10
- y_labels.map.with_index do |_, index|
11
- [0, graph_width_pdf, index * y_label_increment]
10
+ def collect
11
+ y_labels.map.with_index do |_, index|
12
+ [0, graph_width_pdf, index * y_label_increment]
13
+ end
12
14
  end
13
- end
14
15
 
15
- private
16
+ private
16
17
 
17
- def y_label_increment
18
- graph_height_pdf.to_f / (y_labels.count - 1)
18
+ def y_label_increment
19
+ graph_height_pdf.to_f / (y_labels.count - 1)
20
+ end
19
21
  end
20
22
  end
@@ -1,21 +1,23 @@
1
- class LinearYPdfDataCollector
2
- attr_reader :input_data, :graph_height_pdf, :y_labels
3
- def initialize(input_data, graph_height_pdf, y_labels)
4
- @input_data = input_data
5
- @graph_height_pdf = graph_height_pdf
6
- @y_labels = y_labels
7
- end
1
+ module PrawnCharts
2
+ class LinearYPdfDataCollector
3
+ attr_reader :input_data, :graph_height_pdf, :y_labels
4
+ def initialize(input_data, graph_height_pdf, y_labels)
5
+ @input_data = input_data
6
+ @graph_height_pdf = graph_height_pdf
7
+ @y_labels = y_labels
8
+ end
8
9
 
9
- def collect
10
- input_data.inject([]) do |memo, (_, y_units)|
11
- result = y_units.nil? ? nil : y_units * pdf_points_per_unit
12
- memo << result
10
+ def collect
11
+ input_data.inject([]) do |memo, (_, y_units)|
12
+ result = y_units.nil? ? nil : y_units * pdf_points_per_unit
13
+ memo << result
14
+ end
13
15
  end
14
- end
15
16
 
16
- private
17
+ private
17
18
 
18
- def pdf_points_per_unit
19
- graph_height_pdf.to_f / y_labels.max
19
+ def pdf_points_per_unit
20
+ graph_height_pdf.to_f / y_labels.max
21
+ end
20
22
  end
21
23
  end
@@ -1,19 +1,21 @@
1
- class LogYPdfDataCollector
2
- attr_reader :input_data, :graph_height_pdf, :y_labels
3
- def initialize(input_data, graph_height_pdf, y_labels)
4
- @input_data = input_data
5
- @graph_height_pdf = graph_height_pdf
6
- @y_labels = y_labels
7
- end
1
+ module PrawnCharts
2
+ class LogYPdfDataCollector
3
+ attr_reader :input_data, :graph_height_pdf, :y_labels
4
+ def initialize(input_data, graph_height_pdf, y_labels)
5
+ @input_data = input_data
6
+ @graph_height_pdf = graph_height_pdf
7
+ @y_labels = y_labels
8
+ end
8
9
 
9
- def pdf_points_per_unit
10
- graph_height_pdf.to_f / (y_labels.count - 1)
11
- end
10
+ def pdf_points_per_unit
11
+ graph_height_pdf.to_f / (y_labels.count - 1)
12
+ end
12
13
 
13
- def collect
14
- input_data.inject([]) do |memo, (_, y_units)|
15
- result = y_units.nil? ? nil : (Math.log10(y_units) * pdf_points_per_unit)
16
- memo << result
14
+ def collect
15
+ input_data.inject([]) do |memo, (_, y_units)|
16
+ result = y_units.nil? ? nil : (Math.log10(y_units) * pdf_points_per_unit)
17
+ memo << result
18
+ end
17
19
  end
18
20
  end
19
21
  end
@@ -1,24 +1,26 @@
1
- class PdfDataCollector
2
- attr_reader :scale, :input_data, :graph_width_pdf, :graph_height_pdf, :y_labels
3
- def initialize(scale, input_data, graph_width_pdf, graph_height_pdf, y_labels)
4
- @scale = scale
5
- @input_data = input_data
6
- @graph_width_pdf = graph_width_pdf
7
- @graph_height_pdf = graph_height_pdf
8
- @y_labels = y_labels
9
- end
1
+ module PrawnCharts
2
+ class PdfDataCollector
3
+ attr_reader :scale, :input_data, :graph_width_pdf, :graph_height_pdf, :y_labels
4
+ def initialize(scale, input_data, graph_width_pdf, graph_height_pdf, y_labels)
5
+ @scale = scale
6
+ @input_data = input_data
7
+ @graph_width_pdf = graph_width_pdf
8
+ @graph_height_pdf = graph_height_pdf
9
+ @y_labels = y_labels
10
+ end
10
11
 
11
- def collect
12
- x_pdf_data.zip(y_pdf_data).select { |x, y| [x, y] unless y.nil? }
13
- end
12
+ def collect
13
+ x_pdf_data.zip(y_pdf_data).select { |x, y| [x, y] unless y.nil? }
14
+ end
14
15
 
15
- private
16
+ private
16
17
 
17
- def x_pdf_data
18
- XPdfDataCollector.new(input_data, graph_width_pdf).collect
19
- end
18
+ def x_pdf_data
19
+ XPdfDataCollector.new(input_data, graph_width_pdf).collect
20
+ end
20
21
 
21
- def y_pdf_data
22
- YPdfDataCollector.new(scale, input_data, graph_height_pdf, y_labels).collect
22
+ def y_pdf_data
23
+ YPdfDataCollector.new(scale, input_data, graph_height_pdf, y_labels).collect
24
+ end
23
25
  end
24
26
  end
@@ -1,27 +1,29 @@
1
- class XLabelsDataCollector
2
- attr_reader :input_data, :graph_width_pdf, :label_height, :label_width
3
- def initialize(input_data, graph_width_pdf, label_height, label_width)
4
- @input_data = input_data
5
- @graph_width_pdf = graph_width_pdf
6
- @label_height = label_height
7
- @label_width = label_width
8
- end
1
+ module PrawnCharts
2
+ class XLabelsDataCollector
3
+ attr_reader :input_data, :graph_width_pdf, :label_height, :label_width
4
+ def initialize(input_data, graph_width_pdf, label_height, label_width)
5
+ @input_data = input_data
6
+ @graph_width_pdf = graph_width_pdf
7
+ @label_height = label_height
8
+ @label_width = label_width
9
+ end
9
10
 
10
- def horizontal_offset
11
- label_width / 2
12
- end
11
+ def horizontal_offset
12
+ label_width / 2
13
+ end
13
14
 
14
- def collect
15
- input_data.map.with_index do |(label, _), index|
16
- x_pdf = x_pdf_data[index] - horizontal_offset
17
- y_pdf = -(label_height)
18
- [label, [x_pdf, y_pdf] ]
15
+ def collect
16
+ input_data.map.with_index do |(label, _), index|
17
+ x_pdf = x_pdf_data[index] - horizontal_offset
18
+ y_pdf = -(label_height)
19
+ [label, [x_pdf, y_pdf] ]
20
+ end
19
21
  end
20
- end
21
22
 
22
- private
23
+ private
23
24
 
24
- def x_pdf_data
25
- XPdfDataCollector.new(input_data, graph_width_pdf).collect
25
+ def x_pdf_data
26
+ XPdfDataCollector.new(input_data, graph_width_pdf).collect
27
+ end
26
28
  end
27
29
  end
@@ -1,14 +1,16 @@
1
- class XPdfDataCollector
2
- def initialize(input_data, graph_width_pdf)
3
- @input_data = input_data
4
- @graph_width_pdf = graph_width_pdf
5
- end
1
+ module PrawnCharts
2
+ class XPdfDataCollector
3
+ def initialize(input_data, graph_width_pdf)
4
+ @input_data = input_data
5
+ @graph_width_pdf = graph_width_pdf
6
+ end
6
7
 
7
- def pdf_points_per_unit
8
- @graph_width_pdf.to_f / (@input_data.length - 1)
9
- end
8
+ def pdf_points_per_unit
9
+ @graph_width_pdf.to_f / (@input_data.length - 1)
10
+ end
10
11
 
11
- def collect
12
- 0.step(@graph_width_pdf, pdf_points_per_unit).to_a
12
+ def collect
13
+ 0.step(@graph_width_pdf, pdf_points_per_unit).to_a
14
+ end
13
15
  end
14
16
  end
@@ -1,33 +1,35 @@
1
- class YLabelsDataCollector
2
- attr_reader :y_labels, :graph_height_pdf, :label_width, :label_height, :y_label_offset
3
- def initialize(y_labels, graph_height_pdf, label_width, label_height, y_label_offset)
4
- @y_labels = y_labels
5
- @graph_height_pdf = graph_height_pdf
6
- @label_width = label_width
7
- @label_height = label_height
8
- @y_label_offset = y_label_offset
9
- end
1
+ module PrawnCharts
2
+ class YLabelsDataCollector
3
+ attr_reader :y_labels, :graph_height_pdf, :label_width, :label_height, :y_label_offset
4
+ def initialize(y_labels, graph_height_pdf, label_width, label_height, y_label_offset)
5
+ @y_labels = y_labels
6
+ @graph_height_pdf = graph_height_pdf
7
+ @label_width = label_width
8
+ @label_height = label_height
9
+ @y_label_offset = y_label_offset
10
+ end
10
11
 
11
- def collect
12
- y_labels.map.with_index do |y_label, index|
13
- y_label = number_to_string_with_commas(y_label)
14
- x_pdf = -label_width - y_label_offset
15
- y_pdf = index * y_label_increment + vertical_offset
16
- [y_label, [x_pdf, y_pdf]]
12
+ def collect
13
+ y_labels.map.with_index do |y_label, index|
14
+ y_label = number_to_string_with_commas(y_label)
15
+ x_pdf = -label_width - y_label_offset
16
+ y_pdf = index * y_label_increment + vertical_offset
17
+ [y_label, [x_pdf, y_pdf]]
18
+ end
17
19
  end
18
- end
19
20
 
20
- private
21
+ private
21
22
 
22
- def vertical_offset
23
- label_height / 2
24
- end
23
+ def vertical_offset
24
+ label_height / 2
25
+ end
25
26
 
26
- def y_label_increment
27
- graph_height_pdf.to_f / (y_labels.count - 1)
28
- end
27
+ def y_label_increment
28
+ graph_height_pdf.to_f / (y_labels.count - 1)
29
+ end
29
30
 
30
- def number_to_string_with_commas(number)
31
- number.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse
31
+ def number_to_string_with_commas(number)
32
+ number.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse
33
+ end
32
34
  end
33
35
  end
@@ -1,19 +1,21 @@
1
- class YPdfDataCollector
2
- attr_reader :scale, :input_data, :graph_height_pdf, :y_labels
3
- def initialize(scale, input_data, graph_height_pdf, y_labels)
4
- @scale = scale
5
- @input_data = input_data
6
- @graph_height_pdf = graph_height_pdf
7
- @y_labels = y_labels
8
- end
1
+ module PrawnCharts
2
+ class YPdfDataCollector
3
+ attr_reader :scale, :input_data, :graph_height_pdf, :y_labels
4
+ def initialize(scale, input_data, graph_height_pdf, y_labels)
5
+ @scale = scale
6
+ @input_data = input_data
7
+ @graph_height_pdf = graph_height_pdf
8
+ @y_labels = y_labels
9
+ end
9
10
 
10
- def collect
11
- if scale == :linear
12
- LinearYPdfDataCollector.new(input_data, graph_height_pdf, y_labels).collect
13
- elsif scale == :log
14
- LogYPdfDataCollector.new(input_data, graph_height_pdf, y_labels).collect
15
- else
16
- raise("Scale must be :linear or :log")
11
+ def collect
12
+ if scale == :linear
13
+ LinearYPdfDataCollector.new(input_data, graph_height_pdf, y_labels).collect
14
+ elsif scale == :log
15
+ LogYPdfDataCollector.new(input_data, graph_height_pdf, y_labels).collect
16
+ else
17
+ raise("Scale must be :linear or :log")
18
+ end
17
19
  end
18
20
  end
19
21
  end