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

Methods

Included Modules

Magick Deprecated

Classes and Modules

Module Gruff::Base::StackedMixin

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
LEGEND_MARGIN = TITLE_MARGIN = 20.0   Space around text elements. Mostly used for vertical spacing
LABEL_MARGIN = 10.0
DEFAULT_MARGIN = 20.0
DEFAULT_TARGET_WIDTH = 800
THOUSAND_SEPARATOR = ','

Attributes

additional_line_values  [RW]  Experimental
bottom_margin  [RW]  Blank space below the graph
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.

The font= method below fulfills the role of the writer, so we only need a reader here.

font_color  [RW] 
has_left_labels  [RW]  Used internally for horizontal graph types.
hide_legend  [RW]  Prevent drawing of the legend
hide_line_markers  [RW]  Prevent drawing of line markers
hide_line_numbers  [RW]  Prevent drawing of line numbers
hide_title  [RW]  Prevent drawing of the title
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

left_margin  [RW]  Blank space to the left of the graph
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.

legend_margin  [RW]  Blank space below the legend
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."
right_margin  [RW]  Blank space to the right of the graph
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
title_margin  [RW]  Blank space below the title
top_margin  [RW]  Blank space above 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')

Sets the font for graph text to the font at font_path.

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.

Sets the top, bottom, left and right margins to margin.

Replace the entire color list with a new array of colors. Also aliased as the colors= setter method.

If you specify fewer colors than the number of datasets you intend to draw, ‘increment_color’ will cycle through the array, reusing colors as needed.

Note that (as with the ‘theme’ method), you should set up your color list before you send your data (via the ‘data’ method). Calls to the ‘data’ method made prior to this call will use whatever color scheme was in place at the time data was called.

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 greyscale theme

A color scheme similar to the popular presentation software.

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

A pastel theme

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

Return a calculation of center

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

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

Draws horizontal background lines and labels

Shows an error message because you have no data.

Draws a title on the graph.

Used by StackedBar and child classes.

May need to be moved to the StackedBar class.

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

Resets everything to defaults (except data).

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.

Return the sum of values in an array.

Duplicated to not conflict with active_support in Rails.

[Validate]