Class Lilygraph
In: lib/lilygraph.rb
Parent: Object

This is the main graph to use if you want to create a graph!

  graph = Lilygraph.new(:title => "My Awesome Graph")
  graph.data = [1,2,3]
  graph.labels = ['One','Two','Three']
  graph.render

This class outputs svg as a string once you call render.

Methods

new   render   update_options  

Constants

DEFAULT_OPTIONS = { :height => '100%', :width => '100%', :indent => 2, :padding => 14, :bar_text => true, :viewbox => { :width => 800, :height => 600   Default options for the initializer

Attributes

data  [RW]  This is the data for the graph. It should be an array where every item is either a number or an array of numbers.

For a simple bar graph:

  graph.data=[1,2,3]

For a grouped bar graph:

  graph.data=[[1,10],[2,20],[3,30]]
labels  [RW]  An array of labels to use on the y axis. Make sure you have the right number of labels. The size of this array should = the size of the data array.

Public Class methods

Returns a new graph creator with some default options specified via a hash:

height:String to use as height parameter on the svg tag. Default is ‘100%’.
width:String to use as width parameter on the svg tag. Default is ‘100%’.
indent:Indent option to the XmlMarkup object. Defaults to 2.
padding:Number of svg units in between two bars. Defaults to 14.
bar_text:(Boolean) Whether or not to include the text labels above every bar. Defaults to true.
viewbox:Hash of :height and :width keys to use for the viewbox parameter of the svg tag. Defaults to {:height => 600, :width => 800}.
margin:Hash of margins to use for graph (in svg units). Defaults to {:top => 50, :left => 50, :right => 50, :bottom => 100}.

For example, this creates a graph with a title and different indent setting:

  graph = Lilygraph.new(:title => 'Testing a title', :indent => 4)

Public Instance methods

This returns a string of the graph as an svg. You can pass in a block in order to add your own items to the graph. Your block is passed the XmlMarkup object to use as well as the options hash in case you need to use some of that data.

  graph.render do |xml|
    xml.text "Hello", :x => 5, :y => 25
  end

Updates the graph options with items from the passed in hash. Please refer to new for a description of available options.

[Validate]