Squid
Open Source Ruby Library to Plot Charts in PDF
Free Ruby API that gives software developers the ability to draw charts in PDF files with just a couple of lines of Ruby code. It supports different kinds of chart types, such as line, point, and more.
What is One Squid Library?
Squid is an open source Ruby library that makes developers job easy by helping them to create their own application to draw charts in PDF file with just a couple of lines of Ruby code. The library is very easy to use and provides all the basic functionality for creating charts. It enables developers to draw plot view or unique of a websites, adding names of the series, inserting names of the categories, adjusting border, setting height, editing labels and much more. The library supports different kind of chart types, such as line, point and more.
The Squid library allows developers to expand Prawn providing method to easily draw graphs in PDF files. Prawn is also a very useful Ruby library that supports generating PDF documents and managing it but lacking high-level components for drawing graphs. Squid just add a single method to Prawn library Document class which can be used by developers to plot graph inside a PDF page. You can add data by plotting as a hash, with each key/value representing a series. The library is open source and is available as MIT license for public use.
Getting Started with Squid
You easily download the Squid library and use it. The recommended way for the installation is by using Rubygem. Please use the following command.
Install Squid via RubyGems
gem install squid
Create Charts in PDF using Ruby
The open source Ruby library Squid enables software developers to create charts inside a PDF document with a couple of lines of Ruby code. The library makes it very easy to generate graphics in a PDF by using the <code>chart</code> method. You can easily provide a hash containing the data of the series and plot it using a one-liner code. You can easily identify the format of the axis values and add labels for the chart.
How to Generate PDF File With a Chart via Ruby API?
require 'squid'
Prawn::Document.generate 'web traffic.pdf' do
chart views: {2013 => 182, 2014 => 46, 2015 => 134}
end
Miscellaneous Graphic Plotting Feature
The Plotter class wraps a Prawn::Document object to provide a new useful method for o plotting graph elements. The Squid library has included a very useful set of features for handling graphics and images inside Ruby applications. The library supports features like adding a border around charts, plotting a baseline at the bottom of the graph, drawing a bounding box of the given height, set the format of the axis values, customize the height of charts, write value labels on the chart, adds a legend in the top-right corner, adding background color, draws a horizontal line and many more.
How to Create Basic Chart with Squid via Ruby Code?
# By default, chart
plots every category on the baseline.
filename = File.basename(__FILE__).gsub('.rb', '.pdf')
Prawn::ManualBuilder::Example.generate(filename) do
data = {views: {'Jan 1' => 12, 'Jan 2' => 13, 'Jan 3' => 21, 'Jan 4' => 42,
'Jan 5' => 32, 'Jan 6' => 45, 'Jan 7' => 62, 'Jan 8' => 22, 'Jan 9' => 31,
'Jan 10' => 11, 'Jan 11' => 40, 'Jan 12' => 6, 'Jan 13' => 9}}
chart data, every: 3
end
How to Create Line Chart via Ruby API?
# You can use the :type
option to plot a line chart instead.
filename = File.basename(__FILE__).gsub('.rb', '.pdf')
Prawn::ManualBuilder::Example.generate(filename) do
data = {views: {'Jan 1' => 12, 'Jan 2' => 13, 'Jan 3' => 21, 'Jan 4' => 42,
'Jan 5' => 32, 'Jan 6' => 45, 'Jan 7' => 62, 'Jan 8' => 22, 'Jan 9' => 31,
'Jan 10' => 11, 'Jan 11' => 40, 'Jan 12' => 6, 'Jan 13' => 9}}
chart data, every: 3
end