Class: Gruff::Line
Overview
Here’s how to make a Line graph:
g = Gruff::Line.new
g.title = "A Line Graph"
g.data 'Fries', [20, 23, 19, 8]
g.data 'Hamburgers', [50, 19, 99, 29]
g.write("test/output/line.png")
There are also other options described below, such as #baseline_value, #baseline_color, #hide_dots, and #hide_lines.
Constant Summary
Constants inherited from Base
Base::DATA_COLOR_INDEX, Base::DATA_LABEL_INDEX, Base::DATA_VALUES_INDEX, Base::DATA_VALUES_X_INDEX, Base::DEBUG, Base::DEFAULT_MARGIN, Base::DEFAULT_TARGET_WIDTH, Base::LABEL_MARGIN, Base::LEGEND_MARGIN, Base::THOUSAND_SEPARATOR
Instance Attribute Summary collapse
-
#dot_radius ⇒ Object
Returns the value of attribute dot_radius.
-
#hide_dots ⇒ Object
Hide parts of the graph to fit more datapoints, or for a different appearance.
-
#hide_lines ⇒ Object
Hide parts of the graph to fit more datapoints, or for a different appearance.
-
#line_width ⇒ Object
Dimensions of lines and dots; calculated based on dataset size if left unspecified.
-
#maximum_x_value ⇒ Object
Returns the value of attribute maximum_x_value.
-
#minimum_x_value ⇒ Object
accessors for support of xy data.
-
#reference_line_default_color ⇒ Object
Returns the value of attribute reference_line_default_color.
-
#reference_line_default_width ⇒ Object
Returns the value of attribute reference_line_default_width.
-
#reference_lines ⇒ Object
Allow for reference lines ( which are like baseline … just allowing for more & on both axes ).
Attributes inherited from Base
#additional_line_values, #bottom_margin, #center_labels_over_point, #colors, #font, #font_color, #has_left_labels, #hide_legend, #hide_line_markers, #hide_line_numbers, #hide_title, #label_formatting, #label_max_size, #label_stagger_height, #label_truncation_style, #labels, #left_margin, #legend_at_bottom, #legend_box_size, #legend_font_size, #legend_margin, #marker_color, #marker_count, #marker_font_size, #marker_shadow_color, #maximum_value, #minimum_value, #no_data_message, #right_margin, #show_labels_for_bar_values, #sort, #sorted_drawing, #stacked, #title, #title_font_size, #title_margin, #top_margin, #use_data_label, #x_axis_label, #y_axis_increment, #y_axis_label
Instance Method Summary collapse
- #baseline_color ⇒ Object
- #baseline_color=(new_value) ⇒ Object
-
#baseline_value ⇒ Object
Get the value if somebody has defined it.
-
#baseline_value=(new_value) ⇒ Object
Set a value for a baseline reference line..
- #contains_one_point_only?(data_row) ⇒ Boolean
-
#dataxy(name, x_data_points = [], y_data_points = [], color = nil) ⇒ Object
This method allows one to plot a dataset with both X and Y data.
- #draw ⇒ Object
- #draw_horizontal_reference_line(reference_line) ⇒ Object
- #draw_reference_line(reference_line, left, right, top, bottom) ⇒ Object
- #draw_vertical_reference_line(reference_line) ⇒ Object
- #get_x_coord(x_data_point, width, offset) ⇒ Object
-
#initialize(*args) ⇒ Line
constructor
Call with target pixel width of graph (800, 400, 300), and/or ‘false’ to omit lines (points only).
- #normalize(force = false) ⇒ Object
- #setup_data ⇒ Object
- #sort_norm_data ⇒ Object
Methods inherited from Base
#add_color, #data, #initialize_ivars, #margins=, #replace_colors, #theme=, #theme_37signals, #theme_greyscale, #theme_keynote, #theme_odeo, #theme_pastel, #theme_rails_keynote, #to_blob, #write
Methods included from Deprecated
#graph_height, #graph_left, #graph_top, #graph_width, #scale_measurements, #total_height
Constructor Details
#initialize(*args) ⇒ Line
Call with target pixel width of graph (800, 400, 300), and/or ‘false’ to omit lines (points only).
g = Gruff::Line.new(400) # 400px wide with lines
g = Gruff::Line.new(400, false) # 400px wide, no lines (for backwards compatibility)
g = Gruff::Line.new(false) # Defaults to 800px wide, no lines (for backwards compatibility)
The preferred way is to call hide_dots or hide_lines instead.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/gruff/line.rb', line 69 def initialize(*args) raise ArgumentError, 'Wrong number of arguments' if args.length > 2 if args.empty? || ((not Numeric === args.first) && (not String === args.first)) super() else super args.shift end @reference_lines = Hash.new @reference_line_default_color = 'red' @reference_line_default_width = 5 @hide_dots = @hide_lines = false @maximum_x_value = nil @minimum_x_value = nil end |
Instance Attribute Details
#dot_radius ⇒ Object
Returns the value of attribute dot_radius.
23 24 25 |
# File 'lib/gruff/line.rb', line 23 def dot_radius @dot_radius end |
#hide_dots ⇒ Object
Hide parts of the graph to fit more datapoints, or for a different appearance.
26 27 28 |
# File 'lib/gruff/line.rb', line 26 def hide_dots @hide_dots end |
#hide_lines ⇒ Object
Hide parts of the graph to fit more datapoints, or for a different appearance.
26 27 28 |
# File 'lib/gruff/line.rb', line 26 def hide_lines @hide_lines end |
#line_width ⇒ Object
Dimensions of lines and dots; calculated based on dataset size if left unspecified
22 23 24 |
# File 'lib/gruff/line.rb', line 22 def line_width @line_width end |
#maximum_x_value ⇒ Object
Returns the value of attribute maximum_x_value.
30 31 32 |
# File 'lib/gruff/line.rb', line 30 def maximum_x_value @maximum_x_value end |
#minimum_x_value ⇒ Object
accessors for support of xy data
29 30 31 |
# File 'lib/gruff/line.rb', line 29 def minimum_x_value @minimum_x_value end |
#reference_line_default_color ⇒ Object
Returns the value of attribute reference_line_default_color.
18 19 20 |
# File 'lib/gruff/line.rb', line 18 def reference_line_default_color @reference_line_default_color end |
#reference_line_default_width ⇒ Object
Returns the value of attribute reference_line_default_width.
19 20 21 |
# File 'lib/gruff/line.rb', line 19 def reference_line_default_width @reference_line_default_width end |
#reference_lines ⇒ Object
Allow for reference lines ( which are like baseline … just allowing for more & on both axes )
17 18 19 |
# File 'lib/gruff/line.rb', line 17 def reference_lines @reference_lines end |
Instance Method Details
#baseline_color ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/gruff/line.rb', line 47 def baseline_color if (@reference_lines.key?(:baseline)) @reference_lines[:baseline][:color] else nil end end |
#baseline_color=(new_value) ⇒ Object
55 56 57 58 |
# File 'lib/gruff/line.rb', line 55 def baseline_color=(new_value) @reference_lines[:baseline] ||= Hash.new @reference_lines[:baseline][:color] = new_value end |
#baseline_value ⇒ Object
Get the value if somebody has defined it.
33 34 35 36 37 38 39 |
# File 'lib/gruff/line.rb', line 33 def baseline_value if (@reference_lines.key?(:baseline)) @reference_lines[:baseline][:value] else nil end end |
#baseline_value=(new_value) ⇒ Object
Set a value for a baseline reference line..
42 43 44 45 |
# File 'lib/gruff/line.rb', line 42 def baseline_value=(new_value) @reference_lines[:baseline] ||= Hash.new @reference_lines[:baseline][:value] = new_value end |
#contains_one_point_only?(data_row) ⇒ Boolean
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
# File 'lib/gruff/line.rb', line 288 def contains_one_point_only?(data_row) # Spin through data to determine if there is just one_value present. one_point = false data_row[DATA_VALUES_INDEX].each do |data_point| unless data_point.nil? if one_point # more than one point, bail return false end # there is at least one data point one_point = true end end one_point end |
#dataxy(name, x_data_points = [], y_data_points = [], color = nil) ⇒ Object
This method allows one to plot a dataset with both X and Y data.
Parameters are as follows:
name: string, the title of the dataset
x_data_points: an array containing the x data points for the graph
y_data_points: an array containing the y data points for the graph
color: hex number indicating the line color as an RGB triplet
or
name: string, the title of the dataset
xy_data_points: an array containing both x and y data points for the graph
color: hex number indicating the line color as an RGB triplet
Notes:
-if (x_data_points.length != y_data_points.length) an error is
returned.
-if the color argument is nil, the next color from the default theme will
be used.
-if you want to use a preset theme, you must set it before calling
dataxy().
Example:
g = Gruff::Line.new
g.title = "X/Y Dataset"
g.dataxy("Apples", [1,3,4,5,6,10], [1, 2, 3, 4, 4, 3])
g.dataxy("Bapples", [1,3,4,5,7,9], [1, 1, 2, 2, 3, 3])
g.dataxy("Capples", [[1,1],[2,3],[3,4],[4,5],[5,7],[6,9]])
#you can still use the old data method too if you want:
g.data("Capples", [1, 1, 2, 2, 3, 3])
#labels will be drawn at the x locations of the keys passed in.
In this example the lables are drawn at x positions 2, 4, and 6:
g.labels = {0 => '2003', 2 => '2004', 4 => '2005', 6 => '2006'}
The 0 => '2003' label will be ignored since it is outside the chart range.
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/gruff/line.rb', line 120 def dataxy(name, x_data_points=[], y_data_points=[], color=nil) raise ArgumentError, 'x_data_points is nil!' if x_data_points.length == 0 if x_data_points.all? { |p| p.is_a?(Array) && p.size == 2 } x_data_points, y_data_points = x_data_points.map { |p| p[0] }, x_data_points.map { |p| p[1] } end raise ArgumentError, 'x_data_points.length != y_data_points.length!' if x_data_points.length != y_data_points.length # call the existing data routine for the y data. self.data(name, y_data_points, color) x_data_points = Array(x_data_points) # make sure it's an array # append the x data to the last entry that was just added in the @data member @data.last[DATA_VALUES_X_INDEX] = x_data_points # Update the global min/max values for the x data x_data_points.each do |x_data_point| next if x_data_point.nil? # Setup max/min so spread starts at the low end of the data points if @maximum_x_value.nil? && @minimum_x_value.nil? @maximum_x_value = @minimum_x_value = x_data_point end @maximum_x_value = (x_data_point > @maximum_x_value) ? x_data_point : @maximum_x_value @minimum_x_value = (x_data_point < @minimum_x_value) ? x_data_point : @minimum_x_value end end |
#draw ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 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 |
# File 'lib/gruff/line.rb', line 173 def draw super return unless @has_data # Check to see if more than one datapoint was given. NaN can result otherwise. @x_increment = (@column_count > 1) ? (@graph_width / (@column_count - 1).to_f) : @graph_width @reference_lines.each_value do |curr_reference_line| draw_horizontal_reference_line(curr_reference_line) if curr_reference_line.key?(:norm_value) draw_vertical_reference_line(curr_reference_line) if curr_reference_line.key?(:index) end @norm_data.each do |data_row| prev_x = prev_y = nil @one_point = contains_one_point_only?(data_row) data_row[DATA_VALUES_INDEX].each_with_index do |data_point, index| unless data_point prev_x = prev_y = nil next end x_data = data_row[DATA_VALUES_X_INDEX] if x_data == nil #use the old method: equally spaced points along the x-axis new_x = @graph_left + (@x_increment * index) draw_label(new_x, index) else new_x = get_x_coord(x_data[index], @graph_width, @graph_left) @labels.each do |label_pos, _| draw_label(@graph_left + ((label_pos - @minimum_x_value) * @graph_width) / (@maximum_x_value - @minimum_x_value), label_pos) end end new_y = @graph_top + (@graph_height - data_point * @graph_height) # Reset each time to avoid thin-line errors @d = @d.stroke data_row[DATA_COLOR_INDEX] @d = @d.fill data_row[DATA_COLOR_INDEX] @d = @d.stroke_opacity 1.0 @d = @d.stroke_width line_width || clip_value_if_greater_than(@columns / (@norm_data.first[DATA_VALUES_INDEX].size * 4), 5.0) circle_radius = dot_radius || clip_value_if_greater_than(@columns / (@norm_data.first[DATA_VALUES_INDEX].size * 2.5), 5.0) if !@hide_lines && !prev_x.nil? && !prev_y.nil? @d = @d.line(prev_x, prev_y, new_x, new_y) elsif @one_point # Show a circle if there's just one_point @d = @d.circle(new_x, new_y, new_x - circle_radius, new_y) end @d = @d.circle(new_x, new_y, new_x - circle_radius, new_y) unless @hide_dots prev_x, prev_y = new_x, new_y end end @d.draw(@base_image) end |
#draw_horizontal_reference_line(reference_line) ⇒ Object
163 164 165 166 |
# File 'lib/gruff/line.rb', line 163 def draw_horizontal_reference_line(reference_line) level = @graph_top + (@graph_height - reference_line[:norm_value] * @graph_height) draw_reference_line(reference_line, @graph_left, @graph_left + @graph_width, level, level) end |
#draw_reference_line(reference_line, left, right, top, bottom) ⇒ Object
153 154 155 156 157 158 159 160 161 |
# File 'lib/gruff/line.rb', line 153 def draw_reference_line(reference_line, left, right, top, bottom) @d = @d.push @d.stroke_color(reference_line[:color] || @reference_line_default_color) @d.fill_opacity 0.0 @d.stroke_dasharray(10, 20) @d.stroke_width(reference_line[:width] || @reference_line_default_width) @d.line(left, top, right, bottom) @d = @d.pop end |
#draw_vertical_reference_line(reference_line) ⇒ Object
168 169 170 171 |
# File 'lib/gruff/line.rb', line 168 def draw_vertical_reference_line(reference_line) index = @graph_left + (@x_increment * reference_line[:index]) draw_reference_line(reference_line, index, index, @graph_top, @graph_top + graph_height) end |
#get_x_coord(x_data_point, width, offset) ⇒ Object
284 285 286 |
# File 'lib/gruff/line.rb', line 284 def get_x_coord(x_data_point, width, offset) x_data_point * width + offset end |
#normalize(force = false) ⇒ Object
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/gruff/line.rb', line 254 def normalize(force=false) super(force) @reference_lines.each_value do |curr_reference_line| # We only care about horizontal markers ... for normalization. # Vertical markers won't have a :value, they will have an :index curr_reference_line[:norm_value] = ((curr_reference_line[:value].to_f - @minimum_value) / @spread.to_f) if (curr_reference_line.key?(:value)) end #normalize the x data if it is specified @data.each_with_index do |data_row, index| norm_x_data_points = [] if data_row[DATA_VALUES_X_INDEX] != nil data_row[DATA_VALUES_X_INDEX].each do |x_data_point| norm_x_data_points << ((x_data_point.to_f - @minimum_x_value.to_f) / (@maximum_x_value.to_f - @minimum_x_value.to_f)) end @norm_data[index] << norm_x_data_points end end end |
#setup_data ⇒ Object
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/gruff/line.rb', line 235 def setup_data # Deal with horizontal reference line values that exceed the existing minimum & maximum values. possible_maximums = [@maximum_value.to_f] possible_minimums = [@minimum_value.to_f] @reference_lines.each_value do |curr_reference_line| if (curr_reference_line.key?(:value)) possible_maximums << curr_reference_line[:value].to_f possible_minimums << curr_reference_line[:value].to_f end end @maximum_value = possible_maximums.max @minimum_value = possible_minimums.min super end |
#sort_norm_data ⇒ Object
280 281 282 |
# File 'lib/gruff/line.rb', line 280 def sort_norm_data super unless @data.any? { |d| d[DATA_VALUES_X_INDEX] } end |