prawn_charts 0.0.4 → 0.0.5

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.
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
@@ -1,31 +0,0 @@
1
- module PrawnCharts
2
- module PrawnChartRenderer
3
- def draw_chart(pdf_data)
4
- pdf_data.each_index do |i|
5
- stroke_line(pdf_data[i], pdf_data[i + 1]) unless pdf_data[i + 1].nil?
6
- end
7
- end
8
-
9
- def draw_dots(pdf_data, dot_radius)
10
- pdf_data.each do |point|
11
- fill_circle(point, dot_radius)
12
- end
13
- end
14
-
15
- def draw_horizontal_lines(horizontal_lines_data)
16
- horizontal_lines_data.each do |start_x, end_x, y|
17
- stroke_horizontal_line start_x, end_x, at: y
18
- end
19
- end
20
-
21
- def draw_labels(label_data, width, height, options = {})
22
- label_data.each do |label, pdf_point|
23
- text_box(label, { at: pdf_point, width: width, height: height }.merge(options))
24
- end
25
- end
26
-
27
- def draw_title(inputs, options = {})
28
- text_box(inputs[:title], { at: inputs[:at], width: inputs[:width], height: inputs[:height] }.merge(options))
29
- end
30
- end
31
- end
@@ -1,59 +0,0 @@
1
- require "spec_helper"
2
-
3
- module PrawnCharts
4
- describe ContainerDataCollector do
5
- before do
6
- container_inputs = {
7
- container_left_padding: 7,
8
- y_label_offset: 50,
9
- y_label_width: 25,
10
- y_title_width: 30,
11
- container_right_padding: 20,
12
- container_top_padding: 9,
13
- graph_title_height: 25,
14
- container_bottom_padding: 40,
15
- x_label_height: 22,
16
- x_title_height: 43,
17
- graph_height: 400,
18
- graph_width: 500
19
- }
20
- @container_data_collector = ContainerDataCollector.new(container_inputs)
21
- end
22
-
23
- context "#height" do
24
- it "returns the pdf_height of the graph container" do
25
- expect(@container_data_collector.height).to eq(539)
26
- end
27
- end
28
-
29
- context "#width" do
30
- it "returns the pdf_width of the graph container" do
31
- expect(@container_data_collector.width).to eq(632)
32
- end
33
- end
34
-
35
- context "#graph_top_left_corner" do
36
- it "returns the point for the top-left corner of the graph" do
37
- expect(@container_data_collector.graph_top_left).to eq([112, 505])
38
- end
39
- end
40
-
41
- context "#graph_title_top_left" do
42
- it "returns x, y coordinate for placement of graph title" do
43
- expect(@container_data_collector.graph_title_top_left).to eq([112, 530])
44
- end
45
- end
46
-
47
- context "#y_title_top_left" do
48
- it "returns x, y coordinate for placement of y title" do
49
- expect(@container_data_collector.y_title_top_left).to eq([7, 505])
50
- end
51
- end
52
-
53
- context "#x_title_top_left" do
54
- it "returns x, y coordinate for placement of x title" do
55
- expect(@container_data_collector.x_title_top_left).to eq([112, 83])
56
- end
57
- end
58
- end
59
- end
@@ -1,19 +0,0 @@
1
- require "spec_helper"
2
-
3
- module PrawnCharts
4
- describe HorizontalLinesDataCollector do
5
- before do
6
- graph_height_pdf = 60
7
- graph_width_pdf = 80
8
- y_labels = [0, 10, 100, 1_000, 10_000, 100_000, 1_000_000]
9
- @horizontal_lines_data_collector = HorizontalLinesDataCollector.new(graph_height_pdf, graph_width_pdf, y_labels)
10
- end
11
-
12
- context "#collect" do
13
- it "returns an array with the start, end, and y position of the horizontal lines" do
14
- expected = [[0, 80, 0.0], [0, 80, 10.0], [0, 80, 20.0], [0, 80, 30.0], [0, 80, 40.0], [0, 80, 50.0], [0, 80, 60.0]]
15
- expect(@horizontal_lines_data_collector.collect).to eq(expected)
16
- end
17
- end
18
- end
19
- end
@@ -1,19 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module PrawnCharts
4
- describe LinearYPdfDataCollector do
5
- before do
6
- input_data = [["Jan 11", 5], ["Feb 11", 2], ["Mar 11", 12]]
7
- graph_height_pdf = 160
8
- y_labels = [0, 4, 8, 12, 16]
9
- @linear_y_pdf_data_collector = LinearYPdfDataCollector.new(input_data, graph_height_pdf, y_labels)
10
- end
11
-
12
- context "#collect" do
13
- it "returns an array of y values in pdf points" do
14
- expected = [50, 20, 120]
15
- expect(@linear_y_pdf_data_collector.collect).to eq(expected)
16
- end
17
- end
18
- end
19
- end
@@ -1,26 +0,0 @@
1
- require "spec_helper"
2
-
3
- module PrawnCharts
4
- describe LogYPdfDataCollector do
5
- before do
6
- @graph_height_pdf = 60
7
- @y_labels = [0, 10, 100, 1_000, 10_000, 100_000, 1_000_000]
8
- end
9
-
10
- context "#collect" do
11
- it "returns an array of [x_pdf, y_pdf] for all data points" do
12
- input_data = [["Jan 11", 5], ["Feb 11", 900], ["Mar 11", 800_000]]
13
- log_y_pdf_data_collector = LogYPdfDataCollector.new(input_data, @graph_height_pdf, @y_labels)
14
- expected = [10 * Math.log10(5), 10 * Math.log10(900), 10 * Math.log10(800_000)]
15
- expect(log_y_pdf_data_collector.collect).to eq(expected)
16
- end
17
-
18
- it "it returns nil when a value is nil" do
19
- input_data = [["Jan 11", 5], ["Feb 11", nil], ["Mar 11", 800_000]]
20
- log_y_pdf_data_collector = LogYPdfDataCollector.new(input_data, @graph_height_pdf, @y_labels)
21
- expected = [10 * Math.log10(5), nil, 10 * Math.log10(800_000)]
22
- expect(log_y_pdf_data_collector.collect).to eq(expected)
23
- end
24
- end
25
- end
26
- end
@@ -1,49 +0,0 @@
1
- require "spec_helper"
2
-
3
- module PrawnCharts
4
- describe PdfDataCollector do
5
- context "linear" do
6
- before do
7
- scale = :linear
8
- input_data = [["Jan 11", 5], ["Feb 11", 2], ["Mar 11", 12]]
9
- graph_width_pdf = 100
10
- graph_height_pdf = 120
11
- y_labels = [0, 3, 6, 9, 12]
12
- @linear_pdf_data_collector = PdfDataCollector.new(scale, input_data, graph_width_pdf, graph_height_pdf, y_labels)
13
- end
14
-
15
- it "returns an array of pdf points where line should be plotted" do
16
- expected = [[0, 50], [50, 20], [100, 120]]
17
- expect(@linear_pdf_data_collector.collect).to eq(expected)
18
- end
19
- end
20
-
21
- context "log" do
22
- before do
23
- scale = :log
24
- input_data = [["Jan 11", 5], ["Feb 11", 900], ["Mar 11", 800_000]]
25
- graph_width_pdf = 200
26
- graph_height_pdf = 120
27
- y_labels = [0, 10, 100, 1_000, 10_000, 100_000, 1_000_000]
28
- @log_pdf_data_collector = PdfDataCollector.new(scale, input_data, graph_width_pdf, graph_height_pdf, y_labels)
29
- end
30
-
31
- it "returns an array of pdf points where line should be plotted" do
32
- expected = [[0.0, Math.log10(5) * 20], [100.0, Math.log10(900) * 20], [200.0, Math.log10(800_000) * 20]]
33
- expect(@log_pdf_data_collector.collect).to eq(expected)
34
- end
35
- end
36
-
37
- context "invalid scale input" do
38
- it "raises an error unless scale is :linear or :log" do
39
- scale = :phattie
40
- input_data = [["Jan 11", 5], ["Feb 11", 900], ["Mar 11", 800_000]]
41
- graph_width_pdf = 200
42
- graph_height_pdf = 120
43
- y_labels = [0, 10, 100, 1_000, 10_000, 100_000, 1_000_000]
44
- log_pdf_data_collector = PdfDataCollector.new(scale, input_data, graph_width_pdf, graph_height_pdf, y_labels)
45
- expect{log_pdf_data_collector.collect}.to raise_error(RuntimeError)
46
- end
47
- end
48
- end
49
- end
@@ -1,20 +0,0 @@
1
- require "spec_helper"
2
-
3
- module PrawnCharts
4
- describe XLabelsDataCollector do
5
- before do
6
- input_data = [["Jan 11", 5], ["Feb 11", 2], ["Mar 11", 12]]
7
- graph_width_pdf = 160
8
- vertical_offset = 20
9
- label_width = 50
10
- @x_labels_data_collector = XLabelsDataCollector.new(input_data, graph_width_pdf, vertical_offset, label_width)
11
- end
12
-
13
- context "#collect" do
14
- it "returns array of labels and [x, y] coordinate where the label should be plotted" do
15
- expected = [["Jan 11", [-25.0, -20]], ["Feb 11", [55.0, -20]], ["Mar 11", [135.0, -20]]]
16
- expect(@x_labels_data_collector.collect).to eq(expected)
17
- end
18
- end
19
- end
20
- end
@@ -1,23 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module PrawnCharts
4
- describe XPdfDataCollector do
5
- before do
6
- input_data = [["Jan 11", 5], ["Feb 11", 2], ["Mar 11", 12]]
7
- graph_width_pdf = 160
8
- @x_pdf_data_collector = XPdfDataCollector.new(input_data, graph_width_pdf)
9
- end
10
-
11
- context "#pdf_points_per_unit" do
12
- it "returns number of pdf points per unit on x axis" do
13
- expect(@x_pdf_data_collector.pdf_points_per_unit).to eq(80.0)
14
- end
15
- end
16
-
17
- context "#collect" do
18
- it "returns an array of x pdf points" do
19
- expect(@x_pdf_data_collector.collect).to eq([0, 80, 160])
20
- end
21
- end
22
- end
23
- end
@@ -1,21 +0,0 @@
1
- require "spec_helper"
2
-
3
- module PrawnCharts
4
- describe YLabelsDataCollector do
5
- before do
6
- y_labels = [0, 10, 100, 1_000, 10_000, 100_000, 1_000_000, 10_000_000]
7
- graph_height_pdf = 560
8
- label_width = 15
9
- label_height = 20
10
- y_label_offset = 30
11
- @y_data_collector = YLabelsDataCollector.new(y_labels, graph_height_pdf, label_width, label_height, y_label_offset)
12
- end
13
-
14
- context "#collect" do
15
- it "returns an array of y_labels and points where they should be plotted" do
16
- expected = [["0", [-45, 10.0]], ["10", [-45, 90.0]], ["100", [-45, 170.0]], ["1,000", [-45, 250.0]], ["10,000", [-45, 330.0]], ["100,000", [-45, 410.0]], ["1,000,000", [-45, 490.0]], ["10,000,000", [-45, 570.0]]]
17
- expect(@y_data_collector.collect).to eq(expected)
18
- end
19
- end
20
- end
21
- end