Class: Gruff::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/gruff/base.rb

Constant Summary collapse

LEGEND_MARGIN =

Space around text elements. Mostly used for vertical spacing.

TITLE_MARGIN = 20.0
LABEL_MARGIN =
15.0
DEFAULT_MARGIN =
20.0
DEFAULT_TARGET_WIDTH =
800.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target_width = DEFAULT_TARGET_WIDTH) ⇒ Base

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

Parameters:

  • target_width (Numeric, String) (defaults to: DEFAULT_TARGET_WIDTH)

    The graph image width.



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/gruff/base.rb', line 169

def initialize(target_width = DEFAULT_TARGET_WIDTH)
  if target_width.is_a?(String)
    @columns, @rows = target_width.split('x').map(&:to_f)
  else
    @columns = target_width.to_f
    @rows = target_width.to_f * 0.75
  end
  @columns.freeze
  @rows.freeze

  initialize_graph_scale
  initialize_ivars
  initialize_store

  self.theme = Themes::KEYNOTE
end

Instance Attribute Details

#bold_title=(value) ⇒ Object (writeonly)

Specifies whether to draw the title bolded or not. Default is true.



98
99
100
# File 'lib/gruff/base.rb', line 98

def bold_title=(value)
  @bold_title = value
end

#bottom_margin=(value) ⇒ Object (writeonly)

Blank space below the graph. Default is 20.



32
33
34
# File 'lib/gruff/base.rb', line 32

def bottom_margin=(value)
  @bottom_margin = value
end

#center_labels_over_point=(value) ⇒ Object (writeonly)

Used internally for spacing.

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



58
59
60
# File 'lib/gruff/base.rb', line 58

def center_labels_over_point=(value)
  @center_labels_over_point = value
end

#colorsObject

Get or set the list of colors that will be used to draw the bars or lines.



89
90
91
# File 'lib/gruff/base.rb', line 89

def colors
  @colors
end

#font_color=(value) ⇒ Object (writeonly)

Specifies the text color.



101
102
103
# File 'lib/gruff/base.rb', line 101

def font_color=(value)
  @font_color = value
end

#has_left_labels=(value) ⇒ Object (writeonly)

Used internally for horizontal graph types. Default is false.



61
62
63
# File 'lib/gruff/base.rb', line 61

def has_left_labels=(value)
  @has_left_labels = value
end

#hide_legend=(value) ⇒ Object (writeonly)

Prevent drawing of the legend. Default is false.



107
108
109
# File 'lib/gruff/base.rb', line 107

def hide_legend=(value)
  @hide_legend = value
end

#hide_line_markers=(value) ⇒ Object (writeonly)

Prevent drawing of line markers. Default is false.



104
105
106
# File 'lib/gruff/base.rb', line 104

def hide_line_markers=(value)
  @hide_line_markers = value
end

#hide_line_numbers=(value) ⇒ Object (writeonly)

Prevent drawing of line numbers. Default is false.



113
114
115
# File 'lib/gruff/base.rb', line 113

def hide_line_numbers=(value)
  @hide_line_numbers = value
end

#hide_title=(value) ⇒ Object (writeonly)

Prevent drawing of the title. Default is false.



110
111
112
# File 'lib/gruff/base.rb', line 110

def hide_title=(value)
  @hide_title = value
end

#label_max_size=(value) ⇒ Object (writeonly)

Truncates labels if longer than max specified.



79
80
81
# File 'lib/gruff/base.rb', line 79

def label_max_size=(value)
  @label_max_size = value
end

#label_stagger_height=(value) ⇒ Object (writeonly)

Height of staggering between labels (Bar graph only).



76
77
78
# File 'lib/gruff/base.rb', line 76

def label_stagger_height=(value)
  @label_stagger_height = value
end

#label_truncation_style=(value) ⇒ Object (writeonly)

