Class | Lilygraph |
In: |
lib/lilygraph.rb
|
Parent: | Object |
This is the main class 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.
DEFAULT_OPTIONS | = | { :height => '100%', :width => '100%', :indent => 2, :padding => 14, :bar_text => true, :viewbox => { :width => 800, :height => 600 | Default options for the initializer |
colors | [RW] |
This allows you to set colors for the bars.
If you just want a single color: graph.colors='#0000aa' If you want to make each bar (or bar group) different colors: graph.colors=['#aa0000','#00aa00','#0000aa'] If you want every bar group to be the same, but each bar in the groups to have a different color graph.colors=[['#aa0000','#00aa00','#0000aa']] If you want to set every bar group color: graph.colors=[['#aa0000','#00aa00','#0000aa'],['#bb0000','#00bb00','#0000bb']] Last but not least you can set the color value to any object that responds to call (like a Proc). The proc takes four arguments. data_index: The index of the current bar (or group) number_index: The index of the current bar INSIDE of the current bar group (always 0 if you don‘t have grouped bars) data_size: total number of bar or groups. number_size: total number of bars in the current group (always 1 if you don‘t have bar groups) The default proc looks like: graph.colors=Proc.new do |data_index, number_index, data_size, number_size| Color::HSL.from_fraction(Float(data_index) / Float(data_size), 1.0, 0.4 + (Float(number_index) / Float(number_size) * 0.4)).to_rgb.html end |
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. |
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)
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.