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 =
10.0
DEFAULT_MARGIN =
20.0
DEFAULT_TARGET_WIDTH =
800.0
THOUSAND_SEPARATOR =
','

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



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/gruff/base.rb', line 173

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

  initialize_ivars

  self.theme = Themes::KEYNOTE
end

Instance Attribute Details

#additional_line_valuesObject

Experimental



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

def additional_line_values
  @additional_line_values
end

#bold_titleObject

Specifies whether to draw the title bolded or not.



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

def bold_title
  @bold_title
end

#bottom_marginObject

Blank space below the graph.



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

def bottom_margin
  @bottom_margin
end

#center_labels_over_pointObject

Used internally for spacing.

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



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

def center_labels_over_point
  @center_labels_over_point
end

#colorsObject

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



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

def colors
  @colors
end

#fontObject

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.



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

def font
  @font
end

#font_colorObject

Returns the value of attribute font_color.



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

def font_color
  @font_color
end

#has_left_labelsObject

Used internally for horizontal graph types.



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

def has_left_labels
  @has_left_labels
end

#hide_legendObject

Prevent drawing of the legend.



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

def hide_legend
  @hide_legend
end

#hide_line_markersObject

Prevent drawing of line markers.



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

def hide_line_markers
  @hide_line_markers
end

#hide_line_numbersObject

Prevent drawing of line numbers.



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

def hide_line_numbers
  @hide_line_numbers
end

#hide_titleObject

Prevent drawing of the title.



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

def hide_title
  @hide_title
end

#label_max_sizeObject

Truncates labels if longer than max specified.



81
82
83
# File 'lib/gruff/base.rb', line 81

def label_max_size
  @label_max_size
end

#label_stagger_heightObject

Height of staggering between labels (Bar graph only).



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

def label_stagger_height
  @label_stagger_height
end

#label_truncation_styleObject

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



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

def label_truncation_style
  @label_truncation_style
end

#labelsObject

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 }


55
56
57
# File 'lib/gruff/base.rb', line 55

def labels
  @labels
end

#left_marginObject

Blank space to the left of the graph.



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

def left_margin
  @left_margin
end

#legend_at_bottomObject

Display the legend under the graph.



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

def legend_at_bottom
  @legend_at_bottom
end

#legend_box_sizeObject

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.



162
163
164
# File 'lib/gruff/base.rb', line 162

def legend_box_size
  @legend_box_size
end

#legend_font_sizeObject

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.



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

def legend_font_size
  @legend_font_size
end

#legend_marginObject

Blank space below the legend.



46
47
48
# File 'lib/gruff/base.rb', line 46

def legend_margin
  @legend_margin
end

#marker_colorObject

The color of the auxiliary lines.



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

def marker_color
  @marker_color
end

#marker_countObject

The number of horizontal lines shown for reference.



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

def marker_count
  @marker_count
end

#marker_font_sizeObject

The font size of the labels around the graph.



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

def marker_font_size
  @marker_font_size
end

#marker_shadow_colorObject

Returns the value of attribute marker_shadow_color.



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

def marker_shadow_color
  @marker_shadow_color
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.



378
379
380
# File 'lib/gruff/base.rb', line 378

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.



368
369
370
# File 'lib/gruff/base.rb', line 368

def minimum_value
  @minimum_value || store.min
end

#no_data_messageObject

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



123
124
125
# File 'lib/gruff/base.rb', line 123

def no_data_message
  @no_data_message
end

#right_marginObject

Blank space to the right of the graph.



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

def right_margin
  @right_margin
end

#sortObject

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



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

def sort
  @sort
end

#sorted_drawingObject

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



153
154
155
# File 'lib/gruff/base.rb', line 153

def sorted_drawing
  @sorted_drawing
end

#titleObject

The large title of the graph displayed at the top.



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

def title
  @title
end

#title_fontObject

Same as font but for the title.



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

def title_font
  @title_font
end

#title_font_sizeObject

The font size of the large title at the top of the graph.



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

def title_font_size
  @title_font_size
end

#title_marginObject

Blank space below the title.



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

def title_margin
  @title_margin
end

#top_marginObject

Blank space above the graph.



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

def top_margin
  @top_margin
end

#use_data_labelObject

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



166
167
168
# File 'lib/gruff/base.rb', line 166

def use_data_label
  @use_data_label
end

#x_axis_incrementObject

Manually set increment of the vertical marking lines.



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

def x_axis_increment
  @x_axis_increment
end

#x_axis_labelObject

A label for the bottom of the graph.



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

def x_axis_label
  @x_axis_label
end

#y_axis_incrementObject

Manually set increment of the horizontal marking lines.



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

def y_axis_increment
  @y_axis_increment
end

#y_axis_labelObject

A label for the left side of the graph.



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

def y_axis_label
  @y_axis_label
end

Instance Method Details

#add_color(colorname) ⇒ Object

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

Examples:

add_color('#c0e9d3')


265
266
267
# File 'lib/gruff/base.rb', line 265

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.

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


360
361
362
# File 'lib/gruff/base.rb', line 360

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

#margins=(margin) ⇒ Object

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



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

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


282
283
284
# File 'lib/gruff/base.rb', line 282

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', :top_bottom]
}

background_image: ‘squirrel.png’ is also possible.

(Or hopefully something better looking than that.)



299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/gruff/base.rb', line 299

def theme=(options)
  reset_themes

  defaults = {
    colors: %w[black white],
    additional_line_colors: [],
    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
  @additional_line_colors = @theme_options[:additional_line_colors]

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

#theme_37signalsObject



326
327
328
# File 'lib/gruff/base.rb', line 326

def theme_37signals
  self.theme = Themes::THIRTYSEVEN_SIGNALS
end

#theme_greyscaleObject



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

def theme_greyscale
  self.theme = Themes::GREYSCALE
end

#theme_keynoteObject



322
323
324
# File 'lib/gruff/base.rb', line 322

def theme_keynote
  self.theme = Themes::KEYNOTE
end

#theme_odeoObject



334
335
336
# File 'lib/gruff/base.rb', line 334

def theme_odeo
  self.theme = Themes::ODEO
end

#theme_pastelObject



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

def theme_pastel
  self.theme = Themes::PASTEL
end

#theme_rails_keynoteObject



330
331
332
# File 'lib/gruff/base.rb', line 330

def theme_rails_keynote
  self.theme = Themes::RAILS_KEYNOTE
end

#to_blob(file_format = 'PNG') ⇒ Object

Return the graph as a rendered binary blob.



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

def to_blob(file_format = 'PNG')
  draw
  Gruff::Renderer.to_blob(file_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')


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

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