Class: Gruff::Base
- Inherits:
-
Object
- Object
- Gruff::Base
- Defined in:
- lib/gruff/base.rb
Direct Known Subclasses
Area, Bar, Bezier, Bullet, Dot, Line, Net, PhotoBar, Pie, Scatter, Scene, SideBar, Spider, StackedArea, StackedBar
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
Instance Attribute Summary collapse
-
#bold_title ⇒ Object
Specifies whether to draw the title bolded or not.
-
#bottom_margin ⇒ Object
Blank space below the graph.
-
#center_labels_over_point ⇒ Object
Used internally for spacing.
-
#colors ⇒ Object
Get or set the list of colors that will be used to draw the bars or lines.
-
#font ⇒ Object
Font used for titles, labels, etc.
-
#font_color ⇒ Object
Specifies the text color.
-
#has_left_labels ⇒ Object
Used internally for horizontal graph types.
-
#hide_legend ⇒ Object
Prevent drawing of the legend.
-
#hide_line_markers ⇒ Object
Prevent drawing of line markers.
-
#hide_line_numbers ⇒ Object
Prevent drawing of line numbers.
-
#hide_title ⇒ Object
Prevent drawing of the title.
-
#label_max_size ⇒ Object
Truncates labels if longer than max specified.
-
#label_stagger_height ⇒ Object
Height of staggering between labels (Bar graph only).
-
#label_truncation_style ⇒ Object
How truncated labels visually appear if they exceed #label_max_size.
-
#labels ⇒ Object
A hash of names for the individual columns, where the key is the array index for the column this label represents.
-
#left_margin ⇒ Object
Blank space to the left of the graph.
-
#legend_at_bottom ⇒ Object
Display the legend under the graph.
-
#legend_box_size ⇒ Object
Optionally set the size of the colored box by each item in the legend.
-
#legend_font_size ⇒ Object
Optionally set the size of the font.
-
#legend_margin ⇒ Object
Blank space below the legend.
-
#marker_color ⇒ Object
Set the color of the auxiliary lines.
-
#marker_count ⇒ Object
Set the number of horizontal lines shown for reference.
-
#marker_font_size ⇒ Object
The font size of the labels around the graph.
-
#marker_shadow_color ⇒ Object
Set the shadow color of the auxiliary lines.
-
#maximum_value ⇒ Object
You can manually set a maximum value, such as a percentage-based graph that always goes to 100.
-
#minimum_value ⇒ Object
You can manually set a minimum value instead of having the values guessed for you.
-
#no_data_message ⇒ Object
Set a message shown when there is no data.
-
#right_margin ⇒ Object
Blank space to the right of the graph.
-
#sort ⇒ Object
Set to
trueif you want the data sets sorted with largest avg values drawn first. -
#sorted_drawing ⇒ Object
Set to
trueif you want the data sets drawn with largest avg values drawn first. -
#title ⇒ Object
Set the large title of the graph displayed at the top.
-
#title_font ⇒ Object
Same as #font but for the title.
-
#title_font_size ⇒ Object
Set the font size of the large title at the top of the graph.
-
#title_margin ⇒ Object
Blank space below the title.
-
#top_margin ⇒ Object
Blank space above the graph.
-
#use_data_label ⇒ Object
With Side Bars use the data label for the marker value to the left of the bar.
-
#x_axis_increment ⇒ Object
Set increment of the vertical marking lines.
-
#x_axis_label ⇒ Object
Set a label for the bottom of the graph.
-
#y_axis_increment ⇒ Object
Set increment of the horizontal marking lines.
-
#y_axis_label ⇒ Object
Set a label for the left side of the graph.
Instance Method Summary collapse
-
#add_color(colorname) ⇒ Object
Add a color to the list of available colors for lines.
-
#data(name, data_points = [], color = nil) ⇒ Object
Input the data in the graph.
-
#initialize(target_width = DEFAULT_TARGET_WIDTH) ⇒ Base
constructor
If one numerical argument is given, the graph is drawn at 4/3 ratio according to the given width (
800results in 800x600,400gives 400x300, etc.). -
#margins=(margin) ⇒ Object
Sets the top, bottom, left and right margins to
margin. -
#replace_colors(color_list = []) ⇒ Object
Replace the entire color list with a new array of colors.
-
#theme=(options) ⇒ Object
You can set a theme manually.
-
#theme_37signals ⇒ Object
Apply 37signals theme.
-
#theme_greyscale ⇒ Object
Apply greyscale theme.
-
#theme_keynote ⇒ Object
Apply Apple’s keynote theme.
-
#theme_odeo ⇒ Object
Apply Odeo theme.
-
#theme_pastel ⇒ Object
Apply pastel theme.
-
#theme_rails_keynote ⇒ Object
Apply Rails theme.
-
#to_blob(file_format = 'PNG') ⇒ Object
Return the graph as a rendered binary blob.
-
#write(file_name = 'graph.png') ⇒ Object
Writes the graph to a file.
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’).
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/gruff/base.rb', line 174 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
#bold_title ⇒ Object
Specifies whether to draw the title bolded or not. Default is true.
103 104 105 |
# File 'lib/gruff/base.rb', line 103 def bold_title @bold_title end |
#bottom_margin ⇒ Object
Blank space below the graph. Default is 20.
32 33 34 |
# File 'lib/gruff/base.rb', line 32 def bottom_margin @bottom_margin end |
#center_labels_over_point ⇒ Object
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 @center_labels_over_point end |
#colors ⇒ Object
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 ⇒ Object
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.
97 98 99 |
# File 'lib/gruff/base.rb', line 97 def font @font end |
#font_color ⇒ Object
Specifies the text color.
106 107 108 |
# File 'lib/gruff/base.rb', line 106 def font_color @font_color end |
#has_left_labels ⇒ Object
Used internally for horizontal graph types. Default is false.
61 62 63 |
# File 'lib/gruff/base.rb', line 61 def has_left_labels @has_left_labels end |
#hide_legend ⇒ Object
Prevent drawing of the legend. Default is false.
112 113 114 |
# File 'lib/gruff/base.rb', line 112 def hide_legend @hide_legend end |
#hide_line_markers ⇒ Object
Prevent drawing of line markers. Default is false.
109 110 111 |
# File 'lib/gruff/base.rb', line 109 def hide_line_markers @hide_line_markers end |
#hide_line_numbers ⇒ Object
Prevent drawing of line numbers. Default is false.
118 119 120 |
# File 'lib/gruff/base.rb', line 118 def hide_line_numbers @hide_line_numbers end |
#hide_title ⇒ Object
Prevent drawing of the title. Default is false.
115 116 117 |
# File 'lib/gruff/base.rb', line 115 def hide_title @hide_title end |
#label_max_size ⇒ Object
Truncates labels if longer than max specified.
79 80 81 |
# File 'lib/gruff/base.rb', line 79 def label_max_size @label_max_size end |
#label_stagger_height ⇒ Object
Height of staggering between labels (Bar graph only).
76 77 78 |
# File 'lib/gruff/base.rb', line 76 def label_stagger_height @label_stagger_height end |
#label_truncation_style ⇒ Object
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 @label_truncation_style end |
#labels ⇒ Object
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.
53 54 55 |
# File 'lib/gruff/base.rb', line 53 def labels @labels end |
#left_margin ⇒ Object
Blank space to the left of the graph. Default is 20.
38 39 40 |
# File 'lib/gruff/base.rb', line 38 def left_margin @left_margin end |
#legend_at_bottom ⇒ Object
Display the legend under the graph. Default is false.
134 135 136 |
# File 'lib/gruff/base.rb', line 134 def legend_at_bottom @legend_at_bottom end |
#legend_box_size ⇒ Object
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.
160 161 162 |
# File 'lib/gruff/base.rb', line 160 def legend_box_size @legend_box_size end |
#legend_font_size ⇒ Object
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.
131 132 133 |
# File 'lib/gruff/base.rb', line 131 def legend_font_size @legend_font_size end |
#legend_margin ⇒ Object
Blank space below the legend. Default is 20.
44 45 46 |
# File 'lib/gruff/base.rb', line 44 def legend_margin @legend_margin end |
#marker_color ⇒ Object
Set the color of the auxiliary lines.
140 141 142 |
# File 'lib/gruff/base.rb', line 140 def marker_color @marker_color end |
#marker_count ⇒ Object
Set the number of horizontal lines shown for reference.
146 147 148 |
# File 'lib/gruff/base.rb', line 146 def marker_count @marker_count end |
#marker_font_size ⇒ Object
The font size of the labels around the graph. Default is 21.
137 138 139 |
# File 'lib/gruff/base.rb', line 137 def marker_font_size @marker_font_size end |
#marker_shadow_color ⇒ Object
Set the shadow color of the auxiliary lines.
143 144 145 |
# File 'lib/gruff/base.rb', line 143 def marker_shadow_color @marker_shadow_color end |
#maximum_value ⇒ Object
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.
399 400 401 |
# File 'lib/gruff/base.rb', line 399 def maximum_value @maximum_value || store.max end |
#minimum_value ⇒ Object
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.
389 390 391 |
# File 'lib/gruff/base.rb', line 389 def minimum_value @minimum_value || store.min end |
#no_data_message ⇒ Object
Set a message shown when there is no data. Fits up to 20 characters. Defaults to “No Data.”.
122 123 124 |
# File 'lib/gruff/base.rb', line 122 def end |
#right_margin ⇒ Object
Blank space to the right of the graph. Default is 20.
35 36 37 |
# File 'lib/gruff/base.rb', line 35 def right_margin @right_margin end |
#sort ⇒ Object
Set to true if you want the data sets sorted with largest avg values drawn first. Default is false.
150 151 152 |
# File 'lib/gruff/base.rb', line 150 def sort @sort end |
#sorted_drawing ⇒ Object
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.
154 155 156 |
# File 'lib/gruff/base.rb', line 154 def sorted_drawing @sorted_drawing end |
#title ⇒ Object
Set the large title of the graph displayed at the top.
92 93 94 |
# File 'lib/gruff/base.rb', line 92 def title @title end |
#title_font ⇒ Object
Same as #font but for the title.
100 101 102 |
# File 'lib/gruff/base.rb', line 100 def title_font @title_font end |
#title_font_size ⇒ Object
Set the font size of the large title at the top of the graph. Default is 36.
125 126 127 |
# File 'lib/gruff/base.rb', line 125 def title_font_size @title_font_size end |
#title_margin ⇒ Object
Blank space below the title. Default is 20.
41 42 43 |
# File 'lib/gruff/base.rb', line 41 def title_margin @title_margin end |
#top_margin ⇒ Object
Blank space above the graph. Default is 20.
29 30 31 |
# File 'lib/gruff/base.rb', line 29 def top_margin @top_margin end |
#use_data_label ⇒ Object
With Side Bars use the data label for the marker value to the left of the bar. Default is false.
164 165 166 |
# File 'lib/gruff/base.rb', line 164 def use_data_label @use_data_label end |
#x_axis_increment ⇒ Object
Set increment of the vertical marking lines.
70 71 72 |
# File 'lib/gruff/base.rb', line 70 def x_axis_increment @x_axis_increment end |
#x_axis_label ⇒ Object
Set a label for the bottom of the graph.
64 65 66 |
# File 'lib/gruff/base.rb', line 64 def x_axis_label @x_axis_label end |
#y_axis_increment ⇒ Object
Set increment of the horizontal marking lines.
73 74 75 |
# File 'lib/gruff/base.rb', line 73 def y_axis_increment @y_axis_increment end |
#y_axis_label ⇒ Object
Set a label for the left side of the graph.
67 68 69 |
# File 'lib/gruff/base.rb', line 67 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.
272 273 274 |
# File 'lib/gruff/base.rb', line 272 def add_color(colorname) @colors << colorname end |
#data(name, data_points = [], color = nil) ⇒ Object
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.
381 382 383 |
# File 'lib/gruff/base.rb', line 381 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.
253 254 255 |
# File 'lib/gruff/base.rb', line 253 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.
291 292 293 |
# File 'lib/gruff/base.rb', line 291 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.)
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
# File 'lib/gruff/base.rb', line 310 def theme=() reset_themes defaults = { colors: %w[black white], marker_color: 'white', marker_shadow_color: nil, font_color: 'black', background_colors: nil, background_image: nil } = defaults.merge @colors = [:colors] @marker_color = [:marker_color] @marker_shadow_color = [:marker_shadow_color] @font_color = [:font_color] || @marker_color Gruff::Renderer.setup(@columns, @rows, @font, @scale, ) end |
#theme_37signals ⇒ Object
Apply 37signals theme.
337 338 339 |
# File 'lib/gruff/base.rb', line 337 def theme_37signals self.theme = Themes::THIRTYSEVEN_SIGNALS end |
#theme_greyscale ⇒ Object
Apply greyscale theme.
357 358 359 |
# File 'lib/gruff/base.rb', line 357 def theme_greyscale self.theme = Themes::GREYSCALE end |
#theme_keynote ⇒ Object
Apply Apple’s keynote theme.
332 333 334 |
# File 'lib/gruff/base.rb', line 332 def theme_keynote self.theme = Themes::KEYNOTE end |
#theme_odeo ⇒ Object
Apply Odeo theme.
347 348 349 |
# File 'lib/gruff/base.rb', line 347 def theme_odeo self.theme = Themes::ODEO end |
#theme_pastel ⇒ Object
Apply pastel theme.
352 353 354 |
# File 'lib/gruff/base.rb', line 352 def theme_pastel self.theme = Themes::PASTEL end |
#theme_rails_keynote ⇒ Object
Apply Rails theme.
342 343 344 |
# File 'lib/gruff/base.rb', line 342 def theme_rails_keynote self.theme = Themes::RAILS_KEYNOTE end |
#to_blob(file_format = 'PNG') ⇒ Object
Return the graph as a rendered binary blob.
416 417 418 419 |
# File 'lib/gruff/base.rb', line 416 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’
410 411 412 413 |
# File 'lib/gruff/base.rb', line 410 def write(file_name = 'graph.png') draw Gruff::Renderer.write(file_name) end |