Class Gruff::Base
In: lib/gruff/base.rb
Parent: Object

Methods

Included Modules

Magick Deprecated

Constants

DEBUG = false   Draw extra lines showing where the margins and text centers are
DATA_LABEL_INDEX = 0   Used for navigating the array of data to plot
DATA_VALUES_INDEX = 1
DATA_COLOR_INDEX = 2
TOP_MARGIN = BOTTOM_MARGIN = RIGHT_MARGIN = LEFT_MARGIN = 20.0   Blank space around the edges of the graph
LEGEND_MARGIN = TITLE_MARGIN = LABEL_MARGIN = 10.0   Space around text elements. Mostly used for vertical spacing

Attributes

additional_line_values  [RW]  Experimental
center_labels_over_point  [RW]  Used internally for spacing.

By default, labels are centered over the point they represent.

colors  [RW]  Get or set the list of colors that will be used to draw the bars or lines.
font  [R]  Font used for titles, labels, etc. Works best if you provide the full path to the TTF font file. RMagick must be built with the Freetype libraries for this to work properly.

Tries to find Bitstream Vera (Vera.ttf) in the location specified by ENV[‘MAGICK_FONT_PATH’]. Uses default RMagick font otherwise.

font_color  [RW] 
has_left_labels  [RW]  Used internally for horizontal graph types.
hide_legend  [RW]  Hide various elements
hide_line_markers  [RW]  Hide various elements
hide_line_numbers  [RW]  Hide various elements
hide_title  [RW]  Hide various elements
labels  [RW]  A hash of names for the individual columns, where the key is the array index for the column this label represents.

Not all columns need to be named.

Example: 0 => 2005, 3 => 2006, 5 => 2007, 7 => 2008

legend_box_size  [RW]  Optionally set the size of the colored box by each item in the legend. Default is 20.0

Will be scaled down if graph is smaller than 800px wide.

legend_font_size  [RW]  Optionally set the size of the font. Based on an 800x600px graph. Default is 20.

Will be scaled down if graph is smaller than 800px wide.

marker_color  [RW]  The color of the auxiliary lines
marker_count  [RW]  The number of horizontal lines shown for reference
marker_font_size  [RW]  The font size of the labels around the graph
maximum_value  [RW]  You can manually set a maximum value, such as a percentage-based graph that always goes to 100.

If you use this, you must set it after you have given all your data to the graph object.

minimum_value  [RW]  You can manually set a minimum value instead of having the values guessed for you.

Set it after you have given all your data to the graph object.

no_data_message  [RW]  Message shown when there is no data. Fits up to 20 characters. Defaults to "No Data."
sort  [RW]  Set to false if you don‘t want the data to be sorted with largest avg values at the back.
stacked  [RW]  Experimental
title  [RW]  The large title of the graph displayed at the top
title_font_size  [RW]  The font size of the large title at the top of the graph
x_axis_label  [RW]  A label for the bottom of the graph
y_axis_increment  [RW]  Manually set increment of the horizontal marking lines
y_axis_label  [RW]  A label for the left side of the graph

Public Class methods

If one numerical argument is given, the graph is drawn at 4/3 ratio according to the given width (800 results in 800x600, 400 gives 400x300, etc.).

Or, send a geometry string for other ratios (‘800x400’, ‘400x225’).

Looks for Bitstream Vera as the default font. Expects an environment var of MAGICK_FONT_PATH to be set. (Uses RMagick‘s default font otherwise.)

Public Instance methods

Add a color to the list of available colors for lines.

Example:

 add_color('#c0e9d3')

Parameters are an array where the first element is the name of the dataset and the value is an array of values to plot.

Can be called multiple times with different datasets for a multi-valued graph.

If the color argument is nil, the next color from the default theme will be used.

NOTE: If you want to use a preset theme, you must set it before calling data().

Example:

 data("Bart S.", [95, 45, 78, 89, 88, 76], '#ffcc00')

Set instance variables for this object.

Subclasses can override this, call super, then set values separately.

This makes it possible to set defaults in a subclass but still allow developers to change this values in their program.

Replace the entire color list with a new array of colors. You need to have one more color than the number of datasets you intend to draw. Also aliased as the colors= setter method.

Example:

 replace_colors('#cc99cc', '#d9e043', '#34d8a2')

You can set a theme manually. Assign a hash to this method before you send your data.

 graph.theme = {
   :colors => %w(orange purple green white red),
   :marker_color => 'blue',
   :background_colors => %w(black grey)
 }

:background_image => ‘squirrel.png’ is also possible.

(Or hopefully something better looking than that.)

A color scheme plucked from the colors on the popular usability blog.

A color scheme similar to the popular presentation software.

A color scheme similar to that used on the popular podcast site.

A color scheme from the colors used on the 2005 Rails keynote presentation at RubyConf.

Return the graph as a rendered binary blob.

Writes the graph to a file. Defaults to ‘graph.png‘

Example: write(‘graphs/my_pretty_graph.png’)

Protected Instance methods

Overridden by subclasses to do the actual plotting of the graph.

Subclasses should start by calling super() for this method.

Draw the optional labels for the x axis and y axis.

Draws column labels below graph, centered over x_offset

TODO Allow WestGravity as an option

Draws a legend with the names of the datasets matched to the colors used to draw them.

Draws horizontal background lines and labels

Used by StackedBar and child classes.

May need to be moved to the StackedBar class.

Overridden by subclasses such as stacked bar.

Overridden by subclasses that need it.

Overridden by subclasses that need it.

Make copy of data with values scaled between 0-100

Finds the best background to render based on the provided theme options.

Creates a @base_image to draw on.

Use with a theme definition method to draw a gradiated background.

Use with a theme to use an image (800x600 original) background.

Make a new image at the current size with a solid color.

Use with a theme to make a transparent background

Return a comparable fontsize for the current graph.

Calculates size of drawable area and draws the decorations.

  • line markers
  • legend
  • title

Calculates size of drawable area, general font dimensions, etc.

Sort with largest overall summed value at front of array so it shows up correctly in the drawn graph.

[Validate]