How truncated labels visually appear if they exceed #label_max_size=.

  • :absolute - does not show trailing dots to indicate truncation. This is the default.

  • :trailing_dots - shows trailing dots to indicate truncation (note that #label_max_size= must be greater than 3).



86
87
88
# File 'lib/gruff/base.rb', line 86

def label_truncation_style=(value)
  @label_truncation_style = value
end

#labels=(value) ⇒ Object (writeonly)

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.

Examples:

{ 0 => 2005, 3 => 2006, 5 => 2007, 7 => 2008 }


53
54
55
# File 'lib/gruff/base.rb', line 53

def labels=(value)
  @labels = value
end

#left_margin=(value) ⇒ Object (writeonly)

Blank space to the left of the graph. Default is 20.



38
39
40
# File 'lib/gruff/base.rb', line 38

def left_margin=(value)
  @left_margin = value
end

#legend_at_bottom=(value) ⇒ Object (writeonly)

Display the legend under the graph. Default is false.



129
130
131
# File 'lib/gruff/base.rb', line 129

def legend_at_bottom=(value)
  @legend_at_bottom = value
end

#legend_box_size=(value) ⇒ Object (writeonly)

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.



155
156
157
# File 'lib/gruff/base.rb', line 155

def legend_box_size=(value)
  @legend_box_size = value
end

#legend_font_size=(value) ⇒ Object (writeonly)

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

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



126
127
128
# File 'lib/gruff/base.rb', line 126

def legend_font_size=(value)
  @legend_font_size = value
end

#legend_margin=(value) ⇒ Object (writeonly)

Blank space below the legend. Default is 20.



44
45
46
# File 'lib/gruff/base.rb', line 44

def legend_margin=(value)
  @legend_margin = value
end

#marker_color=(value) ⇒ Object (writeonly)

Set the color of the auxiliary lines.



135
136
137
# File 'lib/gruff/base.rb', line 135

def marker_color=(value)
  @marker_color = value
end

#marker_count=(value) ⇒ Object (writeonly)

Set the number of horizontal lines shown for reference.



141
142
143
# File 'lib/gruff/base.rb', line 141

def marker_count=(value)
  @marker_count = value
end

#marker_font_size=(value) ⇒ Object (writeonly)

The font size of the labels around the graph. Default is 21.



132
133
134
# File 'lib/gruff/base.rb', line 132

def marker_font_size=(value)
  @marker_font_size = value
end

#marker_shadow_color=(value) ⇒ Object (writeonly)

Set the shadow color of the auxiliary lines.



138
139
140
# File 'lib/gruff/base.rb', line 138

def marker_shadow_color=(value)
  @marker_shadow_color = value
end

#maximum_valueObject

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.



405
406
407
# File 'lib/gruff/base.rb', line 405

def maximum_value
  @maximum_value || store.max
end

#minimum_valueObject

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.



395
396
397
# File 'lib/gruff/base.rb', line 395

def minimum_value
  @minimum_value || store.min
end

#no_data_message=(value) ⇒ Object (writeonly)

Set a message shown when there is no data. Fits up to 20 characters. Defaults to “No Data.”.



117
118
119
# File 'lib/gruff/base.rb', line 117

def no_data_message=(value)
  @no_data_message = value
end

#right_margin=(value) ⇒ Object (writeonly)

Blank space to the right of the graph. Default is 20.



35
36
37
# File 'lib/gruff/base.rb', line 35

def right_margin=(value)
  @right_margin = value
end

#sort=(value) ⇒ Object (writeonly)

Set to true if you want the data sets sorted with largest avg values drawn first. Default is false.



145
146
147
# File 'lib/gruff/base.rb', line 145

def sort=(value)
  @sort = value
end

#sorted_drawing=(value) ⇒ Object (writeonly)

Set to true if you want the data sets drawn with largest avg values drawn first. This does not affect the legend. Default is false.



149
150
151
# File 'lib/gruff/base.rb', line 149

def sorted_drawing=(value)
  @sorted_drawing = value
end

#title=(value) ⇒ Object (writeonly)

Set the large title of the graph displayed at the top.



92
93
94
# File 'lib/gruff/base.rb', line 92

def title=(value)
  @title = value
end

#title_font=(value) ⇒ Object (writeonly)

Same as #font= but for the title.



95
96
97
# File 'lib/gruff/base.rb', line 95

def title_font=(value)
  @title_font = value
end

#title_font_size=(value) ⇒ Object (writeonly)

Set the font size of the large title at the top of the graph. Default is 36.



120
121
122
# File 'lib/gruff/base.rb', line 120

def title_font_size=(value)
  @title_font_size = value
end

#title_margin=(value) ⇒ Object (writeonly)

Blank space below the title. Default is 20.



41
42
43
# File 'lib/gruff/base.rb', line 41

def title_margin=(value)
  @title_margin = value
end

#top_margin=(value) ⇒ Object (writeonly)

Blank space above the graph. Default is 20.



29
30
31
# File 'lib/gruff/base.rb', line 29

def top_margin=(value)
  @top_margin = value
end

#use_data_label=(value) ⇒ Object (writeonly)

With Side Bars use the data label for the marker value to the left of the bar. Default is false.



159
160
161
# File 'lib/gruff/base.rb', line 159

def use_data_label=(value)
  @use_data_label = value
end

#x_axis_increment=(value) ⇒ Object (writeonly)

Set increment of the vertical marking lines.



70
71
72
# File 'lib/gruff/base.rb', line 70

def x_axis_increment=(value)
  @x_axis_increment = value
end

#x_axis_label=(value) ⇒ Object (writeonly)

Set a label for the bottom of the graph.



64
65
66
# File 'lib/gruff/base.rb', line 64

def x_axis_label=(value)
  @x_axis_label = value
end

#y_axis_increment=(value) ⇒ Object (writeonly)

Set increment of the horizontal marking lines.



73
74
75
# File 'lib/gruff/base.rb', line 73

def y_axis_increment=(value)
  @y_axis_increment = value
end

#y_axis_label=(value) ⇒ Object (writeonly)

Set a label for the left side of the graph.



67
68
69
# File 'lib/gruff/base.rb', line 67

def y_axis_label=(value)
  @y_axis_label = value
end

Instance Method Details

#add_color(colorname) ⇒ Object

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

Examples:

add_color('#c0e9d3')

Parameters:

  • colorname (String)

    The color.



269
270
271
# File 'lib/gruff/base.rb', line 269

def add_color(colorname)
  @colors << colorname
end

#data(name, data_points = [], color = nil) ⇒ Object

Note:

If you want to use a preset theme, you must set it before calling #data.

Input the data in the graph.

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.

Examples:

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

Parameters:

  • name (String, Symbol)

    The name of the dataset.

  • data_points (Array) (defaults to: [])

    The array of dataset.

  • color (String) (defaults to: nil)

    The color for drawing graph of dataset.



387
388
389
# File 'lib/gruff/base.rb', line 387

def data(name, data_points = [], color = nil)
  store.add(name, data_points, color)
end

#font=(font_path) ⇒ Object

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

Parameters:

  • font_path (String)

    The path to font.



258
259
260
261
# File 'lib/gruff/base.rb', line 258

def font=(font_path)
  @font = font_path
  Gruff::Renderer.font = @font
end

#margins=(margin) ⇒ Object

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

Parameters:

  • margin (Numeric)

    The margin size.



250
251
252
# File 'lib/gruff/base.rb', line 250

def margins=(margin)
  @top_margin = @left_margin = @right_margin = @bottom_margin = margin
end

#replace_colors(color_list = []) ⇒ Object

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, it 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.

Examples:

replace_colors ['#cc99cc', '#d9e043', '#34d8a2']

Parameters:

  • color_list (Array) (defaults to: [])

    The array of colors.



288
289
290
# File 'lib/gruff/base.rb', line 288

def replace_colors(color_list = [])
  @colors = color_list
end

#theme=(options) ⇒ Object

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: ['black', 'grey'],
  background_direction: :top_bottom
}

