prawn_charts 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. data/README.md +87 -17
  2. data/lib/prawn_charts.rb +1 -2
  3. data/lib/prawn_charts/core_extensions.rb +7 -0
  4. data/lib/prawn_charts/data_collector.rb +68 -0
  5. data/lib/prawn_charts/data_points.rb +16 -0
  6. data/lib/prawn_charts/examples/linear_example.rb +55 -0
  7. data/lib/prawn_charts/examples/log_example.rb +56 -133
  8. data/lib/prawn_charts/helpers.rb +26 -0
  9. data/lib/prawn_charts/linear_vertical_data_collector.rb +23 -0
  10. data/lib/prawn_charts/log_vertical_data_collector.rb +27 -0
  11. data/lib/prawn_charts/prawn_chart_renderer.rb +139 -0
  12. data/lib/prawn_charts/renderer_assistant.rb +146 -0
  13. data/lib/prawn_charts/version.rb +1 -1
  14. data/spec/data_collector_spec.rb +64 -0
  15. data/spec/linear_vertical_data_collector_spec.rb +17 -0
  16. data/spec/log_vertical_data_collector.rb +12 -0
  17. data/todo +5 -0
  18. metadata +18 -31
  19. data/lib/prawn_charts/data_collectors/container/container_data_collector.rb +0 -59
  20. data/lib/prawn_charts/data_collectors/container/graph_title_data_collector.rb +0 -15
  21. data/lib/prawn_charts/data_collectors/graph/horizontal_lines_data_collector.rb +0 -22
  22. data/lib/prawn_charts/data_collectors/graph/linear_y_pdf_data_collector.rb +0 -23
  23. data/lib/prawn_charts/data_collectors/graph/log_y_pdf_data_collector.rb +0 -21
  24. data/lib/prawn_charts/data_collectors/graph/pdf_data_collector.rb +0 -26
  25. data/lib/prawn_charts/data_collectors/graph/x_labels_data_collector.rb +0 -29
  26. data/lib/prawn_charts/data_collectors/graph/x_pdf_data_collector.rb +0 -16
  27. data/lib/prawn_charts/data_collectors/graph/y_labels_data_collector.rb +0 -35
  28. data/lib/prawn_charts/data_collectors/graph/y_pdf_data_collector.rb +0 -21
  29. data/lib/prawn_charts/examples/simple_linear_example.rb +0 -59
  30. data/lib/prawn_charts/examples/simple_log_example.rb +0 -59
  31. data/lib/prawn_charts/renderers/prawn_chart_renderer.rb +0 -31
  32. data/spec/data_collectors/container/container_data_collector_spec.rb +0 -59
  33. data/spec/data_collectors/graph/horizontal_lines_data_collector_spec.rb +0 -19
  34. data/spec/data_collectors/graph/linear_y_pdf_data_collector_spec.rb +0 -19
  35. data/spec/data_collectors/graph/log_y_pdf_data_collector_spec.rb +0 -26
  36. data/spec/data_collectors/graph/pdf_data_collector_spec.rb +0 -49
  37. data/spec/data_collectors/graph/x_labels_data_collector_spec.rb +0 -20
  38. data/spec/data_collectors/graph/x_pdf_data_collector_spec.rb +0 -23
  39. data/spec/data_collectors/graph/y_labels_data_collector_spec.rb +0 -21
@@ -0,0 +1,12 @@
1
+ require "spec_helper"
2
+
3
+ module PrawnCharts
4
+ describe LogVerticalDataCollector do
5
+ context "#convert_to_pdf" do
6
+ it "converts a y_units data point to a y_pdf_points data point" do
7
+ collector = LogVerticalDataCollector.new(400, [0, 10, 100, 1_000])
8
+ expect(collector.convert_to_pdf(17)).to be_within(0.1).of 164.0
9
+ end
10
+ end
11
+ end
12
+ end
data/todo ADDED
@@ -0,0 +1,5 @@
1
+ 1. Document code
2
+ 2. Analyze how messages are being sent in the program
3
+ 3. Update draw_graph method so it is more elegant
4
+ 4. Read through all the code and refactor
5
+ 5. Add tests
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prawn_charts
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-08-08 00:00:00.000000000 Z
12
+ date: 2013-10-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: prawn
@@ -58,31 +58,23 @@ files:
58
58
  - images/prawn_charts_log_example.png
