Class: Gruff::Base
- Inherits:
-
Object
- Object
- Gruff::Base
- Includes:
- Deprecated, Magick
- Defined in:
- lib/gruff/base.rb
Direct Known Subclasses
Area, Bar, Bullet, Line, Net, PhotoBar, Pie, Scene, SideBar, Spider, StackedArea, StackedBar
Defined Under Namespace
Modules: StackedMixin
Constant Summary collapse
- DEBUG =
Draw extra lines showing where the margins and text centers are
false
- DATA_LABEL_INDEX =
Used for navigating the array of data to plot
0
- DATA_VALUES_INDEX =
1
- DATA_COLOR_INDEX =
2
- DATA_ATTRS_INDEX =
3
- LEGEND_MARGIN =
Space around text elements. Mostly used for vertical spacing
TITLE_MARGIN = LABEL_MARGIN = 10.0
- DEFAULT_TARGET_WIDTH =
800
Instance Attribute Summary collapse
-
#additional_line_values ⇒ Object
Experimental.
-
#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
Returns the value of attribute font_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_rotation ⇒ Object
Labels can be rotated from 0-90 degrees.
-
#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_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.
-
#marker_color ⇒ Object
The color of the auxiliary lines.
-
#marker_count ⇒ Object
The number of horizontal lines shown for reference.
-
#marker_font_size ⇒ Object
The font size of the labels around the graph.
-
#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
Message shown when there is no data.
-
#right_margin ⇒ Object
Blank space to the right of the graph.
-
#sort ⇒ Object
Set to false if you don’t want the data to be sorted with largest avg values at the back.
-
#stacked ⇒ Object
Experimental.
-
#title ⇒ Object
The large title of the graph displayed at the top.
-
#title_font_size ⇒ Object
The font size of the large title at the top of the graph.
-
#top_margin ⇒ Object
Blank space above the graph.
-
#x_axis_label ⇒ Object
A label for the bottom of the graph.
-
#y_axis_increment ⇒ Object
Manually set increment of the horizontal marking lines.
-
#y_axis_label ⇒ Object
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
Parameters are an array where the first element is the name of the dataset and the value is an array of values to plot.
-
#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 (800 results in 800x600, 400 gives 400x300, etc.).
-
#initialize_ivars ⇒ Object
Set instance variables for this object.
-
#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.
-
#to_blob(fileformat = 'PNG') ⇒ Object
Return the graph as a rendered binary blob.
-
#write(filename = "graph.png") ⇒ Object
Writes the graph to a file.
Methods included from Deprecated
#graph_height, #graph_left, #graph_top, #graph_width, #scale_measurements, #total_height
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’).
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.)
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/gruff/base.rb', line 176 def initialize(target_width=DEFAULT_TARGET_WIDTH) @top_margin = @bottom_margin = @left_margin = @right_margin = 20.0 if not Numeric === target_width 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 # Call any other initializers -- open-ended to allow for # subclass extensions self.class.instance_methods. select{|m| m.to_s =~ /^initialize_/o}.each do |initializer| self.send(initializer) end reset_themes theme_keynote end |
Instance Attribute Details
#additional_line_values ⇒ Object
Experimental
157 158 159 |
# File 'lib/gruff/base.rb', line 157 def additional_line_values @additional_line_values end |
#bottom_margin ⇒ Object
Blank space below the graph
48 49 50 |
# File 'lib/gruff/base.rb', line 48 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.
70 71 72 |
# File 'lib/gruff/base.rb', line 70 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.
87 88 89 |
# File 'lib/gruff/base.rb', line 87 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.
Tries to find Bitstream Vera (Vera.ttf) in the location specified by ENV. Uses default RMagick font otherwise.
The font= method below fulfills the role of the writer, so we only need a reader here.
101 102 103 |
# File 'lib/gruff/base.rb', line 101 def font @font end |
#font_color ⇒ Object
Returns the value of attribute font_color.
103 104 105 |
# File 'lib/gruff/base.rb', line 103 def font_color @font_color end |
#has_left_labels ⇒ Object
Used internally for horizontal graph types.
73 74 75 |
# File 'lib/gruff/base.rb', line 73 def has_left_labels @has_left_labels end |
#hide_legend ⇒ Object
Prevent drawing of the legend
109 110 111 |
# File 'lib/gruff/base.rb', line 109 def hide_legend @hide_legend end |
#hide_line_markers ⇒ Object
Prevent drawing of line markers
106 107 108 |
# File 'lib/gruff/base.rb', line 106 def hide_line_markers @hide_line_markers end |
#hide_line_numbers ⇒ Object
Prevent drawing of line numbers
115 116 117 |
# File 'lib/gruff/base.rb', line 115 def hide_line_numbers @hide_line_numbers end |
#hide_title ⇒ Object
Prevent drawing of the title
112 113 114 |
# File 'lib/gruff/base.rb', line 112 def hide_title @hide_title end |
#label_rotation ⇒ Object
Labels can be rotated from 0-90 degrees
65 66 67 |
# File 'lib/gruff/base.rb', line 65 def label_rotation @label_rotation 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.
Example: 0 => 2005, 3 => 2006, 5 => 2007, 7 => 2008
62 63 64 |
# File 'lib/gruff/base.rb', line 62 def labels @labels end |
#left_margin ⇒ Object
Blank space to the left of the graph
54 55 56 |
# File 'lib/gruff/base.rb', line 54 def left_margin @left_margin 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.
166 167 168 |
# File 'lib/gruff/base.rb', line 166 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 graph is smaller than 800px wide.
128 129 130 |
# File 'lib/gruff/base.rb', line 128 def legend_font_size @legend_font_size end |
#marker_color ⇒ Object
The color of the auxiliary lines
134 135 136 |
# File 'lib/gruff/base.rb', line 134 def marker_color @marker_color end |
#marker_count ⇒ Object
The number of horizontal lines shown for reference
137 138 139 |
# File 'lib/gruff/base.rb', line 137 def marker_count @marker_count end |
#marker_font_size ⇒ Object
The font size of the labels around the graph
131 132 133 |
# File 'lib/gruff/base.rb', line 131 def marker_font_size @marker_font_size 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.
150 151 152 |
# File 'lib/gruff/base.rb', line 150 def maximum_value @maximum_value 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.
143 144 145 |
# File 'lib/gruff/base.rb', line 143 def minimum_value @minimum_value end |
#no_data_message ⇒ Object
Message shown when there is no data. Fits up to 20 characters. Defaults to “No Data.”
119 120 121 |
# File 'lib/gruff/base.rb', line 119 def @no_data_message end |
#right_margin ⇒ Object
Blank space to the right of the graph
51 52 53 |
# File 'lib/gruff/base.rb', line 51 def right_margin @right_margin end |
#sort ⇒ Object
Set to false if you don’t want the data to be sorted with largest avg values at the back.
154 155 156 |
# File 'lib/gruff/base.rb', line 154 def sort @sort end |
#stacked ⇒ Object
Experimental
160 161 162 |
# File 'lib/gruff/base.rb', line 160 def stacked @stacked end |
#title ⇒ Object
The large title of the graph displayed at the top
90 91 92 |
# File 'lib/gruff/base.rb', line 90 def title @title end |
#title_font_size ⇒ Object
The font size of the large title at the top of the graph
122 123 124 |
# File 'lib/gruff/base.rb', line 122 def title_font_size @title_font_size end |
#top_margin ⇒ Object
Blank space above the graph
45 46 47 |
# File 'lib/gruff/base.rb', line 45 def top_margin @top_margin end |
#x_axis_label ⇒ Object
A label for the bottom of the graph
76 77 78 |
# File 'lib/gruff/base.rb', line 76 def x_axis_label @x_axis_label end |
#y_axis_increment ⇒ Object
Manually set increment of the horizontal marking lines
84 85 86 |
# File 'lib/gruff/base.rb', line 84 def y_axis_increment @y_axis_increment end |
#y_axis_label ⇒ Object
A label for the left side of the graph
79 80 81 |
# File 'lib/gruff/base.rb', line 79 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.
Example:
add_color('#c0e9d3')
264 265 266 |
# File 'lib/gruff/base.rb', line 264 def add_color(colorname) @colors << colorname end |
#data(name, data_points = [], color = nil) ⇒ Object
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')
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
# File 'lib/gruff/base.rb', line 292 def data(name, data_points=[], color=nil) data_points = Array(data_points) # make sure it's an array @data << [name, data_points, (color || increment_color)] # Set column count if this is larger than previous counts @column_count = (data_points.length > @column_count) ? data_points.length : @column_count # Pre-normalize data_points.each_with_index do |data_point, index| next if data_point.nil? # Setup max/min so spread starts at the low end of the data points if @maximum_value.nil? && @minimum_value.nil? @maximum_value = @minimum_value = data_point end # TODO Doesn't work with stacked bar graphs # Original: @maximum_value = larger_than_max?(data_point, index) ? max(data_point, index) : @maximum_value @maximum_value = larger_than_max?(data_point) ? data_point : @maximum_value @has_data = true if @maximum_value > 0 @minimum_value = less_than_min?(data_point) ? data_point : @minimum_value @has_data = true if @minimum_value < 0 end end |
#initialize_ivars ⇒ Object
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.
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/gruff/base.rb', line 205 def initialize_ivars # Internal for calculations @raw_columns = 800.0 @raw_rows = 800.0 * (@rows/@columns) @column_count = 0 @marker_count = nil @maximum_value = @minimum_value = nil @has_data = false @data = Array.new @labels = Hash.new @labels_seen = Hash.new @sort = true @title = nil @scale = @columns / @raw_columns vera_font_path = File.('Vera.ttf', ENV['MAGICK_FONT_PATH']) @font = File.exists?(vera_font_path) ? vera_font_path : nil @marker_font_size = 21.0 @legend_font_size = 20.0 @title_font_size = 36.0 @legend_box_size = 20.0 @no_data_message = "No Data" @hide_line_markers = @hide_legend = @hide_title = @hide_line_numbers = false @center_labels_over_point = true @has_left_labels = false @label_rotation = 0 @additional_line_values = [] @additional_line_colors = [] @theme_options = {} @x_axis_label = @y_axis_label = nil @y_axis_increment = nil @stacked = nil @norm_data = nil end |
#margins=(margin) ⇒ Object
Sets the top, bottom, left and right margins to margin
.
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. 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']
274 275 276 |
# File 'lib/gruff/base.rb', line 274 def replace_colors(color_list=[]) @colors = color_list end |
#to_blob(fileformat = 'PNG') ⇒ Object
Return the graph as a rendered binary blob.
327 328 329 330 331 332 |
# File 'lib/gruff/base.rb', line 327 def to_blob(fileformat='PNG') draw() return @base_image.to_blob do self.format = fileformat end end |
#write(filename = "graph.png") ⇒ Object
Writes the graph to a file. Defaults to ‘graph.png’
Example:
write('graphs/my_pretty_graph.png')
321 322 323 324 |
# File 'lib/gruff/base.rb', line 321 def write(filename="graph.png") draw() @base_image.write(filename) end |