background_image: ‘squirrel.png’ is also possible.

background_direction accepts one of following parameters.

  • :top_bottom

  • :bottom_top

  • :left_right

  • :right_left

  • :topleft_bottomright

  • :topright_bottomleft

(Or hopefully something better looking than that.)

Parameters:

  • options (Hash)

    The optional setting for theme



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/gruff/base.rb', line 316

def theme=(options)
  reset_themes

  defaults = {
    colors: %w[black white],
    marker_color: 'white',
    marker_shadow_color: nil,
    font_color: 'black',
    background_colors: nil,
    background_image: nil
  }
  @theme_options = defaults.merge options

  @colors = @theme_options[:colors]
  @marker_color = @theme_options[:marker_color]
  @marker_shadow_color = @theme_options[:marker_shadow_color]
  @font_color = @theme_options[:font_color] || @marker_color

  Gruff::Renderer.setup(@columns, @rows, @font, @scale, @theme_options)
end

#theme_37signalsObject

Apply 37signals theme.



343
344
345
# File 'lib/gruff/base.rb', line 343

def theme_37signals
  self.theme = Themes::THIRTYSEVEN_SIGNALS
end

#theme_greyscaleObject

Apply greyscale theme.



363
364
365
# File 'lib/gruff/base.rb', line 363

def theme_greyscale
  self.theme = Themes::GREYSCALE
end

#theme_keynoteObject

Apply Apple’s keynote theme.



338
339
340
# File 'lib/gruff/base.rb', line 338

def theme_keynote
  self.theme = Themes::KEYNOTE
end

#theme_odeoObject

Apply Odeo theme.



353
354
355
# File 'lib/gruff/base.rb', line 353

def theme_odeo
  self.theme = Themes::ODEO
end

#theme_pastelObject

Apply pastel theme.



358
359
360
# File 'lib/gruff/base.rb', line 358

def theme_pastel
  self.theme = Themes::PASTEL
end

#theme_rails_keynoteObject

Apply Rails theme.



348
349
350
# File 'lib/gruff/base.rb', line 348

def theme_rails_keynote
  self.theme = Themes::RAILS_KEYNOTE
end

#to_blob(image_format = 'PNG') ⇒ Object

Return the graph as a rendered binary blob.

Parameters:

  • image_format (String) (defaults to: 'PNG')

    The image format of binary blob.



424
425
426
427
# File 'lib/gruff/base.rb', line 424

def to_blob(image_format = 'PNG')
  draw
  Gruff::Renderer.to_blob(image_format)
end

#write(file_name = 'graph.png') ⇒ Object

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

Examples:

write('graphs/my_pretty_graph.png')

Parameters:

  • file_name (String) (defaults to: 'graph.png')

    The file name of output image.



416
417
418
419
# File 'lib/gruff/base.rb', line 416

def write(file_name = 'graph.png')
  draw
  Gruff::Renderer.write(file_name)
end