59
59
  - images/simple_linear_example.png
60
60
  - lib/prawn_charts.rb
61
- - lib/prawn_charts/data_collectors/container/container_data_collector.rb
62
- - lib/prawn_charts/data_collectors/container/graph_title_data_collector.rb
63
- - lib/prawn_charts/data_collectors/graph/horizontal_lines_data_collector.rb
64
- - lib/prawn_charts/data_collectors/graph/linear_y_pdf_data_collector.rb
65
- - lib/prawn_charts/data_collectors/graph/log_y_pdf_data_collector.rb
66
- - lib/prawn_charts/data_collectors/graph/pdf_data_collector.rb
67
- - lib/prawn_charts/data_collectors/graph/x_labels_data_collector.rb
68
- - lib/prawn_charts/data_collectors/graph/x_pdf_data_collector.rb
69
- - lib/prawn_charts/data_collectors/graph/y_labels_data_collector.rb
70
- - lib/prawn_charts/data_collectors/graph/y_pdf_data_collector.rb
61
+ - lib/prawn_charts/core_extensions.rb
62
+ - lib/prawn_charts/data_collector.rb
63
+ - lib/prawn_charts/data_points.rb
64
+ - lib/prawn_charts/examples/linear_example.rb
71
65
  - lib/prawn_charts/examples/log_example.rb
72
- - lib/prawn_charts/examples/simple_linear_example.rb
73
- - lib/prawn_charts/examples/simple_log_example.rb
74
- - lib/prawn_charts/renderers/prawn_chart_renderer.rb
66
+ - lib/prawn_charts/helpers.rb
67
+ - lib/prawn_charts/linear_vertical_data_collector.rb
68
+ - lib/prawn_charts/log_vertical_data_collector.rb
69
+ - lib/prawn_charts/prawn_chart_renderer.rb
70
+ - lib/prawn_charts/renderer_assistant.rb
75
71
  - lib/prawn_charts/version.rb
76
72
  - prawn_charts.gemspec
77
- - spec/data_collectors/container/container_data_collector_spec.rb
78
- - spec/data_collectors/graph/horizontal_lines_data_collector_spec.rb
79
- - spec/data_collectors/graph/linear_y_pdf_data_collector_spec.rb
80
- - spec/data_collectors/graph/log_y_pdf_data_collector_spec.rb
81
- - spec/data_collectors/graph/pdf_data_collector_spec.rb
82
- - spec/data_collectors/graph/x_labels_data_collector_spec.rb
83
- - spec/data_collectors/graph/x_pdf_data_collector_spec.rb
84
- - spec/data_collectors/graph/y_labels_data_collector_spec.rb
73
+ - spec/data_collector_spec.rb
74
+ - spec/linear_vertical_data_collector_spec.rb
75
+ - spec/log_vertical_data_collector.rb
85
76
  - spec/spec_helper.rb
77
+ - todo
86
78
  homepage: ''
87
79
  licenses: []
88
80
  post_install_message:
@@ -108,12 +100,7 @@ signing_key:
108
100
  specification_version: 3
109
101
  summary: Library to create line graphs in Prawn
110
102
  test_files:
111
- - spec/data_collectors/container/container_data_collector_spec.rb
112
- - spec/data_collectors/graph/horizontal_lines_data_collector_spec.rb
113
- - spec/data_collectors/graph/linear_y_pdf_data_collector_spec.rb
114
- - spec/data_collectors/graph/log_y_pdf_data_collector_spec.rb
115
- - spec/data_collectors/graph/pdf_data_collector_spec.rb
116
- - spec/data_collectors/graph/x_labels_data_collector_spec.rb
117
- - spec/data_collectors/graph/x_pdf_data_collector_spec.rb
118
- - spec/data_collectors/graph/y_labels_data_collector_spec.rb
103
+ - spec/data_collector_spec.rb
104
+ - spec/linear_vertical_data_collector_spec.rb
105
+ - spec/log_vertical_data_collector.rb
119
106
  - spec/spec_helper.rb
