prawn_charts 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/data_collectors/container/container_data_collector.rb +57 -55
- data/lib/data_collectors/container/graph_title_data_collector.rb +12 -10
- data/lib/data_collectors/graph/horizontal_lines_data_collector.rb +16 -14
- data/lib/data_collectors/graph/linear_y_pdf_data_collector.rb +17 -15
- data/lib/data_collectors/graph/log_y_pdf_data_collector.rb +16 -14
- data/lib/data_collectors/graph/pdf_data_collector.rb +20 -18
- data/lib/data_collectors/graph/x_labels_data_collector.rb +22 -20
- data/lib/data_collectors/graph/x_pdf_data_collector.rb +12 -10
- data/lib/data_collectors/graph/y_labels_data_collector.rb +27 -25
- data/lib/data_collectors/graph/y_pdf_data_collector.rb +17 -15
- data/lib/examples/log_example.rb +124 -122
- data/lib/examples/simple_linear_example.rb +56 -54
- data/lib/examples/simple_log_example.rb +56 -54
- data/lib/prawn_charts/version.rb +1 -1
- data/lib/renderers/prawn_chart_renderer.rb +21 -19
- data/spec/data_collectors/container/container_data_collector_spec.rb +43 -41
- data/spec/data_collectors/graph/horizontal_lines_data_collector_spec.rb +13 -11
- data/spec/data_collectors/graph/linear_y_pdf_data_collector_spec.rb +13 -11
- data/spec/data_collectors/graph/log_y_pdf_data_collector_spec.rb +19 -17
- data/spec/data_collectors/graph/pdf_data_collector_spec.rb +38 -36
- data/spec/data_collectors/graph/x_labels_data_collector_spec.rb +14 -12
- data/spec/data_collectors/graph/x_pdf_data_collector_spec.rb +15 -13
- data/spec/data_collectors/graph/y_labels_data_collector_spec.rb +15 -13
- metadata +1 -1
@@ -1,57 +1,59 @@
|
|
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
|
-
|
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
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
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
|
-
|
11
|
-
|
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
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
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
|
-
|
10
|
-
|
11
|
-
|
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
|
-
|
16
|
+
private
|
16
17
|
|
17
|
-
|
18
|
-
|
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
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
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
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
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
|
-
|
17
|
+
private
|
17
18
|
|
18
|
-
|
19
|
-
|
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
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
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
|
-
|
10
|
-
|
11
|
-
|
10
|
+
def pdf_points_per_unit
|
11
|
+
graph_height_pdf.to_f / (y_labels.count - 1)
|
12
|
+
end
|
12
13
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
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
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
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
|
-
|
12
|
-
|
13
|
-
|
12
|
+
def collect
|
13
|
+
x_pdf_data.zip(y_pdf_data).select { |x, y| [x, y] unless y.nil? }
|
14
|
+
end
|
14
15
|
|
15
|
-
|
16
|
+
private
|
16
17
|
|
17
|
-
|
18
|
-
|
19
|
-
|
18
|
+
def x_pdf_data
|
19
|
+
XPdfDataCollector.new(input_data, graph_width_pdf).collect
|
20
|
+
end
|
20
21
|
|
21
|
-
|
22
|
-
|
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
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
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
|
-
|
11
|
-
|
12
|
-
|
11
|
+
def horizontal_offset
|
12
|
+
label_width / 2
|
13
|
+
end
|
13
14
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
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
|
-
|
23
|
+
private
|
23
24
|
|
24
|
-
|
25
|
-
|
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
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
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
|
-
|
8
|
-
|
9
|
-
|
8
|
+
def pdf_points_per_unit
|
9
|
+
@graph_width_pdf.to_f / (@input_data.length - 1)
|
10
|
+
end
|
10
11
|
|
11
|
-
|
12
|
-
|
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
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
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
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
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
|
-
|
21
|
+
private
|
21
22
|
|
22
|
-
|
23
|
-
|
24
|
-
|
23
|
+
def vertical_offset
|
24
|
+
label_height / 2
|
25
|
+
end
|
25
26
|
|
26
|
-
|
27
|
-
|
28
|
-
|
27
|
+
def y_label_increment
|
28
|
+
graph_height_pdf.to_f / (y_labels.count - 1)
|
29
|
+
end
|
29
30
|
|
30
|
-
|
31
|
-
|
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
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
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
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
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
|