Class: Gruff::Base

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

### Methods

add_color data draw draw_axis_labels draw_label draw_legend draw_line_markers draw_no_data draw_title font= get_maximum_by_stack initialize_ivars margins= new normalize render_background render_gradiated_background render_image_background render_solid_background render_transparent_background replace_colors reset_themes scale_fontsize setup_drawing setup_graph_measurements sort_norm_data theme= theme_37signals theme_greyscale theme_keynote theme_odeo theme_pastel theme_rails_keynote to_blob write

### 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 = LABEL_MARGIN = 10.0 |  | Space around text elements. Mostly used for vertical spacing
DEFAULT_TARGET_WIDTH | = | 800

### 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.
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
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

new(target_width=DEFAULT_TARGET_WIDTH)

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_color(colorname)

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

Example:

     add_color('#c0e9d3')

data(name, data_points=[], color=nil)

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')

font=(font_path)

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

initialize_ivars()

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.

margins=(margin)

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

replace_colors(color_list=[])

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']

theme=(options)

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.)

theme_37signals()

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

theme_greyscale()

A greyscale theme

theme_keynote()

A color scheme similar to the popular presentation software.

theme_odeo()

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

theme_pastel()

A pastel theme

theme_rails_keynote()

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

to_blob(fileformat='PNG')

Return the graph as a rendered binary blob.

write(filename="graph.png")

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

Example:

      write('graphs/my_pretty_graph.png')

### Protected Instance methods

draw()

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

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

draw_axis_labels()

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

draw_label(x_offset, index)

Draws column labels below graph, centered over x_offset

draw_legend()

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

draw_line_markers()

Draws horizontal background lines and labels

draw_no_data()

Shows an error message because you have no data.

draw_title()

Draws a title on the graph.

get_maximum_by_stack()

Used by StackedBar and child classes.

May need to be moved to the StackedBar class.

normalize(force=false)

Make copy of data with values scaled between 0-100

render_background()

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

Creates a @base_image to draw on.

render_gradiated_background(top_color, bottom_color)

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

render_image_background(image_path)

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

render_solid_background(color)

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

render_transparent_background()

Use with a theme to make a transparent background

reset_themes()

Resets everything to defaults (except data).

scale_fontsize(value)

Return a comparable fontsize for the current graph.

setup_drawing()

Calculates size of drawable area and draws the decorations.

- line markers
- legend
- title

setup_graph_measurements()

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

sort_norm_data()

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

[Validate]