@@ -1,59 +0,0 @@
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
58
- end
59
- end
@@ -1,15 +0,0 @@
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
10
-
11
- def collect
12
- { title: graph_title, at: position, height: graph_title_height, width: graph_title_width }
13
- end
14
- end
15
- end
@@ -1,22 +0,0 @@
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
9
-
10
- def collect
11
- y_labels.map.with_index do |_, index|
12
- [0, graph_width_pdf, index * y_label_increment]
13
- end
14
- end
15
-
16
- private
17
-
18
- def y_label_increment
19
- graph_height_pdf.to_f / (y_labels.count - 1)
20
- end
21
- end
22
- end
@@ -1,23 +0,0 @@
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
9
-
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
15
- end
16
-
17
- private
18
-
19
- def pdf_points_per_unit
20
- graph_height_pdf.to_f / y_labels.max
21
- end
22
- end
23
- end
@@ -1,21 +0,0 @@
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
9
-
10
- def pdf_points_per_unit
11
- graph_height_pdf.to_f / (y_labels.count - 1)
12
- end
13
-
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
19
- end
20
- end
21
- end
@@ -1,26 +0,0 @@
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
11
-
12
- def collect
13
- x_pdf_data.zip(y_pdf_data).select { |x, y| [x, y] unless y.nil? }
14
- end
15
-
16
- private
17
-
18
- def x_pdf_data
19
- XPdfDataCollector.new(input_data, graph_width_pdf).collect
20
- end
21
-
22
- def y_pdf_data
23
- YPdfDataCollector.new(scale, input_data, graph_height_pdf, y_labels).collect
24
- end
25
- end
26
- end
@@ -1,29 +0,0 @@
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
10
-
11
- def horizontal_offset
12
- label_width / 2
13
- end
14
-
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
21
- end
22
-
23
- private
24
-
25
- def x_pdf_data
26
- XPdfDataCollector.new(input_data, graph_width_pdf).collect
27
- end
28
- end
29
- end
@@ -1,16 +0,0 @@
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
7
-
8
- def pdf_points_per_unit
9
- @graph_width_pdf.to_f / (@input_data.length - 1)
10
- end
11
-
12
- def collect
13
- 0.step(@graph_width_pdf, pdf_points_per_unit).to_a
14
- end
15
- end
16
- end
@@ -1,35 +0,0 @@
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
11
-
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
19
- end
20
-
21
- private
22
-
23
- def vertical_offset
24
- label_height / 2
25
- end
26
-
27
- def y_label_increment
28
- graph_height_pdf.to_f / (y_labels.count - 1)
29
- end
30
-
31
- def number_to_string_with_commas(number)
32
- number.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse
33
- end
34
- end
35
- end
@@ -1,21 +0,0 @@
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
10
-
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
19
- end
20
- end
21
- end
@@ -1,59 +0,0 @@
1
- require_relative "./../prawn_charts"
2
-
3
- module PrawnCharts
4
- ##################################
5
- #Graph Data Collectors
6
- ##################################
7
- input_data = [["Apr-11", 10],
8
- ["May-11", 30],
9
- ["Jun-11", 50],
10
- ["Jul-11", 3],
11
- ["Aug-11", 40],
12
- ["Sep-11", nil],
13
- ["Oct-11", nil],
14
- ["Dec-11", 100],
15
- ["Jan-12", nil],
16
- ["Feb-12", nil],
17
- ["Mar-12", 5],
18
- ["Apr-12", nil],
19
- ["May-12", nil],
20
- ["Jun-12", 75]]
21
- scale = :linear
22
- graph_height_pdf = 200
23
- graph_width_pdf = graph_height_pdf * 1.7
24
- dot_radius = 4
25
-
26
- x_label_width = 50
27
- x_label_height = 35
28
- x_label_text_box_options = { overflow: :shrink_to_fit, align: :center, rotate: 45 }
29
-
30
- y_labels = [0, 25, 50, 75, 100, 125]
31
- y_label_height = 30
32
- y_label_width = 65
33
- y_label_offset = 10
34
- y_label_text_box_options = { align: :right, valign: :center }
35
-
36
- pdf_data = PdfDataCollector.new(scale, input_data, graph_width_pdf, graph_height_pdf, y_labels).collect
37
- x_label_data = XLabelsDataCollector.new(input_data, graph_width_pdf, x_label_height, x_label_width).collect
38
- y_label_data = YLabelsDataCollector.new(y_labels, graph_height_pdf, y_label_width, y_label_height, y_label_offset).collect
39
- horizontal_lines_data = HorizontalLinesDataCollector.new(graph_height_pdf, graph_width_pdf, y_labels).collect
40
-
41
- ##################################
42
- #Create PDF
43
- ##################################
44
-
45
- Prawn::Document.extensions << PrawnChartRenderer
46
-
47
- pdf = Prawn::Document.new
48
-
49
- pdf.bounding_box([50, pdf.cursor], :width => graph_width_pdf, :height => graph_height_pdf) do
50
- pdf.stroke_bounds
51
- pdf.draw_chart(pdf_data)
52
- pdf.draw_dots(pdf_data, dot_radius)
53
- pdf.draw_labels(x_label_data, x_label_width, x_label_height, x_label_text_box_options)
54
- pdf.draw_labels(y_label_data, y_label_width, y_label_height, y_label_text_box_options)
55
- pdf.draw_horizontal_lines(horizontal_lines_data)
56
- end
57
-
58
- pdf.render_file(Dir.home + "/desktop/simple_linear_prawn_graph.pdf")
59
- end
@@ -1,59 +0,0 @@
1
- require_relative "./../prawn_charts"
2
-
3
- module PrawnCharts
4
- ##################################
5
- #Graph Data Collectors
6
- ##################################
7
- input_data = [["Apr-11", 5_000_000],
8
- ["May-11", nil],
9
- ["Jun-11", nil],
10
- ["Jul-11", nil],
11
- ["Aug-11", 1_000_000],
12
- ["Sep-11", nil],
13
- ["Oct-11", nil],
14
- ["Dec-11", 10_000],
15
- ["Jan-12", nil],
16
- ["Feb-12", nil],
17
- ["Mar-12", 150_000],
18
- ["Apr-12", nil],
19
- ["May-12", nil],
20
- ["Jun-12", 7_500_000]]
21
- scale = :log
22
- graph_height_pdf = 200
23
- graph_width_pdf = graph_height_pdf * 1.7
24
- dot_radius = 4
25
-
26
- x_label_width = 50
27
- x_label_height = 35
28
- x_label_text_box_options = { overflow: :shrink_to_fit, align: :center, rotate: 45 }
29
-
30
- y_labels = [0, 10, 100, 1_000, 10_000, 100_000, 1_000_000, 10_000_000]
31
- y_label_height = 30
32
- y_label_width = 65
33
- y_label_offset = 10
34
- y_label_text_box_options = { align: :right, valign: :center }
35
-
36
- pdf_data = PdfDataCollector.new(scale, input_data, graph_width_pdf, graph_height_pdf, y_labels).collect
37
- x_label_data = XLabelsDataCollector.new(input_data, graph_width_pdf, x_label_height, x_label_width).collect
38
- y_label_data = YLabelsDataCollector.new(y_labels, graph_height_pdf, y_label_width, y_label_height, y_label_offset).collect
39
- horizontal_lines_data = HorizontalLinesDataCollector.new(graph_height_pdf, graph_width_pdf, y_labels).collect
40
-
41
- ##################################
42
- #Create PDF
43
- ##################################
44
-
45
- Prawn::Document.extensions << PrawnChartRenderer
46
-
47
- pdf = Prawn::Document.new
48
-
49
- pdf.bounding_box([50, pdf.cursor], :width => graph_width_pdf, :height => graph_height_pdf) do
50
- pdf.stroke_bounds
51
- pdf.draw_chart(pdf_data)
52
- pdf.draw_dots(pdf_data, dot_radius)
53
- pdf.draw_labels(x_label_data, x_label_width, x_label_height, x_label_text_box_options)
54
- pdf.draw_labels(y_label_data, y_label_width, y_label_height, y_label_text_box_options)
55
- pdf.draw_horizontal_lines(horizontal_lines_data)
56
- end
57
-
58
- pdf.render_file(Dir.home + "/desktop/simple_log_prawn_graph.pdf")
59
- end