Prawn::Graph - Easy Graphing for Prawn

Gem Version License Code Climate Test Coverage Build Status security Maintained: yes

An extension for the prawn pdf library which adds the ability to draw graphs (or charts if you perfer) in PDF documents.

Because Prawn::Graph only uses the native PDF drawing functions exposed by Prawn, it removes the need to depend on projects like Gruff to generate heavy PNG / JPG images of such graphs and charts and then include those large blobs in your documents. The results may not be as pretty (yet), but the file-size differences are dramatic.

By default, graphs are drawn in monochrome, as that's likely how they will be printed.

This is free and open source software released under ther terms of the MIT Licence.

Its copyright is held by Ryan Stenhouse and the other contributors and it was first released in 2010.

Compatibility

This gem is built assuming a Ruby version of 2.0 or higher. Older Ruby versions may work but are not officially supported. We aim for compatibilty with 1.x and 2.x series of prawn. Any incomaptibilities with prawn versions should be treated as bugs and added to the issue tracker.

We build automatically using Travis CI. Our .travis.yml file targets the same Ruby versions as prawn itself does.

Legacy support and deprecation notices.

To provide backwards-compatibility (of sorts), prawn-graph provides bar_graph (aliased as bar_chart) and line_graph (aliased as line_chart) methods as part of its interface which function the same as their equivalents in version 0.0.4. These methods are, however, deprecated and will be removed when version 1.0.0 is released. Using these methods will result in a warn level message from Ruby.

These legacy methods will still be buggy as they are using the same rendering methods as before. Where possible you should use the new graph (aliased as chart) interface. Unless serious problems are uncovered, bugs reported with legacy rendering will not be considered.

Unlike previous versions of prawn-graph, this version does not at this time include a theme api or the ability to change the colors used to render the graph.

Installation

To use prawn-graph, you can add the following to your Gemfile:

 gem 'prawn-graph', ' ~> 0.9'

Alternatively, you can use Rubygems directly: gem install prawn-graph.

Acknowledgements

With thanks to 株式会社アルム (Allm Inc) for allowing Ryan Stenhouse the time to rebuild this version of prawn-graph. This updated version of prawn-graph was inspired and guided by prawn-svg by Roger Nesbitt.

Prawn Graph was originally sponsored by and built for use at Purchasing Card Consultancy Ltd while Ryan Stenhouse was employed there.

Supported graph / chart types

This version of Prawn::Graph supports the following graph / chart types:

  • Bar Charts
  • Line Charts

Is your favourite chart type not supported yet? Please request it, or if you are feeling particularly adventurous - please add it!

Using prawn-graph

  require 'prawn-graph'

  series = []
  series << Prawn::Graph::Series.new([1,2,3,4,5], title: "Some numbers", type: :bar)

  Prawn::Document.generate('test.pdf') do
    graph series
  end

When called with just a set of data, prawn-graph will do its best to make the graph fit in the available space. For a little more control over how the graphs are rendered in the document you can pass the following options:

Option Data type Description
:at [integer, integer] Specify the location on the page you want the graph to appear.
:width integer Desired width of the graph. Defaults to horizontal space available.
:height integer Desired height of the graph. Defaults to vertical space available.

Advanced example

  require 'prawn-graph'

  series = []
  series << Prawn::Graph::Series.new([5,4,3,2,1,1,2,8,7,5,4,9,2], title: "A label for a series", type: :bar)
  series << Prawn::Graph::Series.new([5,4,3,2,1,1,2,8,7,5,4,9,2].reverse, title: "Another label", type: :bar)
  series << Prawn::Graph::Series.new([1,2,3,4,5,9,6,4,5,6,3,2,9], title: "Yet another label", type: :bar)
  series << Prawn::Graph::Series.new([1,2,3,4,5,9,6,4,5,6,3,2,9].shuffle, title: "One final label", type: :line)

  Prawn::Document.generate('test.pdf') do
    graph series, width: 500, height: 200, title: "A Title for the chart", at: [10,700]
  end

Output

Prawn Graph Example Output

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.