prawn-graph 0.0.4 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. checksums.yaml +7 -0
  2. data/.codeclimate.yml +16 -0
  3. data/.gitignore +9 -0
  4. data/.rspec +2 -0
  5. data/.rubocop.yml +1168 -0
  6. data/.travis.yml +17 -0
  7. data/CODE_OF_CONDUCT.md +49 -0
  8. data/CONTRIBUTORS.md +6 -0
  9. data/Gemfile +4 -0
  10. data/LICENSE.txt +21 -0
  11. data/README.md +142 -0
  12. data/Rakefile +7 -43
  13. data/bin/console +14 -0
  14. data/bin/setup +8 -0
  15. data/lib/prawn-graph.rb +16 -0
  16. data/lib/prawn/graph/calculations.rb +1 -0
  17. data/lib/prawn/graph/calculations/layout_calculator.rb +108 -0
  18. data/lib/prawn/graph/chart_components.rb +2 -0
  19. data/lib/prawn/graph/chart_components/canvas.rb +138 -0
  20. data/lib/prawn/graph/chart_components/series_renderer.rb +173 -0
  21. data/lib/prawn/graph/charts.rb +4 -0
  22. data/lib/prawn/graph/charts/bar.rb +18 -0
  23. data/lib/prawn/graph/charts/base.rb +69 -0
  24. data/lib/prawn/graph/charts/legacy.rb +4 -0
  25. data/lib/prawn/graph/charts/legacy/bar.rb +28 -0
  26. data/lib/prawn/graph/charts/legacy/base.rb +193 -0
  27. data/lib/prawn/graph/charts/legacy/grid.rb +51 -0
  28. data/lib/prawn/graph/charts/legacy/line.rb +39 -0
  29. data/lib/prawn/graph/charts/line.rb +18 -0
  30. data/lib/prawn/graph/extension.rb +59 -0
  31. data/lib/prawn/graph/series.rb +79 -0
  32. data/lib/prawn/graph/theme.rb +41 -0
  33. data/lib/prawn/graph/version.rb +5 -0
  34. data/prawn-graph.gemspec +42 -0
  35. metadata +156 -80
  36. data/README.markdown +0 -64
  37. data/examples/example_helper.rb +0 -10
  38. data/examples/graph/advanced_bar_chart.rb +0 -22
  39. data/examples/graph/bar_chart.pdf +0 -185
  40. data/examples/graph/bar_chart.rb +0 -18
  41. data/examples/graph/line_chart.pdf +0 -219
  42. data/examples/graph/line_chart.rb +0 -18
  43. data/examples/graph/themed_bar_chart.rb +0 -18
  44. data/examples/graph/themed_line_chart.rb +0 -18
  45. data/lib/prawn/graph.rb +0 -94
  46. data/lib/prawn/graph/bar.rb +0 -64
  47. data/lib/prawn/graph/base.rb +0 -231
  48. data/lib/prawn/graph/chart.rb +0 -4
  49. data/lib/prawn/graph/errors.rb +0 -7
  50. data/lib/prawn/graph/grid.rb +0 -50
  51. data/lib/prawn/graph/line.rb +0 -74
  52. data/lib/prawn/graph/themes.rb +0 -116
  53. data/lib/prawn/graph/themes/37signals.yml +0 -14
  54. data/lib/prawn/graph/themes/keynote.yml +0 -14
  55. data/lib/prawn/graph/themes/monochome.yml +0 -8
  56. data/lib/prawn/graph/themes/odeo.yml +0 -14
data/.travis.yml ADDED
@@ -0,0 +1,17 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 2.1.0-p0
5
+ - 2.1.8
6
+ - 2.2.0-p0
7
+ - 2.2.4
8
+ - 2.3.0
9
+ - jruby-9.0.5.0
10
+ - rbx-3.19
11
+ matrix:
12
+ allow_failures:
13
+ - rvm: rbx-3.19
14
+ before_install: gem install bundler -v 1.11.2
15
+ addons:
16
+ code_climate:
17
+ repo_token: 9646aae52d1d1670d545c46bd94f5d86b92d516c5e3cb9890fcef62acc3a34b6
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at ryan@ryanstenhouse.eu. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/CONTRIBUTORS.md ADDED
@@ -0,0 +1,6 @@
1
+ Past and present direct and indirect contributors to prawn-graph
2
+ ----------------------------------------------------------------
3
+
4
+ * Ryan Stenhouse <https://github.com/hhry>
5
+ * Roger Nesbitt <https://github.com/mogest>
6
+ * S. Hessam M. Mehr <https://github.com/hessammehr>
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in prawn-graph.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Ryan Stenhouse
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,142 @@
1
+ # Prawn::Graph - Easy Graphing for Prawn
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/prawn-graph.svg)](https://badge.fury.io/rb/prawn-graph)
4
+ [![License](http://img.shields.io/:license-mit-blue.svg)](http://sujrd.mit-license.org)
5
+ [![Code Climate](https://codeclimate.com/github/HHRy/prawn-graph/badges/gpa.svg)](https://codeclimate.com/github/HHRy/prawn-graph)
6
+ [![Test Coverage](https://codeclimate.com/github/HHRy/prawn-graph/badges/coverage.svg)](https://codeclimate.com/github/HHRy/prawn-graph/coverage)
7
+ [![Build Status](https://travis-ci.org/HHRy/prawn-graph.svg?branch=master)](https://travis-ci.org/HHRy/prawn-graph)
8
+ [![security](https://hakiri.io/github/HHRy/prawn-graph/master.svg)](https://hakiri.io/github/HHRy/prawn-graph/master)
9
+ ![Maintained: yes](https://img.shields.io/badge/maintained-yes-brightgreen.png)
10
+
11
+ An extension for the [prawn pdf library][5] which adds the ability to draw graphs (or charts if
12
+ you perfer) in PDF documents.
13
+
14
+ Because Prawn::Graph only uses the native PDF drawing functions exposed by Prawn, it removes the need to
15
+ depend on projects like Gruff to generate heavy PNG / JPG images of such graphs and charts and then include
16
+ those large blobs in your documents. The results may not be as pretty (yet), but the file-size differences
17
+ are dramatic.
18
+
19
+ By default, graphs are drawn in monochrome, as that's likely how they will be printed.
20
+
21
+ This is free and open source software released under ther terms of the [MIT Licence](http://opensource.org/licenses/MIT).
22
+
23
+ Its copyright is held by Ryan Stenhouse and the [other contributors][8] and it was first released in
24
+ 2010.
25
+
26
+ ## Compatibility
27
+
28
+ This gem is built assuming a Ruby version of 2.0 or higher. Older Ruby versions may work but are not
29
+ officially supported. We aim for compatibilty with 1.x and 2.x series of prawn. Any incomaptibilities
30
+ with prawn versions should be treated as bugs and added to the [issue tracker][2].
31
+
32
+ We build automatically using Travis CI. Our [.travis.yml][9] file targets the same Ruby versions as
33
+ [prawn itself][5] does.
34
+
35
+
36
+ ### Legacy support and deprecation notices.
37
+
38
+ To provide backwards-compatibility (of sorts), `prawn-graph` provides `bar_graph` (aliased as `bar_chart`)
39
+ and `line_graph` (aliased as `line_chart`) methods as part of its interface which function the same as their
40
+ equivalents in version `0.0.4`. These methods are, however, deprecated and **will** be removed when version
41
+ `1.0.0` is released. Using these methods will result in a `warn` level message from Ruby.
42
+
43
+ These legacy methods will still be buggy as they are using the same rendering methods as before. Where possible
44
+ you should use the new `graph` (aliased as `chart`) interface. Unless serious problems are uncovered, bugs
45
+ reported with legacy rendering **will not be considered**.
46
+
47
+ Unlike previous versions of `prawn-graph`, this version does not at this time include a theme api or the
48
+ ability to change the colors used to render the graph.
49
+
50
+ ## Installation
51
+
52
+ To use prawn-graph, you can add the following to your `Gemfile`:
53
+
54
+ ```Gemfile
55
+ gem 'prawn-graph', '0.9.0'
56
+ ```
57
+
58
+ Alternatively, you can use Rubygems directly: `gem install prawn-graph`.
59
+
60
+ ## Acknowledgements
61
+
62
+ With thanks to [株式会社アルム][3] ([Allm Inc][4]) for allowing Ryan Stenhouse the time to rebuild this version of
63
+ prawn-graph. This updated version of prawn-graph was inspired and guided by [prawn-svg][1] by [Roger Nesbitt][6].
64
+
65
+ Prawn Graph was originally sponsored by and built for use at [Purchasing Card Consultancy Ltd][7] while
66
+ Ryan Stenhouse was employed there.
67
+
68
+ ## Supported graph / chart types
69
+
70
+ This version of Prawn::Graph supports the following graph / chart types:
71
+
72
+ * Bar Charts
73
+ * Line Charts
74
+
75
+ Is your favourite chart type not supported yet? [Please request it][2], or if you are feeling particularly
76
+ adventurous - please add it!
77
+
78
+ ## Using prawn-graph
79
+
80
+ ```ruby
81
+ require 'prawn-graph'
82
+
83
+ series = []
84
+ series << Prawn::Graph::Series.new([1,2,3,4,5], title: "Some numbers", type: :bar)
85
+
86
+ Prawn::Document.generate('test.pdf') do
87
+ graph series
88
+ end
89
+ ```
90
+
91
+ When called with just a set of data, prawn-graph will do its best to make the graph fit in the
92
+ available space. For a little more control over how the graphs are rendered in the document
93
+ you can pass the following options:
94
+
95
+ Option | Data type | Description
96
+ ----------- | --------- | -----------
97
+ :at | [integer, integer] | Specify the location on the page you want the graph to appear.
98
+ :width | integer | Desired width of the graph. Defaults to horizontal space available.
99
+ :height | integer | Desired height of the graph. Defaults to vertical space available.
100
+
101
+ ### Advanced example
102
+
103
+ ```ruby
104
+ require 'prawn-graph'
105
+
106
+ series = []
107
+ series << Prawn::Graph::Series.new([5,4,3,2,1,1,2,8,7,5,4,9,2], title: "Some more numbers", type: :bar)
108
+ series << Prawn::Graph::Series.new([5,4,3,2,1,1,2,8,7,5,4,9,2].reverse, title: "Some more numbers", type: :bar)
109
+ series << Prawn::Graph::Series.new([1,2,3,4,5,9,6,4,5,6,3,2,9], title: "Some numbers", type: :bar)
110
+ series << Prawn::Graph::Series.new([1,2,3,4,5,9,6,4,5,6,3,2,9].shuffle, title: "Some numbers", type: :line)
111
+
112
+ Prawn::Document.generate('test.pdf') do
113
+ graph series, width: 500, height: 200, title: "A very long title will porbably be clipped", at: [10,700]
114
+ end
115
+ ```
116
+
117
+ ## Development
118
+
119
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can
120
+ also run `bin/console` for an interactive prompt that will allow you to experiment.
121
+
122
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the
123
+ version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version,
124
+ push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
125
+
126
+ ## Contributing
127
+
128
+ Bug reports and pull requests are welcome [on GitHub][2]. This project is intended to be a
129
+ safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org)
130
+ code of conduct.
131
+
132
+
133
+
134
+ [1]: https://github.com/mogest/prawn-svg/
135
+ [2]: https://github.com/hhry/prawn-graph/issues/
136
+ [3]: http://www.allm.net/
137
+ [4]: http://www.allm.net/en/
138
+ [5]: http://github.com/prawnpdf/prawn/
139
+ [6]: https://github.com/mogest/
140
+ [7]: http://www.pccl.co.uk/
141
+ [8]: https://github.com/HHRy/prawn-graph/blob/master/CONTRIBUTORS.md
142
+ [9]: https://github.com/HHRy/prawn-graph/blob/master/.travis.yml
data/Rakefile CHANGED
@@ -1,48 +1,12 @@
1
- require 'rubygems'
2
- require 'rake'
3
- require "rake/gempackagetask"
4
- require "rake/rdoctask"
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+ require 'yard'
5
4
 
6
- PRAWN_GRAPH_VERSION = '0.0.4'
7
5
 
8
- spec = Gem::Specification.new do |spec|
9
- spec.name = "prawn-graph"
10
- spec.version = PRAWN_GRAPH_VERSION
11
- spec.platform = Gem::Platform::RUBY
12
- spec.summary = "An extension to Prawn that provides the ability to draw basic graphs and charts natively in your PDFs."
13
- spec.files = Dir.glob("{examples,lib,spec,vendor,data}/**/**/*") +
14
- ["Rakefile"]
15
- spec.require_path = "lib"
16
-
17
- spec.test_files = Dir[ "test/*_test.rb" ]
18
- spec.has_rdoc = true
19
- spec.extra_rdoc_files = %w{README.markdown}
20
- spec.rdoc_options << '--title' << 'Prawn Documentation' <<
21
- '--main' << 'README' << '-q'
22
- spec.author = "Ryan Stenhouse"
23
- spec.email = " ryan@ryanstenhouse.eu"
24
- spec.rubyforge_project = "prawn"
25
- spec.add_dependency 'prawn'
26
- spec.homepage = "http://ryanstenhouse.eu"
27
- spec.description = <<END_DESC
28
- An extension to Prawn that provides the ability to draw basic graphs and charts natively in your PDFs.
29
- END_DESC
6
+ YARD::Rake::YardocTask.new do |t|
7
+ t.files = ['lib/**/*.rb', '-', '*.md']
30
8
  end
31
9
 
32
- desc "genrates documentation"
33
- Rake::RDocTask.new do |rdoc|
34
- rdoc.rdoc_files.include( "README.markdown",
35
- "COPYING",
36
- "LICENSE",
37
- "HACKING", "lib/" )
38
- rdoc.main = "README.markdown"
39
- rdoc.rdoc_dir = "doc/html"
40
- rdoc.title = "Prawn Documentation"
41
- end
10
+ RSpec::Core::RakeTask.new(:spec)
42
11
 
43
-
44
- Rake::GemPackageTask.new(spec) do |pkg|
45
- pkg.need_zip = true
46
- pkg.need_tar = true
47
- end
48
-
12
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "prawn-graph"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,16 @@
1
+ require "ostruct"
2
+ require "bigdecimal"
3
+ require "prawn"
4
+ require "prawn/graph/version"
5
+ require "prawn/graph/calculations"
6
+
7
+ require "prawn/graph/chart_components"
8
+
9
+ require "prawn/graph/theme"
10
+ require "prawn/graph/series"
11
+ require "prawn/graph/charts"
12
+
13
+ require "prawn/graph/extension"
14
+
15
+
16
+ Prawn::Document.extensions << Prawn::Graph::Extension
@@ -0,0 +1 @@
1
+ require_relative "calculations/layout_calculator"
@@ -0,0 +1,108 @@
1
+ module Prawn
2
+ module Graph
3
+ module Calculations
4
+
5
+ class LayoutCalculator
6
+ attr_reader :bounds
7
+ attr_reader :series_key_area, :title_area, :graph_area, :canvas_width, :canvas_height
8
+
9
+ class Dimensions < OpenStruct
10
+ def renderable?
11
+ width > 0 && height > 0
12
+ end
13
+
14
+ def point
15
+ [x, y]
16
+ end
17
+ end
18
+
19
+ def initialize(bounds, attributes = nil, theme = Prawn::Graph::Theme::Default)
20
+ @bounds = bounds
21
+ @graph_area = Dimensions.new({ width: 0, height: 0, x: 0, y: 0 })
22
+ @title_area = Dimensions.new({ width: 0, height: 0, x: 0, y: 0 })
23
+ @series_key_area = Dimensions.new({ width: 0, height: 0, x: 0, y: 0 })
24
+ @theme = theme
25
+ set_from_attributes(attributes) if attributes
26
+ end
27
+
28
+ def calculate
29
+ calculate_width_and_height_of_canvas
30
+ calculate_key_area
31
+ calculate_title_area
32
+ calculate_graph_area
33
+
34
+ self
35
+ end
36
+
37
+ def hpadding
38
+ ((BigDecimal(canvas_width) / 100) * 2).round
39
+ end
40
+
41
+ def vpadding
42
+ ((BigDecimal(canvas_height) / 100) * 2).round
43
+ end
44
+
45
+ def invalid?
46
+ canvas_width > bounds[0] || canvas_height > bounds[1]
47
+ end
48
+
49
+ private
50
+
51
+ def set_from_attributes(attributes)
52
+ @canvas_width = BigDecimal(attributes[:width], 10) rescue 0
53
+ @canvas_height = BigDecimal(attributes[:height], 10) rescue 0
54
+ @num_series = attributes[:series_count] || 1
55
+ @title = attributes[:title]
56
+ end
57
+
58
+ def calculate_width_and_height_of_canvas
59
+ if @canvas_width.zero? && @canvas_height.zero?
60
+ @canvas_width = BigDecimal(bounds[0], 10)
61
+ @canvas_height = BigDecimal(bounds[1], 10)
62
+ elsif !@canvas_width.zero? && @canvas_height.zero?
63
+ @canvas_height = (@canvas_width / bounds_aspect_ratio).round
64
+ elsif !@canvas_height.zero? && @canvas_width.zero?
65
+ @canvas_width = (@canvas_height * bounds_aspect_ratio).round
66
+ end
67
+ end
68
+
69
+ def bounds_aspect_ratio
70
+ BigDecimal(bounds[0], 10) / BigDecimal(bounds[1], 10)
71
+ end
72
+
73
+ def calculate_title_area
74
+ unless @title.nil?
75
+ @title_area[:width] = (canvas_width - @series_key_area[:width] - (2*hpadding))
76
+ @title_area[:x] = hpadding
77
+ @title_area[:height] = @theme.font_sizes.main_title + vpadding
78
+ @title_area[:y] = canvas_height + vpadding
79
+ end
80
+ end
81
+
82
+ def calculate_key_area
83
+ if @num_series > 1
84
+ @series_key_area[:width] = ( (canvas_width / 100) * 25 ).round
85
+ @series_key_area[:x] = (canvas_width - @series_key_area[:width] - hpadding)
86
+ @series_key_area[:y] = canvas_height + vpadding
87
+ @series_key_area[:height] = (canvas_height - vpadding)
88
+ end
89
+ end
90
+
91
+ def calculate_graph_area
92
+ @graph_area[:width] = (canvas_width - @series_key_area[:width] - (2*hpadding))
93
+ @graph_area[:x] = hpadding
94
+
95
+ if !@title_area.renderable?
96
+ @graph_area[:y] = canvas_height + vpadding
97
+ @graph_area[:height] = (canvas_height - vpadding)
98
+ else
99
+ @graph_area[:y] = (@title_area[:y] - @title_area[:height])
100
+ @graph_area[:height] = (canvas_height - vpadding - @title_area[:height])
101
+ end
102
+ end
103
+
104
+ end
105
+
106
+ end
107
+ end
108
+ end